org.apache.http.entity.AbstractHttpEntity Java Examples

The following examples show how to use org.apache.http.entity.AbstractHttpEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlockingHttpClient.java    From bitso-java with MIT License 6 votes vote down vote up
private String sendPost(String url, AbstractHttpEntity body, HashMap<String, String> headers)
        throws ClientProtocolException, IOException {
    throttle();

    HttpPost postRequest = new HttpPost(url);
    if (headers != null) {
        for (Entry<String, String> e : headers.entrySet()) {
            postRequest.addHeader(e.getKey(), e.getValue());
        }
    }

    postRequest.setEntity(body);

    CloseableHttpResponse closeableHttpResponse = HttpClients.createDefault().execute(postRequest);
    String response = Helpers.convertInputStreamToString(closeableHttpResponse.getEntity().getContent());

    return response;
}
 
Example #2
Source File: ElasticSearchRestTargetRepository.java    From ache with Apache License 2.0 6 votes vote down vote up
@Override
public boolean insert(Page page) {
    TargetModelElasticSearch document = new TargetModelElasticSearch(page);
    String docId = encodeUrl(page.getURL().toString());
    // We use upsert to avoid overriding existing fields in previously indexed documents
    String endpoint = String.format("/%s/%s/%s/_update", indexName, typeName, docId);
    Map<String, ?> body = ImmutableMap.of(
        "doc", document,
        "doc_as_upsert", true
    );
    AbstractHttpEntity entity = createJsonEntity(serializeAsJson(body));
    try {
        Response response = client.performRequest("POST", endpoint, EMPTY_MAP, entity);
        return response.getStatusLine().getStatusCode() == 201;
    } catch (IOException e) {
        throw new RuntimeException("Failed to index page.", e);
    }
}
 
Example #3
Source File: RequestProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private void addContentTypeHeaderIfMissing(
		HttpEntityEnclosingRequest request) {
	if (request.getEntity().getContentType() == null) {
		((AbstractHttpEntity) request.getEntity())
				.setContentType(HTTP.OCTET_STREAM_TYPE);
	}
}
 
Example #4
Source File: HttpComponentsConnector.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
private HttpEntity getHttpEntity(final ClientRequest clientRequest) {
    final Object entity = clientRequest.getEntity();
    if(entity == null) {
        return null;
    }
    return new AbstractHttpEntity() {
        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public long getContentLength() {
            return -1;
        }

        @Override
        public InputStream getContent() throws IllegalStateException {
            return null;
        }

        @Override
        public void writeTo(final OutputStream outputStream) throws IOException {
            clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
                @Override
                public OutputStream getOutputStream(final int contentLength) {
                    return outputStream;
                }
            });
            clientRequest.writeEntity();
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
}
 
Example #5
Source File: HttpComponentsConnector.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
private HttpEntity getHttpEntity(final ClientRequest clientRequest) {
    final Object entity = clientRequest.getEntity();
    if(entity == null) {
        return null;
    }
    return new AbstractHttpEntity() {
        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public long getContentLength() {
            return -1;
        }

        @Override
        public InputStream getContent() throws IllegalStateException {
            return null;
        }

        @Override
        public void writeTo(final OutputStream outputStream) throws IOException {
            clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
                @Override
                public OutputStream getOutputStream(final int contentLength) {
                    return outputStream;
                }
            });
            clientRequest.writeEntity();
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
}
 
Example #6
Source File: S3WriteFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public HttpResponseOutputStream<StorageObject> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final S3Object object = this.getDetails(file, status);
    final DelayedHttpEntityCallable<StorageObject> command = new DelayedHttpEntityCallable<StorageObject>() {
        @Override
        public StorageObject call(final AbstractHttpEntity entity) throws BackgroundException {
            try {
                final RequestEntityRestStorageService client = session.getClient();
                client.putObjectWithRequestEntityImpl(
                    containerService.getContainer(file).getName(), object, entity, status.getParameters());
                if(log.isDebugEnabled()) {
                    log.debug(String.format("Saved object %s with checksum %s", file, object.getETag()));
                }
            }
            catch(ServiceException e) {
                throw new S3ExceptionMappingService().map("Upload {0} failed", e, file);
            }
            return object;
        }

        @Override
        public long getContentLength() {
            return status.getLength();
        }
    };
    return this.write(file, status, command);
}
 
Example #7
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
private AbstractHttpEntity getRequestEntity(InputStream input, ContentType contentType,
    long inputStreamLength) throws GalaxyFDSClientException {

  if (fdsConfig.isMd5CalculateEnabled()) {
    inputStreamLength = -1;
  }
  if (input instanceof ByteArrayInputStream) {
    try {
      if (inputStreamLength > Integer.MAX_VALUE ||
          // -1 means length unknown, use all data
          (inputStreamLength < 0 && inputStreamLength != -1)) {
        throw new GalaxyFDSClientException(
            "Invalid length [" + inputStreamLength + "] for byteArrayInputStream");
      }
      byte[] data = IOUtils.toByteArray(input);
      int len = inputStreamLength >= 0 ? (int) inputStreamLength : data.length;
      return new ByteArrayEntity(data, 0, len, contentType);
    } catch (IOException e) {
      throw new GalaxyFDSClientException("Failed to get content of input stream", e);
    }
  } else {
    BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
    InputStreamEntity entity = new InputStreamEntity(bufferedInputStream, inputStreamLength,
        contentType);
    return entity;
  }
}
 
Example #8
Source File: AndroidGDataClient.java    From mytracks with Apache License 2.0 5 votes vote down vote up
private HttpEntity createEntityForEntry(GDataSerializer entry, int format)
    throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try {
    entry.serialize(baos, format);
  } catch (IOException ioe) {
    Log.e(TAG, "Unable to serialize entry.", ioe);
    throw ioe;
  } catch (ParseException pe) {
    Log.e(TAG, "Unable to serialize entry.", pe);
    throw new IOException("Unable to serialize entry: " + pe.getMessage());
  }

  byte[] entryBytes = baos.toByteArray();

  if (entryBytes != null && Log.isLoggable(TAG, Log.DEBUG)) {
    try {
      Log.d(TAG, "Serialized entry: " + new String(entryBytes, "UTF-8"));
    } catch (UnsupportedEncodingException uee) {
      // should not happen
      throw new IllegalStateException("UTF-8 should be supported!", uee);
    }
  }

  AbstractHttpEntity entity = new ByteArrayEntity(entryBytes);
  entity.setContentType(entry.getContentType());
  return entity;
}
 
Example #9
Source File: HttpConnHelper.java    From QiQuYingServer with Apache License 2.0 5 votes vote down vote up
/**
 * 设置POST请求参数
 * 
 * @param request
 * @param postParams
 * @throws UnsupportedEncodingException
 */
private static void setPostParams(HttpRequestBase request,
		Map<String, String> postParams) throws UnsupportedEncodingException {
	List<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>();
	for (Map.Entry<String, String> entry : postParams.entrySet()) {
		postData.add(new BasicNameValuePair(entry.getKey(), entry
				.getValue()));
	}
	AbstractHttpEntity entity = new UrlEncodedFormEntity(postData,
			Consts.UTF_8);
	((HttpPost) request).setEntity(entity);
}
 
Example #10
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public UploadPartResult uploadPart(String bucketName, String objectName, String uploadId,
    int partNumber, InputStream in, FDSObjectMetadata metadata) throws GalaxyFDSClientException {
  URI uri = formatUri(fdsConfig.getBaseUri(), bucketName + "/" + objectName, null);
  ContentType contentType = ContentType.APPLICATION_OCTET_STREAM;
  HashMap<String, String> params = new HashMap<String, String>();
  params.put("uploadId", uploadId);
  params.put("partNumber", String.valueOf(partNumber));
  if (fdsConfig.isMd5CalculateEnabled()) {
    if (metadata == null) {
      metadata = new FDSObjectMetadata();
    }
    metadata.addHeader(XiaomiHeader.MD5_ATTACHED_STREAM.getName(), "1");
    try {
      in = new FDSMd5InputStream(in);
    } catch (NoSuchAlgorithmException e) {
      throw new GalaxyFDSClientException("Cannot init md5", e);
    }
  }
  AbstractHttpEntity requestEntity = getRequestEntity(in, contentType);
  HttpUriRequest httpRequest = fdsHttpClient.prepareRequestMethod(uri, HttpMethod.PUT,
    contentType, metadata, params, null, requestEntity);

  HttpResponse response = fdsHttpClient.executeHttpRequest(httpRequest, Action.UploadPart);

  UploadPartResult uploadPartResult = (UploadPartResult) fdsHttpClient.processResponse(response,
    UploadPartResult.class, "upload part of object [" + objectName + "] to bucket [" + bucketName
      + "]" + "; part number [" + partNumber + "], upload id [" + uploadId + "]");

  return uploadPartResult;
}
 
Example #11
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
private PutObjectResult uploadWithInputStream(String bucketName, String objectName,
    InputStream input, long contentLength, FDSObjectMetadata metadata, String storageClass)
        throws GalaxyFDSClientException {
  if (metadata == null) {
    metadata = new FDSObjectMetadata();
  }
  checkAndSetStorageClass(metadata, storageClass);
  ContentType contentType = ContentType.APPLICATION_OCTET_STREAM;
  if (metadata != null && metadata.getContentType() != null) {
    contentType = ContentType.create(metadata.getContentType());
  }
  if (fdsConfig.isMd5CalculateEnabled()) {
    if (metadata == null) {
      metadata = new FDSObjectMetadata();
    }
    metadata.addHeader(XiaomiHeader.MD5_ATTACHED_STREAM.getName(), "1");
    try {
      input = new FDSMd5InputStream(input);
    } catch (NoSuchAlgorithmException e) {
      throw new GalaxyFDSClientException("Cannot init md5", e);
    }
  }
  URI uri = formatUri(fdsConfig.getUploadBaseUri(),
    bucketName + "/" + (objectName == null ? "" : objectName), (SubResource[]) null);
  AbstractHttpEntity requestEntity = getRequestEntity(input, contentType, contentLength);

  HttpMethod m = objectName == null ? HttpMethod.POST : HttpMethod.PUT;
  HttpUriRequest httpRequest = fdsHttpClient.prepareRequestMethod(uri, m, contentType, metadata,
    null, null, requestEntity);

  HttpResponse response = fdsHttpClient.executeHttpRequest(httpRequest,
    objectName == null ? Action.PostObject : Action.PutObject);

  return (PutObjectResult) fdsHttpClient.processResponse(response, PutObjectResult.class,
    m.name() + " object [" + objectName + "] to bucket [" + bucketName + "]");
}
 
Example #12
Source File: ExchangeDavRequest.java    From davmail with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create PROPPATCH method.
 *
 * @param path path
 */
public ExchangeDavRequest(String path) {
    super(path);
    AbstractHttpEntity httpEntity = new AbstractHttpEntity() {
        byte[] content;

        @Override
        public boolean isRepeatable() {
            return true;
        }

        @Override
        public long getContentLength() {
            if (content == null) {
                content = generateRequestContent();
            }
            return content.length;
        }

        @Override
        public InputStream getContent() throws UnsupportedOperationException {
            if (content == null) {
                content = generateRequestContent();
            }
            return new ByteArrayInputStream(content);
        }

        @Override
        public void writeTo(OutputStream outputStream) throws IOException {
            if (content == null) {
                content = generateRequestContent();
            }
            outputStream.write(content);
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };

    httpEntity.setContentType(XML_CONTENT_TYPE);
    setEntity(httpEntity);
}
 
Example #13
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 4 votes vote down vote up
private AbstractHttpEntity getRequestEntity(InputStream input, ContentType contentType)
    throws GalaxyFDSClientException {
  return getRequestEntity(input, contentType, -1/* unknown length */);
}
 
Example #14
Source File: DumpDataFromElasticsearch.java    From ache with Apache License 2.0 4 votes vote down vote up
private AbstractHttpEntity createJsonEntity(String mapping) {
    return new NStringEntity(mapping, ContentType.APPLICATION_JSON);
}
 
Example #15
Source File: DumpDataFromElasticsearch.java    From ache with Apache License 2.0 4 votes vote down vote up
@Override
public void execute() throws Exception {

    FileWriter fw = new FileWriter(new File(this.outputFile));

    ElasticSearchConfig config = new ElasticSearchConfig(Arrays.asList(hostAddress));

    RestClient client = ElasticSearchClientFactory.createClient(config);

    Map<String, ?> esQuery;
    if (this.query == null || this.query.isEmpty()) {
        esQuery = ImmutableMap.of(
                "match_all", EMPTY_MAP
        );
    } else {
        esQuery = ImmutableMap.of(
                "query_string", ImmutableMap.of(
                        "default_field", "_all",
                        "query", this.query
                )
        );
    }

    Map<String, ?> body = ImmutableMap.of(
            "query", esQuery
    );

    AbstractHttpEntity entity = createJsonEntity(serializeAsJson(body));

    String searchEndpoint = String
            .format("/%s/%s/_search?scroll=%dm", indexName, typeName, esTimeout);
    Response scrollResponse = client.performRequest("POST", searchEndpoint, EMPTY_MAP, entity);

    JsonNode jsonResponse = mapper.readTree(scrollResponse.getEntity().getContent());
    List<String> hits = getHits(jsonResponse);
    String scrollId = getScrollId(jsonResponse);
    int i = 0;
    while (hits.size() > 0) {
        System.out.printf("Processing scroll: %d hits: %d\n", ++i, hits.size());
        for (String hit : hits) {
            fw.write(hit);
            fw.write('\n');
        }
        fw.flush();

        // Execute next scroll search request
        Map<String, ?> scrollBody = ImmutableMap.of(
                "scroll", (this.esTimeout + "m"),
                "scroll_id", scrollId
        );
        entity = createJsonEntity(serializeAsJson(scrollBody));

        String scrollEndpoint = "/_search/scroll";
        scrollResponse = client.performRequest("POST", scrollEndpoint, EMPTY_MAP, entity);

        jsonResponse = mapper.readTree(scrollResponse.getEntity().getContent());
        hits = getHits(jsonResponse);
        scrollId = getScrollId(jsonResponse);
    }
    client.close();
    fw.close();
    System.out.println("Done.");
}
 
Example #16
Source File: ElasticSearchRestTargetRepository.java    From ache with Apache License 2.0 4 votes vote down vote up
private AbstractHttpEntity createJsonEntity(String mapping) {
    return new NStringEntity(mapping, ContentType.APPLICATION_JSON);
}
 
Example #17
Source File: LZ4EntityWrapper.java    From clickhouse-jdbc with Apache License 2.0 4 votes vote down vote up
public LZ4EntityWrapper(AbstractHttpEntity content, int maxCompressBlockSize) {
    this.delegate = content;
    this.maxCompressBlockSize = maxCompressBlockSize;
}
 
Example #18
Source File: RestRequest.java    From davmail with GNU General Public License v2.0 4 votes vote down vote up
public RestRequest(String uri) {
    super(uri);

    AbstractHttpEntity httpEntity = new AbstractHttpEntity() {
        byte[] content;

        @Override
        public boolean isRepeatable() {
            return true;
        }

        @Override
        public long getContentLength() {
            if (content == null) {
                content = getJsonContent();
            }
            return content.length;
        }

        @Override
        public InputStream getContent() throws UnsupportedOperationException {
            if (content == null) {
                content = getJsonContent();
            }
            return new ByteArrayInputStream(content);
        }

        @Override
        public void writeTo(OutputStream outputStream) throws IOException {
            if (content == null) {
                content = getJsonContent();
            }
            outputStream.write(content);
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
    httpEntity.setContentType(JSON_CONTENT_TYPE);
    setEntity(httpEntity);
}
 
Example #19
Source File: HttpConnHelper.java    From QiQuYingServer with Apache License 2.0 2 votes vote down vote up
/**
 * 设置POST请求参数
 * 
 * @param request
 * @param postParams
 * @throws UnsupportedEncodingException
 */
private static void setPostParams(HttpRequestBase request, String postParams)
		throws UnsupportedEncodingException {
	AbstractHttpEntity entity = new StringEntity(postParams, Consts.UTF_8);
	((HttpPost) request).setEntity(entity);
}
 
Example #20
Source File: DelayedHttpEntityCallable.java    From cyberduck with GNU General Public License v3.0 votes vote down vote up
T call(AbstractHttpEntity entity) throws BackgroundException;