Examples of HttpUriRequest


Examples of org.apache.http.client.methods.HttpUriRequest

      return false;
    }
  }

  private HttpResponse makeUnsecureFetch(HttpRequest request) throws GadgetException {
    HttpUriRequest httpMethod = null;
    Preconditions.checkNotNull(request);
    final String methodType = request.getMethod();

    final org.apache.http.HttpResponse response;
    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();
      } catch (UnsupportedOperationException e) {
        // ignore
      }
    }
  }
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", ssf, 443));

    DefaultHttpClient client = new DefaultHttpClient(ccm, base.getParams());
   
    final HttpUriRequest httpurirequest = new HttpGet(url);
    HttpResponse response = client.execute(httpurirequest);
    HttpEntity entity = response.getEntity();
   
        BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
        StringBuilder sb = new StringBuilder();
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

  }


  public static Tuple<String, InputStream> doSubmit(final String url, final List<NameValuePair> content, final String requestHeader, final boolean useMethodGET) throws IOException {
    final HttpClient httpclient = new DefaultHttpClient();
    final HttpUriRequest httpurirequest;
    if(useMethodGET){
      // first build uri with get parameters...
      String urlAndParams = url;
      boolean firstTime = true;
      for(final NameValuePair param: content){
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

   * @return Tuple with content type and the answer as stream
   * @throws IOException
   */
  public static Tuple<String, InputStream> doSubmitStream(final String url, final InputStream stream, final String requestHeader) throws IOException {
    final HttpClient httpclient = new DefaultHttpClient();
    final HttpUriRequest httpurirequest;

    final HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Accept", requestHeader);
    final InputStreamEntity ise = new InputStreamEntity(stream,-1);
    httppost.setEntity(ise);
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

    public boolean httpHead(Node graphName) {
        return doHead(target(graphName)) ;
    }

    protected boolean doHead(String url) {
        HttpUriRequest httpHead = new HttpHead(url) ;
        try {
            HttpOp.execHttpHead(url, WebContent.defaultGraphAcceptHeader, noResponse, null, null, this.authenticator) ;
            return true ;
        } catch (HttpException ex) {
            if ( ex.getResponseCode() == HttpSC.NOT_FOUND_404 )
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

                        + " Total=" + (res.getHeadersSize() + res.getBodySize()));
            }

            // If we redirected automatically, the URL may have changed
            if (getAutoRedirects()){
                HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
                HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                URI redirectURI = req.getURI();
                if (redirectURI.isAbsolute()){
                    res.setURL(redirectURI.toURL());
                } else {
                    res.setURL(new URL(new URL(target.toURI()),redirectURI.toString()));
                }
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

        }
    }

    @Override
    public boolean interrupt() {
        HttpUriRequest request = currentRequest;
        if (request != null) {
            currentRequest = null; // don't try twice
            try {
                request.abort();
            } catch (UnsupportedOperationException e) {
                log.warn("Could not abort pending request", e);
            }
        }
        return request != null;
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

                this.log.debug("[exchange: " + state.getId() + "] Response processed");
            }
            connManager.releaseConnection();
        } else {
            if (state.getRedirect() != null) {
                final HttpUriRequest redirect = state.getRedirect();
                final URI uri = redirect.getURI();
                if (this.log.isDebugEnabled()) {
                    this.log.debug("[exchange: " + state.getId() + "] Redirecting to '" + uri + "'");
                }
                state.setRedirect(null);
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

                final int maxRedirects = config.getMaxRedirects() >= 0 ? config.getMaxRedirects() : 100;
                if (state.getRedirectCount() >= maxRedirects) {
                    throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
                }
                state.incrementRedirectCount();
                final HttpUriRequest redirect = this.redirectStrategy.getRedirect(currentRequest, currentResponse,
                    localContext);
                state.setRedirect(redirect);
                return true;
            }
        }
View Full Code Here

Examples of org.apache.http.client.methods.HttpUriRequest

        }
    }
   
    @Test
    public void testPostBody() throws Exception {
        HttpUriRequest method = new HttpPost("http://localhost:9080/users/homer");
        ((HttpEntityEnclosingRequestBase)method).setEntity(new StringEntity(MSG_BODY));
       
        HttpResponse response = doExecute(method);
       
        assertHttpResponse(response, 200, "text/plain");
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.