Review Board 1.7.22


Shindig does not accept relative URLs for template libraries

Review Request #4818 - Created April 20, 2012 and submitted

Erik Bi
shindig-1548
Reviewers
shindig
ddumont, rbaxter, ssievers
shindig
per spec, Gadget should be able to use relative URL to define the template-libray, but right now when it uses this approach, client side will end up making a /gadgets/makeRequest call and passes in the relative URL value and fails with an HTTP status of 400.

Solution: 
At client side, the opensocial-template feature will get the template-library URL value from global variable "features", so in this patch it updates the process of generating the init JS which will be passed back to client side to init the "features" variable(in Class "org.apache.shindig.gadgets.config.CoreUtilConfigContributor"), adds some specific logic to handle "template-library" parameter, convert the relative url to an absolute one. 

 
http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/config/CoreUtilConfigContributor.java
Revision 1327432 New Change
[20] 19 lines
[+20]
20

    
   
20

   
21
import java.util.Collection;
21
import java.util.Collection;
22
import java.util.Map;
22
import java.util.Map;
23
import java.util.Set;
23
import java.util.Set;
24

    
   
24

   

    
   
25
import org.apache.shindig.common.uri.Uri;
25
import org.apache.shindig.gadgets.Gadget;
26
import org.apache.shindig.gadgets.Gadget;
26
import org.apache.shindig.gadgets.admin.GadgetAdminStore;
27
import org.apache.shindig.gadgets.admin.GadgetAdminStore;
27
import org.apache.shindig.gadgets.features.FeatureRegistry;
28
import org.apache.shindig.gadgets.features.FeatureRegistry;
28
import org.apache.shindig.gadgets.spec.Feature;
29
import org.apache.shindig.gadgets.spec.Feature;
29

    
   
30

   

    
   
31
import com.google.caja.util.Lists;
30
import com.google.common.collect.Maps;
32
import com.google.common.collect.Maps;
31
import com.google.inject.Inject;
33
import com.google.inject.Inject;
32
import com.google.inject.Singleton;
34
import com.google.inject.Singleton;
33

    
   
35

   
34
/**
36
/**
[+20] [20] 6 lines
[+20]
41
public class CoreUtilConfigContributor implements ConfigContributor {
43
public class CoreUtilConfigContributor implements ConfigContributor {
42

    
   
44

   
43
  private final FeatureRegistry registry;
45
  private final FeatureRegistry registry;
44
  private final GadgetAdminStore gadgetAdminStore;
46
  private final GadgetAdminStore gadgetAdminStore;
45

    
   
47

   

    
   
48
  private static final String TEMPLATES_FEATURE_NAME = "opensocial-templates";

    
   
49
  private static final String REQUIRE_LIBRARY_PARAM = "requireLibrary";

    
   
50

   
46
  @Inject
51
  @Inject
47
  public CoreUtilConfigContributor(final FeatureRegistry registry,
52
  public CoreUtilConfigContributor(final FeatureRegistry registry,
48
          GadgetAdminStore gadgetAdminStore) {
53
          GadgetAdminStore gadgetAdminStore) {
49
    this.registry = registry;
54
    this.registry = registry;
50
    this.gadgetAdminStore = gadgetAdminStore;
55
    this.gadgetAdminStore = gadgetAdminStore;
[+20] [20] 16 lines
[+20] [+] public void contribute(Map<String, Object> config, Gadget gadget) {
67
      // Flatten out the multimap a bit for backwards compatibility:  map keys
72
      // Flatten out the multimap a bit for backwards compatibility:  map keys
68
      // with just 1 value into the string, treat others as arrays
73
      // with just 1 value into the string, treat others as arrays
69
      Map<String, Object> paramFeaturesInConfig = Maps.newHashMap();
74
      Map<String, Object> paramFeaturesInConfig = Maps.newHashMap();
70
      for (String paramName : feature.getParams().keySet()) {
75
      for (String paramName : feature.getParams().keySet()) {
71
        Collection<String> paramValues = feature.getParams().get(paramName);
76
        Collection<String> paramValues = feature.getParams().get(paramName);

    
   
77
        // Resolve the template URL to convert relative URL to absolute URL for Jira 1548

    
   
78
        if (TEMPLATES_FEATURE_NAME.equals(feature.getName())

    
   
79
                && REQUIRE_LIBRARY_PARAM.equals(paramName)) {

    
   
80
          Uri abURI = null;

    
   
81
          if (paramValues.size() == 1) {

    
   
82
            abURI = Uri.parse(paramValues.iterator().next().trim());

    
   
83
            abURI = gadget.getContext().getUrl().resolve(abURI);

    
   
84
            paramFeaturesInConfig.put(paramName, abURI.toString());

    
   
85
          } else {

    
   
86
            Collection<String> abReqLibs = Lists.newArrayList();

    
   
87
            for (String libraryUrl : paramValues) {

    
   
88
              abURI = Uri.parse(libraryUrl.trim());

    
   
89
              abURI = gadget.getContext().getUrl().resolve(abURI);

    
   
90
              abReqLibs.add(abURI.toString());

    
   
91
            }

    
   
92
            paramFeaturesInConfig.put(paramName, abReqLibs);

    
   
93
          }

    
   
94
        } else {
72
        if (paramValues.size() == 1) {
95
          if (paramValues.size() == 1) {
73
          paramFeaturesInConfig.put(paramName, paramValues.iterator().next());
96
            paramFeaturesInConfig.put(paramName, paramValues.iterator().next());
74
        } else {
97
          } else {
75
          paramFeaturesInConfig.put(paramName, paramValues);
98
            paramFeaturesInConfig.put(paramName, paramValues);
76
        }
99
          }
77
      }
100
        }

    
   
101
      }
78

    
   
102

   
79
      featureMap.put(feature.getName(), paramFeaturesInConfig);
103
      featureMap.put(feature.getName(), paramFeaturesInConfig);
80
    }
104
    }
81
    config.put("core.util", featureMap);
105
    config.put("core.util", featureMap);
82
  }
106
  }
83

    
   
107

   
84
  /** {@inheritDoc} */
108
  /** {@inheritDoc} */
85
  public void contribute(Map<String,Object> config, String container, String host) {
109
  public void contribute(Map<String,Object> config, String container, String host) {
86
    // not used for container configuration
110
    // not used for container configuration
87
  }
111
  }
88
}
112
}
  1. http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/config/CoreUtilConfigContributor.java: Loading...