/** 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;
 }
  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));
      }
    }
  }
  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");
  }
Пример #4
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);
    }
  }
Пример #5
0
 public void put(
     String url,
     Header[] headers,
     HttpEntity entity,
     String contentType,
     AjaxCallBack<? extends Object> callBack) {
   HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPut(url), entity);
   if (headers != null) request.setHeaders(headers);
   sendRequest(httpClient, httpContext, request, contentType, callBack);
 }
 /**
  * Perform a HTTP PUT request and track the Android Context which initiated the request. And set
  * one-time headers for the request
  *
  * @param context the Android Context which initiated the request.
  * @param url the URL to send the request to.
  * @param headers set one-time headers for this request
  * @param entity a raw {@link HttpEntity} to send with the request, for example, use this to send
  *     string/json/xml payloads to a server by passing a {@link
  *     org.apache.http.entity.StringEntity}.
  * @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 void put(
     Context context,
     String url,
     Header[] headers,
     HttpEntity entity,
     String contentType,
     AsyncHttpResponseHandler responseHandler) {
   HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPut(url), entity);
   if (headers != null) request.setHeaders(headers);
   sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
 }
Пример #7
0
 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);
 }
Пример #8
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);
 }
 // 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;
 }
  private HttpEntityEnclosingRequestBase addEntityToRequestBase(
      HttpEntityEnclosingRequestBase requestBase, HttpEntity entity) {
    if (entity != null) {
      requestBase.setEntity(entity);
    }

    return requestBase;
  }
Пример #12
0
 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);
   }
 }
Пример #13
0
 @Override
 public boolean matches(HttpRequest request) {
   URI uri = URI.create(request.getRequestLine().getUri());
   if (method != null && !method.equals(request.getRequestLine().getMethod())) {
     return false;
   }
   if (hostname != null && !hostname.equals(uri.getHost())) {
     return false;
   }
   if (path != null && !path.equals(uri.getRawPath())) {
     return false;
   }
   if (noParams && !uri.getRawQuery().equals(null)) {
     return false;
   }
   if (params.size() > 0) {
     Map<String, String> requestParams = ParamsParser.parseParams(request);
     if (!requestParams.equals(params)) {
       return false;
     }
   }
   if (headers.size() > 0) {
     Map<String, String> actualRequestHeaders = new HashMap<>();
     for (Header header : request.getAllHeaders()) {
       actualRequestHeaders.put(header.getName(), header.getValue());
     }
     if (!headers.equals(actualRequestHeaders)) {
       return false;
     }
   }
   if (postBodyMatcher != null) {
     if (!(request instanceof HttpEntityEnclosingRequestBase)) {
       return false;
     }
     HttpEntityEnclosingRequestBase postOrPut = (HttpEntityEnclosingRequestBase) request;
     try {
       if (!postBodyMatcher.matches(postOrPut.getEntity())) {
         return false;
       }
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
   return true;
 }
  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);
  }
Пример #15
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);
   }
 }
Пример #16
0
  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);
  }
Пример #17
0
 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);
   }
 }
Пример #18
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;
  }
  /** {@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);
    }
  }
 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);
   }
 }
Пример #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);
    }
  }
  @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
 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);
   }
 }
Пример #24
0
  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
 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);
   }
 }
Пример #26
0
 /**
  * 传递参数的数据请求
  *
  * @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;
 }
Пример #27
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());
  }
Пример #28
0
  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);
    }
  }
Пример #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);
    }
  }