use of javax.ws.rs.core.Context in project graphhopper by graphhopper.
the class SPTResource method doGet.
// Annotating this as application/json because errors come out as json, and
// IllegalArgumentExceptions are not mapped to a fixed mediatype, because in RouteResource, it could be GPX.
@GET
@Produces({ "text/csv", "application/json" })
public Response doGet(@Context UriInfo uriInfo, @QueryParam("profile") String profileName, @QueryParam("reverse_flow") @DefaultValue("false") boolean reverseFlow, @QueryParam("point") @NotNull GHPointParam point, @QueryParam("columns") String columnsParam, @QueryParam("time_limit") @DefaultValue("600") LongParam timeLimitInSeconds, @QueryParam("distance_limit") @DefaultValue("-1") LongParam distanceInMeter) {
StopWatch sw = new StopWatch().start();
PMap hintsMap = new PMap();
RouteResource.initHints(hintsMap, uriInfo.getQueryParameters());
hintsMap.putObject(Parameters.CH.DISABLE, true);
hintsMap.putObject(Parameters.Landmark.DISABLE, true);
if (Helper.isEmpty(profileName)) {
profileName = profileResolver.resolveProfile(hintsMap).getName();
removeLegacyParameters(hintsMap);
}
errorIfLegacyParameters(hintsMap);
Profile profile = graphHopper.getProfile(profileName);
if (profile == null)
throw new IllegalArgumentException("The requested profile '" + profileName + "' does not exist");
LocationIndex locationIndex = graphHopper.getLocationIndex();
Graph graph = graphHopper.getGraphHopperStorage();
Weighting weighting = graphHopper.createWeighting(profile, hintsMap);
BooleanEncodedValue inSubnetworkEnc = graphHopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName));
if (hintsMap.has(Parameters.Routing.BLOCK_AREA)) {
GraphEdgeIdFinder.BlockArea blockArea = GraphEdgeIdFinder.createBlockArea(graph, locationIndex, Collections.singletonList(point.get()), hintsMap, new FiniteWeightFilter(weighting));
weighting = new BlockAreaWeighting(weighting, blockArea);
}
Snap snap = locationIndex.findClosest(point.get().lat, point.get().lon, new DefaultSnapFilter(weighting, inSubnetworkEnc));
if (!snap.isValid())
throw new IllegalArgumentException("Point not found:" + point);
QueryGraph queryGraph = QueryGraph.create(graph, snap);
NodeAccess nodeAccess = queryGraph.getNodeAccess();
TraversalMode traversalMode = profile.isTurnCosts() ? EDGE_BASED : NODE_BASED;
ShortestPathTree shortestPathTree = new ShortestPathTree(queryGraph, queryGraph.wrapWeighting(weighting), reverseFlow, traversalMode);
if (distanceInMeter.get() > 0) {
shortestPathTree.setDistanceLimit(distanceInMeter.get());
} else {
double limit = timeLimitInSeconds.get() * 1000;
shortestPathTree.setTimeLimit(limit);
}
final String COL_SEP = ",", LINE_SEP = "\n";
List<String> columns;
if (!Helper.isEmpty(columnsParam))
columns = Arrays.asList(columnsParam.split(","));
else
columns = Arrays.asList("longitude", "latitude", "time", "distance");
if (columns.isEmpty())
throw new IllegalArgumentException("Either omit the columns parameter or specify the columns via comma separated values");
Map<String, EncodedValue> pathDetails = new HashMap<>();
for (String col : columns) {
if (encodingManager.hasEncodedValue(col))
pathDetails.put(col, encodingManager.getEncodedValue(col, EncodedValue.class));
}
StreamingOutput out = output -> {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(output, Helper.UTF_CS))) {
StringBuilder sb = new StringBuilder();
for (String col : columns) {
if (sb.length() > 0)
sb.append(COL_SEP);
sb.append(col);
}
sb.append(LINE_SEP);
writer.write(sb.toString());
shortestPathTree.search(snap.getClosestNode(), l -> {
IsoLabelWithCoordinates label = isoLabelWithCoordinates(nodeAccess, l);
sb.setLength(0);
for (int colIndex = 0; colIndex < columns.size(); colIndex++) {
String col = columns.get(colIndex);
if (colIndex > 0)
sb.append(COL_SEP);
switch(col) {
case "node_id":
sb.append(label.nodeId);
continue;
case "prev_node_id":
sb.append(label.prevNodeId);
continue;
case "edge_id":
sb.append(label.edgeId);
continue;
case "prev_edge_id":
sb.append(label.prevEdgeId);
continue;
case "distance":
sb.append(label.distance);
continue;
case "prev_distance":
sb.append(label.prevCoordinate == null ? 0 : label.prevDistance);
continue;
case "time":
sb.append(label.timeMillis);
continue;
case "prev_time":
sb.append(label.prevCoordinate == null ? 0 : label.prevTimeMillis);
continue;
case "longitude":
sb.append(Helper.round6(label.coordinate.lon));
continue;
case "prev_longitude":
sb.append(label.prevCoordinate == null ? null : Helper.round6(label.prevCoordinate.lon));
continue;
case "latitude":
sb.append(Helper.round6(label.coordinate.lat));
continue;
case "prev_latitude":
sb.append(label.prevCoordinate == null ? null : Helper.round6(label.prevCoordinate.lat));
continue;
}
if (!EdgeIterator.Edge.isValid(label.edgeId))
continue;
EdgeIteratorState edge = queryGraph.getEdgeIteratorState(label.edgeId, label.nodeId);
if (edge == null)
continue;
if (col.equals(Parameters.Details.STREET_NAME)) {
sb.append(edge.getName().replaceAll(",", ""));
continue;
}
EncodedValue ev = pathDetails.get(col);
if (ev instanceof DecimalEncodedValue) {
DecimalEncodedValue dev = (DecimalEncodedValue) ev;
sb.append(reverseFlow ? edge.getReverse(dev) : edge.get(dev));
} else if (ev instanceof EnumEncodedValue) {
EnumEncodedValue eev = (EnumEncodedValue) ev;
sb.append(reverseFlow ? edge.getReverse(eev) : edge.get(eev));
} else if (ev instanceof BooleanEncodedValue) {
BooleanEncodedValue eev = (BooleanEncodedValue) ev;
sb.append(reverseFlow ? edge.getReverse(eev) : edge.get(eev));
} else if (ev instanceof IntEncodedValue) {
IntEncodedValue eev = (IntEncodedValue) ev;
sb.append(reverseFlow ? edge.getReverse(eev) : edge.get(eev));
} else {
throw new IllegalArgumentException("Unknown property " + col);
}
}
sb.append(LINE_SEP);
try {
writer.write(sb.toString());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
logger.info("took: " + sw.stop().getSeconds() + ", visited nodes:" + shortestPathTree.getVisitedNodes() + ", " + uriInfo.getQueryParameters());
} catch (IOException e) {
throw new RuntimeException(e);
}
};
// Give media type explicitly since we are annotating CSV and JSON, because error messages are JSON.
return Response.ok(out).type("text/csv").build();
}
use of javax.ws.rs.core.Context in project graphhopper by graphhopper.
the class MVTResource method doGetXyz.
@GET
@Path("{z}/{x}/{y}.mvt")
@Produces("application/x-protobuf")
public Response doGetXyz(@Context HttpServletRequest httpReq, @Context UriInfo uriInfo, @PathParam("z") int zInfo, @PathParam("x") int xInfo, @PathParam("y") int yInfo, @QueryParam(Parameters.Details.PATH_DETAILS) List<String> pathDetails) {
if (zInfo <= 9) {
VectorTile.Tile.Builder mvtBuilder = VectorTile.Tile.newBuilder();
return Response.fromResponse(Response.ok(mvtBuilder.build().toByteArray(), PBF).build()).header("X-GH-Took", "0").build();
}
StopWatch totalSW = new StopWatch().start();
Coordinate nw = num2deg(xInfo, yInfo, zInfo);
Coordinate se = num2deg(xInfo + 1, yInfo + 1, zInfo);
LocationIndexTree locationIndex = (LocationIndexTree) graphHopper.getLocationIndex();
final NodeAccess na = graphHopper.getGraphHopperStorage().getNodeAccess();
BBox bbox = new BBox(nw.x, se.x, se.y, nw.y);
if (!bbox.isValid())
throw new IllegalStateException("Invalid bbox " + bbox);
final GeometryFactory geometryFactory = new GeometryFactory();
VectorTile.Tile.Builder mvtBuilder = VectorTile.Tile.newBuilder();
final IGeometryFilter acceptAllGeomFilter = geometry -> true;
final Envelope tileEnvelope = new Envelope(se, nw);
final MvtLayerParams layerParams = new MvtLayerParams(256, 4096);
final UserDataKeyValueMapConverter converter = new UserDataKeyValueMapConverter();
if (!encodingManager.hasEncodedValue(RoadClass.KEY))
throw new IllegalStateException("You need to configure GraphHopper to store road_class, e.g. graph.encoded_values: road_class,max_speed,... ");
final EnumEncodedValue<RoadClass> roadClassEnc = encodingManager.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
final AtomicInteger edgeCounter = new AtomicInteger(0);
// in toFeatures addTags of the converter is called and layerProps is filled with keys&values => those need to be stored in the layerBuilder
// otherwise the decoding won't be successful and "undefined":"undefined" instead of "speed": 30 is the result
final MvtLayerProps layerProps = new MvtLayerProps();
final VectorTile.Tile.Layer.Builder layerBuilder = MvtLayerBuild.newLayerBuilder("roads", layerParams);
locationIndex.query(bbox, edgeId -> {
EdgeIteratorState edge = graphHopper.getGraphHopperStorage().getEdgeIteratorStateForKey(edgeId * 2);
LineString lineString;
RoadClass rc = edge.get(roadClassEnc);
if (zInfo >= 14) {
PointList pl = edge.fetchWayGeometry(FetchMode.ALL);
lineString = pl.toLineString(false);
} else if (rc == RoadClass.MOTORWAY || zInfo > 10 && (rc == RoadClass.PRIMARY || rc == RoadClass.TRUNK) || zInfo > 11 && (rc == RoadClass.SECONDARY) || zInfo > 12) {
double lat = na.getLat(edge.getBaseNode());
double lon = na.getLon(edge.getBaseNode());
double toLat = na.getLat(edge.getAdjNode());
double toLon = na.getLon(edge.getAdjNode());
lineString = geometryFactory.createLineString(new Coordinate[] { new Coordinate(lon, lat), new Coordinate(toLon, toLat) });
} else {
// skip edge for certain zoom
return;
}
edgeCounter.incrementAndGet();
Map<String, Object> map = new HashMap<>(2);
map.put("name", edge.getName());
for (String str : pathDetails) {
// how to indicate an erroneous parameter?
if (str.contains(",") || !encodingManager.hasEncodedValue(str))
continue;
EncodedValue ev = encodingManager.getEncodedValue(str, EncodedValue.class);
if (ev instanceof EnumEncodedValue)
map.put(ev.getName(), edge.get((EnumEncodedValue) ev).toString());
else if (ev instanceof DecimalEncodedValue)
map.put(ev.getName(), edge.get((DecimalEncodedValue) ev));
else if (ev instanceof BooleanEncodedValue)
map.put(ev.getName(), edge.get((BooleanEncodedValue) ev));
else if (ev instanceof IntEncodedValue)
map.put(ev.getName(), edge.get((IntEncodedValue) ev));
}
lineString.setUserData(map);
// doing some AffineTransformation
TileGeomResult tileGeom = JtsAdapter.createTileGeom(lineString, tileEnvelope, geometryFactory, layerParams, acceptAllGeomFilter);
List<VectorTile.Tile.Feature> features = JtsAdapter.toFeatures(tileGeom.mvtGeoms, layerProps, converter);
layerBuilder.addAllFeatures(features);
});
MvtLayerBuild.writeProps(layerBuilder, layerProps);
mvtBuilder.addLayers(layerBuilder.build());
byte[] bytes = mvtBuilder.build().toByteArray();
totalSW.stop();
logger.debug("took: " + totalSW.getSeconds() + ", edges:" + edgeCounter.get());
return Response.ok(bytes, PBF).header("X-GH-Took", "" + totalSW.getSeconds() * 1000).build();
}
use of javax.ws.rs.core.Context in project indy by Commonjava.
the class DeprecatedStoreAdminHandler method store.
/*
* (non-Javadoc)
* @see org.commonjava.indy.core.rest.admin.DeployPointAdminResource#store(java.lang.String)
*/
@ApiOperation("Update an existing store")
@ApiResponses({ @ApiResponse(code = 200, message = "The store was updated"), @ApiResponse(code = 400, message = "The store specified in the body JSON didn't match the URL parameters") })
@ApiImplicitParams({ @ApiImplicitParam(allowMultiple = false, paramType = "body", name = "body", required = true, dataType = "org.commonjava.indy.model.core.ArtifactStore", value = "The artifact store definition JSON") })
@Path("/{name}")
@PUT
@Consumes(ApplicationContent.application_json)
public Response store(@ApiParam(allowableValues = "hosted,group,remote", required = true) @PathParam("type") final String type, @ApiParam(required = true) @PathParam("name") final String name, @Context final HttpServletRequest request, @Context final SecurityContext securityContext) {
String altPath = Paths.get(MavenPackageTypeDescriptor.MAVEN_ADMIN_REST_BASE_PATH, type, name).toString();
Consumer<Response.ResponseBuilder> modifier = (rb) -> responseHelper.markDeprecated(rb, altPath);
final StoreType st = StoreType.get(type);
Response response = null;
String json = null;
try {
json = IOUtils.toString(request.getInputStream());
json = objectMapper.patchLegacyStoreJson(json);
} catch (final IOException e) {
final String message = "Failed to read " + st.getStoreClass().getSimpleName() + " from request body.";
logger.error(message, e);
response = responseHelper.formatResponse(e, message, modifier);
}
if (response != null) {
return response;
}
ArtifactStore store = null;
try {
store = objectMapper.readValue(json, st.getStoreClass());
} catch (final IOException e) {
final String message = "Failed to parse " + st.getStoreClass().getSimpleName() + " from request body.";
logger.error(message, e);
response = responseHelper.formatResponse(e, message, modifier);
}
if (response != null) {
return response;
}
if (!name.equals(store.getName())) {
response = responseHelper.markDeprecated(Response.status(Status.BAD_REQUEST).entity(String.format("Store in URL path is: '%s' but in JSON it is: '%s'", name, store.getName())), altPath).build();
}
try {
String user = securityManager.getUser(securityContext, request);
logger.info("Storing: {}", store);
if (adminController.store(store, user, false)) {
response = responseHelper.markDeprecated(ok(), altPath).build();
} else {
logger.warn("{} NOT modified!", store);
response = responseHelper.markDeprecated(notModified(), altPath).build();
}
} catch (final IndyWorkflowException e) {
logger.error(e.getMessage(), e);
response = responseHelper.formatResponse(e, modifier);
}
return response;
}
use of javax.ws.rs.core.Context in project indy by Commonjava.
the class DeprecatedStoreAdminHandler method delete.
@ApiOperation("Delete an artifact store")
@ApiResponses({ @ApiResponse(code = 204, response = ArtifactStore.class, message = "The store was deleted (or didn't exist in the first place)") })
@Path("/{name}")
@DELETE
public Response delete(@ApiParam(allowableValues = "hosted,group,remote", required = true) @PathParam("type") final String type, @ApiParam(required = true) @PathParam("name") final String name, @Context final HttpServletRequest request, @Context final SecurityContext securityContext) {
String altPath = Paths.get(MavenPackageTypeDescriptor.MAVEN_ADMIN_REST_BASE_PATH, type, name).toString();
Consumer<Response.ResponseBuilder> modifier = (rb) -> responseHelper.markDeprecated(rb, altPath);
final StoreType st = StoreType.get(type);
final StoreKey key = new StoreKey(st, name);
logger.info("Deleting: {}", key);
Response response;
try {
String summary = null;
try {
summary = IOUtils.toString(request.getInputStream());
} catch (final IOException e) {
// no problem, try to get the summary from a header instead.
logger.info("store-deletion change summary not in request body, checking headers.");
}
if (isEmpty(summary)) {
summary = request.getHeader(METADATA_CHANGELOG);
}
if (isEmpty(summary)) {
summary = "Changelog not provided";
}
String user = securityManager.getUser(securityContext, request);
adminController.delete(key, user, summary, false);
response = responseHelper.markDeprecated(noContent(), altPath).build();
} catch (final IndyWorkflowException e) {
logger.error(e.getMessage(), e);
response = responseHelper.formatResponse(e, modifier);
}
return response;
}
use of javax.ws.rs.core.Context in project indy by Commonjava.
the class DeprecatedStoreAdminHandler method create.
@ApiOperation("Create a new store")
@ApiResponses({ @ApiResponse(code = 201, response = ArtifactStore.class, message = "The store was created"), @ApiResponse(code = 409, message = "A store with the specified type and name already exists") })
@ApiImplicitParams({ @ApiImplicitParam(allowMultiple = false, paramType = "body", name = "body", required = true, dataType = "org.commonjava.indy.model.core.ArtifactStore", value = "The artifact store definition JSON") })
@POST
@Consumes(ApplicationContent.application_json)
@Produces(ApplicationContent.application_json)
public Response create(@ApiParam(allowableValues = "hosted,group,remote", required = true) @PathParam("type") final String type, @Context final UriInfo uriInfo, @Context final HttpServletRequest request, @Context final SecurityContext securityContext) {
String altPath = Paths.get(MavenPackageTypeDescriptor.MAVEN_ADMIN_REST_BASE_PATH, type).toString();
Consumer<Response.ResponseBuilder> modifier = (rb) -> responseHelper.markDeprecated(rb, altPath);
final StoreType st = StoreType.get(type);
Response response = null;
String json = null;
try {
json = IOUtils.toString(request.getInputStream());
json = objectMapper.patchLegacyStoreJson(json);
} catch (final IOException e) {
final String message = "Failed to read " + st.getStoreClass().getSimpleName() + " from request body.";
logger.error(message, e);
response = responseHelper.formatResponse(e, message, modifier);
}
if (response != null) {
return response;
}
ArtifactStore store = null;
try {
store = objectMapper.readValue(json, st.getStoreClass());
} catch (final IOException e) {
final String message = "Failed to parse " + st.getStoreClass().getSimpleName() + " from request body.";
logger.error(message, e);
response = responseHelper.formatResponse(e, message, modifier);
}
if (response != null) {
return response;
}
logger.info("\n\nGot artifact store: {}\n\n", store);
try {
String user = securityManager.getUser(securityContext, request);
if (adminController.store(store, user, false)) {
final URI uri = uriInfo.getBaseUriBuilder().path(getClass()).path(store.getName()).build(store.getKey().getType().singularEndpointName());
response = responseHelper.formatCreatedResponseWithJsonEntity(uri, store, modifier);
} else {
response = responseHelper.markDeprecated(status(CONFLICT).entity("{\"error\": \"Store already exists.\"}").type(application_json), altPath).build();
}
} catch (final IndyWorkflowException e) {
logger.error(e.getMessage(), e);
response = responseHelper.formatResponse(e, modifier);
}
return response;
}
Aggregations