Search in sources :

Example 1 with Cleanables

use of org.jboss.resteasy.plugins.server.Cleanables in project resteasy by resteasy.

the class SseEventOutputImpl method clearContextData.

public void clearContextData() {
    Map<Class<?>, Object> map = ResteasyContext.getContextDataMap(false);
    Cleanables cleanables = map != null ? (Cleanables) map.get(Cleanables.class) : null;
    if (cleanables != null) {
        for (Iterator<Cleanable> it = cleanables.getCleanables().iterator(); it.hasNext(); ) {
            try {
                it.next().clean();
            } catch (Exception e) {
            // Empty
            }
        }
        ResteasyContext.clearContextData();
    }
}
Also used : Cleanables(org.jboss.resteasy.plugins.server.Cleanables) Cleanable(org.jboss.resteasy.plugins.server.Cleanable) ProcessingException(jakarta.ws.rs.ProcessingException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) UncheckedIOException(java.io.UncheckedIOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with Cleanables

use of org.jboss.resteasy.plugins.server.Cleanables in project resteasy by resteasy.

the class DataSourceProvider method readDataSource.

/**
 * @param in input stream
 * @param mediaType media type
 * @return data source
 * @throws IOException if I/O error occurred
 */
public static DataSource readDataSource(final InputStream in, final MediaType mediaType) throws IOException {
    byte[] memoryBuffer = new byte[4096];
    int readCount = in.read(memoryBuffer, 0, memoryBuffer.length);
    File tempFile = null;
    if (readCount > 0) {
        byte[] buffer = new byte[4096];
        int count = in.read(buffer, 0, buffer.length);
        if (count > -1) {
            tempFile = File.createTempFile("resteasy-provider-datasource", null);
            FileOutputStream fos = new FileOutputStream(tempFile);
            Cleanables cleanables = ResteasyContext.getContextData(Cleanables.class);
            if (cleanables != null) {
                cleanables.addCleanable(new TempFileCleanable(tempFile));
            }
            fos.write(buffer, 0, count);
            try {
                ProviderHelper.writeTo(in, fos);
            } finally {
                fos.close();
            }
        }
    }
    if (readCount == -1)
        readCount = 0;
    return new SequencedDataSource(memoryBuffer, 0, readCount, tempFile, mediaType.toString());
}
Also used : Cleanables(org.jboss.resteasy.plugins.server.Cleanables) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with Cleanables

use of org.jboss.resteasy.plugins.server.Cleanables in project resteasy by resteasy.

the class FileProvider method readFrom.

public File readFrom(Class<File> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
    LogMessages.LOGGER.debugf("Provider : %s,  Method : readFrom", getClass().getName());
    File downloadedFile = null;
    if (_downloadDirectory != null) {
        try {
            downloadedFile = File.createTempFile(PREFIX, SUFFIX, new File(_downloadDirectory));
        } catch (final IOException ex) {
            // could make this configurable, so we fail on fault rather than
            // default.
            LogMessages.LOGGER.couldNotBindToDirectory(_downloadDirectory);
        }
    }
    if (downloadedFile == null)
        downloadedFile = File.createTempFile(PREFIX, SUFFIX);
    Cleanables cleanables = ResteasyContext.getContextData(Cleanables.class);
    if (cleanables != null) {
        cleanables.addCleanable(new FileHolder(downloadedFile));
    } else {
        LogMessages.LOGGER.temporaryFileCreated(downloadedFile.getPath());
    }
    if (NoContent.isContentLengthZero(httpHeaders))
        return downloadedFile;
    OutputStream output = new BufferedOutputStream(new FileOutputStream(downloadedFile));
    try {
        ProviderHelper.writeTo(entityStream, output);
    } finally {
        output.close();
    }
    return downloadedFile;
}
Also used : Cleanables(org.jboss.resteasy.plugins.server.Cleanables) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) AsyncOutputStream(org.jboss.resteasy.spi.AsyncOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with Cleanables

use of org.jboss.resteasy.plugins.server.Cleanables in project resteasy by resteasy.

the class SynchronousDispatcher method clearContextData.

public void clearContextData() {
    Map<Class<?>, Object> map = ResteasyContext.getContextDataMap(false);
    Cleanables cleanables = map != null ? (Cleanables) map.get(Cleanables.class) : null;
    if (cleanables != null) {
        for (Iterator<Cleanable> it = cleanables.getCleanables().iterator(); it.hasNext(); ) {
            try {
                it.next().clean();
            } catch (Exception e) {
            // Empty
            }
        }
        ResteasyContext.clearContextData();
    }
    // just in case there were internalDispatches that need to be cleaned up
    MessageBodyParameterInjector.clearBodies();
}
Also used : Cleanables(org.jboss.resteasy.plugins.server.Cleanables) Cleanable(org.jboss.resteasy.plugins.server.Cleanable) NotFoundException(jakarta.ws.rs.NotFoundException) UnhandledException(org.jboss.resteasy.spi.UnhandledException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) InternalServerErrorException(org.jboss.resteasy.spi.InternalServerErrorException)

Example 5 with Cleanables

use of org.jboss.resteasy.plugins.server.Cleanables in project resteasy by resteasy.

the class SynchronousDispatcher method pushContextObjects.

@SuppressWarnings("unchecked")
public void pushContextObjects(final HttpRequest request, final HttpResponse response) {
    @SuppressWarnings("rawtypes") Map contextDataMap = ResteasyContext.getContextDataMap();
    contextDataMap.put(HttpRequest.class, request);
    contextDataMap.put(HttpResponse.class, response);
    contextDataMap.put(HttpHeaders.class, request.getHttpHeaders());
    contextDataMap.put(UriInfo.class, request.getUri());
    contextDataMap.put(Request.class, new RequestImpl(request, response));
    contextDataMap.put(ResteasyAsynchronousContext.class, request.getAsyncContext());
    ResourceContext resourceContext = new ResourceContext() {

        @Override
        public <T> T getResource(Class<T> resourceClass) {
            return providerFactory.injectedInstance(resourceClass, request, response);
        }

        @Override
        public <T> T initResource(T resource) {
            providerFactory.injectProperties(resource, request, response);
            return resource;
        }
    };
    contextDataMap.put(ResourceContext.class, resourceContext);
    contextDataMap.putAll(defaultContextObjects);
    contextDataMap.put(Cleanables.class, new Cleanables());
    contextDataMap.put(PostResourceMethodInvokers.class, new PostResourceMethodInvokers());
}
Also used : ResourceContext(jakarta.ws.rs.container.ResourceContext) Cleanables(org.jboss.resteasy.plugins.server.Cleanables) HashMap(java.util.HashMap) Map(java.util.Map) RequestImpl(org.jboss.resteasy.specimpl.RequestImpl)

Aggregations

Cleanables (org.jboss.resteasy.plugins.server.Cleanables)5 IOException (java.io.IOException)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 CompletionException (java.util.concurrent.CompletionException)2 Cleanable (org.jboss.resteasy.plugins.server.Cleanable)2 NotFoundException (jakarta.ws.rs.NotFoundException)1 ProcessingException (jakarta.ws.rs.ProcessingException)1 ResourceContext (jakarta.ws.rs.container.ResourceContext)1 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStream (java.io.OutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 RequestImpl (org.jboss.resteasy.specimpl.RequestImpl)1 AsyncOutputStream (org.jboss.resteasy.spi.AsyncOutputStream)1 InternalServerErrorException (org.jboss.resteasy.spi.InternalServerErrorException)1 UnhandledException (org.jboss.resteasy.spi.UnhandledException)1