Search in sources :

Example 1 with RuntimeIoException

use of com.opentext.ia.sdk.support.io.RuntimeIoException in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ResponseBodyFactory method create.

@Override
public T create(Response response, Runnable closeResponse) {
    Runnable closeResult = closeResponse;
    AtomicReference<InputStream> resultStream = new AtomicReference<>();
    boolean resourceOwnershipTransferred = false;
    try {
        if (response.getBody() == null) {
            return null;
        }
        resultStream.set(response.getBody());
        closeResult = () -> {
            IOUtils.closeQuietly(resultStream.get());
            closeResponse.run();
        };
        T result = doCreate(response, resultStream.get(), closeResult);
        resourceOwnershipTransferred = true;
        return result;
    } catch (final IOException e) {
        throw new RuntimeIoException(e);
    } finally {
        if (!resourceOwnershipTransferred) {
            closeResult.run();
        }
    }
}
Also used : InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException)

Example 2 with RuntimeIoException

use of com.opentext.ia.sdk.support.io.RuntimeIoException in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ApacheHttpClient method execute.

@SuppressWarnings("PMD.AvoidRethrowingException")
protected <T> T execute(HttpRequestBase request, ResponseFactory<T> factory) throws IOException {
    CloseableHttpResponse httpResponse;
    try {
        httpResponse = client.execute(request);
    } catch (HttpResponseException e) {
        throw new HttpException(e.getStatusCode(), e);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw new HttpException(500, e);
    }
    Runnable closeResponse = () -> {
        IOUtils.closeQuietly(httpResponse);
        request.releaseConnection();
    };
    boolean shouldCloseResponse = true;
    try {
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (!isOk(statusCode)) {
            HttpEntity entity = httpResponse.getEntity();
            String body = toString(entity);
            String method = request.getMethod();
            URI uri = request.getURI();
            String reasonPhrase = statusLine.getReasonPhrase();
            throw new HttpException(statusCode, String.format("%n%s %s%n==> %d %s%n%s", method, uri, statusCode, reasonPhrase, body));
        }
        T result = factory.create(new ApacheResponse(httpResponse), closeResponse);
        shouldCloseResponse = false;
        return result;
    } catch (IOException e) {
        throw new RuntimeIoException(e);
    } finally {
        if (shouldCloseResponse) {
            closeResponse.run();
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) URI(java.net.URI) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) StatusLine(org.apache.http.StatusLine)

Example 3 with RuntimeIoException

use of com.opentext.ia.sdk.support.io.RuntimeIoException in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ArchiveClients method appResourceCache.

private static ApplicationIngestionResourcesCache appResourceCache(RestClient restClient, ServerConfiguration configuration) {
    try {
        ApplicationIngestionResourcesCache resourceCache = new ApplicationIngestionResourcesCache(configuration.getApplicationName());
        Services services = restClient.get(configuration.getBillboardUri(), Services.class);
        Tenant tenant = getTenant(restClient, services);
        Application application = getApplication(restClient, tenant, configuration.getApplicationName());
        cacheResourceUris(resourceCache, restClient, application);
        return resourceCache;
    } catch (IOException e) {
        throw new RuntimeIoException(e);
    }
}
Also used : ApplicationIngestionResourcesCache(com.opentext.ia.sdk.client.impl.ApplicationIngestionResourcesCache) IOException(java.io.IOException) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException)

Example 4 with RuntimeIoException

use of com.opentext.ia.sdk.support.io.RuntimeIoException in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class WhenGeneratingFiles method shouldProvideStreamOfGeneratedFileToWrappedGenerator.

@Test
public void shouldProvideStreamOfGeneratedFileToWrappedGenerator() throws IOException {
    byte[] content = randomBytes();
    Assembler<String> wrapped = mock(Assembler.class);
    doAnswer(invocation -> {
        try {
            try (OutputStream stream = invocation.getArgumentAt(0, DataBuffer.class).openForWriting()) {
                stream.write(content);
            }
        } catch (IOException e) {
            throw new RuntimeIoException(e);
        }
        return null;
    }).when(wrapped).start(any(DataBuffer.class));
    FileGenerator<String> generator = new FileGenerator<>(wrapped);
    File generated = generator.generate(Collections.singletonList(randomString())).getFile();
    try (InputStream stream = new FileInputStream(generated)) {
        assertArrayEquals("Content", content, IOUtils.toByteArray(stream));
    }
}
Also used : RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) DataBuffer(com.opentext.ia.sdk.support.io.DataBuffer) Test(org.junit.Test)

Example 5 with RuntimeIoException

use of com.opentext.ia.sdk.support.io.RuntimeIoException in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class PropertiesBasedConfigurer method configure.

@Override
public void configure() {
    try {
        initRestClient();
        applyConfiguration();
    } catch (IOException e) {
        throw new RuntimeIoException(e);
    }
}
Also used : IOException(java.io.IOException) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException)

Aggregations

RuntimeIoException (com.opentext.ia.sdk.support.io.RuntimeIoException)6 IOException (java.io.IOException)5 ApplicationIngestionResourcesCache (com.opentext.ia.sdk.client.impl.ApplicationIngestionResourcesCache)1 Header (com.opentext.ia.sdk.support.http.Header)1 DataBuffer (com.opentext.ia.sdk.support.io.DataBuffer)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 HttpEntity (org.apache.http.HttpEntity)1 StatusLine (org.apache.http.StatusLine)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 Test (org.junit.Test)1