コード例 #1
0
  public static void writeDataToRequest(
      HttpRequest newWebRequest, String postedData, boolean disableRequestCompression) {

    HttpEntityEnclosingRequestBase requestMethod = (HttpEntityEnclosingRequestBase) newWebRequest;

    try {
      if (disableRequestCompression) {
        StringEntity entity = new StringEntity(postedData, ContentType.APPLICATION_JSON);
        entity.setChunked(true);
        requestMethod.setEntity(entity);
      } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzipOS = new GZIPOutputStream(baos);
        IOUtils.write(postedData, gzipOS, Consts.UTF_8.name());
        IOUtils.closeQuietly(gzipOS);
        ByteArrayEntity entity =
            new ByteArrayEntity(baos.toByteArray(), ContentType.APPLICATION_JSON);
        entity.setChunked(true);
        requestMethod.setEntity(entity);
      }

    } catch (IOException e) {
      throw new RuntimeException("Unable to gzip data!", e);
    }
  }
コード例 #2
0
 // todo: remove the need for a synchronized method -- this is the only method that needs to be
 // synchronized for now
 //       since it modifies the sourceRequest
 public synchronized HttpRequestBase process(HttpEntityEnclosingRequestBase method)
     throws IOException {
   final int contentLength = getEntityLength();
   setHeaders(method);
   method.setEntity(new InputStreamEntity(sourceRequest.getInputStream(), contentLength));
   return method;
 }
 private HttpEntityEnclosingRequestBase addEntityToRequestBase(
     HttpEntityEnclosingRequestBase httpEntityEnclosingRequestBase, HttpEntity httpEntity) {
   if (httpEntity != null) {
     httpEntityEnclosingRequestBase.setEntity(httpEntity);
   }
   return httpEntityEnclosingRequestBase;
 }
コード例 #4
0
  protected AsyncRequestWrapperImpl(
      final ODataClient odataClient, final ODataRequest odataRequest) {
    this.odataRequest = odataRequest;
    this.odataRequest.setAccept(this.odataRequest.getAccept());
    this.odataRequest.setContentType(this.odataRequest.getContentType());

    extendHeader(HttpHeader.PREFER, new ODataPreferences().respondAsync());

    this.odataClient = odataClient;
    final HttpMethod method = odataRequest.getMethod();

    // target uri
    this.uri = odataRequest.getURI();

    HttpClient _httpClient =
        odataClient.getConfiguration().getHttpClientFactory().create(method, this.uri);
    if (odataClient.getConfiguration().isGzipCompression()) {
      _httpClient = new DecompressingHttpClient(_httpClient);
    }
    this.httpClient = _httpClient;

    this.request =
        odataClient.getConfiguration().getHttpUriRequestFactory().create(method, this.uri);

    if (request instanceof HttpEntityEnclosingRequestBase) {
      if (odataRequest instanceof AbstractODataBasicRequest) {
        AbstractODataBasicRequest br = (AbstractODataBasicRequest) odataRequest;
        HttpEntityEnclosingRequestBase httpRequest = ((HttpEntityEnclosingRequestBase) request);
        httpRequest.setEntity(new InputStreamEntity(br.getPayload(), -1));
      }
    }
  }
コード例 #5
0
  private static Object apiCall(String path, Map<String, Object> fields, String method) {

    if (method == "GET" || method == "DELETE") {
      String query = URLEncodedUtils.format(getForm(fields), HTTP.UTF_8);
      URI uri = getURI(path, query);
      System.out.println(method + ": " + safelog(uri));
      HttpUriRequest request = (method == "GET") ? new HttpGet(uri) : new HttpDelete(uri);
      return executeParse(request);
    } else if (method == "POST" || method == "PUT") {
      URI uri = getURI(path, null);
      System.out.println(method + ": " + safelog(uri));
      HttpEntityEnclosingRequestBase request =
          (method == "POST") ? new HttpPost(uri) : new HttpPut(uri);
      try {
        UrlEncodedFormEntity body = new UrlEncodedFormEntity(getForm(fields));
        if (DEBUG) System.out.println("body: " + safelog(body));
        request.setEntity(body);
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Unsupported Encoding Exception", e);
      }
      return executeParse(request);
    }

    throw new RuntimeException("Invalid HTTP method");
  }
コード例 #6
0
 /** create an apache request */
 private HttpUriRequest createApacheRequest(AbstractRequest request) throws HttpClientException {
   HttpEntityEnclosingRequestBase entityRequset = null;
   switch (request.getMethod()) {
     case Get:
       return new HttpGet(request.getFullUri());
     case Head:
       return new HttpHead(request.getFullUri());
     case Delete:
       return new HttpDelete(request.getFullUri());
     case Trace:
       return new HttpTrace(request.getFullUri());
     case Options:
       return new HttpOptions(request.getFullUri());
     case Post:
       entityRequset = new HttpPost(request.getFullUri());
       break;
     case Put:
       entityRequset = new HttpPut(request.getFullUri());
       break;
     case Patch:
       entityRequset = new HttpPatch(request.getFullUri());
       break;
     default:
       return new HttpGet(request.getFullUri());
   }
   entityRequset.setEntity(EntityBuilder.build(request));
   return entityRequset;
 }
コード例 #7
0
  private HttpEntityEnclosingRequestBase addEntityToRequestBase(
      HttpEntityEnclosingRequestBase requestBase, HttpEntity entity) {
    if (entity != null) {
      requestBase.setEntity(entity);
    }

    return requestBase;
  }
コード例 #8
0
ファイル: HttpClientStack.java プロジェクト: Xiaoyuyexi/TFB
 private static void setEntityIfNonEmptyBody(
     HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {
   byte[] body = request.getBody();
   if (body != null) {
     HttpEntity entity = new ByteArrayEntity(body);
     httpRequest.setEntity(entity);
   }
 }
コード例 #9
0
 /**
  * Sets a JSON String as a request entity.
  *
  * @param httpRequest The request to set entity.
  * @param json The JSON String to set.
  */
 protected void setEntity(HttpEntityEnclosingRequestBase httpRequest, String json) {
   try {
     StringEntity entity = new StringEntity(json, "UTF-8");
     entity.setContentType("application/json");
     httpRequest.setEntity(entity);
   } catch (UnsupportedEncodingException e) {
     log.error("Error setting request data. " + e.getMessage());
     throw new IllegalArgumentException(e);
   }
 }
コード例 #10
0
ファイル: Http.java プロジェクト: ZhangUP/CloudposSDK
 public <T> void post(
     String url,
     Header[] headers,
     AjaxParams params,
     String contentType,
     AjaxCallBack<T> callBack) {
   HttpEntityEnclosingRequestBase request = new HttpPost(url);
   if (params != null) request.setEntity(paramsToEntity(params));
   if (headers != null) request.setHeaders(headers);
   sendRequest(httpClient, httpContext, request, contentType, callBack);
 }
  /** {@inheritDoc } */
  @Override
  public ODataEntityUpdateResponse execute() {
    final InputStream input = getPayload();
    ((HttpEntityEnclosingRequestBase) request)
        .setEntity(URIUtils.buildInputStreamEntity(odataClient, input));

    try {
      return new ODataEntityUpdateResponseImpl(httpClient, doExecute());
    } finally {
      IOUtils.closeQuietly(input);
    }
  }
コード例 #12
0
ファイル: QiitaExecutor.java プロジェクト: jabaraster/qiita4j
 private void setFormValue(HttpEntityEnclosingRequestBase request, Map<String, String> params)
     throws QiitaException {
   List<NameValuePair> nameValuePairs = new ArrayList<>();
   for (Entry<String, String> entry : params.entrySet()) {
     nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
   }
   try {
     request.setEntity(new UrlEncodedFormEntity(nameValuePairs, UTF_8.name()));
   } catch (UnsupportedEncodingException e) {
     throw new QiitaException(e);
   }
 }
コード例 #13
0
  protected HttpEntityEnclosingRequestBase setupPostRequest(
      HttpEntityEnclosingRequestBase request, String postText) {
    if (postText == null || postText.length() == 0) return request;

    StringEntity entity = new StringEntity(postText, "UTF-8");
    BasicHeader basicHeader = new BasicHeader(HTTP.CONTENT_TYPE, "application/json");
    // request.getParams().setBooleanParameter("http.protocol.expect-continue", false);
    entity.setContentType(basicHeader);
    request.setEntity(entity);

    return request;
  }
コード例 #14
0
 protected void assignContent(HttpEntityEnclosingRequestBase request, Map<String, String> params)
     throws UnsupportedEncodingException {
   if (params != null && !params.isEmpty()) {
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
     for (Map.Entry<String, String> param : params.entrySet())
       pairs.add(new BasicNameValuePair(param.getKey(), param.getValue()));
     StringEntity entity =
         new StringEntity(URLEncodedUtils.format(pairs, UTF_8.name()), UTF_8.name());
     entity.setContentType(CONTENT_TYPE_WWW_FORM_URLENCODED);
     request.setEntity(entity);
   }
 }
コード例 #15
0
 /**
  * Perform a HTTP POST request and track the Android Context which initiated the request. Set
  * headers only for this request
  *
  * @param context the Android Context which initiated the request.
  * @param url the URL to send the request to.
  * @param headers set headers only for this request
  * @param params additional POST parameters to send with the request.
  * @param contentType the content type of the payload you are sending, for example
  *     application/json if sending a json payload.
  * @param responseHandler the response handler instance that should handle the response.
  */
 public RequestHandle post(
     Context context,
     String url,
     Header[] headers,
     RequestParams params,
     String contentType,
     AsyncHttpResponseHandler responseHandler) {
   HttpEntityEnclosingRequestBase request = new HttpPost(url);
   if (params != null) request.setEntity(paramsToEntity(params, responseHandler));
   if (headers != null) request.setHeaders(headers);
   return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
 }
コード例 #16
0
  public BrowserMobHttpResponse execute() {
    // deal with PUT/POST requests
    if (method instanceof HttpEntityEnclosingRequestBase) {
      HttpEntityEnclosingRequestBase enclodingRequest = (HttpEntityEnclosingRequestBase) method;

      if (!nvps.isEmpty()) {
        try {
          if (!multiPart) {
            enclodingRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
          } else {
            for (NameValuePair nvp : nvps) {
              multipartEntity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
            }
            enclodingRequest.setEntity(multipartEntity);
          }
        } catch (UnsupportedEncodingException e) {
          LOG.severe("Could not find UTF-8 encoding, something is really wrong", e);
        }
      } else if (multipartEntity != null) {
        enclodingRequest.setEntity(multipartEntity);
      } else if (byteArrayEntity != null) {
        enclodingRequest.setEntity(byteArrayEntity);
      } else if (stringEntity != null) {
        enclodingRequest.setEntity(stringEntity);
      } else if (inputStreamEntity != null) {
        enclodingRequest.setEntity(inputStreamEntity);
      }
    }

    return client.execute(this);
  }
コード例 #17
0
 private HttpResponse executePutPost(
     HttpEntityEnclosingRequestBase request, String content, boolean useBackend) {
   try {
     if (LOG.isTraceEnabled()) {
       LOG.trace("Content: {}", content);
     }
     StringEntity e = new StringEntity(content, "UTF-8");
     e.setContentType("application/json");
     request.setEntity(e);
     return executeRequest(request, useBackend);
   } catch (Exception e) {
     throw Exceptions.propagate(e);
   }
 }
コード例 #18
0
 protected void addEntity(HttpEntity entity, HttpRequestBase uriRequest) throws IOException {
   if (entity != null) {
     AbstractHttpEntity httpEntity = null;
     if (entity instanceof MultipartEntity) {
       ByteArrayOutputStream os = new ByteArrayOutputStream();
       entity.writeTo(os);
       os.flush();
       httpEntity = new org.apache.http.entity.ByteArrayEntity(os.toByteArray());
       os.close();
     } else {
       httpEntity = new InputStreamEntity(entity.getContent(), entity.getContentLength());
     }
     httpEntity.setContentType(entity.getContentType().toString());
     ((HttpEntityEnclosingRequestBase) uriRequest).setEntity(httpEntity);
   }
 }
コード例 #19
0
ファイル: AuthContext.java プロジェクト: vilterp/hitman
  public Either<IOException, HttpResponse> execUploadRequest(
      String path, List<FormBodyPart> parts, HTTPMethod method, String acceptType) {
    HttpEntityEnclosingRequestBase httpReq = null;
    String url = getUriForPath(path);
    if (method.equals(HTTPMethod.POST)) {
      httpReq = new HttpPost(url);
    } else if (method.equals(HTTPMethod.PUT)) {
      httpReq = new HttpPut(url);
    } else {
      throw new IllegalArgumentException("gotta be this or that: post or put");
    }
    MultipartEntity entity = new MultipartEntity();
    for (FormBodyPart part : parts) {
      entity.addPart(part);
    }
    httpReq.setEntity(entity);

    return execRequest(httpReq, acceptType);
  }
コード例 #20
0
ファイル: MyHttp.java プロジェクト: woting2015/WtAppProject
 /**
  * 传递参数的数据请求
  *
  * @param paramString
  * @param paramList
  * @return
  * @throws ClientProtocolException
  * @throws IOException
  * @throws JSONException
  */
 public static String httpPost(String paramString, List<NameValuePair> paramList)
     throws ClientProtocolException, IOException {
   HttpClient defaultHttpClient = getHttp();
   HttpUriRequest httpPost = new HttpPost(paramString);
   if (null != paramList)
     ((HttpEntityEnclosingRequestBase) httpPost)
         .setEntity(
             new UrlEncodedFormEntity(
                 (java.util.List<? extends org.apache.http.NameValuePair>) paramList, "UTF-8"));
   HttpEntity httpEntity = defaultHttpClient.execute(httpPost).getEntity();
   if (httpEntity == null) return null;
   String resultStr = EntityUtils.toString(httpEntity);
   CookieStore mCookieStore = ((AbstractHttpClient) defaultHttpClient).getCookieStore();
   List<org.apache.http.cookie.Cookie> cookies = mCookieStore.getCookies();
   for (int i = 0; i < cookies.size(); i++) {
     // 这里是读取Cookie['PHPSESSID']的值存在静态变量中,保证每次都是同一个值
     cookieStr = cookies.get(i).getName() + "=" + cookies.get(i).getValue();
   }
   return resultStr;
 }
コード例 #21
0
  /**
   * Serializes this object to an existing {@link HttpRequestBase} which can be sent as an HTTP
   * request. Specifically, sends the object's link, user-defined metadata and vclock as HTTP
   * headers and the value as the body. Used by {@link RiakClient} to create PUT requests.
   *
   * <p>if the this RiakObject's value is a stream, and no length is set, the stream is first
   * buffered into a byte array before being written
   */
  public void writeToHttpMethod(HttpRequestBase httpMethod) {
    // Serialize headers
    String basePath = getBasePathFromHttpMethod(httpMethod);
    writeLinks(httpMethod, basePath);
    for (String name : userMetaData.keySet()) {
      httpMethod.addHeader(Constants.HDR_USERMETA_REQ_PREFIX + name, userMetaData.get(name));
    }

    writeIndexes(httpMethod);

    if (vclock != null) {
      httpMethod.addHeader(Constants.HDR_VCLOCK, vclock);
    }

    // Serialize body
    if (httpMethod instanceof HttpEntityEnclosingRequestBase) {
      HttpEntityEnclosingRequestBase entityEnclosingMethod =
          (HttpEntityEnclosingRequestBase) httpMethod;
      AbstractHttpEntity entity = null;
      // Any value set using setValueAsStream() has precedent over value
      // set using setValue()
      if (valueStream != null) {
        if (valueStreamLength != null && valueStreamLength >= 0) {
          entity = new InputStreamEntity(valueStream, valueStreamLength);
        } else {
          // since apache http client 4.1 no longer supports buffering stream entities, but we can't
          // change API
          // behaviour, here we have to buffer the whole content
          entity = new ByteArrayEntity(ClientUtils.bufferStream(valueStream));
        }
      } else if (value != null) {
        entity = new ByteArrayEntity(value);
      } else {
        entity = new ByteArrayEntity(EMPTY);
      }
      entity.setContentType(contentType);
      entityEnclosingMethod.setEntity(entity);
    }
  }
コード例 #22
0
  @Override
  public IFcrepoResponse execute() throws FcrepoOperationFailedException {
    final HttpEntityEnclosingRequestBase request =
        (HttpEntityEnclosingRequestBase) new HttpPatch(uri);

    if (ifMatch != null) {
      request.setHeader("If-Match", ifMatch);
    }

    if (ifUnmodifiedSince != null) {
      request.setHeader("If-Unmodified-Since", ifUnmodifiedSince);
    }

    if (contentLocation != null) {
      request.setHeader("Content-Location", contentLocation);
    }

    if (body != null) {
      request.setEntity(new InputStreamEntity(body));
    }

    return executeRequest(request);
  }
コード例 #23
0
  /**
   * If Request is MultiPartRequest type, then set MultipartEntity in the httpRequest object.
   *
   * @param httpRequest
   * @param request
   * @throws AuthFailureError
   */
  private static void setMultiPartBody(
      HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {

    // Return if Request is not MultiPartRequest
    if (!(request instanceof MultiPartRequest)) {
      return;
    }

    // MultipartEntity multipartEntity = new
    // MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    /* example for setting a HttpMultipartMode */
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    // Iterate the fileUploads
    Map<String, File> fileUpload = ((MultiPartRequest) request).getFileUploads();
    for (Map.Entry<String, File> entry : fileUpload.entrySet()) {

      builder.addPart(((String) entry.getKey()), new FileBody((File) entry.getValue()));
    }

    ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
    // Iterate the stringUploads
    Map<String, String> stringUpload = ((MultiPartRequest) request).getStringUploads();
    for (Map.Entry<String, String> entry : stringUpload.entrySet()) {
      try {
        builder.addPart(
            ((String) entry.getKey()), new StringBody((String) entry.getValue(), contentType));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    httpRequest.setEntity(builder.build());
  }
コード例 #24
0
ファイル: AuthContext.java プロジェクト: vilterp/hitman
  public Either<IOException, HttpResponse> execNormalRequest(
      String path, Map<String, String> params, HTTPMethod method, String acceptType) {
    String url = getUriForPath(path);
    if (params == null) {
      params = new HashMap<String, String>();
    }
    HttpUriRequest httpReq = null;
    if (method.equals(HTTPMethod.GET)) {
      HttpGet get = new HttpGet(url);
      for (Map.Entry<String, String> param : params.entrySet()) {
        get.getParams().setParameter(param.getKey(), param.getValue());
      }
      httpReq = get;
    } else {
      HttpEntityEnclosingRequestBase postOrPut = null;
      if (method.equals(HTTPMethod.PUT)) {
        postOrPut = new HttpPut(url);
      } else {
        postOrPut = new HttpPost(url);
      }
      MultipartEntity entity = new MultipartEntity();

      List<NameValuePair> args = new ArrayList<NameValuePair>();
      for (Map.Entry<String, String> param : params.entrySet()) {
        args.add(new BasicNameValuePair(param.getKey(), param.getValue()));
      }
      try {
        postOrPut.setEntity(new UrlEncodedFormEntity(args));
      } catch (UnsupportedEncodingException e) {
        return new Left<IOException, HttpResponse>(e);
      }
      httpReq = postOrPut;
    }

    return execRequest(httpReq, acceptType);
  }
コード例 #25
0
ファイル: Olingo2AppImpl.java プロジェクト: mnki/camel
  private <T> void writeContent(
      final Edm edm,
      HttpEntityEnclosingRequestBase httpEntityRequest,
      final UriInfoWithType uriInfo,
      final Object content,
      final Olingo2ResponseHandler<T> responseHandler) {

    try {
      // process resource by UriType
      final ODataResponse response = writeContent(edm, uriInfo, content);

      // copy all response headers
      for (String header : response.getHeaderNames()) {
        httpEntityRequest.setHeader(header, response.getHeader(header));
      }

      // get (http) entity which is for default Olingo2 implementation an InputStream
      if (response.getEntity() instanceof InputStream) {
        httpEntityRequest.setEntity(new InputStreamEntity((InputStream) response.getEntity()));
        /*
                        // avoid sending it without a header field set
                        if (!httpEntityRequest.containsHeader(HttpHeaders.CONTENT_TYPE)) {
                            httpEntityRequest.addHeader(HttpHeaders.CONTENT_TYPE, getContentType());
                        }
        */
      }

      // execute HTTP request
      final Header requestContentTypeHeader =
          httpEntityRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE);
      final ContentType requestContentType =
          requestContentTypeHeader != null
              ? ContentType.parse(requestContentTypeHeader.getValue())
              : contentType;
      execute(
          httpEntityRequest,
          requestContentType,
          new AbstractFutureCallback<T>(responseHandler) {
            @SuppressWarnings("unchecked")
            @Override
            public void onCompleted(HttpResponse result)
                throws IOException, EntityProviderException, BatchException,
                    ODataApplicationException {

              // if a entity is created (via POST request) the response body contains the new
              // created entity
              HttpStatusCodes statusCode =
                  HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode());

              // look for no content, or no response body!!!
              final boolean noEntity =
                  result.getEntity() == null || result.getEntity().getContentLength() == 0;
              if (statusCode == HttpStatusCodes.NO_CONTENT || noEntity) {
                responseHandler.onResponse(
                    (T) HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode()));
              } else {

                switch (uriInfo.getUriType()) {
                  case URI9:
                    // $batch
                    final List<BatchSingleResponse> singleResponses =
                        EntityProvider.parseBatchResponse(
                            result.getEntity().getContent(),
                            result.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());

                    // parse batch response bodies
                    final List<Olingo2BatchResponse> responses =
                        new ArrayList<Olingo2BatchResponse>();
                    Map<String, String> contentIdLocationMap = new HashMap<String, String>();

                    final List<Olingo2BatchRequest> batchRequests =
                        (List<Olingo2BatchRequest>) content;
                    final Iterator<Olingo2BatchRequest> iterator = batchRequests.iterator();

                    for (BatchSingleResponse response : singleResponses) {
                      final Olingo2BatchRequest request = iterator.next();

                      if (request instanceof Olingo2BatchChangeRequest
                          && ((Olingo2BatchChangeRequest) request).getContentId() != null) {

                        contentIdLocationMap.put(
                            "$" + ((Olingo2BatchChangeRequest) request).getContentId(),
                            response.getHeader(HttpHeaders.LOCATION));
                      }

                      try {
                        responses.add(parseResponse(edm, contentIdLocationMap, request, response));
                      } catch (Exception e) {
                        // report any parsing errors as error response
                        responses.add(
                            new Olingo2BatchResponse(
                                Integer.parseInt(response.getStatusCode()),
                                response.getStatusInfo(),
                                response.getContentId(),
                                response.getHeaders(),
                                new ODataApplicationException(
                                    "Error parsing response for " + request + ": " + e.getMessage(),
                                    Locale.ENGLISH,
                                    e)));
                      }
                    }
                    responseHandler.onResponse((T) responses);
                    break;

                  case URI4:
                  case URI5:
                    // simple property
                    // get the response content as Object for $value or Map<String, Object>
                    // otherwise
                    final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
                    final EdmProperty simpleProperty =
                        simplePropertyPath.get(simplePropertyPath.size() - 1);
                    if (uriInfo.isValue()) {
                      responseHandler.onResponse(
                          (T)
                              EntityProvider.readPropertyValue(
                                  simpleProperty, result.getEntity().getContent()));
                    } else {
                      responseHandler.onResponse(
                          (T)
                              EntityProvider.readProperty(
                                  getContentType(),
                                  simpleProperty,
                                  result.getEntity().getContent(),
                                  EntityProviderReadProperties.init().build()));
                    }
                    break;

                  case URI3:
                    // complex property
                    // get the response content as Map<String, Object>
                    final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
                    final EdmProperty complexProperty =
                        complexPropertyPath.get(complexPropertyPath.size() - 1);
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readProperty(
                                getContentType(),
                                complexProperty,
                                result.getEntity().getContent(),
                                EntityProviderReadProperties.init().build()));
                    break;

                  case URI7A:
                    // $links with 0..1 cardinality property
                    // get the response content as String
                    final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet();
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readLink(
                                getContentType(),
                                targetLinkEntitySet,
                                result.getEntity().getContent()));
                    break;

                  case URI7B:
                    // $links with * cardinality property
                    // get the response content as java.util.List<String>
                    final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet();
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readLinks(
                                getContentType(),
                                targetLinksEntitySet,
                                result.getEntity().getContent()));
                    break;

                  case URI1:
                  case URI2:
                  case URI6A:
                  case URI6B:
                    // Entity
                    // get the response content as an ODataEntry object
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readEntry(
                                response.getContentHeader(),
                                uriInfo.getTargetEntitySet(),
                                result.getEntity().getContent(),
                                EntityProviderReadProperties.init().build()));
                    break;

                  default:
                    throw new ODataApplicationException(
                        "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
                }
              }
            }
          });
    } catch (ODataException e) {
      responseHandler.onException(e);
    } catch (URISyntaxException e) {
      responseHandler.onException(e);
    } catch (UnsupportedEncodingException e) {
      responseHandler.onException(e);
    } catch (IOException e) {
      responseHandler.onException(e);
    }
  }
コード例 #26
0
ファイル: QiitaExecutor.java プロジェクト: jabaraster/qiita4j
 private void setBody(HttpEntityEnclosingRequestBase request, String body) {
   request.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON));
 }
コード例 #27
0
  S3Response invoke(
      @Nullable String bucket, @Nullable String object, @Nullable String temporaryEndpoint)
      throws S3Exception, CloudException, InternalException {
    if (wire.isDebugEnabled()) {
      wire.debug("");
      wire.debug(
          "----------------------------------------------------------------------------------");
    }
    try {
      StringBuilder url = new StringBuilder();
      boolean leaveOpen = false;
      HttpRequestBase method;
      HttpClient client;
      int status;

      if (provider.getEC2Provider().isAWS()) {
        url.append("https://");
        if (temporaryEndpoint == null) {
          boolean validDomainName = isValidDomainName(bucket);
          if (bucket != null && validDomainName) {
            url.append(bucket);
            url.append(".");
          }
          url.append("s3.amazonaws.com/");
          if (bucket != null && !validDomainName) {
            url.append(bucket);
            url.append("/");
          }
        } else {
          url.append(temporaryEndpoint);
          url.append("/");
        }
      } else if (provider.getEC2Provider().isStorage()
          && "google".equalsIgnoreCase(provider.getProviderName())) {
        url.append("https://");
        if (temporaryEndpoint == null) {
          if (bucket != null) {
            url.append(bucket);
            url.append(".");
          }
          url.append("commondatastorage.googleapis.com/");
        } else {
          url.append(temporaryEndpoint);
          url.append("/");
        }
      } else {
        int idx = 0;

        if (!provider.getContext().getEndpoint().startsWith("http")) {
          url.append("https://");
        } else {
          idx = provider.getContext().getEndpoint().indexOf("https://");
          if (idx == -1) {
            idx = "http://".length();
            url.append("http://");
          } else {
            idx = "https://".length();
            url.append("https://");
          }
        }
        String service = "";
        if (provider.getEC2Provider().isEucalyptus()) {
          service = "Walrus/";
        }

        if (temporaryEndpoint == null) {
          url.append(provider.getContext().getEndpoint().substring(idx));
          if (!provider.getContext().getEndpoint().endsWith("/")) {
            url.append("/").append(service);
          } else {
            url.append(service);
          }
        } else {
          url.append(temporaryEndpoint);
          url.append("/");
          url.append(service);
        }
        if (bucket != null) {
          url.append(bucket);
          url.append("/");
        }
      }
      if (object != null) {
        url.append(object);
      } else if (parameters != null) {
        boolean first = true;

        if (object != null && object.indexOf('?') != -1) {
          first = false;
        }
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
          String key = entry.getKey();
          String val = entry.getValue();

          if (first) {
            url.append("?");
            first = false;
          } else {
            url.append("&");
          }
          if (val != null) {
            url.append(AWSCloud.encode(key, false));
            url.append("=");
            url.append(AWSCloud.encode(val, false));
          } else {
            url.append(AWSCloud.encode(key, false));
          }
        }
      }

      if (provider.getEC2Provider().isStorage()
          && provider.getProviderName().equalsIgnoreCase("Google")) {
        headers.put(AWSCloud.P_GOOG_DATE, getDate());
      } else {
        headers.put(AWSCloud.P_AWS_DATE, getDate());
      }
      method = action.getMethod(url.toString());
      if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
          method.addHeader(entry.getKey(), entry.getValue());
        }
      }
      if (contentType == null && body != null) {
        contentType = "application/xml";
        method.addHeader("Content-Type", contentType);
      }
      try {
        String hash = null;
        String signature;

        signature =
            provider.signS3(
                new String(provider.getContext().getAccessPublic(), "utf-8"),
                provider.getContext().getAccessPrivate(),
                method.getMethod(),
                hash,
                contentType,
                headers,
                bucket,
                object);
        method.addHeader(AWSCloud.P_CFAUTH, signature);
      } catch (UnsupportedEncodingException e) {
        logger.error(e);
        e.printStackTrace();
        throw new InternalException(e);
      }
      if (body != null) {
        try {
          ((HttpEntityEnclosingRequestBase) method)
              .setEntity(new StringEntity(body, "application/xml", "utf-8"));
        } catch (UnsupportedEncodingException e) {
          throw new InternalException(e);
        }
      } else if (uploadFile != null) {
        ((HttpEntityEnclosingRequestBase) method)
            .setEntity(new FileEntity(uploadFile, contentType));
      }
      attempts++;
      client = getClient(url.toString(), body == null && uploadFile == null);

      if (wire.isDebugEnabled()) {
        wire.debug("[" + url.toString() + "]");
        wire.debug(method.getRequestLine().toString());
        for (Header header : method.getAllHeaders()) {
          wire.debug(header.getName() + ": " + header.getValue());
        }
        wire.debug("");
        if (body != null) {
          try {
            wire.debug(EntityUtils.toString(((HttpEntityEnclosingRequestBase) method).getEntity()));
          } catch (IOException ignore) {
          }

          wire.debug("");
        } else if (uploadFile != null) {
          wire.debug("-- file upload --");
          wire.debug("");
        }
      }
      S3Response response = new S3Response();
      HttpResponse httpResponse;

      try {
        httpResponse = client.execute(method);
        if (wire.isDebugEnabled()) {
          wire.debug(httpResponse.getStatusLine().toString());
          for (Header header : httpResponse.getAllHeaders()) {
            wire.debug(header.getName() + ": " + header.getValue());
          }
          wire.debug("");
        }
        status = httpResponse.getStatusLine().getStatusCode();
      } catch (IOException e) {
        logger.error(url + ": " + e.getMessage());
        e.printStackTrace();
        throw new InternalException(e);
      }
      response.headers = httpResponse.getAllHeaders();

      HttpEntity entity = httpResponse.getEntity();
      InputStream input = null;

      if (entity != null) {
        try {
          input = entity.getContent();
        } catch (IOException e) {
          throw new CloudException(e);
        }
      }
      try {
        if (status == HttpServletResponse.SC_OK
            || status == HttpServletResponse.SC_CREATED
            || status == HttpServletResponse.SC_ACCEPTED) {
          Header clen = httpResponse.getFirstHeader("Content-Length");
          long len = -1L;

          if (clen != null) {
            len = Long.parseLong(clen.getValue());
          }
          if (len != 0L) {
            try {
              Header ct = httpResponse.getFirstHeader("Content-Type");

              if (ct != null
                  && (ct.getValue().startsWith("application/xml")
                      || ct.getValue().startsWith("text/xml"))) {
                try {
                  response.document = parseResponse(input);
                  return response;
                } finally {
                  input.close();
                }
              } else if (ct != null
                  && ct.getValue().startsWith("application/octet-stream")
                  && len < 1) {
                return null;
              } else {
                response.contentLength = len;
                if (ct != null) {
                  response.contentType = ct.getValue();
                }
                response.input = input;
                response.method = method;
                leaveOpen = true;
                return response;
              }
            } catch (IOException e) {
              logger.error(e);
              e.printStackTrace();
              throw new CloudException(e);
            }
          } else {
            return response;
          }
        } else if (status == HttpServletResponse.SC_NO_CONTENT) {
          return response;
        } else if (status == HttpServletResponse.SC_NOT_FOUND) {
          throw new S3Exception(status, null, null, "Object not found.");
        } else {
          if (status == HttpServletResponse.SC_SERVICE_UNAVAILABLE
              || status == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
            if (attempts >= 5) {
              String msg;

              if (status == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
                msg = "Cloud service is currently unavailable.";
              } else {
                msg = "The cloud service encountered a server error while processing your request.";
              }
              logger.error(msg);
              throw new CloudException(msg);
            } else {
              leaveOpen = true;
              if (input != null) {
                try {
                  input.close();
                } catch (IOException ignore) {
                }
              }
              try {
                Thread.sleep(5000L);
              } catch (InterruptedException ignore) {
              }
              return invoke(bucket, object);
            }
          }
          try {
            Document doc;

            try {
              logger.warn("Received error code: " + status);
              doc = parseResponse(input);
            } finally {
              input.close();
            }
            if (doc != null) {
              String endpoint = null, code = null, message = null, requestId = null;
              NodeList blocks = doc.getElementsByTagName("Error");

              if (blocks.getLength() > 0) {
                Node error = blocks.item(0);
                NodeList attrs;

                attrs = error.getChildNodes();
                for (int i = 0; i < attrs.getLength(); i++) {
                  Node attr = attrs.item(i);

                  if (attr.getNodeName().equals("Code") && attr.hasChildNodes()) {
                    code = attr.getFirstChild().getNodeValue().trim();
                  } else if (attr.getNodeName().equals("Message") && attr.hasChildNodes()) {
                    message = attr.getFirstChild().getNodeValue().trim();
                  } else if (attr.getNodeName().equals("RequestId") && attr.hasChildNodes()) {
                    requestId = attr.getFirstChild().getNodeValue().trim();
                  } else if (attr.getNodeName().equals("Endpoint") && attr.hasChildNodes()) {
                    endpoint = attr.getFirstChild().getNodeValue().trim();
                  }
                }
              }
              if (endpoint != null && code.equals("TemporaryRedirect")) {
                if (temporaryEndpoint != null) {
                  throw new CloudException("Too deep redirect to " + endpoint);
                } else {
                  return invoke(bucket, object, endpoint);
                }
              } else {
                if (message == null) {
                  throw new CloudException(
                      "Unable to identify error condition: "
                          + status
                          + "/"
                          + requestId
                          + "/"
                          + code);
                }
                throw new S3Exception(status, requestId, code, message);
              }
            } else {
              throw new CloudException("Unable to parse error.");
            }
          } catch (IOException e) {
            if (status == HttpServletResponse.SC_FORBIDDEN) {
              throw new S3Exception(
                  status, "", "AccessForbidden", "Access was denied without explanation.");
            }
            throw new CloudException(e);
          } catch (RuntimeException e) {
            throw new CloudException(e);
          } catch (Error e) {
            throw new CloudException(e);
          }
        }
      } finally {
        if (!leaveOpen) {
          if (input != null) {
            try {
              input.close();
            } catch (IOException ignore) {
            }
          }
        }
      }
    } finally {
      if (wire.isDebugEnabled()) {
        wire.debug(
            "----------------------------------------------------------------------------------");
        wire.debug("");
      }
    }
  }
コード例 #28
0
ファイル: AppFabricTestBase.java プロジェクト: domarps/cdap
  /** Deploys an application with (optionally) a defined app name and app version */
  protected HttpResponse deploy(
      Class<?> application,
      @Nullable String apiVersion,
      @Nullable String namespace,
      @Nullable String appName,
      @Nullable String appVersion,
      @Nullable Config appConfig)
      throws Exception {
    namespace = namespace == null ? Constants.DEFAULT_NAMESPACE : namespace;
    apiVersion = apiVersion == null ? Constants.Gateway.API_VERSION_3_TOKEN : apiVersion;

    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(ManifestFields.MAIN_CLASS, application.getName());
    if (appVersion != null) {
      manifest.getMainAttributes().put(ManifestFields.BUNDLE_VERSION, appVersion);
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final JarOutputStream jarOut = new JarOutputStream(bos, manifest);
    final String pkgName = application.getPackage().getName();

    // Grab every classes under the application class package.
    try {
      ClassLoader classLoader = application.getClassLoader();
      if (classLoader == null) {
        classLoader = ClassLoader.getSystemClassLoader();
      }
      Dependencies.findClassDependencies(
          classLoader,
          new ClassAcceptor() {
            @Override
            public boolean accept(String className, URL classUrl, URL classPathUrl) {
              try {
                if (className.startsWith(pkgName)) {
                  jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class"));
                  InputStream in = classUrl.openStream();
                  try {
                    ByteStreams.copy(in, jarOut);
                  } finally {
                    in.close();
                  }
                  return true;
                }
                return false;
              } catch (Exception e) {
                throw Throwables.propagate(e);
              }
            }
          },
          application.getName());

      // Add webapp
      jarOut.putNextEntry(new ZipEntry("webapp/default/netlens/src/1.txt"));
      ByteStreams.copy(new ByteArrayInputStream("dummy data".getBytes(Charsets.UTF_8)), jarOut);
    } finally {
      jarOut.close();
    }

    HttpEntityEnclosingRequestBase request;
    String versionedApiPath = getVersionedAPIPath("apps/", apiVersion, namespace);
    if (appName == null) {
      request = getPost(versionedApiPath);
    } else {
      request = getPut(versionedApiPath + appName);
    }
    request.setHeader(Constants.Gateway.API_KEY, "api-key-example");
    request.setHeader("X-Archive-Name", application.getSimpleName() + ".jar");
    if (appConfig != null) {
      request.setHeader("X-App-Config", GSON.toJson(appConfig));
    }
    request.setEntity(new ByteArrayEntity(bos.toByteArray()));
    return execute(request);
  }
コード例 #29
0
  public HttpResponse fetch(org.apache.shindig.gadgets.http.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 (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 = 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(), StringUtils.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 = FETCHER.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())) {
        LOG.info(
            "Timeout for "
                + request.getUri()
                + " Exception: "
                + e.getClass().getName()
                + " - "
                + e.getMessage()
                + " - "
                + (now - started)
                + "ms");
        return HttpResponse.timeout();
      }

      LOG.log(
          Level.INFO,
          "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();
        } catch (UnsupportedOperationException e) {
          // ignore
        }
    }
  }
コード例 #30
0
  private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
      String urlstr = coreUrl + queryParams.toQueryString();

      boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
      if ("GET".equals(req.getMethod())) {
        method = new HttpGet(urlstr);
      } else if ("HEAD".equals(req.getMethod())) {
        method = new HttpHead(urlstr);
      } else if (isPostOrPutRequest) {
        HttpEntityEnclosingRequestBase entityRequest =
            "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
        InputStream in =
            new CloseShieldInputStream(req.getInputStream()); // Prevent close of container streams
        HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
        entityRequest.setEntity(entity);
        method = entityRequest;
      } else if ("DELETE".equals(req.getMethod())) {
        method = new HttpDelete(urlstr);
      } else {
        throw new SolrException(
            SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
      }

      for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
        String headerName = e.nextElement();
        if (!"host".equalsIgnoreCase(headerName)
            && !"authorization".equalsIgnoreCase(headerName)
            && !"accept".equalsIgnoreCase(headerName)) {
          method.addHeader(headerName, req.getHeader(headerName));
        }
      }
      // These headers not supported for HttpEntityEnclosingRequests
      if (method instanceof HttpEntityEnclosingRequest) {
        method.removeHeaders(TRANSFER_ENCODING_HEADER);
        method.removeHeaders(CONTENT_LENGTH_HEADER);
      }

      final HttpResponse response =
          solrDispatchFilter.httpClient.execute(
              method, HttpClientUtil.createNewHttpClientRequestContext());
      int httpStatus = response.getStatusLine().getStatusCode();
      httpEntity = response.getEntity();

      resp.setStatus(httpStatus);
      for (HeaderIterator responseHeaders = response.headerIterator();
          responseHeaders.hasNext(); ) {
        Header header = responseHeaders.nextHeader();

        // We pull out these two headers below because they can cause chunked
        // encoding issues with Tomcat
        if (header != null
            && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER)
            && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
          resp.addHeader(header.getName(), header.getValue());
        }
      }

      if (httpEntity != null) {
        if (httpEntity.getContentEncoding() != null)
          resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
        if (httpEntity.getContentType() != null)
          resp.setContentType(httpEntity.getContentType().getValue());

        InputStream is = httpEntity.getContent();
        OutputStream os = resp.getOutputStream();

        IOUtils.copyLarge(is, os);
      }

    } catch (IOException e) {
      sendError(
          new SolrException(
              SolrException.ErrorCode.SERVER_ERROR,
              "Error trying to proxy request for url: " + coreUrl,
              e));
    } finally {
      Utils.consumeFully(httpEntity);
    }
  }