Search in sources :

Example 1 with BufferingOutputStream

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());
    }
}
Also used : CompressedResourceEntity(io.milton.http.entity.CompressedResourceEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) BufferingOutputStream(io.milton.common.BufferingOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferingOutputStream(io.milton.common.BufferingOutputStream) GetableResource(io.milton.resource.GetableResource) NotFoundException(io.milton.http.exceptions.NotFoundException) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NotFoundException(io.milton.http.exceptions.NotFoundException) InputStreamEntity(io.milton.http.entity.InputStreamEntity)

Example 2 with BufferingOutputStream

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");
        }
    }
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) BufferingOutputStream(io.milton.common.BufferingOutputStream) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) ReplaceableResource(io.milton.resource.ReplaceableResource)

Example 3 with BufferingOutputStream

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;
    }
}
Also used : BufferingOutputStream(io.milton.common.BufferingOutputStream) NotFoundException(io.milton.http.exceptions.NotFoundException) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 4 with BufferingOutputStream

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);
    }
}
Also used : ReadingException(io.milton.common.ReadingException) InputStream(java.io.InputStream) BufferingOutputStream(io.milton.common.BufferingOutputStream) IOException(java.io.IOException) WritingException(io.milton.common.WritingException)

Aggregations

BufferingOutputStream (io.milton.common.BufferingOutputStream)4 BadRequestException (io.milton.http.exceptions.BadRequestException)3 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)3 NotFoundException (io.milton.http.exceptions.NotFoundException)2 IOException (java.io.IOException)2 ReadingException (io.milton.common.ReadingException)1 WritingException (io.milton.common.WritingException)1 CompressedResourceEntity (io.milton.http.entity.CompressedResourceEntity)1 InputStreamEntity (io.milton.http.entity.InputStreamEntity)1 ConflictException (io.milton.http.exceptions.ConflictException)1 GetableResource (io.milton.resource.GetableResource)1 ReplaceableResource (io.milton.resource.ReplaceableResource)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1