Search in sources :

Example 51 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project OpenTripPlanner by opentripplanner.

the class Renderer method generateStreamingGeotiffResponse.

private static Response generateStreamingGeotiffResponse(final GridCoverage2D coverage) {
    StreamingOutput streamingOutput = new StreamingOutput() {

        public void write(OutputStream outStream) {
            try {
                long t0 = System.currentTimeMillis();
                GeoTiffWriteParams wp = new GeoTiffWriteParams();
                wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
                wp.setCompressionType("LZW");
                ParameterValueGroup params = new GeoTiffFormat().getWriteParameters();
                params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp);
                new GeoTiffWriter(outStream).write(coverage, (GeneralParameterValue[]) params.values().toArray(new GeneralParameterValue[1]));
                // new GeoTiffWriter(outStream).write(coverage, null); //wasn't this line writing twice and trashing compressed version?
                long t1 = System.currentTimeMillis();
                LOG.debug("wrote geotiff in {}msec", t1 - t0);
            } catch (Exception e) {
                LOG.error("exception while preparing geotiff : {}", e.getMessage());
                throw new WebApplicationException(e);
            }
        }
    };
    CacheControl cc = new CacheControl();
    cc.setMaxAge(3600);
    cc.setNoCache(false);
    return Response.ok(streamingOutput).type("image/geotiff").cacheControl(cc).build();
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) GeoTiffFormat(org.geotools.gce.geotiff.GeoTiffFormat) WebApplicationException(javax.ws.rs.WebApplicationException) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) OutputStream(java.io.OutputStream) GeoTiffWriteParams(org.geotools.gce.geotiff.GeoTiffWriteParams) StreamingOutput(javax.ws.rs.core.StreamingOutput) GeoTiffWriter(org.geotools.gce.geotiff.GeoTiffWriter) CacheControl(javax.ws.rs.core.CacheControl) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 52 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project OpenTripPlanner by opentripplanner.

the class LIsochrone method getZippedShapefileIsochrone.

@GET
@Produces("application/x-zip-compressed")
public Response getZippedShapefileIsochrone(@QueryParam("shpName") String shpName, @QueryParam("stream") @DefaultValue("true") boolean stream) throws Exception {
    SimpleFeatureCollection contourFeatures = makeContourFeatures(computeIsochrone());
    /* Output the staged features to Shapefile */
    final File shapeDir = Files.createTempDir();
    File shapeFile = new File(shapeDir, shpName + ".shp");
    LOG.debug("writing out shapefile {}", shapeFile);
    ShapefileDataStore outStore = new ShapefileDataStore(shapeFile.toURI().toURL());
    outStore.createSchema(contourSchema);
    Transaction transaction = new DefaultTransaction("create");
    SimpleFeatureStore featureStore = (SimpleFeatureStore) outStore.getFeatureSource();
    featureStore.setTransaction(transaction);
    try {
        featureStore.addFeatures(contourFeatures);
        transaction.commit();
    } catch (Exception e) {
        transaction.rollback();
        throw e;
    } finally {
        transaction.close();
    }
    // Note: the order is important
    shapeDir.deleteOnExit();
    for (File f : shapeDir.listFiles()) f.deleteOnExit();
    /* Zip up the shapefile components */
    StreamingOutput output = new DirectoryZipper(shapeDir);
    if (stream) {
        return Response.ok().entity(output).build();
    } else {
        File zipFile = new File(shapeDir, shpName + ".zip");
        OutputStream fos = new FileOutputStream(zipFile);
        output.write(fos);
        zipFile.deleteOnExit();
        return Response.ok().entity(zipFile).build();
    }
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 53 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project streamline by hortonworks.

the class WSUtils method wrapWithStreamingOutput.

public static StreamingOutput wrapWithStreamingOutput(final InputStream inputStream) {
    return new StreamingOutput() {

        public void write(OutputStream os) throws IOException, WebApplicationException {
            OutputStream wrappedOutputStream = os;
            if (!(os instanceof BufferedOutputStream)) {
                wrappedOutputStream = new BufferedOutputStream(os);
            }
            ByteStreams.copy(inputStream, wrappedOutputStream);
            wrappedOutputStream.flush();
        }
    };
}
Also used : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) BufferedOutputStream(java.io.BufferedOutputStream)

Example 54 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project streamline by hortonworks.

the class UDFCatalogResource method downloadUdf.

/**
 * Download the jar corresponding to a specific UDF.
 * <p>
 * E.g. curl http://localhost:8080/api/v1/catalog/udfs/download/34 -o /tmp/file.jar
 * </p>
 */
@Timed
@GET
@Produces({ "application/java-archive", "application/json" })
@Path("/udfs/download/{udfId}")
public Response downloadUdf(@PathParam("udfId") Long udfId, @Context SecurityContext securityContext) throws IOException {
    SecurityUtil.checkPermissions(authorizer, securityContext, UDF.NAMESPACE, udfId, READ, EXECUTE);
    UDF udf = catalogService.getUDF(udfId);
    if (udf != null) {
        StreamingOutput streamOutput = WSUtils.wrapWithStreamingOutput(catalogService.downloadFileFromStorage(udf.getJarStoragePath()));
        return Response.ok(streamOutput).build();
    }
    throw EntityNotFoundException.byId(udfId.toString());
}
Also used : UDF(com.hortonworks.streamline.streams.catalog.UDF) StreamingOutput(javax.ws.rs.core.StreamingOutput) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 55 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project streamline by hortonworks.

the class NotifierInfoCatalogResource method downloadNotifier.

/**
 * Download the jar corresponding to a specific Notifier.
 * <p>
 * E.g. curl http://localhost:8080/api/v1/catalog/notifiers/download/34 -o /tmp/notifier.jar
 * </p>
 */
@Timed
@GET
@Produces({ "application/java-archive", "application/json" })
@Path("/notifiers/download/{notifierId}")
public Response downloadNotifier(@PathParam("notifierId") Long notifierId, @Context SecurityContext securityContext) throws IOException {
    SecurityUtil.checkPermissions(authorizer, securityContext, Notifier.NAMESPACE, notifierId, READ, EXECUTE);
    Notifier notifier = catalogService.getNotifierInfo(notifierId);
    if (notifier != null) {
        StreamingOutput streamOutput = WSUtils.wrapWithStreamingOutput(catalogService.downloadFileFromStorage(notifier.getJarFileName()));
        return Response.ok(streamOutput).build();
    }
    throw EntityNotFoundException.byId(notifierId.toString());
}
Also used : StreamingOutput(javax.ws.rs.core.StreamingOutput) Notifier(com.hortonworks.streamline.streams.catalog.Notifier) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)190 OutputStream (java.io.OutputStream)84 Response (javax.ws.rs.core.Response)76 Path (javax.ws.rs.Path)53 Produces (javax.ws.rs.Produces)52 IOException (java.io.IOException)50 GET (javax.ws.rs.GET)50 File (java.io.File)45 InputStream (java.io.InputStream)45 Test (org.junit.Test)44 WebApplicationException (javax.ws.rs.WebApplicationException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 List (java.util.List)26 MediaType (javax.ws.rs.core.MediaType)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ArrayList (java.util.ArrayList)20 Consumes (javax.ws.rs.Consumes)20 HashMap (java.util.HashMap)19 POST (javax.ws.rs.POST)19 FileOutputStream (java.io.FileOutputStream)17