Package org.apache.shindig.common.uri

Examples of org.apache.shindig.common.uri.UriBuilder



  protected Uri normalizeUrl(Uri url) {
    if (url.getScheme() == null) {
      // Assume http
      url = new UriBuilder(url).setScheme("http").toUri();
    }

    if (url.getPath() == null || url.getPath().length() == 0) {
      url = new UriBuilder(url).setPath("/").toUri();
    }

    return url;
  }
View Full Code Here


        // TODO: Spec does not say what to do for an invalid relative path.
        // Just ignoring for now.
      }
    }

    UriBuilder uri = new UriBuilder(href);
    uri.addQueryParameter("lang", context.getLocale().getLanguage());
    uri.addQueryParameter("country", context.getLocale().getCountry());

    OAuthArguments oauthArgs = new OAuthArguments(view);
    oauthArgs.setProxiedContentRequest(true);

    HttpRequest request = new HttpRequest(uri.toUri())
        .setIgnoreCache(context.getIgnoreCache())
        .setOAuthArguments(oauthArgs)
        .setAuthType(view.getAuthType())
        .setSecurityToken(context.getToken())
        .setContainer(context.getContainer())
View Full Code Here

  @Test
  public void renderProxiedRelative() throws Exception {
    Uri base = EXPECTED_PROXIED_HTML_HREF;
    final Uri relative = Uri.parse("/some/path?foo=bar");
    Uri resolved = new UriBuilder(base.resolve(relative))
      .addQueryParameter("lang", GadgetSpec.DEFAULT_LOCALE.getLanguage())
      .addQueryParameter("country", GadgetSpec.DEFAULT_LOCALE.getCountry())
      .toUri();

    HttpRequest request = new HttpRequest(resolved);
View Full Code Here

    assertEquals(PROXIED_HTML_CONTENT, content);
  }

  @Test
  public void renderProxiedCustomLocale() throws Exception {
    UriBuilder uri = new UriBuilder(PROXIED_HTML_HREF);
    uri.putQueryParameter("lang", "foo");
    uri.putQueryParameter("country", "BAR");

    Gadget gadget = makeHrefGadget("none");
    gadget.setContext(new GadgetContext() {
      @Override
      public Locale getLocale() {
        return new Locale("foo", "BAR");
      }

      @Override
      public SecurityToken getToken() {
        return new AnonymousSecurityToken();
      }
    });

    pipeline.plainResponses.put(uri.toUri(), new HttpResponse(PROXIED_HTML_CONTENT));
    String content = proxyRenderer.render(gadget);
    assertEquals(PROXIED_HTML_CONTENT, content);
  }
View Full Code Here

          HttpRequest request, OAuthResponseParams responseParams) {
        SecurityToken st = request.getSecurityToken();
        Uri activeUrl = Uri.parse(st.getActiveUrl());
        assertEquals(GADGET_MAKE_REQUEST_URL, activeUrl.toString());
        assertEquals(GadgetTokenStoreTest.DEFAULT_CALLBACK, baseCallback);
        return new UriBuilder()
            .setScheme("http")
            .setAuthority(activeUrl.getAuthority())
            .setPath("/realcallback")
            .toString();
      }     
View Full Code Here

    String container = gadget.getContext().getContainer();
    String jsHost = getReqConfig(container, JS_HOST_PARAM);
    String jsPathBase = getReqConfig(container, JS_PATH_PARAM);
   
    // We somewhat cheat in that jsHost may contain protocol/scheme as well.
    UriBuilder uri = new UriBuilder(Uri.parse(jsHost));
   
    // Add JS info to path and set it in URI.
    StringBuilder jsPath = new StringBuilder(jsPathBase);
    if (!jsPathBase.endsWith("/")) {
      jsPath.append("/");
    }
    jsPath.append(addJsLibs(extern));
    jsPath.append(JS_SUFFIX);
    uri.setPath(jsPath.toString());
   
    // Standard container param, as JS may be container-specific.
    uri.addQueryParameter(Param.CONTAINER.getKey(), container);
   
    // Pass through nocache param for dev purposes.
    uri.addQueryParameter(Param.NO_CACHE.getKey(),
        gadget.getContext().getIgnoreCache() ? "1" : "0");
   
    // Pass through debug param for debugging use.
    uri.addQueryParameter(Param.DEBUG.getKey(),
        gadget.getContext().getDebug() ? "1" : "0");
   
    // Pass through gadget Uri
    if (addGadgetUri()) {
      uri.addQueryParameter(Param.URL.getKey(), gadget.getSpec().getUrl().toString());
    }
   
    // Finally, version it, but only if !nocache.
    if (versioner != null && !gadget.getContext().getIgnoreCache()) {
      uri.addQueryParameter(Param.VERSION.getKey(),
          versioner.version(gadget.getSpec().getUrl(), container, extern));
    }
   
    return uri.toUri();
  }
View Full Code Here

  public void setTemplatingSignal(TemplatingSignal tplSignal) {
    this.tplSignal = tplSignal;
  }
 
  public Uri makeRenderingUri(Gadget gadget) {
    UriBuilder uri;
    View view = gadget.getCurrentView();
   
    GadgetContext context = gadget.getContext();
    String container = context.getContainer();
   
    if (View.ContentType.URL.equals(view.getType())) {
      // A. type=url. Initializes all except standard parameters.
      uri = new UriBuilder(view.getHref());
    } else {
      // B. Others, aka. type=html and html_sanitized.
      uri = new UriBuilder();

      // 1. Set base path.
      uri.setPath(getReqVal(container, IFRAME_BASE_PATH_KEY));
   
      // 2. Set host/authority.
      String host;
      if (usingLockedDomain(gadget, container)) {
        host = ldGen.getLockedDomainPrefix(gadget.getSpec().getUrl()) +
            getReqVal(container, LOCKED_DOMAIN_SUFFIX_KEY);
      } else {
        host = getReqVal(container, UNLOCKED_DOMAIN_KEY);
      }
      uri.setAuthority(host);
   
      // 3. Set protocol/schema.
      uri.setScheme(getScheme(gadget, container));
     
      // 4. Add the URL.
      uri.addQueryParameter(Param.URL.getKey(), context.getUrl().toString());
    }
   
    // Add container, whose input derived other components of the URI.
    uri.addQueryParameter(Param.CONTAINER.getKey(), container);
   
    // Add remaining non-url standard parameters, in templated or filled form.
    boolean useTpl = tplSignal != null ? tplSignal.useTemplates() : DEFAULT_USE_TEMPLATES;
    addParam(uri, Param.VIEW.getKey(), view.getName(), useTpl, false);
    addParam(uri, Param.LANG.getKey(), context.getLocale().getLanguage(), useTpl, false);
    addParam(uri, Param.COUNTRY.getKey(), context.getLocale().getCountry(), useTpl, false);
    addParam(uri, Param.DEBUG.getKey(), context.getDebug() ? "1" : "0", useTpl, false);
    addParam(uri, Param.NO_CACHE.getKey(), context.getIgnoreCache() ? "1" : "0", useTpl, false);
   
    // Add all UserPrefs
    UserPrefs prefs = context.getUserPrefs();
    for (UserPref up : gadget.getSpec().getUserPrefs()) {
      String name = up.getName();
      String data = prefs.getPref(name);
      if (data == null) {
        data = up.getDefaultValue();
      }
     
      boolean upInFragment = !view.needsUserPrefSubstitution();
      addParam(uri, UriCommon.USER_PREF_PREFIX + up.getName(), data, useTpl, upInFragment);
    }
   
    if (versioner != null) {
      // Added on the query string, obviously not templated.
      addParam(uri, Param.VERSION.getKey(),
          versioner.version(gadget.getSpec().getUrl(), container), false, false);
    }
   
    if (gadget.getAllFeatures().contains(SECURITY_TOKEN_FEATURE_NAME) ||
        config.getBool(container, SECURITY_TOKEN_ALWAYS_KEY)) {
      boolean securityTokenOnQuery = isTokenNeededForRendering(gadget);
      String securityToken = null// Always templated at the moment, can ignore.
      addParam(uri, Param.SECURITY_TOKEN.getKey(), securityToken, true, !securityTokenOnQuery);
    }
   
    addExtras(uri);
   
    return uri.toUri();
  }
View Full Code Here

  protected boolean isTokenNeededForRendering(Gadget gadget) {
    return true;
  }
 
  public UriStatus validateRenderingUri(Uri inUri) {
    UriBuilder uri = new UriBuilder(inUri);
   
    String gadgetStr = uri.getQueryParameter(Param.URL.getKey());
    Uri gadgetUri = null;
    try {
      gadgetUri = Uri.parse(gadgetStr);
    } catch (Exception e) {
      // RuntimeException eg. InvalidArgumentException
      return UriStatus.BAD_URI;
    }
   
    String container = uri.getQueryParameter(Param.CONTAINER.getKey());
    if (container == null) {
      container = ContainerConfig.DEFAULT_CONTAINER;
    }
   
    // Validate domain.
    String host = uri.getAuthority().toLowerCase();
    String gadgetLdPrefix = ldGen.getLockedDomainPrefix(gadgetUri).toLowerCase();

    // If the uri starts with gadget's locked domain prefix, then the suffix
    // must be an exact match as well.
    // Lower-case to prevent casing from being relevant.
    if (ldEnabled && !lockedDomainExclusion()) {
      if (host.startsWith(gadgetLdPrefix)) {
        // Strip off prefix.
        host = host.substring(gadgetLdPrefix.length());
        String ldSuffix = getReqVal(container, LOCKED_DOMAIN_SUFFIX_KEY);
        if (!ldSuffix.equalsIgnoreCase(host)) {
          return UriStatus.INVALID_DOMAIN;
        }
      } else {
        // We need to also ensure that the URI isn't that of another valid
        // locked-domain gadget. We do this test second as it's less efficient.
        // Also, since we've already tested the "valid" locked domain case
        // we can simply say the URI is invalid if it ends with any valid
        // locked domain suffix.
        for (String ldSuffix : ldSuffixes) {
          if (host.endsWith(ldSuffix)) {
            return UriStatus.INVALID_DOMAIN;
          }
        }
      }
    }
   
    String version = uri.getQueryParameter(Param.VERSION.getKey());
    if (versioner == null || version == null) {
      return UriStatus.VALID_UNVERSIONED;
    }
   
    return versioner.validate(gadgetUri, container, version);
View Full Code Here

  public HttpRequest sanitizeAndSign(HttpRequest base, List<Parameter> params,
      boolean tokenEndpoint) throws OAuthRequestException {
    if (params == null) {
      params = Lists.newArrayList();
    }
    UriBuilder target = new UriBuilder(base.getUri());
    String query = target.getQuery();
    target.setQuery(null);
    params.addAll(sanitize(OAuth.decodeForm(query)));

    switch(OAuthUtil.getSignatureType(tokenEndpoint, base.getHeader("Content-Type"))) {
      case URL_ONLY:
        break;
      case URL_AND_FORM_PARAMS:
        try {
          params.addAll(sanitize(OAuth.decodeForm(base.getPostBodyAsString())));
        } catch (IllegalArgumentException e) {
          // Occurs if OAuth.decodeForm finds an invalid URL to decode.
          throw responseParams.oauthRequestException(OAuthError.INVALID_REQUEST,
              "Could not decode body", e);
        }
        break;
      case URL_AND_BODY_HASH:
        try {
          byte[] body = IOUtils.toByteArray(base.getPostBody());
          byte[] hash = DigestUtils.sha(body);
          String b64 = new String(Base64.encodeBase64(hash), CharsetUtil.UTF8.name());
          params.add(new Parameter(OAuthConstants.OAUTH_BODY_HASH, b64));
        } catch (IOException e) {
          throw responseParams.oauthRequestException(OAuthError.UNKNOWN_PROBLEM,
              "Error taking body hash", e);
        }
        break;
    }

    addIdentityParams(params);

    addSignatureParams(params);

    try {
      OAuthMessage signed = OAuthUtil.newRequestMessage(accessorInfo.getAccessor(),
          base.getMethod(), target.toString(), params);
      HttpRequest oauthHttpRequest = createHttpRequest(base, selectOAuthParams(signed));
      // Following 302s on OAuth responses is unlikely to be productive.
      oauthHttpRequest.setFollowRedirects(false);
      return oauthHttpRequest;
    } catch (OAuthException e) {
View Full Code Here

        // TODO: Spec does not say what to do for an invalid relative path.
        // Just ignoring for now.
      }
    }

    UriBuilder uri = new UriBuilder(href);
    uri.addQueryParameter("lang", context.getLocale().getLanguage());
    uri.addQueryParameter("country", context.getLocale().getCountry());

    OAuthArguments oauthArgs = new OAuthArguments(view);
    oauthArgs.setProxiedContentRequest(true);

    HttpRequest request = new HttpRequest(uri.toUri())
        .setIgnoreCache(context.getIgnoreCache())
        .setOAuthArguments(oauthArgs)
        .setAuthType(view.getAuthType())
        .setSecurityToken(context.getToken())
        .setContainer(context.getContainer())
View Full Code Here

TOP

Related Classes of org.apache.shindig.common.uri.UriBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.