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