Review Board 1.7.22


Gadget Admin Code Does Not Take Into Account Ports In Gadget URLs

Review Request #2425 - Created Oct. 18, 2011 and submitted

Ryan Baxter
SHINDIG-1643
Reviewers
shindig
ddumont
shindig
If there is a gadget in the admin store, for example http://foo.com/gadget.xml and we try to render http://foo.com:80/gadget.xml shindig does not think http://foo.com:80/gadget.xml is the same as http://foo.com/gadget.xml are the same gadget so it does not think the gadget should be allowed to render.
Updated unit tests to test new functionality
http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/admin/ContainerAdminData.java
Revision 1185758 New Change
[20] 15 lines
[+20]
16
 * specific language governing permissions and limitations
16
 * specific language governing permissions and limitations
17
 * under the License.
17
 * under the License.
18
 */
18
 */
19
package org.apache.shindig.gadgets.admin;
19
package org.apache.shindig.gadgets.admin;
20

    
   
20

   

    
   
21
import java.net.MalformedURLException;

    
   
22
import java.net.URL;
21
import java.util.Map;
23
import java.util.Map;
22
import java.util.Set;
24
import java.util.Set;
23

    
   
25

   
24
import com.google.caja.util.Maps;
26
import com.google.caja.util.Maps;
25
import com.google.common.base.Objects;
27
import com.google.common.base.Objects;
26

    
   
28

   
27
/**
29
/**
28
 * Container's administration data.
30
 * Container's administration data.
29
 *
31
 *
30
 * @version $Id: $
32
 * @version $Id: $
31
 */
33
 */
32
public class ContainerAdminData {
34
public class ContainerAdminData {

    
   
35
  private static final String STAR = "*";

    
   
36
  private static final String HTTP = "http";

    
   
37
  private static final String HTTPS = "https";

    
   
38
  private static final int HTTP_PORT = 80;

    
   
39
  private static final int HTTPS_PORT = 443;

    
   
40

   
33
  private Map<String, GadgetAdminData> gadgetAdminMap;
41
  private Map<String, GadgetAdminData> gadgetAdminMap;
34

    
   
42

   
35
  /**
43
  /**
36
   * Constructor
44
   * Constructor
37
   */
45
   */
[+20] [20] 99 lines
[+20] [+] public boolean hasGadgetAdminData(String gadgetUrl) {
137
   *          The gadget URL.
145
   *          The gadget URL.
138
   * @return The key in the map for the gadget URL.
146
   * @return The key in the map for the gadget URL.
139
   */
147
   */
140
  private String getGadgetAdminDataKey(String gadgetUrl) {
148
  private String getGadgetAdminDataKey(String gadgetUrl) {
141
    Set<String> gadgetUrls = this.gadgetAdminMap.keySet();
149
    Set<String> gadgetUrls = this.gadgetAdminMap.keySet();

    
   
150
    String normalizedGadgetUrl = createUrlWithPort(gadgetUrl);
142
    String key = null;
151
    String key = null;
143
    for (String url : gadgetUrls) {
152
    for (String url : gadgetUrls) {
144
      if (url.endsWith("*") && gadgetUrl.startsWith(url.substring(0, url.length() - 1))) {
153
      String normalizedUrl = createUrlWithPort(url);
145
        if (key == null || (key != null && key.length() < url.length())) {
154
      if (normalizedUrl.endsWith(STAR)

    
   
155
              && normalizedGadgetUrl.startsWith(normalizedUrl.substring(0,

    
   
156
                      normalizedUrl.length() - 1))) {

    
   
157
        if (key == null || (key != null && key.length() < normalizedUrl.length())) {
146
          key = url;
158
          key = url;
147
        }
159
        }

    
   
160
      } else if (normalizedUrl.equals(normalizedGadgetUrl)) {

    
   
161
        key = url;

    
   
162
        break;
148
      }
163
      }
149
    }
164
    }
150
    return key;
165
    return key;
151
  }
166
  }

    
   
167
  

    
   
168
  /**

    
   
169
   * Creates a new URL with the default port if one is not already there.

    
   
170
   *

    
   
171
   * @param gadgetUrl

    
   
172
   *          The gadget URL to add the port to.

    
   
173
   * @return A new URL with the default port.

    
   
174
   */

    
   
175
  private String createUrlWithPort(String gadgetUrl) {

    
   
176
    try {

    
   
177
      URL origUrl = new URL(gadgetUrl);

    
   
178
      URL urlWithPort = null;

    
   
179
      String origHost = origUrl.getHost();

    
   
180
      if (origUrl.getPort() <= 0 && origHost != null && origHost.length() != 0

    
   
181
              && !STAR.equals(origHost)) {

    
   
182
        if (origUrl.getProtocol().equalsIgnoreCase(HTTP)) {

    
   
183
          urlWithPort = new URL(origUrl.getProtocol(), origUrl.getHost(), HTTP_PORT, origUrl.getFile());

    
   
184
        }

    
   
185
        if (origUrl.getProtocol().equalsIgnoreCase(HTTPS)) {

    
   
186
          urlWithPort = new URL(origUrl.getProtocol(), origUrl.getHost(), HTTPS_PORT, origUrl.getFile());

    
   
187
        }

    
   
188
        return urlWithPort == null ? origUrl.toString() : urlWithPort.toString();

    
   
189
      } else {

    
   
190
        return origUrl.toString();

    
   
191
      }

    
   
192
    } catch (MalformedURLException e) {

    
   
193
      return gadgetUrl;

    
   
194
    }

    
   
195
  }
152

    
   
196

   
153
  @Override
197
  @Override
154
  public boolean equals(Object obj) {
198
  public boolean equals(Object obj) {
155
    if (obj instanceof ContainerAdminData) {
199
    if (obj instanceof ContainerAdminData) {
156
      ContainerAdminData test = (ContainerAdminData) obj;
200
      ContainerAdminData test = (ContainerAdminData) obj;
[+20] [20] 12 lines
http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/admin/BasicGadgetAdminStoreTest.java
Revision 1185758 New Change
 
http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/admin/ContainerAdminDataTest.java
Revision 1185758 New Change
 
  1. http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/admin/ContainerAdminData.java: Loading...
  2. http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/admin/BasicGadgetAdminStoreTest.java: Loading...
  3. http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/admin/ContainerAdminDataTest.java: Loading...