@Override
  public void signRequest(HttpUriRequest request) {
    AWSCredentials credentials = awsCredentialsProvider.getCredentials();
    if (credentials instanceof AWSSessionCredentials) {
      request.addHeader(
          SESSION_TOKEN_HEADER, ((AWSSessionCredentials) credentials).getSessionToken());
    }
    String canonicalRequest = createCanonicalRequest(request);
    log.debug("canonicalRequest: " + canonicalRequest);
    String[] requestParts = canonicalRequest.split("\n");
    String signedHeaders = requestParts[requestParts.length - 2];
    String stringToSign = createStringToSign(canonicalRequest);
    log.debug("stringToSign: " + stringToSign);
    String authScope = stringToSign.split("\n")[2];
    String signature = createSignature(stringToSign);

    String authHeader =
        String.format(
            AUTH_HEADER_FORMAT,
            credentials.getAWSAccessKeyId(),
            authScope,
            signedHeaders,
            signature);

    request.addHeader(AUTH_HEADER_NAME, authHeader);
  }
示例#2
0
  /** {@inheritDoc} */
  public GWTLatLng geocode(final String geolocation) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(geolocation));
    method.addHeader("User-Agent", "OpenNMS-MapquestGeocoder/1.0");
    if (m_referer != null) {
      method.addHeader("Referer", m_referer);
    }

    try {
      InputStream responseStream = m_httpClient.execute(method).getEntity().getContent();
      final ElementTree tree = ElementTree.fromStream(responseStream);
      if (tree == null) {
        throw new GeocoderException(
            "an error occurred connecting to the Nominatim geocoding service (no XML tree was found)");
      }

      final List<ElementTree> places = tree.findAll("//place");
      if (places.size() > 1) {
        LogUtils.warnf(this, "more than one location returned for query: %s", geolocation);
      } else if (places.size() == 0) {
        throw new GeocoderException("Nominatim returned an OK status code, but no places");
      }
      final ElementTree place = places.get(0);

      Double latitude = Double.valueOf(place.getAttribute("lat"));
      Double longitude = Double.valueOf(place.getAttribute("lon"));
      return new GWTLatLng(latitude, longitude);
    } catch (GeocoderException e) {
      throw e;
    } catch (Throwable e) {
      throw new GeocoderException("unable to get lat/lng from Nominatim", e);
    }
  }
示例#3
0
 private void SetupHTTPConnectionParams(HttpUriRequest method) {
   HttpConnectionParams.setConnectionTimeout(method.getParams(), CONNECTION_TIMEOUT_MS);
   HttpConnectionParams.setSoTimeout(method.getParams(), SOCKET_TIMEOUT_MS);
   // mClient.setHttpRequestRetryHandler(requestRetryHandler);
   method.addHeader("Accept-Encoding", "gzip, deflate");
   method.addHeader("Accept-Charset", "UTF-8,*;q=0.5");
 }
  /**
   * Authenticate with the chosen bridge
   *
   * @param ip, the ip address of the bridge you want to authenticate with
   * @return the username of the hue bridge, that has been placed on the white list
   */
  private String authWithBridge(String ip)
      throws URISyntaxException, MalformedURLException, UnsupportedEncodingException {

    URI uri = new URL("http://" + ip + "/api").toURI();

    DefaultHttpClient client = new DefaultHttpClient();
    HttpUriRequest request = new HttpPost(uri);

    JSONObject obj = new JSONObject();
    try {
      obj.put("devicetype", "OpenRemoteController");
    } catch (JSONException e) {
      logger.error("JSONExceptionn when creating json object", e);
    }

    StringEntity data = new StringEntity(obj.toString(), "UTF-8");

    ((HttpPost) request).setEntity(data);
    String resp = "";
    HttpResponse response = null;
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    request.addHeader("User-Agent", "OpenRemoteController");
    request.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

    request.addHeader("Content-Type", "applicaion/json");

    try {
      response = client.execute(request);
      resp = responseHandler.handleResponse(response);
    } catch (ClientProtocolException e) {
      logger.error("ClientProtocolException when executing HTTP method", e);
    } catch (IOException e) {
      logger.error("IOException when executing HTTP method", e);
    } finally {
      try {
        if ((response != null) && (response.getEntity() != null)) {
          response.getEntity().consumeContent();
        }
      } catch (IOException ignored) {
      }
      client.getConnectionManager().shutdown();
    }

    JSONObject jsonResponse = null;
    String jsonUsername = "";
    try {
      jsonResponse = new JSONArray(resp.toString()).getJSONObject(0).getJSONObject("success");
      jsonUsername = jsonResponse.getString("username");
    } catch (JSONException e) {
      logger.error("JSONException when reading returned json", e);
    }
    return jsonUsername;
  }
 /**
  * Add the given headers to the given HTTP request.
  *
  * @param httpRequest the request to add the headers to
  * @param headers the headers to add
  */
 static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) {
   for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
     String headerName = entry.getKey();
     if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265
       String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; ");
       httpRequest.addHeader(headerName, headerValue);
     } else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName)
         && !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) {
       for (String headerValue : entry.getValue()) {
         httpRequest.addHeader(headerName, headerValue);
       }
     }
   }
 }
  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);
  }
  public JestResult execute(Action clientRequest) throws IOException {

    String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI());

    HttpUriRequest request =
        constructHttpMethod(
            clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData());

    // add headers added to action
    if (!clientRequest.getHeaders().isEmpty()) {
      for (Entry<String, Object> header : clientRequest.getHeaders().entrySet()) {
        request.addHeader(header.getKey(), header.getValue() + "");
      }
    }

    HttpResponse response = httpClient.execute(request);

    // If head method returns no content, it is added according to response code thanks to
    // https://github.com/hlassiege
    if (request.getMethod().equalsIgnoreCase("HEAD")) {
      if (response.getEntity() == null) {
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
          response.setEntity(new StringEntity("{\"ok\" : true, \"found\" : true}"));
        } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
          response.setEntity(new StringEntity("{\"ok\" : false, \"found\" : false}"));
        }
      }
    }
    return deserializeResponse(response, clientRequest.getName(), clientRequest.getPathToResult());
  }
示例#8
0
文件: Fetcher.java 项目: mpermana/pet
  public static Object fetch(DocumentSource source) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
      URIBuilder builder = new URIBuilder(source.url);
      for (Entry<String, String> it : source.parameters.entrySet()) {
        builder.addParameter(it.getKey(), it.getValue());
      }
      URI uri = builder.build();

      HttpUriRequest request = new HttpGet(uri);
      request.addHeader("Accept", "application/json");
      CloseableHttpResponse response = httpclient.execute(request);

      String headers = response.getFirstHeader("Content-Type").getValue();
      InputStream inputStream = response.getEntity().getContent();
      if (headers.contains("text/html")) {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        Document document = Jsoup.parse(inputStream, null, source.url);
        return document;
      } else if (headers.contains("json")) {
        ObjectMapper om = new ObjectMapper();
        return om.readValue(inputStream, HashMap.class);
      } else {
        IOUtils.copy(inputStream, System.err);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return null;
  }
示例#9
0
 private void a(
     Context paramContext,
     DefaultHttpClient paramDefaultHttpClient,
     HttpContext paramHttpContext,
     HttpHost paramHttpHost,
     HttpUriRequest paramHttpUriRequest,
     String paramString,
     an paraman) {
   if (paramString != null) paramHttpUriRequest.addHeader("Content-Type", paramString);
   Future localFuture =
       this.b.submit(
           new am(
               paramContext,
               paramDefaultHttpClient,
               paramHttpContext,
               paramHttpHost,
               paramHttpUriRequest,
               paraman));
   if (paramContext != null) {
     Object localObject = (List) this.c.get(paramContext);
     if (localObject == null) {
       localObject = new LinkedList();
       this.c.put(paramContext, localObject);
     }
     ((List) localObject).add(new WeakReference(localFuture));
   }
 }
  // Private stuff
  protected void sendRequest(
      DefaultHttpClient client,
      HttpContext httpContext,
      HttpUriRequest uriRequest,
      String contentType,
      AsyncHttpResponseHandler responseHandler,
      Context context) {
    if (contentType != null) {
      uriRequest.addHeader("Content-Type", contentType);
    }

    Future<?> request =
        threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));

    if (context != null) {
      // Add request to request map
      List<WeakReference<Future<?>>> requestList = requestMap.get(context);
      if (requestList == null) {
        requestList = new LinkedList<WeakReference<Future<?>>>();
        requestMap.put(context, requestList);
      }

      requestList.add(new WeakReference<Future<?>>(request));

      // TODO: Remove dead weakrefs from requestLists?
    }
  }
 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;
 }
  private HttpUriRequest newHcRequest(HttpRequest request) throws IOException {
    URI uri = request.getUri().toJavaUri();
    HttpUriRequest httpUriRequest = HttpMethod.valueOf(request.getMethod()).newMessage(uri);

    for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
      httpUriRequest.addHeader(entry.getKey(), StringUtils.join(entry.getValue(), ','));
    }
    return httpUriRequest;
  }
示例#13
0
 protected Object sendSyncRequest(
     DefaultHttpClient client,
     HttpContext httpContext,
     HttpUriRequest uriRequest,
     String contentType) {
   if (contentType != null) {
     uriRequest.addHeader("Content-Type", contentType);
   }
   return new SyncRequestHandler(client, httpContext, charset).sendRequest(uriRequest);
 }
示例#14
0
 /**
  * 设置Cookie方法1
  *
  * @param request
  * @param cookies
  */
 public void setRequestCookiesStr(HttpUriRequest request, Map<String, String> cookies) {
   StringBuilder cookieStr = new StringBuilder();
   if (cookies != null && cookies.size() > 0) {
     for (Entry<String, String> cookie : cookies.entrySet()) {
       cookieStr.append(cookie.getKey()).append('=').append(cookie.getValue()).append(';');
     }
     cookieStr.deleteCharAt(cookieStr.length() - 1);
     request.addHeader("Cookie", cookieStr.toString());
   }
 }
示例#15
0
  protected <T> void sendRequest(
      DefaultHttpClient client,
      HttpContext httpContext,
      HttpUriRequest uriRequest,
      String contentType,
      AjaxCallBack<T> ajaxCallBack) {
    if (contentType != null) {
      uriRequest.addHeader("Content-Type", contentType);
    }

    new HttpHandler<T>(client, httpContext, ajaxCallBack, charset)
        .executeOnExecutor(executor, uriRequest);
  }
示例#16
0
  /**
   * Setup request.
   *
   * @param path the path
   * @param method the method
   * @param params the vars
   * @return the http uri request
   */
  private HttpUriRequest setupRequest(
      String path, final String method, final List<NameValuePair> params) {

    String normalizedPath = path.toLowerCase();
    StringBuilder sb = new StringBuilder();

    // If we've given a fully qualified uri then skip building the endpoint
    if (normalizedPath.startsWith("http://") || normalizedPath.startsWith("https://")) {
      sb.append(path);
    } else {
      sb.append(getEndpoint());

      if (!normalizedPath.startsWith("/")) {
        sb.append("/");
      }
      sb.append(path);
    }

    path = sb.toString();

    HttpUriRequest request = buildMethod(method, path, params);

    request.addHeader(new BasicHeader("X-Twilio-Client", "java-" + VERSION));
    request.addHeader(new BasicHeader("User-Agent", "twilio-java/" + VERSION));
    request.addHeader(new BasicHeader("Accept", "application/json"));
    request.addHeader(new BasicHeader("Accept-Charset", "utf-8"));

    if (httpclient
        instanceof
        DefaultHttpClient) { // as DefaultHttpClient class has final method, I need httpClient to be
                             // a plain interface to be able to mock it
      ((DefaultHttpClient) httpclient)
          .getCredentialsProvider()
          .setCredentials(
              new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
              new UsernamePasswordCredentials(username, password));
    }
    return request;
  }
 protected void sendRequest(
     DefaultHttpClient defaultHttpClient,
     HttpContext httpContext,
     HttpUriRequest httpUriRequest,
     String str,
     AsyncHttpResponseHandler asyncHttpResponseHandler,
     Context context) {
   if (str != null) {
     httpUriRequest.addHeader("Content-Type", str);
   }
   new AsyncHttpRequest(defaultHttpClient, httpContext, httpUriRequest, asyncHttpResponseHandler)
       .run();
 }
 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);
   }
 }
  @Test(
      groups = {"wso2.am"},
      description = "Checking CORS headers in pre-flight response")
  public void CheckCORSHeadersInPreFlightResponse() throws Exception {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpUriRequest option = new HttpOptions(getAPIInvocationURLHttp(API_CONTEXT, API_VERSION));
    option.addHeader("Origin", "http://localhost");
    HttpResponse response = httpclient.execute(option);

    assertEquals(
        response.getStatusLine().getStatusCode(), HTTP_RESPONSE_CODE_OK, "Response code mismatch.");

    Header[] responseHeaders = response.getAllHeaders();

    log.info("Response Headers: CheckCORSHeadersInPreFlightResponse");
    for (Header header : responseHeaders) {
      log.info(header.getName() + " : " + header.getValue());
    }

    Header header = pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_ORIGIN_HEADER);
    assertNotNull(
        header, ACCESS_CONTROL_ALLOW_ORIGIN_HEADER + " header is not available in the response.");
    assertEquals(
        header.getValue(),
        ACCESS_CONTROL_ALLOW_ORIGIN_HEADER_VALUE,
        ACCESS_CONTROL_ALLOW_ORIGIN_HEADER + " header value mismatch.");

    header = pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_METHODS_HEADER);
    assertNotNull(
        header, ACCESS_CONTROL_ALLOW_METHODS_HEADER + " header is not available in the response.");
    assertEquals(
        header.getValue(),
        ACCESS_CONTROL_ALLOW_METHODS_HEADER_VALUE,
        ACCESS_CONTROL_ALLOW_METHODS_HEADER + " header value mismatch.");

    header = pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_HEADERS_HEADER);
    assertNotNull(
        header, ACCESS_CONTROL_ALLOW_HEADERS_HEADER + " header is not available in the response.");
    assertEquals(
        header.getValue(),
        ACCESS_CONTROL_ALLOW_HEADERS_HEADER_VALUE,
        ACCESS_CONTROL_ALLOW_HEADERS_HEADER + " header value mismatch.");

    assertNull(
        pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER),
        ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER
            + " header is available in the response, "
            + "but it should not be.");
  }
示例#20
0
  public void executeAsync(
      final Action clientRequest, final JestResultHandler<JestResult> resultHandler)
      throws ExecutionException, InterruptedException, IOException {

    synchronized (this) {
      if (asyncClient.getStatus() == IOReactorStatus.INACTIVE) {
        asyncClient.start();
      }
    }

    String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI());

    final HttpUriRequest request =
        constructHttpMethod(
            clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData());

    // add headers added to action
    if (!clientRequest.getHeaders().isEmpty()) {
      for (Entry<String, Object> header : clientRequest.getHeaders().entrySet()) {
        request.addHeader(header.getKey(), header.getValue() + "");
      }
    }

    asyncClient.execute(
        request,
        new FutureCallback<HttpResponse>() {
          @Override
          public void completed(final HttpResponse response) {
            try {
              JestResult jestResult =
                  deserializeResponse(
                      response, clientRequest.getName(), clientRequest.getPathToResult());
              resultHandler.completed(jestResult);
            } catch (IOException e) {
              log.error(
                  "Exception occurred while serializing the response. Exception: "
                      + e.getMessage());
            }
          }

          @Override
          public void failed(final Exception ex) {
            resultHandler.failed(ex);
          }

          @Override
          public void cancelled() {}
        });
  }
示例#21
0
  /** public for unit test, not to be used otherwise */
  public void execute(
      HttpUriRequest httpUriRequest,
      ContentType contentType,
      FutureCallback<HttpResponse> callback) {

    // add accept header when its not a form or multipart
    final String contentTypeString = contentType.toString();
    if (!ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType.getMimeType())
        && !contentType.getMimeType().startsWith(MULTIPART_MIME_TYPE)) {
      // otherwise accept what is being sent
      httpUriRequest.addHeader(HttpHeaders.ACCEPT, contentTypeString);
    }
    // is something being sent?
    if (httpUriRequest instanceof HttpEntityEnclosingRequestBase
        && httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
      httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeString);
    }

    // set user specified custom headers
    if (httpHeaders != null && !httpHeaders.isEmpty()) {
      for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
        httpUriRequest.setHeader(entry.getKey(), entry.getValue());
      }
    }

    // add client protocol version if not specified
    if (!httpUriRequest.containsHeader(ODataHttpHeaders.DATASERVICEVERSION)) {
      httpUriRequest.addHeader(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V20);
    }
    if (!httpUriRequest.containsHeader(MAX_DATA_SERVICE_VERSION)) {
      httpUriRequest.addHeader(MAX_DATA_SERVICE_VERSION, ODataServiceVersion.V30);
    }

    // execute request
    client.execute(httpUriRequest, callback);
  }
示例#22
0
  private String safelyExecuteRequest(String url, int expectedStatus, HttpUriRequest request) {
    if (hostHeader != null) {
      request.addHeader(HOST, hostHeader);
    }

    try (CloseableHttpResponse response = httpClient.execute(request)) {
      int statusCode = response.getStatusLine().getStatusCode();
      if (statusCode != expectedStatus) {
        throw new VerificationException(
            "Expected status " + expectedStatus + " for " + url + " but was " + statusCode);
      }

      return getEntityAsStringAndCloseStream(response);
    } catch (Exception e) {
      return throwUnchecked(e, String.class);
    }
  }
  public HttpResponse executeHttpRequest(HttpUriRequest request, int expectedStatusCode) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("kermit", "kermit");
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
    try {
      if (request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
        // Revert to default content-type
        request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
      }
      HttpResponse response = client.execute(request);
      Assert.assertNotNull(response.getStatusLine());
      Assert.assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode());
      return response;

    } catch (ClientProtocolException e) {
      Assert.fail(e.getMessage());
    } catch (IOException e) {
      Assert.fail(e.getMessage());
    }
    return null;
  }
 protected void sendRequest(
     DefaultHttpClient defaultHttpClient,
     HttpContext httpContext,
     HttpUriRequest httpUriRequest,
     String str,
     AsyncHttpResponseHandler asyncHttpResponseHandler,
     Context context) {
   if (str != null) {
     httpUriRequest.addHeader("Content-Type", str);
   }
   Future submit =
       this.threadPool.submit(
           new AsyncHttpRequest(
               defaultHttpClient, httpContext, httpUriRequest, asyncHttpResponseHandler));
   if (context != null) {
     List list = (List) this.requestMap.get(context);
     if (list == null) {
       list = new LinkedList();
       this.requestMap.put(context, list);
     }
     list.add(new WeakReference(submit));
   }
 }
  /**
   * 请求
   *
   * @date 2013-1-7 上午11:08:54
   * @param toFetchURL
   * @return
   */
  public FetchResult request(FetchRequest req) throws Exception {
    FetchResult fetchResult = new FetchResult();
    HttpUriRequest request = null;
    HttpEntity entity = null;
    String toFetchURL = req.getUrl();
    boolean isPost = false;
    try {
      if (Http.Method.GET.equalsIgnoreCase(req.getHttpMethod())) request = new HttpGet(toFetchURL);
      else if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())) {
        request = new HttpPost(toFetchURL);
        isPost = true;
      } else if (Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod()))
        request = new HttpPut(toFetchURL);
      else if (Http.Method.HEAD.equalsIgnoreCase(req.getHttpMethod()))
        request = new HttpHead(toFetchURL);
      else if (Http.Method.OPTIONS.equalsIgnoreCase(req.getHttpMethod()))
        request = new HttpOptions(toFetchURL);
      else if (Http.Method.DELETE.equalsIgnoreCase(req.getHttpMethod()))
        request = new HttpDelete(toFetchURL);
      else throw new Exception("Unknown http method name");

      // 同步信号量,在真正对服务端进行访问之前进行访问间隔的控制
      // TODO 针对每个请求有一个delay的参数设置
      synchronized (mutex) {
        // 获取当前时间
        long now = (new Date()).getTime();
        // 对同一个Host抓取时间间隔进行控制,若在设置的时限内则进行休眠
        if (now - lastFetchTime < config.getPolitenessDelay())
          Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
        // 不断更新最后的抓取时间,注意,是针对HOST的,不是针对某个URL的
        lastFetchTime = (new Date()).getTime();
      }

      // 设置请求GZIP压缩,注意,前面必须设置GZIP解压缩处理
      request.addHeader("Accept-Encoding", "gzip");
      for (Iterator<Entry<String, String>> it = headers.entrySet().iterator(); it.hasNext(); ) {
        Entry<String, String> entry = it.next();
        request.addHeader(entry.getKey(), entry.getValue());
      }

      // 记录请求信息
      Header[] headers = request.getAllHeaders();
      for (Header h : headers) {
        Map<String, List<String>> hs = req.getHeaders();
        String key = h.getName();
        List<String> val = hs.get(key);
        if (val == null) val = new ArrayList<String>();
        val.add(h.getValue());

        hs.put(key, val);
      }
      req.getCookies().putAll(this.cookies);
      fetchResult.setReq(req);

      HttpEntity reqEntity = null;
      if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())
          || Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod())) {
        if (!req.getFiles().isEmpty()) {
          reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
          for (Iterator<Entry<String, List<File>>> it = req.getFiles().entrySet().iterator();
              it.hasNext(); ) {
            Entry<String, List<File>> e = it.next();
            String paramName = e.getKey();
            for (File file : e.getValue()) {
              // For File parameters
              ((MultipartEntity) reqEntity).addPart(paramName, new FileBody(file));
            }
          }

          for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator();
              it.hasNext(); ) {
            Entry<String, List<Object>> e = it.next();
            String paramName = e.getKey();
            for (Object paramValue : e.getValue()) {
              // For usual String parameters
              ((MultipartEntity) reqEntity)
                  .addPart(
                      paramName,
                      new StringBody(
                          String.valueOf(paramValue), "text/plain", Charset.forName("UTF-8")));
            }
          }
        } else {
          List<NameValuePair> params = new ArrayList<NameValuePair>(req.getParams().size());
          for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator();
              it.hasNext(); ) {
            Entry<String, List<Object>> e = it.next();
            String paramName = e.getKey();
            for (Object paramValue : e.getValue()) {
              params.add(new BasicNameValuePair(paramName, String.valueOf(paramValue)));
            }
          }
          reqEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
        }

        if (isPost) ((HttpPost) request).setEntity(reqEntity);
        else ((HttpPut) request).setEntity(reqEntity);
      }

      // 执行请求,获取服务端返回内容
      HttpResponse response = httpClient.execute(request);
      headers = response.getAllHeaders();
      for (Header h : headers) {
        Map<String, List<String>> hs = fetchResult.getHeaders();
        String key = h.getName();
        List<String> val = hs.get(key);
        if (val == null) val = new ArrayList<String>();
        val.add(h.getValue());

        hs.put(key, val);
      }
      // 设置已访问URL
      fetchResult.setFetchedUrl(toFetchURL);
      String uri = request.getURI().toString();
      if (!uri.equals(toFetchURL))
        if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL))
          fetchResult.setFetchedUrl(uri);

      entity = response.getEntity();
      // 服务端返回的状态码
      int statusCode = response.getStatusLine().getStatusCode();
      if (statusCode != HttpStatus.SC_OK) {
        if (statusCode != HttpStatus.SC_NOT_FOUND) {
          Header locationHeader = response.getFirstHeader("Location");
          // 如果是301、302跳转,获取跳转URL即可返回
          if (locationHeader != null
              && (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                  || statusCode == HttpStatus.SC_MOVED_TEMPORARILY))
            fetchResult.setMovedToUrl(
                URLCanonicalizer.getCanonicalURL(locationHeader.getValue(), toFetchURL));
        }
        // 只要不是OK的除了设置跳转URL外设置statusCode即可返回
        // 判断是否有忽略状态码的设置
        if (this.site.getSkipStatusCode() != null
            && this.site.getSkipStatusCode().trim().length() > 0) {
          String[] scs = this.site.getSkipStatusCode().split(",");
          for (String code : scs) {
            int c = CommonUtil.toInt(code);
            // 忽略此状态码,依然解析entity
            if (statusCode == c) {
              assemPage(fetchResult, entity);
              break;
            }
          }
        }
        fetchResult.setStatusCode(statusCode);
        return fetchResult;
      }

      // 处理服务端返回的实体内容
      if (entity != null) {
        fetchResult.setStatusCode(statusCode);
        assemPage(fetchResult, entity);
        return fetchResult;
      }
    } catch (Throwable e) {
      fetchResult.setFetchedUrl(e.toString());
      fetchResult.setStatusCode(Status.INTERNAL_SERVER_ERROR.ordinal());
      return fetchResult;
    } finally {
      try {
        if (entity == null && request != null) request.abort();
      } catch (Exception e) {
        throw e;
      }
    }

    fetchResult.setStatusCode(Status.UNSPECIFIED_ERROR.ordinal());
    return fetchResult;
  }
示例#26
0
 protected void addApiKeyHeader(HttpUriRequest httpUriRequest) {
   final String header = "x-api-key";
   final String apiKey = context.getString(R.string.api_key);
   httpUriRequest.addHeader(header, apiKey);
 }
    @Override
    protected List<SmartDevice> doInBackground(final Void... params) {
      mLogger.entering(getClass().getName(), "doInBackground", params);

      List<SmartDevice> devices = new ArrayList<SmartDevice>();

      DConnectMessage message = null;

      try {
        URIBuilder builder = new URIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);

        SharedPreferences prefs =
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (prefs.getBoolean(getString(R.string.key_settings_dconn_ssl), false)) {
          builder.setScheme("https");
        } else {
          builder.setScheme("http");
        }

        builder.setHost(
            prefs.getString(
                getString(R.string.key_settings_dconn_host), getString(R.string.default_host)));

        builder.setPort(
            Integer.parseInt(
                prefs.getString(
                    getString(R.string.key_settings_dconn_port),
                    getString(R.string.default_port))));

        builder.addParameter(DConnectMessage.EXTRA_ACCESS_TOKEN, getAccessToken());

        HttpUriRequest request = new HttpGet(builder.build());
        request.addHeader(DConnectMessage.HEADER_GOTAPI_ORIGIN, getPackageName());
        mLogger.info(request.getMethod() + " " + request.getURI());
        HttpResponse response = mDConnectClient.execute(request);
        message = (new HttpMessageFactory()).newDConnectMessage(response);
      } catch (URISyntaxException e) {
        e.printStackTrace();

        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      } catch (IOException e) {
        e.printStackTrace();

        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      }

      if (message == null) {
        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      }

      int result = message.getInt(DConnectMessage.EXTRA_RESULT);
      if (result == DConnectMessage.RESULT_ERROR) {
        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      }

      List<Object> services = message.getList(ServiceDiscoveryProfileConstants.PARAM_SERVICES);
      if (services != null) {
        for (Object object : services) {
          @SuppressWarnings("unchecked")
          Map<String, Object> service = (Map<String, Object>) object;
          SmartDevice device =
              new SmartDevice(
                  service.get(ServiceDiscoveryProfileConstants.PARAM_ID).toString(),
                  service.get(ServiceDiscoveryProfileConstants.PARAM_NAME).toString());
          devices.add(device);
          mLogger.info("Found smart device: " + device.getId());
        }
      }

      mLogger.exiting(getClass().getName(), "doInBackground", devices);
      return devices;
    }
示例#28
0
 private void setHeaders(HttpUriRequest request, List<NameValuePair> headers) {
   if (headers.isEmpty()) return;
   for (NameValuePair header : headers) {
     request.addHeader(header.getName(), header.getValue());
   }
 }
示例#29
0
 public void setRequestProperty(String name, String value) {
   req.addHeader(name, value);
 }
示例#30
0
 private HttpResponse execute(HttpUriRequest request) throws IOException {
   request.addHeader("Accept-Encoding", "gzip");
   return httpclient.execute(request);
 }