Java Code Examples for org.apache.http.client.methods.HttpPut#setConfig()

The following examples show how to use org.apache.http.client.methods.HttpPut#setConfig() . 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: HttpClientUtils.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public static int putData(final HttpClient httpClient,
    final RequestConfig requestConfig,
    final String host,
    final int port,
    final String path,
    final JSONObject entity) throws IOException, URISyntaxException {
  URI uri = new URIBuilder()
      .setScheme("http")
      .setHost(host)
      .setPort(port)
      .setPath(path)
      .build();

  HttpPut httpPut = new HttpPut(uri);
  httpPut.setConfig(requestConfig);

  StringEntity params =new StringEntity(entity.toJSONString());
  httpPut.setEntity(params);

  return httpClient.execute(httpPut, HttpClientUtils.createResponseCodeExtractor());
}
 
Example 2
Source File: SwiftAPIDirect.java    From stocator with Apache License 2.0 6 votes vote down vote up
/**
 * @param path path to object
 * @param inputStream input stream
 * @param account JOSS Account object
 * @param metadata custom metadata
 * @param size the object size
 * @param type the content type
 * @return HTTP response code
 * @throws IOException if error
 */
private static int httpPUT(String path,
    InputStream inputStream, JossAccount account, SwiftConnectionManager scm,
    Map<String, String> metadata, long size, String type)
        throws IOException {
  LOG.debug("HTTP PUT {} request on {}", path);
  final HttpPut httpPut = new HttpPut(path);
  httpPut.addHeader("X-Auth-Token", account.getAuthToken());
  httpPut.addHeader("Content-Type", type);
  httpPut.addHeader(Constants.USER_AGENT_HTTP_HEADER, Constants.STOCATOR_USER_AGENT);
  if (metadata != null && !metadata.isEmpty()) {
    for (Map.Entry<String, String> entry : metadata.entrySet()) {
      httpPut.addHeader("X-Object-Meta-" + entry.getKey(), entry.getValue());
    }
  }
  final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
  httpPut.setConfig(config);
  final InputStreamEntity entity = new InputStreamEntity(inputStream, size);
  httpPut.setEntity(entity);
  final CloseableHttpClient httpclient = scm.createHttpConnection();
  final CloseableHttpResponse response = httpclient.execute(httpPut);
  int responseCode = response.getStatusLine().getStatusCode();
  LOG.debug("HTTP PUT {} response. Status code {}", path, responseCode);
  response.close();
  return responseCode;
}
 
Example 3
Source File: HttpClientHandler.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
@Override
public void upload(final File src, final URL dest, final CopyProgressListener listener, final TimeoutConstraint timeoutConstraint) throws IOException {
    final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
    final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout)
            .setConnectTimeout(connectionTimeout)
            .setAuthenticationEnabled(hasCredentialsConfigured(dest))
            .setTargetPreferredAuthSchemes(getAuthSchemePreferredOrder())
            .setProxyPreferredAuthSchemes(getAuthSchemePreferredOrder())
            .setExpectContinueEnabled(true)
            .build();
    final HttpPut put = new HttpPut(normalizeToString(dest));
    put.setConfig(requestConfig);
    put.setEntity(new FileEntity(src));
    try (final CloseableHttpResponse response = this.httpClient.execute(put)) {
        validatePutStatusCode(dest, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
    }
}
 
Example 4
Source File: LogSender.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
public boolean send(File compressedLog) throws IOException {
    
    boolean success = true;
    
    HttpClient client = HttpClientBuilder.create().build();
    HttpPut request = new HttpPut(serverURL+"?type=compress");
    request.addHeader("computer", config.getDeviceName());
    
    // Set request parameters
    RequestConfig.Builder reqConf = RequestConfig.custom();
    reqConf.setSocketTimeout(2000);
    reqConf.setConnectTimeout(1000);
    request.setConfig(reqConf.build());

    setRequest(request, compressedLog);

    HttpResponse response = client.execute(request);

    int responseCode = response.getStatusLine().getStatusCode();
    if (responseCode != 201) {
        success = false;
    }
    
    return success;
}
 
Example 5
Source File: Client.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
private PublishResponse publish(URI uri, StringEntity entity, String owner) throws IOException, URISyntaxException {
  HttpPut put = new HttpPut(uri);
  put.setConfig(DEFAULT_REQUEST_CONFIG);
  put.setEntity(entity);
  put.setHeader("Content-Type", "application/json; charset=UTF-8");
  put.setHeader("User-Agent", HttpConstants.getUserAgent());

  PublishResponse response = execute(put, PublishResponse.class);
  if (response.getError() == null && owner != null) {
    changeOwner(response.getId(), owner);
  }

  return response;
}
 
Example 6
Source File: Client.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
private PublishResponse changeOwner(String id, String owner) throws IOException, URISyntaxException {
  URI uri = createChangeOwnerUri(id, owner);
  HttpPut put = new HttpPut(uri);
  put.setConfig(DEFAULT_REQUEST_CONFIG);
  put.setHeader("Content-Type", "application/json; charset=UTF-8");
  put.setHeader("User-Agent", HttpConstants.getUserAgent());

  return execute(put, PublishResponse.class);
}
 
Example 7
Source File: RunController.java    From wings with Apache License 2.0 5 votes vote down vote up
/**
 * Upload triples to a rdf store
 * @param tstoreurl: triple store url. e.g., http://ontosoft.isi.edu:3030/provenance/data
 * @param graphurl: graph url.
 * @param filepath
 */
private void publishFile(String tstoreurl, String graphurl, String filepath) {
  System.out.println("Publishing the filepath " + filepath + " on graph " + graphurl);
  try {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPut putRequest = new HttpPut(tstoreurl + "?graph=" + graphurl);

    //Todo: move it to configuration
    int timeoutSeconds = 10;
    int CONNECTION_TIMEOUT_MS = timeoutSeconds * 1000;
    RequestConfig requestConfig = RequestConfig.custom()
        .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
        .setConnectTimeout(CONNECTION_TIMEOUT_MS)
        .setSocketTimeout(CONNECTION_TIMEOUT_MS)
        .build();
    putRequest.setConfig(requestConfig);

    File file = new File(filepath);
    String content = FileUtils.readFileToString(file);
    if (content != null) {
      StringEntity input = new StringEntity(content);
      input.setContentType("text/turtle");
      putRequest.setEntity(input);
      HttpResponse response = httpClient.execute(putRequest);
      int statusCode = response.getStatusLine().getStatusCode();
      if (statusCode > 299) {
        System.err.println("Unable to upload the domain " + statusCode);
        System.err.println(response.getStatusLine().getReasonPhrase());
      } else {
        System.err.println("Success uploading the domain " + statusCode);
        System.err.println(response.getStatusLine().getReasonPhrase());
      }
    }
    else {
      System.err.println("File content is null " + filepath);
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
Example 8
Source File: SiteToSiteRestApiClient.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private HttpPut createPut(final String path) {
    final URI url = getUri(path);
    final HttpPut put = new HttpPut(url);
    put.setConfig(getRequestConfig());
    return put;
}
 
Example 9
Source File: SiteToSiteRestApiClient.java    From nifi with Apache License 2.0 4 votes vote down vote up
private HttpPut createPut(final String path) {
    final URI url = getUri(path);
    final HttpPut put = new HttpPut(url);
    put.setConfig(getRequestConfig());
    return put;
}