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();
}
}
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());
}
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;
}
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();
}
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());
}
Aggregations