com.adobe.platform.operation.internal.http.MultiPartHttpResponse 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 static com.adobe.platform.operation.internal.http.DefaultRequestHeaders.DC_REQUEST_ID_HEADER_KEY;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.io.IOUtils;
import com.adobe.platform.operation.internal.util.JsonUtil;
public class MultiPartHttpResponse implements HttpResponse {
private static final String CONTENT_TYPE_MULTIPART_MIXED_STRING = "multipart/mixed";
private static final String MIME_TYPE_APPLICATION_JSON_STRING = "application/json";
private static final String MIME_TYPE_APPLICATION_OCTET_STREAM_STRING = "application/octet-stream";
private int statusCode;
private Map headers;
private T inlineJsonResponseDto;
private Class inlineJsonResponseType;
private InputStream inlineContentResponse;
public MultiPartHttpResponse(int statusCode, Map headers,
InputStream inputStream,
Class inlineJsonResponseType) throws IOException, MessagingException {
this.statusCode = statusCode;
this.headers = headers;
this.inlineJsonResponseType = inlineJsonResponseType;
parseInlineResponseData(inputStream);
}
private void parseInlineResponseData(InputStream inputStream) throws IOException, MessagingException {
ByteArrayDataSource datasource =
new ByteArrayDataSource(IOUtils.toByteArray(inputStream), CONTENT_TYPE_MULTIPART_MIXED_STRING);
MimeMultipart multipart = new MimeMultipart(datasource);
int count = multipart.getCount();
for (int i = 0; i < count; i++) {
MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(i);
if (bodyPart.isMimeType(MIME_TYPE_APPLICATION_JSON_STRING)) {
processJsonData(bodyPart.getInputStream());
} else if (bodyPart.isMimeType(MIME_TYPE_APPLICATION_OCTET_STREAM_STRING)) {
processBinaryData(bodyPart.getInputStream());
}
}
inputStream.close();
}
@Override
public Map getHeaders() {
return headers;
}
@Override
public int getStatusCode() {
return statusCode;
}
@Override
public String getRequestId() {
if (headers != null) {
return headers.get(DC_REQUEST_ID_HEADER_KEY);
}
return null;
}
@Override
public T getBody() {
return inlineJsonResponseDto;
}
@Override
public InputStream getResponseAsStream() {
return inlineContentResponse;
}
@Override
public void consume() throws IOException {
if (inlineContentResponse != null) {
inlineContentResponse.close();
}
}
private void processJsonData(InputStream inputStream) {
this.inlineJsonResponseDto = JsonUtil.deserializeJsonStream(inputStream, this.inlineJsonResponseType);
}
private void processBinaryData(InputStream inputStream) {
this.inlineContentResponse = inputStream;
}
}