use of io.milton.common.BufferingOutputStream in project lobcder by skoulouzis.
the class CompressingResponseHandler method respondContent.
@Override
public void respondContent(Resource resource, Response response, Request request, Map<String, String> params) throws NotAuthorizedException, BadRequestException, NotFoundException {
if (resource instanceof GetableResource) {
GetableResource r = (GetableResource) resource;
String acceptableContentTypes = request.getAcceptHeader();
String contentType = r.getContentType(acceptableContentTypes);
// Experimental support for already compressed content...
String acceptableEncodings = request.getAcceptEncodingHeader();
if (r instanceof CompressedResource) {
CompressedResource compressedResource = (CompressedResource) r;
String acceptableEncoding = compressedResource.getSupportedEncoding(acceptableEncodings);
if (acceptableEncoding != null) {
response.setContentTypeHeader(contentType);
cacheControlHelper.setCacheControl(r, response, request.getAuthorization());
Long contentLength = compressedResource.getCompressedContentLength(acceptableEncoding);
response.setContentLengthHeader(contentLength);
response.setContentEncodingHeader(Response.ContentEncoding.GZIP);
response.setVaryHeader("Accept-Encoding");
response.setEntity(new CompressedResourceEntity(compressedResource, params, contentType, acceptableEncoding));
return;
}
}
if (canCompress(r, contentType, acceptableEncodings)) {
log.trace("respondContent: compressable");
// get the zipped content before sending so we can determine its
// compressed size
BufferingOutputStream tempOut = new BufferingOutputStream(maxMemorySize);
try {
OutputStream gzipOut = new GZIPOutputStream(tempOut);
r.sendContent(gzipOut, null, params, contentType);
gzipOut.flush();
gzipOut.close();
tempOut.flush();
} catch (NotFoundException e) {
throw e;
} catch (Exception ex) {
tempOut.deleteTempFileIfExists();
throw new RuntimeException(ex);
} finally {
FileUtils.close(tempOut);
}
log.trace("respondContent-compressed: " + resource.getClass());
setRespondContentCommonHeaders(response, resource, Response.Status.SC_OK, request.getAuthorization());
response.setContentEncodingHeader(Response.ContentEncoding.GZIP);
response.setVaryHeader("Accept-Encoding");
Long contentLength = tempOut.getSize();
if (contentLength != null) {
response.setContentLengthHeader(contentLength);
}
response.setContentTypeHeader(contentType);
cacheControlHelper.setCacheControl(r, response, request.getAuthorization());
response.setEntity(new InputStreamEntity(tempOut.getInputStream()));
} else {
log.trace("respondContent: not compressable");
// We really should set this header, but it causes IE to not cache files (eg images)
// response.setVaryHeader( "Accept-Encoding" );
wrapped.respondContent(resource, response, request, params);
}
} else {
throw new RuntimeException("Cant generate content for non-Getable resource: " + resource.getClass());
}
}
use of io.milton.common.BufferingOutputStream in project lobcder by skoulouzis.
the class MiltonFtpFile method createOutputStream.
@Override
public OutputStream createOutputStream(long offset) throws IOException {
log.debug("createOutputStream: " + offset);
final BufferingOutputStream out = new BufferingOutputStream(50000);
if (r instanceof ReplaceableResource) {
log.debug("resource is replaceable");
final ReplaceableResource rr = (ReplaceableResource) r;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
rr.replaceContent(out.getInputStream(), out.getSize());
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
}
}
};
out.setOnClose(runnable);
return out;
} else {
CollectionResource col;
try {
col = getParent();
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
}
if (col == null) {
throw new IOException("parent not found");
} else if (col instanceof PutableResource) {
final PutableResource putableResource = (PutableResource) col;
final String newName = path.getName();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
putableResource.createNew(newName, out.getInputStream(), out.getSize(), null);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
};
out.setOnClose(runnable);
return out;
} else {
throw new IOException("folder doesnt support PUT, and the resource is not replaceable");
}
}
}
use of io.milton.common.BufferingOutputStream in project lobcder by skoulouzis.
the class MiltonFtpFile method createInputStream.
@Override
public InputStream createInputStream(long offset) throws IOException {
if (r instanceof GetableResource) {
GetableResource gr = (GetableResource) r;
String ct = gr.getContentType(null);
BufferingOutputStream out = new BufferingOutputStream(50000);
try {
gr.sendContent(out, null, null, ct);
out.close();
return out.getInputStream();
} catch (NotFoundException ex) {
log.warn("Not found exception", ex);
return null;
} catch (BadRequestException ex) {
log.warn("bad request", ex);
return null;
} catch (NotAuthorizedException ex) {
log.warn("not authorising", ex);
return null;
}
} else {
return null;
}
}
use of io.milton.common.BufferingOutputStream in project lobcder by skoulouzis.
the class BufferingGetableResourceEntity method write.
@Override
public void write(Response response, OutputStream outputStream) throws Exception {
log.trace("buffering content...");
BufferingOutputStream tempOut = new BufferingOutputStream(maxMemorySize);
try {
getResource().sendContent(tempOut, getRange(), getParams(), getContentType());
tempOut.close();
} catch (IOException ex) {
tempOut.deleteTempFileIfExists();
throw new RuntimeException("Exception generating buffered content", ex);
}
Long bufContentLength = tempOut.getSize();
if (contentLength != null) {
if (!contentLength.equals(bufContentLength)) {
throw new RuntimeException("Content Length specified by resource: " + contentLength + " is not equal to the size of content when generated: " + bufContentLength + " This error can be suppressed by setting the buffering property to whenNeeded or never");
}
}
response.setContentLengthHeader(bufContentLength);
if (log.isTraceEnabled()) {
log.trace("sending buffered content... " + tempOut.getSize() + " bytes contentLength=" + contentLength);
}
InputStream in = tempOut.getInputStream();
try {
// StreamUtils.readTo(in, outputStream);
IOUtils.copy(in, outputStream);
} catch (ReadingException ex) {
throw new RuntimeException(ex);
} catch (WritingException ex) {
log.warn("exception writing, client probably closed connection", ex);
} finally {
// make sure we close to delete temporary file
IOUtils.closeQuietly(in);
}
}
Aggregations