com.cloudconvert.extractor.ResultExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudconvert-java Show documentation
Show all versions of cloudconvert-java Show documentation
CloudConvert is an online file converter API - more than 200 different audio, video, document, ebook, archive, image, spreadsheet and presentation formats supported.
package com.cloudconvert.extractor;
import com.cloudconvert.client.mapper.ObjectMapperProvider;
import com.cloudconvert.dto.result.Result;
import com.cloudconvert.processor.response.DefaultResponseProcessor;
import com.cloudconvert.processor.response.ResponseProcessor;
import com.cloudconvert.processor.response.successful.ContentResponseProcessor;
import com.cloudconvert.processor.response.successful.InputStreamResponseProcessor;
import com.cloudconvert.processor.response.successful.NoContentResponseProcessor;
import com.cloudconvert.resource.AbstractResource;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.io.EmptyInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Optional;
public class ResultExtractor {
private final ResponseProcessor defaultResponseProcessor;
private final Map, ResponseProcessor> responseProcessors;
public ResultExtractor(final ObjectMapperProvider objectMapperProvider) {
final ContentResponseProcessor contentResponseProcessor = new ContentResponseProcessor(objectMapperProvider);
final NoContentResponseProcessor noContentResponseProcessor = new NoContentResponseProcessor();
final InputStreamResponseProcessor inputStreamResponseProcessor = new InputStreamResponseProcessor();
this.defaultResponseProcessor = new DefaultResponseProcessor();
this.responseProcessors = ImmutableMap., ResponseProcessor>builder()
.put(AbstractResource.VOID_TYPE_REFERENCE, noContentResponseProcessor)
.put(AbstractResource.INPUT_STREAM_TYPE_REFERENCE, inputStreamResponseProcessor)
.put(AbstractResource.MAP_STRING_TO_OBJECT_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.TASK_RESPONSE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.JOB_RESPONSE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.USER_RESPONSE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.WEBHOOKS_RESPONSE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.OPERATION_RESPONSE_PAGEABLE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.TASK_RESPONSE_PAGEABLE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.JOB_RESPONSE_PAGEABLE_TYPE_REFERENCE, contentResponseProcessor)
.put(AbstractResource.WEBHOOKS_RESPONSE_PAGEABLE_TYPE_REFERENCE, contentResponseProcessor).build();
}
public Result extract(
final HttpResponse httpResponse, final TypeReference typeReference
) throws IOException {
final int status = httpResponse.getStatusLine().getStatusCode();
final Header[] headers = httpResponse.getAllHeaders();
final HttpEntity httpEntity = Optional.ofNullable(httpResponse.getEntity()).orElse(new InputStreamEntity(EmptyInputStream.INSTANCE));
try (final InputStream inputStream = httpEntity.getContent()) {
if (status >= 200 && status <= 299) {
return responseProcessors.getOrDefault(typeReference, defaultResponseProcessor).process(status, headers, inputStream, typeReference);
}
return defaultResponseProcessor.process(status, headers, inputStream, typeReference);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy