com.adobe.pdfservices.operation.internal.http.ApacheHttpUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pdfservices-sdk Show documentation
Show all versions of pdfservices-sdk Show documentation
Adobe PDF Services SDK allows you to access RESTful APIs to create, convert, and manipulate PDFs within your applications.
Older versions can be found under groupId: com.adobe.documentservices, artifactId: pdftools-sdk
/*
* Copyright 2019 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
package com.adobe.pdfservices.operation.internal.http;
import com.adobe.pdfservices.operation.config.proxy.ProxyServerConfig;
import com.adobe.pdfservices.operation.internal.constants.RequestKey;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
public class ApacheHttpUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ApacheHttpUtil.class);
public static HttpUriRequest getApacheRequest(HttpRequest httpRequest) {
HttpUriRequest apacheHttpRequest = null;
switch (httpRequest.getHttpMethod()) {
case GET:
LOGGER.debug("Creating apache http get request for {}", httpRequest.getRequestKey());
apacheHttpRequest = createGetRequest(httpRequest);
break;
case POST:
LOGGER.debug("Creating apache http post request for {}", httpRequest.getRequestKey());
apacheHttpRequest = createPostRequest(httpRequest);
break;
case PUT:
LOGGER.debug("Creating apache http put request for {}", httpRequest.getRequestKey());
apacheHttpRequest = createPutRequest(httpRequest);
break;
case DELETE:
LOGGER.debug("Creating apache http delete request for {}", httpRequest.getRequestKey());
apacheHttpRequest = createDeleteRequest(httpRequest);
break;
}
return apacheHttpRequest;
}
private static HttpGet createGetRequest(HttpRequest httpRequest) {
HttpGet apacheHttpGet = new HttpGet(httpRequest.getFinalUri());
setRequestConfig(httpRequest, apacheHttpGet);
httpRequest.getHeaders().forEach(apacheHttpGet::setHeader);
if (httpRequest.getRequestKey().equals(RequestKey.DOWNLOAD)) {
apacheHttpGet.removeHeaders(HttpHeaders.AUTHORIZATION);
apacheHttpGet.removeHeaders(DefaultRequestHeaders.X_API_KEY_HEADER_NAME);
}
return apacheHttpGet;
}
private static HttpPut createPutRequest(HttpRequest httpRequest) {
HttpPut apacheHttpPut = new HttpPut(httpRequest.getFinalUri());
setRequestConfig(httpRequest, apacheHttpPut);
apacheHttpPut.setEntity(httpRequest.getInputStreamEntity());
httpRequest.getHeaders().forEach((key, value) -> {
if (key.equals(HttpHeaders.CONTENT_TYPE) || key.equals(HttpHeaders.ACCEPT)) {
apacheHttpPut.setHeader(key, value);
}
});
return apacheHttpPut;
}
private static void setRequestConfig(HttpRequest httpRequest, HttpRequestBase httpRequestBase) {
HttpRequestConfig requestConfig = httpRequest.getConfig();
if (requestConfig != null) {
RequestConfig.Builder apacheRequestConfigBuilder = RequestConfig.custom()
.setConnectTimeout(requestConfig.getConnectTimeout())
.setSocketTimeout(requestConfig.getSocketTimeout());
ProxyServerConfig proxyServerConfig = requestConfig.getProxyServerConfig();
if (proxyServerConfig != null) {
String scheme = proxyServerConfig.getProxyScheme() == null ? null :
String.valueOf(proxyServerConfig.getProxyScheme());
HttpHost proxy = new HttpHost(proxyServerConfig.getHost(), proxyServerConfig.getPort(), scheme);
apacheRequestConfigBuilder.setProxy(proxy);
}
httpRequestBase.setConfig(apacheRequestConfigBuilder.build());
}
}
private static HttpPost createPostRequest(HttpRequest httpRequest) {
HttpPost apacheHttpPost = new HttpPost(httpRequest.getFinalUri());
if (httpRequest.getEntity() != null) {
apacheHttpPost.setEntity(httpRequest.getEntity());
}
setRequestConfig(httpRequest, apacheHttpPost);
httpRequest.getHeaders().forEach(apacheHttpPost::setHeader);
return apacheHttpPost;
}
private static HttpDelete createDeleteRequest(HttpRequest httpRequest) {
HttpDelete apacheHttpDelete = new HttpDelete(httpRequest.getFinalUri());
setRequestConfig(httpRequest, apacheHttpDelete);
httpRequest.getHeaders().forEach(apacheHttpDelete::setHeader);
return apacheHttpDelete;
}
static Map getHeaders(Header[] headers) {
return Arrays.stream(headers)
.collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue, (key1, key2) -> key1));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy