com.adobe.platform.operation.internal.http.ApacheHttpUtil Maven / Gradle / Ivy
/*
* 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.platform.operation.internal.http;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.platform.operation.exception.SdkException;
import com.adobe.platform.operation.internal.util.StringUtil;
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());
RequestType requestType = httpRequest.getRequestType();
if (requestType.equals(RequestType.REGULAR)) {
apacheHttpRequest = createPostRequest(httpRequest);
} else {
apacheHttpRequest = createMultiPartRequest((MultiPartRequest) httpRequest);
}
break;
case PUT:
break;
}
return apacheHttpRequest;
}
private static HttpGet createGetRequest(HttpRequest httpRequest) {
HttpGet apacheHttpGet = new HttpGet(httpRequest.getFinalUri());
setRequestConfig(httpRequest, apacheHttpGet);
httpRequest.getHeaders().forEach(apacheHttpGet::setHeader);
return apacheHttpGet;
}
private static void setRequestConfig(HttpRequest httpRequest, HttpRequestBase httpRequestBase) {
HttpRequestConfig requestConfig = httpRequest.getConfig();
if (requestConfig != null) {
RequestConfig apacheRequestConfig = RequestConfig.custom()
.setConnectTimeout(requestConfig.getConnectTimeout())
.setSocketTimeout(requestConfig.getSocketTimeout())
.build();
httpRequestBase.setConfig(apacheRequestConfig);
}
}
private static HttpPost createPostRequest(HttpRequest httpRequest) {
HttpPost apacheHttpPost = new HttpPost(httpRequest.getFinalUri());
if (StringUtil.isNotBlank(httpRequest.getBody())) {
try {
apacheHttpPost.setEntity(new StringEntity(httpRequest.getBody()));
} catch (UnsupportedEncodingException e) {
//This exception won't occur, since acp client has no role in constructing api requests
LOGGER.error("Unsupported encoding in body for {} request", httpRequest.getRequestKey());
throw new SdkException(String.format("Unsupported encoding in body for request%s", httpRequest.getRequestKey()), e);
}
}
setRequestConfig(httpRequest, apacheHttpPost);
httpRequest.getHeaders().forEach(apacheHttpPost::setHeader);
return apacheHttpPost;
}
private static HttpPost createMultiPartRequest(MultiPartRequest multiPartRequest) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
HttpPost apacheHttpMultipartRequest = new HttpPost(multiPartRequest.getFinalUri());
List byteArrayParts = multiPartRequest.getByteArrayParts();
StringBodyPart bodyPart = multiPartRequest.getStringBodyPart();
if (bodyPart != null) {
builder.addPart(bodyPart.getName(), new StringBody(bodyPart.getBody(),
ContentType.create(bodyPart.getContentType(), StandardCharsets.UTF_8)));
}
if(byteArrayParts != null) {
for (ByteArrayPart byteArrayPart : byteArrayParts) {
ByteArrayBody byteArrayBody = new ByteArrayBody(byteArrayPart.getBody(), byteArrayPart.getFileName());
builder.addPart(byteArrayPart.getName(), byteArrayBody);
}
}
HttpEntity build = builder.build();
apacheHttpMultipartRequest.setEntity(build);
setRequestConfig(multiPartRequest, apacheHttpMultipartRequest);
multiPartRequest.getHeaders().forEach(apacheHttpMultipartRequest::setHeader);
return apacheHttpMultipartRequest;
}
static Map getHeaders(Header[] headers) {
return Arrays.stream(headers)
.collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue, (key1, key2) -> key1));
}
}