Search in sources :

Example 1 with ContentDisposition

use of com.sun.jersey.core.header.ContentDisposition in project onebusaway-application-modules by camsys.

the class LocalBundleDeployerServiceImpl method getBundleFile.

@Override
public Response getBundleFile(String bundleId, String relativeFilename) {
    _log.info("starting getBundleFile for relative filename " + relativeFilename + " in bundle " + bundleId);
    boolean requestIsForValidBundleFile = bundleProvider.checkIsValidBundleFile(bundleId, relativeFilename);
    if (!requestIsForValidBundleFile) {
        throw new WebApplicationException(new IllegalArgumentException(relativeFilename + " is not listed in bundle metadata."), Response.Status.BAD_REQUEST);
    }
    final File requestedFile;
    try {
        requestedFile = bundleProvider.getBundleFile(bundleId, relativeFilename);
    } catch (FileNotFoundException e) {
        _log.info("FileNotFoundException loading " + relativeFilename + " in " + bundleId + " bundle.");
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
    long fileLength = requestedFile.length();
    StreamingOutput output = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            FileChannel inChannel = null;
            WritableByteChannel outChannel = null;
            try {
                inChannel = new FileInputStream(requestedFile).getChannel();
                outChannel = Channels.newChannel(os);
                inChannel.transferTo(0, inChannel.size(), outChannel);
            } finally {
                if (outChannel != null)
                    outChannel.close();
                if (inChannel != null)
                    inChannel.close();
            }
        }
    };
    ContentDisposition cd = ContentDisposition.type("file").fileName(requestedFile.getName()).build();
    Response response = Response.ok(output, MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", cd).header("Content-Length", fileLength).build();
    _log.info("Returning Response in getBundleFile");
    return response;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) FileChannel(java.nio.channels.FileChannel) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) WritableByteChannel(java.nio.channels.WritableByteChannel) StreamingOutput(javax.ws.rs.core.StreamingOutput) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) ContentDisposition(com.sun.jersey.core.header.ContentDisposition) File(java.io.File)

Example 2 with ContentDisposition

use of com.sun.jersey.core.header.ContentDisposition in project onebusaway-application-modules by camsys.

the class LocalBundleStagerServiceImpl method getBundleFile.

@Override
public Response getBundleFile(String bundleId, String relativeFilename) {
    _log.info("starting getBundleFile for relative filename " + relativeFilename + " in bundle " + bundleId);
    boolean requestIsForValidBundleFile = bundleProvider.checkIsValidBundleFile(bundleId, relativeFilename);
    if (!requestIsForValidBundleFile) {
        throw new WebApplicationException(new IllegalArgumentException(relativeFilename + " is not listed in bundle metadata."), Response.Status.BAD_REQUEST);
    }
    final File requestedFile;
    try {
        requestedFile = bundleProvider.getBundleFile(bundleId, relativeFilename);
    } catch (FileNotFoundException e) {
        _log.info("FileNotFoundException loading " + relativeFilename + " in " + bundleId + " bundle.");
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
    long fileLength = requestedFile.length();
    StreamingOutput output = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            FileChannel inChannel = null;
            WritableByteChannel outChannel = null;
            try {
                inChannel = new FileInputStream(requestedFile).getChannel();
                outChannel = Channels.newChannel(os);
                inChannel.transferTo(0, inChannel.size(), outChannel);
            } finally {
                if (outChannel != null)
                    outChannel.close();
                if (inChannel != null)
                    inChannel.close();
            }
        }
    };
    ContentDisposition cd = ContentDisposition.type("file").fileName(requestedFile.getName()).build();
    Response response = Response.ok(output, MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", cd).header("Content-Length", fileLength).build();
    _log.info("Returning Response in getBundleFile");
    return response;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) FileChannel(java.nio.channels.FileChannel) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) WritableByteChannel(java.nio.channels.WritableByteChannel) StreamingOutput(javax.ws.rs.core.StreamingOutput) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) ContentDisposition(com.sun.jersey.core.header.ContentDisposition) File(java.io.File)

Aggregations

ContentDisposition (com.sun.jersey.core.header.ContentDisposition)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 FileChannel (java.nio.channels.FileChannel)2 WritableByteChannel (java.nio.channels.WritableByteChannel)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Response (javax.ws.rs.core.Response)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2