Exemplo n.º 1
0
  protected HttpResponse getHttpResponse(final HttpUriRequest httpRequest, final String url)
      throws DSSException {

    final HttpClient client = getHttpClient(url);

    final String host = httpRequest.getURI().getHost();
    final int port = httpRequest.getURI().getPort();
    final String scheme = httpRequest.getURI().getScheme();
    final HttpHost targetHost = new HttpHost(host, port, scheme);

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    try {
      final HttpResponse response = client.execute(targetHost, httpRequest, localContext);
      return response;
    } catch (IOException e) {
      throw new DSSException(e);
    }
  }
  String createCanonicalRequest(HttpUriRequest request) {
    StringBuilder result = new StringBuilder();
    result.append(request.getMethod()).append('\n');
    String path = request.getURI().getPath();
    if (path.isEmpty()) {
      path = "/";
    }
    result.append(path).append('\n');
    String queryString = request.getURI().getQuery();
    queryString = queryString != null ? queryString : "";
    addCanonicalQueryString(queryString, result).append('\n');
    addCanonicalHeaders(request, result).append('\n');

    HttpEntity entity = null;
    try {
      if (request instanceof HttpEntityEnclosingRequestBase) {
        entity = ((HttpEntityEnclosingRequestBase) request).getEntity();
      } else {
        entity = new StringEntity("");
      }
      InputStream content = entity.getContent();
      addHashedPayload(content, result);
    } catch (IOException e) {
      throw new RuntimeException("Could not create hash for entity " + entity, e);
    }
    return result.toString();
  }
Exemplo n.º 3
0
  private void runLoop() throws InterruptedException {
    while (!stop) {
      CrawlerTask task = crawlerResponseQueue.take();
      if (task.isExitTask()) {
        crawlerRequestQueue.add(CrawlerTask.createExitTask());
        crawlerResponseQueue.add(task);
        return;
      }
      HttpUriRequest req = task.getRequest();
      activeRequest = req;
      try {
        if (task.getResponse() != null) {
          task.getResponseProcessor()
              .processResponse(crawler, req, task.getResponse(), task.getArgument());
        }
      } catch (Exception e) {
        logger.log(
            Level.WARNING, "Unexpected exception processing crawler request: " + req.getURI(), e);
      } finally {
        synchronized (requestLock) {
          activeRequest = null;
        }
        final HttpEntity entity =
            (task.getResponse() == null) ? (null) : task.getResponse().getRawResponse().getEntity();
        if (entity != null)
          try {
            EntityUtils.consume(entity);
          } catch (IOException e) {
            logger.log(
                Level.WARNING,
                "I/O exception consuming request entity content for "
                    + req.getURI()
                    + " : "
                    + e.getMessage());
          }
      }

      synchronized (counter) {
        counter.addCompletedTask();
        crawler.updateProgress();
      }
      if (task.causedException()) {
        crawler.notifyException(req, task.getException());
      }

      if (outstandingTasks.decrementAndGet() <= 0) {
        crawlerRequestQueue.add(CrawlerTask.createExitTask());
        crawlerResponseQueue.add(CrawlerTask.createExitTask());
        return;
      }
    }
  }
Exemplo n.º 4
0
 private static String getUrlAfterRedirects(HttpContext context) {
   String lastRedirectUrl = (String) context.getAttribute(LAST_REDIRECT_URL);
   if (lastRedirectUrl != null) return lastRedirectUrl;
   else {
     HttpUriRequest currentReq =
         (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
     HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
     String currentUrl =
         (currentReq.getURI().isAbsolute())
             ? currentReq.getURI().toString()
             : (currentHost.toURI() + currentReq.getURI());
     return currentUrl;
   }
 }
Exemplo n.º 5
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    try {
      HttpUriRequest request = queryRequest(uri, projection, selection, selectionArgs, sortOrder);

      if (DEBUG) Log.i(TAG, "will query: " + request.getURI());

      Cursor cursor = getQueryHandler(uri).handleResponse(httpClient.execute(request));
      // httpClient.getConnectionManager().shutdown();
      return cursor;
    } catch (ConnectException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(0, e.getMessage());
    } catch (ClientProtocolException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(0, e.getMessage());
    } catch (IOException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(0, e.getMessage());
    } catch (IllegalArgumentException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(
          0,
          "Unknown URI (not in cache or not answerable by the implementator): " + uri.toString());
    }
  }
Exemplo n.º 6
0
  /**
   * Send an HTTP message to a worker and get the result
   *
   * <p>Note: expects the worker to respond with OK (200) status code.
   *
   * @param uriRequest The request to make
   * @return An InputStream of the response content
   */
  private InputStream askWorker(HttpUriRequest uriRequest) {
    try {
      HttpResponse response = httpClient.execute(uriRequest);

      if (response.getStatusLine().getStatusCode() != Response.Status.OK.getStatusCode()) {
        StatusLine statusLine = response.getStatusLine();
        EntityUtils.consume(response.getEntity());
        logger.warn(
            "Problem asking worker <"
                + metadata.getWorkerId()
                + "> "
                + "("
                + uriRequest.getURI()
                + "), "
                + "reason ["
                + statusLine.getStatusCode()
                + ": "
                + statusLine.getReasonPhrase()
                + "]");
      }

      return response.getEntity().getContent();
    } catch (IOException e) {
      logger.warn("Unable to communicated with worker " + metadata.toString());
      throw Throwables.propagate(e);
    }
  }
Exemplo n.º 7
0
  /**
   * Send an HTTP message to a worker, producing helpful logging if there was a problem
   *
   * @param uriRequest The request to make
   * @param expectedStatus The expected return status
   * @return true if the method was successfully delivered & the worker gave the expected response
   */
  private boolean tellWorker(HttpUriRequest uriRequest, Response.Status expectedStatus) {
    try {
      HttpResponse response = httpClient.execute(uriRequest);

      if (response.getStatusLine().getStatusCode() != expectedStatus.getStatusCode()) {
        StatusLine statusLine = response.getStatusLine();
        EntityUtils.consume(response.getEntity());
        logger.warn(
            "Problem telling worker <"
                + metadata.getWorkerId()
                + "> "
                + "("
                + uriRequest.getURI()
                + "), "
                + "reason ["
                + statusLine.getStatusCode()
                + ": "
                + statusLine.getReasonPhrase()
                + "]");
        return false;
      }

      return true;
    } catch (IOException e) {
      logger.warn("Unable to communicated with worker " + metadata.toString());
      return false;
    }
  }
 @Mock
 public void $init(
     CloudProvider provider,
     HttpClientBuilder clientBuilder,
     HttpUriRequest request,
     ResponseHandler handler) {
   String requestUri = request.getURI().toString();
   if (request.getMethod().equals("GET")
       && requestUri.equals(
           String.format(VM_RESOURCES, ENDPOINT, ACCOUNT_NO, DATACENTER_ID, VM_1_ID))) {
     requestResourceType = 11;
   } else if (request.getMethod().equals("PUT")
       && requestUri.equals(
           String.format(VM_RESOURCES, ENDPOINT, ACCOUNT_NO, DATACENTER_ID, VM_1_ID))) {
     requestResourceType = 12;
     WAPVirtualMachineModel wapVirtualMachineModel = createWAPVirtualMachineModel();
     wapVirtualMachineModel.setOperation(operation);
     assertPut(
         request,
         String.format(VM_RESOURCES, ENDPOINT, ACCOUNT_NO, DATACENTER_ID, VM_1_ID),
         new Header[0],
         wapVirtualMachineModel);
   } else {
     super.$init(provider, clientBuilder, request, handler);
   }
 }
 @Mock
 public void $init(
     CloudProvider provider,
     HttpClientBuilder clientBuilder,
     HttpUriRequest request,
     ResponseHandler handler) {
   String requestUri = request.getURI().toString();
   if (request.getMethod().equals("GET")
       && requestUri.equals(
           String.format(VM_RESOURCES, ENDPOINT, ACCOUNT_NO, DATACENTER_ID, VM_1_ID))) {
     requestResourceType = 1;
   }
   if (request.getMethod().equals("GET")
       && requestUri.equals(
           String.format(VM_NETWORK_ADAPTERS, ENDPOINT, ACCOUNT_NO, DATACENTER_ID, VM_1_ID))) {
     requestResourceType = 2;
   }
   if (request.getMethod().equals("GET")
       && requestUri.equals(String.format(HARDWARE_PROFILES, ENDPOINT, ACCOUNT_NO))) {
     requestResourceType = 3;
   }
   if (request.getMethod().equals("GET")
       && requestUri.equals(String.format(LIST_VM_RESOURCES, ENDPOINT, ACCOUNT_NO))) {
     requestResourceType = 4;
   }
   responseHandler = handler;
 }
  @Test
  public void GetRequest()
      throws URISyntaxException, ClientProtocolException, IOException, ParserConfigurationException,
          UnsupportedOperationException, SAXException {
    HttpUriRequest request = new HttpGet(url);
    URIBuilder uri = new URIBuilder(request.getURI()).addParameter("CountryName", country);
    ((HttpRequestBase) request).setURI(uri.build());

    // Testing headers
    HttpResponse response = client.execute(request);
    Header[] headers = response.getAllHeaders();
    for (Header head : headers) {
      System.out.println(head.getName() + "---" + head.getValue());
    }
    System.out.println("================================================");

    // Testing response
    // parsing xml
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(response.getEntity().getContent());

    // Printing root
    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("string");
    Node aNode = nList.item(0);
    Element element = (Element) aNode;
    System.out.println(element.getFirstChild());
  }
  @Test
  public void shouldHandleMultipleInvocationsOfExecute() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    Robolectric.addPendingHttpResponse(201, "another happy response body");

    requestDirector.execute(null, new HttpGet("http://example.com"), null);
    requestDirector.execute(null, new HttpGet("www.example.com"), null);

    HttpUriRequest request1 = (HttpUriRequest) Robolectric.getSentHttpRequest(0);
    assertThat(request1.getMethod(), equalTo(HttpGet.METHOD_NAME));
    assertThat(request1.getURI(), equalTo(URI.create("http://example.com")));

    HttpUriRequest request2 = (HttpUriRequest) Robolectric.getSentHttpRequest(1);
    assertThat(request2.getMethod(), equalTo(HttpGet.METHOD_NAME));
    assertThat(request2.getURI(), equalTo(URI.create("www.example.com")));
  }
Exemplo n.º 12
0
  public PingInfo ping(String host, int restPort, boolean noProxy) throws Exception {

    PingInfo myPingInfo = pingInfoProvider.createPingInfo();

    RequestConfig.Builder requestConfigBuilder =
        httpClientProvider.createRequestConfigBuilder(host, noProxy);

    String url = String.format(URL_PATTERN, host, restPort) + PingHandler.URL;
    HttpUriRequest request =
        RequestBuilder.post(url)
            .setConfig(requestConfigBuilder.build())
            .addParameter(PingHandler.PING_INFO_INPUT_NAME, gson.toJson(myPingInfo))
            .build();

    CloseableHttpResponse response = httpClientProvider.executeRequest(request);
    try {
      StatusLine statusLine = response.getStatusLine();
      if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
        EntityUtils.consumeQuietly(response.getEntity());
        throw new Exception(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
      }

      HttpEntity entity = response.getEntity();
      String content = EntityUtils.toString(entity);
      EntityUtils.consumeQuietly(entity);
      PingInfo receivedPingInfo = gson.fromJson(content, PingInfo.class);
      receivedPingInfo.getAgentId().setHost(request.getURI().getHost());
      return receivedPingInfo;
    } finally {
      response.close();
    }
  }
Exemplo n.º 13
0
  private HttpStatus commit(HttpUriRequest method) throws IOException {
    HttpStatus status;
    HttpClient httpclient = createHttpClient();
    // reduce the TIME_WAIT
    // method.addHeader("Connection", "close");

    if (method.getFirstHeader("Referer") == null) {
      URI uri = method.getURI();
      method.setHeader("Referer", uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort());
    }
    ;

    try {
      HttpResponse resp = execute(method, httpclient);

      status = new HttpStatus(resp.getStatusLine());
      if (resp.getEntity() != null) {
        status.setMessage(EntityUtils.toString(resp.getEntity(), "UTF-8"));
      }
    } catch (IOException e) {
      // cancel the connection when the error happen
      method.abort();
      throw e;
    } finally {
      HttpClientUtils.abortConnection(method, httpclient);
    }
    return status;
  }
  public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException {
    // Rewrite the supplied URL...
    URI uri = request.getURI();
    String original = uri.toString();
    UrlRules rules = UrlRules.getRules(mResolver);
    UrlRules.Rule rule = rules.matchRule(original);
    String rewritten = rule.apply(original);

    if (rewritten == null) {
      Log.w(TAG, "Blocked by " + rule.mName + ": " + original);
      throw new BlockedRequestException(rule);
    } else if (rewritten == original) {
      return executeWithoutRewriting(request, context); // Pass through
    }

    try {
      uri = new URI(rewritten);
    } catch (URISyntaxException e) {
      throw new RuntimeException("Bad URL from rule: " + rule.mName, e);
    }

    // Wrap request so we can replace the URI.
    RequestWrapper wrapper = wrapRequest(request);
    wrapper.setURI(uri);
    request = wrapper;

    if (Config.LOGV) {
      Log.v(TAG, "Rule " + rule.mName + ": " + original + " -> " + rewritten);
    }
    return executeWithoutRewriting(request, context);
  }
  private HttpResponse executeRequest(HttpClient client, HttpUriRequest request)
      throws ClientProtocolException, IOException {

    if (logger != null && logger.isDebugEnabled()) {

      StringBuilder builder = new StringBuilder();
      Header[] allHeaders = request.getAllHeaders();

      for (Header header : allHeaders) {
        builder.append(header.getName());
        builder.append(":");
        builder.append(header.getValue());
        builder.append("\n");
      }

      logger.debug(
          "Executing request \nurl:["
              + request.getURI().toString()
              + "] \nheaders:\n"
              + builder.toString()
              + "");
    }

    return client.execute(request);
  }
Exemplo n.º 16
0
  @Override
  public Result loadInBackground() {
    final Fetch fetch = mFetch;
    final HttpConnectionHelper helper = mHelper;

    List<NameValuePair> parameters = mParameterExtractor.extract(fetch);
    HttpUriRequest request =
        helper.obtainHttpRequest(fetch.getFetchMethod(), fetch.getUrl(), parameters);
    Utils.debug(request.getURI().toString());
    if (fetch.onPreFetch(request, Collections.unmodifiableList(parameters))) {
      return mResult;
    }
    HttpResponse response = helper.execute(request);
    fetch.onPostFetch(request);
    if (null == response) {
      return null;
    }
    try {
      Result result =
          mResult = mParser.parse(fetch, new InputStreamReader(response.getEntity().getContent()));
      Utils.debug(result);
      return result;
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
Exemplo n.º 17
0
 /**
  * Executes the HTTP request.
  *
  * <p>In case of any exception thrown by HttpClient, it will release the connection. In other
  * cases it is the duty of caller to do it, or process the input stream.
  *
  * @param repository to execute the HTTP method for
  * @param request resource store request that triggered the HTTP request
  * @param httpRequest HTTP request to be executed
  * @param baseUrl The BaseURL used to construct final httpRequest
  * @return response of making the request
  * @throws RemoteStorageException If an error occurred during execution of HTTP request
  */
 @VisibleForTesting
 HttpResponse executeRequest(
     final ProxyRepository repository,
     final ResourceStoreRequest request,
     final HttpUriRequest httpRequest,
     final String baseUrl,
     final boolean contentRequest)
     throws RemoteStorageException {
   final Timer timer = timer(repository, httpRequest, baseUrl);
   final TimerContext timerContext = timer.time();
   Stopwatch stopwatch = null;
   if (outboundRequestLog.isDebugEnabled()) {
     stopwatch = new Stopwatch().start();
   }
   try {
     return doExecuteRequest(repository, request, httpRequest, contentRequest);
   } finally {
     timerContext.stop();
     if (stopwatch != null) {
       outboundRequestLog.debug(
           "[{}] {} {} - {}",
           repository.getId(),
           httpRequest.getMethod(),
           httpRequest.getURI(),
           stopwatch);
     }
   }
 }
 private void attachSignature(
     final HttpUriRequest request, final List<NameValuePair> params, final byte[] content) {
   final RequestDigestBuffer digest =
       RequestDigestBuffer.newBuilder(config)
           .withMethod(request.getMethod())
           .withPath(request.getURI().getPath())
           .withQueryParams(params)
           .withTimestamp(Instant.now(Clock.systemUTC()).toEpochMilli())
           .build();
   final byte[] signature = digest.doFinal(content);
   for (final Header h : digest.requestHeaders(signature)) {
     request.addHeader(h);
   }
 }
 @Override
 public CloseableHttpResponse execute(final HttpUriRequest request, final HttpContext context)
     throws IOException, ClientProtocolException {
   return HttpClientTracing.execute(
       request.getURI().toString(),
       traceHeaderName,
       request,
       new HttpClientTracing.Executor<CloseableHttpResponse>() {
         @Override
         public CloseableHttpResponse execute() throws IOException {
           return client.execute(request, context);
         }
       });
 }
Exemplo n.º 20
0
  private static void printDebugInfo(HttpUriRequest request, HttpResponse response) {
    String br = System.getProperty("line.separator");
    StringBuffer builder = new StringBuffer();
    builder.append("URL: ");
    builder.append(request.getURI().toString());
    builder.append(br);
    builder.append("Request Headers: ");
    builder.append(headersToString(request.getAllHeaders()));
    builder.append(br);
    builder.append("Request Body: ");
    if (request instanceof HttpPost) {
      HttpPost post = (HttpPost) request;
      String body = "This body can't be outputted.";
      try {
        body = ResponseUtil.fromHttpEntityToString(post.getEntity(), "UTF-8");
      } catch (ResponseBodyParseException e) {
        Log.d(TAG, "Parsing request is failed.", e);
      }
      builder.append(body);
    } else {
      builder.append("No body by Get request.");
    }
    builder.append(br);
    builder.append("HTTP Status Code: ");
    builder.append(response.getStatusLine().getStatusCode());
    builder.append(br);
    builder.append("Response Headers: ");
    builder.append(headersToString(response.getAllHeaders()));
    builder.append(br);
    Header contentType = response.getFirstHeader("Content-Type");
    if (contentType != null && "application/json".equals(contentType.getValue())) {
      builder.append("Response Body: ");
      String body = "This body can't be outputted.";
      try {
        body = ResponseUtil.fromHttpEntityToString(response.getEntity(), "UTF-8");
        response.setEntity(new StringEntity(body, "UTF-8"));
      } catch (ResponseBodyParseException e) {
        Log.d(TAG, "Parsing response is failed.", e);
      } catch (UnsupportedEncodingException e) {
        Log.e(TAG, e.getMessage());
      }
      builder.append(body);
      builder.append(br);
    } else {
      builder.append("Response Body is binary data.");
      builder.append(br);
    }

    Log.d(TAG, builder.toString());
  }
Exemplo n.º 21
0
  private ManticoreHttpResponse httpExecute(HttpUriRequest request) {
    int statusCode = 200;
    String ret = "";
    try {
      Logger.verbose(
          LOG_TAG,
          String.format("Executing %s %s", request.getMethod(), request.getURI().toString()));
      ret = _http.execute(request, _handler, _context);
    } catch (HttpResponseException httpEx) {
      // Bad status code
      statusCode = httpEx.getStatusCode();
      ret = httpEx.getLocalizedMessage();
      Logger.warn(
          LOG_TAG,
          String.format("Error executing %s %s", request.getMethod(), request.getURI().toString()),
          httpEx);
    } catch (IOException ioEx) {
      Logger.error(LOG_TAG, "Error loading page " + request.getURI().toString(), ioEx);
      return null;
    }

    return new ManticoreHttpResponse(statusCode, ret, _handler.getHeaders());
  }
Exemplo n.º 22
0
  StringBuilder addCanonicalHeaders(HttpUriRequest request, StringBuilder builder) {
    SortedMap<String, String> sortedHeaders = sortedFormattedHeaders(request.getAllHeaders());
    if (!sortedHeaders.containsKey(DATE_HEADER_NAME)) {
      String timestamp = timestamp();
      sortedHeaders.put(DATE_HEADER_NAME, timestamp);
      request.addHeader(DATE_HEADER_NAME, timestamp);
    }
    if (!sortedHeaders.containsKey(HOST_HEADER_NAME)) {
      sortedHeaders.put(HOST_HEADER_NAME, request.getURI().getHost());
      request.addHeader(HOST_HEADER_NAME, request.getURI().getHost());
    }

    addCanonicalHeaders(sortedHeaders, builder).append('\n');
    return addSignedHeaders(sortedHeaders, builder);
  }
 @Override
 public <T> T execute(
     final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler)
     throws IOException, ClientProtocolException {
   return HttpClientTracing.execute(
       request.getURI().toString(),
       traceHeaderName,
       request,
       new HttpClientTracing.Executor<T>() {
         @Override
         public T execute() throws IOException {
           return client.execute(request, responseHandler);
         }
       });
 }
  private <T> T executeRequest(final HttpUriRequest request, final ResponseHandler<T> handler)
      throws IOException {
    final CloseableHttpClient client = createClientInstance();
    try {
      final CloseableHttpResponse response = client.execute(request);
      //  Wrap the response in a buffer to facilitate error handlers re-playing the content if the
      // response
      //  size is smaller than the max allowable buffer
      if (response.getEntity().getContentLength() >= 0
          && response.getEntity().getContentLength() < config.getMaxBufferSize()) {
        EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
      }

      //  Explicit check for the authorization status of the API key
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        throw new HyppoAuthException(config);
      }

      try {
        log.debug(
            "{} - {} : {}",
            request.getMethod(),
            request.getURI().getPath(),
            response.getStatusLine().getStatusCode());
        return handler.handleResponse(response);
      } finally {
        IOUtils.closeQuietly(response);
      }
    } catch (Exception e) {
      log.error(
          "{} - {} : FAILED - {}", request.getMethod(), request.getURI().getPath(), e.toString());
      throw e;
    } finally {
      IOUtils.closeQuietly(client);
    }
  }
 @Mock
 public void $init(
     CloudProvider provider,
     HttpClientBuilder clientBuilder,
     HttpUriRequest request,
     ResponseHandler handler) {
   String requestUri = request.getURI().toString();
   if (request.getMethod().equals("GET")
       && requestUri.equals(String.format(VMTEMPLATES_RESOURCES, ENDPOINT, ACCOUNT_NO))) {
     requestResourceType = 1;
   }
   if (request.getMethod().equals("GET")
       && requestUri.equals(String.format(HARDWARE_PROFILES, ENDPOINT, ACCOUNT_NO))) {
     requestResourceType = 2;
   }
 }
Exemplo n.º 26
0
  private void assertBuild(final Charset charset) throws Exception {
    final RequestBuilder requestBuilder = RequestBuilder.create("GET").setCharset(charset);
    requestBuilder.setUri("https://somehost.com/stuff");
    requestBuilder.addParameters(createParameters());

    final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
    final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());

    final String uriExpected =
        String.format(
            "https://somehost.com/stuff?parameter1=value1&parameter2=%s&parameter3=%s",
            encodedData1, encodedData2);

    final HttpUriRequest request = requestBuilder.build();
    Assert.assertEquals(uriExpected, request.getURI().toString());
  }
Exemplo n.º 27
0
  /** Generates a cURL command equivalent to the given request. */
  private static String toCurl(HttpUriRequest request) throws IOException {
    StringBuilder builder = new StringBuilder();

    builder.append("curl ");

    for (Header header : request.getAllHeaders()) {
      builder.append("--header \"");
      builder.append(header.toString().trim());
      builder.append("\" ");
    }

    URI uri = request.getURI();

    // If this is a wrapped request, use the URI from the original
    // request instead. getURI() on the wrapper seems to return a
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
      HttpRequest original = ((RequestWrapper) request).getOriginal();
      if (original instanceof HttpUriRequest) {
        uri = ((HttpUriRequest) original).getURI();
      }
    }

    builder.append("\"");
    builder.append(uri);
    builder.append("\"");

    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
      HttpEntity entity = entityRequest.getEntity();
      if (entity != null && entity.isRepeatable()) {
        if (entity.getContentLength() < 1024) {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          entity.writeTo(stream);
          String entityString = stream.toString();

          // TODO: Check the content type, too.
          builder.append(" --data-ascii \"").append(entityString).append("\"");
        } else {
          builder.append(" [TOO MUCH DATA TO INCLUDE]");
        }
      }
    }

    return builder.toString();
  }
Exemplo n.º 28
0
 private void callMethod(HttpUriRequest request, Operation operation) throws IOException {
   // Specify GData protocol version 2.0.
   request.addHeader("GData-Version", "2");
   // Indicate support for gzip-compressed responses.
   request.addHeader("Accept-Encoding", "gzip");
   // Specify authorization token if provided.
   String authToken = mAuthToken;
   if (!TextUtils.isEmpty(authToken)) {
     request.addHeader("Authorization", "GoogleLogin auth=" + authToken);
   }
   // Specify the ETag of a prior response, if available.
   String etag = operation.inOutEtag;
   if (etag != null) {
     request.addHeader("If-None-Match", etag);
   }
   // Execute the HTTP request.
   HttpResponse httpResponse = null;
   try {
     httpResponse = mHttpClient.execute(request);
   } catch (IOException e) {
     Log.w(TAG, "Request failed: " + request.getURI());
     throw e;
   }
   // Get the status code and response body.
   int status = httpResponse.getStatusLine().getStatusCode();
   InputStream stream = null;
   HttpEntity entity = httpResponse.getEntity();
   if (entity != null) {
     // Wrap the entity input stream in a GZIP decoder if necessary.
     stream = entity.getContent();
     if (stream != null) {
       Header header = entity.getContentEncoding();
       if (header != null) {
         if (header.getValue().contains("gzip")) {
           stream = new GZIPInputStream(stream);
         }
       }
     }
   }
   // Return the stream if successful.
   Header etagHeader = httpResponse.getFirstHeader("ETag");
   operation.outStatus = status;
   operation.inOutEtag = etagHeader != null ? etagHeader.getValue() : null;
   operation.outBody = stream;
 }
Exemplo n.º 29
0
 /**
  * 执行请求,返回字节
  *
  * @param charset
  * @param httpUriRequest
  * @return
  */
 public byte[] execute_byte(HttpUriRequest httpUriRequest, Map<String, String> header) {
   byte[] data = null;
   HttpEntity entity = null;
   try {
     CloseableHttpResponse httpResponse = httpclient.execute(httpUriRequest);
     int statusCode = httpResponse.getStatusLine().getStatusCode();
     entity = httpResponse.getEntity();
     Header heade = entity.getContentType();
     if (heade != null) {
       log.info("statusCode : " + statusCode + " ContentType : " + heade.getValue());
       setContent_type(heade.getValue());
     } else {
       log.info("statusCode : " + statusCode + " ContentType : unknown .");
     }
     setStatusCode(statusCode);
     if (statusCode == 200) {
       data = EntityUtils.toByteArray(entity);
     } else if (statusCode == 302 || statusCode == 300 || statusCode == 301) {
       URL referer = httpUriRequest.getURI().toURL();
       httpUriRequest.abort();
       Header location = httpResponse.getFirstHeader("Location");
       String locationurl = location.getValue();
       if (!locationurl.startsWith("http")) {
         URL u = new URL(referer, locationurl);
         locationurl = u.toExternalForm();
       }
       data = GetImg(locationurl, header);
     } else {
       data = EntityUtils.toByteArray(entity);
     }
   } catch (ClientProtocolException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (httpUriRequest != null) {
       httpUriRequest.abort();
     }
     if (entity != null) {
       EntityUtils.consumeQuietly(entity);
     }
   }
   return data;
 }
  @Test
  public void shouldProxyTheRequestMethodUriBodyAndContentType()
      throws ServletException, IOException, URISyntaxException {
    CloseableHttpClient mockHttpClient =
        mock(CloseableHttpClient.class, Mockito.RETURNS_DEEP_STUBS);

    AbstractLightblueProxyServlet servlet =
        getTestServlet(mockHttpClient, null, "http://myservice.com", null);

    HttpServletRequest stubRequest =
        new StubHttpServletRequest(
            "http://my.site.com/app/get/the/thing?foo=bar",
            "GET",
            "{test:0}",
            "application/json",
            "/get/*");

    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    ServletOutputStream outputStream = new FakeServletOutputStream(new ByteArrayOutputStream());
    when(mockResponse.getOutputStream()).thenReturn(outputStream);

    servlet.service(stubRequest, mockResponse);

    ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);

    verify(mockHttpClient).execute(requestCaptor.capture());

    HttpUriRequest request = requestCaptor.getValue();

    assertEquals("GET", request.getMethod());
    assertEquals(new URI("http://myservice.com/the/thing?foo=bar"), request.getURI());
    assertThat(request, instanceOf(HttpEntityEnclosingRequest.class));
    assertEquals(1, request.getHeaders("content-type").length);
    assertEquals("application/json", request.getHeaders("content-type")[0].getValue());

    HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) request;
    ByteArrayOutputStream entityOutStream = new ByteArrayOutputStream();
    entityEnclosingRequest.getEntity().writeTo(entityOutStream);

    byte[] expectedEntityBytes = new byte[stubRequest.getContentLength()];
    stubRequest.getInputStream().read(expectedEntityBytes, 0, stubRequest.getContentLength());

    assertEquals(new String(expectedEntityBytes), entityOutStream.toString());
  }