Package org.apache.shindig.gadgets

Examples of org.apache.shindig.gadgets.GadgetException


        URI gadgetUri = new URI(url);
        JSONObject oauthConfig = oauthConfigs.getJSONObject(url);
        storeConsumerInfos(gadgetUri, oauthConfig);
      }
    } catch (JSONException e) {
      throw new GadgetException(GadgetException.Code.OAUTH_STORAGE_ERROR, e);
    } catch (URISyntaxException e) {
      throw new GadgetException(GadgetException.Code.OAUTH_STORAGE_ERROR, e);
    }
  }
View Full Code Here


    BasicOAuthStoreConsumerKeyAndSecret cks = consumerInfos.get(pk);
    if (cks == null) {
      cks = defaultKey;
    }
    if (cks == null) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR,
          "No key for gadget " + securityToken.getAppUrl() + " and service " + serviceName);
    }
    OAuthConsumer consumer = null;
    if (cks.getKeyType() == KeyType.RSA_PRIVATE) {
      consumer = new OAuthConsumer(null, cks.getConsumerKey(), null, provider);
View Full Code Here

    final long started = System.currentTimeMillis();

    // Break the request Uri to its components:
    Uri uri = request.getUri();
    if (Strings.isNullOrEmpty(uri.getAuthority())) {
      throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
          "Missing domain name for request: " + uri,
          HttpServletResponse.SC_BAD_REQUEST);
    }
    if (Strings.isNullOrEmpty(uri.getScheme())) {
      throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
          "Missing schema for request: " + uri,
          HttpServletResponse.SC_BAD_REQUEST);
    }
    String[] hostparts = StringUtils.splitPreserveAllTokens(uri.getAuthority(),':');
    int port = -1; // default port
    if (hostparts.length > 2) {
      throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
          "Bad host name in request: " + uri.getAuthority(),
          HttpServletResponse.SC_BAD_REQUEST);
    }
    if (hostparts.length == 2) {
      try {
        port = Integer.parseInt(hostparts[1]);
      } catch (NumberFormatException e) {
        throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
            "Bad port number in request: " + uri.getAuthority(),
            HttpServletResponse.SC_BAD_REQUEST);
      }
    }

    String requestUri = uri.getPath();
    // Treat path as / if set as null.
    if (uri.getPath() == null) {
      requestUri = "/";
    }
    if (uri.getQuery() != null) {
      requestUri += '?' + uri.getQuery();
    }

    // Get the http host to connect to.
    HttpHost host = new HttpHost(hostparts[0], port, uri.getScheme());

    try {
      if ("POST".equals(methodType) || "PUT".equals(methodType)) {
        HttpEntityEnclosingRequestBase enclosingMethod = ("POST".equals(methodType))
            ? new HttpPost(requestUri)
        : new HttpPut(requestUri);

            if (request.getPostBodyLength() > 0) {
              enclosingMethod.setEntity(new InputStreamEntity(request.getPostBody(), request.getPostBodyLength()));
            }
            httpMethod = enclosingMethod;
      } else if ("GET".equals(methodType)) {
        httpMethod = new HttpGet(requestUri);
      } else if ("HEAD".equals(methodType)) {
        httpMethod = new HttpHead(requestUri);
      } else if ("DELETE".equals(methodType)) {
        httpMethod = new HttpDelete(requestUri);
      }
      for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
        httpMethod.addHeader(entry.getKey(), Joiner.on(',').join(entry.getValue()));
      }

      // Disable following redirects.
      if (!request.getFollowRedirects()) {
        httpMethod.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
      }

      // HttpClient doesn't handle all cases when breaking url (specifically '_' in domain)
      // So lets pass it the url parsed:
      response = trustAllFetcher.execute(host, httpMethod);

      if (response == null) {
        throw new IOException("Unknown problem with request");
      }

      long now = System.currentTimeMillis();
      if (now - started > slowResponseWarning) {
        slowResponseWarning(request, started, now);
      }

      return makeResponse(response);

    } catch (Exception e) {
      long now = System.currentTimeMillis();

      // Find timeout exceptions, respond accordingly
      if (TIMEOUT_EXCEPTIONS.contains(e.getClass())) {
        if (LOG.isLoggable(Level.INFO)) {
          LOG.logp(Level.INFO, CLASS, "fetch", MessageKeys.TIMEOUT_EXCEPTION, new Object[] {request.getUri(),CLASS,e.getMessage(),now-started});
        }
        return HttpResponse.timeout();
      }
      if (LOG.isLoggable(Level.INFO)) {
        LOG.logp(Level.INFO, CLASS, "fetch", MessageKeys.EXCEPTION_OCCURRED, new Object[] {request.getUri(),now-started});
        LOG.logp(Level.INFO, CLASS, "fetch", "", e);
      }
      // Separate shindig error from external error
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e,
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
      // cleanup any outstanding resources..
      if (httpMethod != null) try {
        httpMethod.abort();
View Full Code Here

    if(extPoint != null) {
      try {
        return extPoint.getContainerOAuth2Store();
      } catch (ContainerExtPointException e) {
        log.logp(Level.WARNING, CLASS, method, "There was an error getting the OAuth2Store for container " + container, e);
        throw new GadgetException(GadgetException.Code.OAUTH_STORAGE_ERROR,
            "There was an error getting the OAuth2Store for container " + container, e);
      }
    } else {
      log.logp(Level.WARNING, CLASS, method, "There was no ContainerExtPoint for container {0}.", new Object[] {container});
      throw new GadgetException(GadgetException.Code.OAUTH_STORAGE_ERROR, "No ContainerExtPoint for container " + container);
    }
  }
View Full Code Here

    return generateKey(container, token.getServiceName(), token.getScope(), token.getUser());
  }

  private String generateKey(String container, String serviceName, String scope, String user) throws GadgetException {
    if(container == null || serviceName == null || user == null) {
      throw new GadgetException(GadgetException.Code.OAUTH_STORAGE_ERROR, "Invalid key parameters, container: " + container + " user: " + user + " service name: " + serviceName);
    }
    return container + ":" + serviceName + ":" + scope + ":" + user;
  }
View Full Code Here

  public DominoOAuth2Accessor getOAuth2Accessor(DominoOAuth2CallbackState state) throws GadgetException {
    DominoOAuth2Accessor ret = accessorStore.get(generateKey(state));
    if (ret == null || !ret.isValid()) {
      final DominoOAuth2Client client = getClient(state);
      if (client == null) {
        throw new GadgetException(Code.OAUTH_STORAGE_ERROR,
            "Could not find OAuth2 client information where container = " + state.getContainer() +
            ", user = " + state.getUser() + ", serviceName = " + state.getServiceName() + ", and scope = " + state.getScope() + ".");
      } else {
        ret = createAccessor(state, client);
        this.storeOAuth2Accessor(ret);
View Full Code Here

  @Override
  public ConsumerInfo getConsumerKeyAndSecret(SecurityToken securityToken,
      String serviceName, OAuthServiceProvider provider)
      throws GadgetException {
    if(securityToken.isAnonymous() || "@anonymous".equals(securityToken.getViewerId())) {
      throw new GadgetException(Code.INVALID_SECURITY_TOKEN, "Anonymous users cannot use OAuth in gadgets");
    }
    ContainerExtPoint extPoint = getContainerExtPoint(securityToken.getContainer());
    DominoOAuthStore store = null;
    try {
      store = extPoint.getContainerOAuthStore();
    } catch (ContainerExtPointException e) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR,
          "Exception thrown when getting the DominoOAuthStore for the container " + securityToken.getContainer());
    }
    if(store == null) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR,
          "No DominoOAuthStore provided for the container " + securityToken.getContainer());
    }
    DominoOAuthClient client = store.getClient(securityToken.getViewerId(), securityToken.getContainer(),
        serviceName, securityToken.getAppUrl());
    if(client == null) {
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR,
          "The Domino OAuth Store for container " + securityToken.getContainer() + " did not return client information for viewer: " +
      securityToken.getViewerId() + ", service: " + serviceName + " gadget: " + securityToken.getAppUrl());
    }
      OAuthConsumer consumer;
      final KeyType keyType = client.getKeyType();
View Full Code Here

  }
 
  private ContainerExtPoint getContainerExtPoint(String container) throws GadgetException {
    ContainerExtPoint extPoint = extPointManager.getExtPoint(container);
    if(extPoint == null) {
      throw new GadgetException(Code.OAUTH_STORAGE_ERROR,
          "No container extension point could be found for the container with the name " + container +
          ".");
    }
    return extPoint;
  }
View Full Code Here

    final long started = System.currentTimeMillis();

    // Break the request Uri to its components:
    Uri uri = request.getUri();
    if (StringUtils.isEmpty(uri.getAuthority())) {
      throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
          "Missing domain name for request: " + uri,
          HttpServletResponse.SC_BAD_REQUEST);
    }
    if (StringUtils.isEmpty(uri.getScheme())) {
      throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
          "Missing schema for request: " + uri,
          HttpServletResponse.SC_BAD_REQUEST);
    }
    String[] hostparts = uri.getAuthority().split(":");
    int port = -1; // default port
    if (hostparts.length > 2) {
      throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
          "Bad host name in request: " + uri.getAuthority(),
          HttpServletResponse.SC_BAD_REQUEST);
    }
    if (hostparts.length == 2) {
      try {
        port = Integer.parseInt(hostparts[1]);
      } catch (NumberFormatException e) {
        throw new GadgetException(GadgetException.Code.INVALID_USER_DATA,
            "Bad port number in request: " + uri.getAuthority(),
            HttpServletResponse.SC_BAD_REQUEST);
      }
    }
    HttpHost host = new HttpHost(hostparts[0], port, uri.getScheme());  
    String requestUri = uri.getPath();
    if (uri.getQuery() != null) {
      requestUri += "?" + uri.getQuery();
    }

    try {
      if ("POST".equals(methodType) || "PUT".equals(methodType)) {
        HttpEntityEnclosingRequestBase enclosingMethod = ("POST".equals(methodType))
          ? new HttpPost(requestUri)
          : new HttpPut(requestUri);

        if (request.getPostBodyLength() > 0) {
          enclosingMethod.setEntity(new InputStreamEntity(request.getPostBody(), request.getPostBodyLength()));
        }
        httpMethod = enclosingMethod;
      } else if ("GET".equals(methodType)) {
        httpMethod = new HttpGet(requestUri);
      } else if ("HEAD".equals(methodType)) {
        httpMethod = new HttpHead(requestUri);
      } else if ("DELETE".equals(methodType)) {
        httpMethod = new HttpDelete(requestUri);
      }
      for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
        httpMethod.addHeader(entry.getKey(), StringUtils.join(entry.getValue(), ','));
      }

      if (!request.getFollowRedirects())
        httpMethod.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);

      // HttpClient doesn't handle all cases when breaking url (specifically '_' in domain)
      // So lets pass it the url parsed:
      response = FETCHER.execute(host, httpMethod);

      if (response == null)
        throw new IOException("Unknown problem with request");

      if (response.getEntity() == null) {
        throw new IOException("Cannot retrieve " + request.getUri() + " reason " + response.getStatusLine().getReasonPhrase());
      }

      long now = System.currentTimeMillis();
      if (now - started > slowResponseWarning) {
        slowResponseWarning(request, started, now);
      }

      return makeResponse(response);

    } catch (Exception e) {
      long now = System.currentTimeMillis();

      // Find timeout exceptions, respond accordingly
      if (TIMEOUT_EXCEPTIONS.contains(e.getClass())) {
        LOG.info("Timeout for " + request.getUri() + " Exception: " + e.getClass().getName() + " - " + e.getMessage() + " - " + (now - started) + "ms");
        return HttpResponse.timeout();
      }

        //changing debug level to FINE from INFO
      LOG.log(Level.FINE, "Got Exception fetching " + request.getUri() + " - " + (now - started) + "ms", e);

      // Separate shindig error from external error
      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e,
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
      // cleanup any outstanding resources..
      if (httpMethod != null) try {
        httpMethod.abort();
View Full Code Here

                    res = reg.get(arr[i]);
                    realStoreConsumerInfo(null, res.getProperty(DashboardConstants.CONSUMER_SERVICE), res);
                }
            }
        } catch (Exception e) {
            throw new GadgetException(GadgetException.Code.OAUTH_STORAGE_ERROR, e);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.shindig.gadgets.GadgetException

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.