下面列出了org.apache.http.client.methods.HttpPut#setConfig() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
/**
* @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;
}
@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());
}
}
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;
}
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;
}
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);
}
/**
* 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();
}
}
private HttpPut createPut(final String path) {
final URI url = getUri(path);
final HttpPut put = new HttpPut(url);
put.setConfig(getRequestConfig());
return put;
}
private HttpPut createPut(final String path) {
final URI url = getUri(path);
final HttpPut put = new HttpPut(url);
put.setConfig(getRequestConfig());
return put;
}