Search in sources :

Example 46 with Context

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();
}
Also used : com.graphhopper.routing.ev(com.graphhopper.routing.ev) java.util(java.util) ProfileResolver(com.graphhopper.routing.ProfileResolver) LoggerFactory(org.slf4j.LoggerFactory) EncodingManager(com.graphhopper.routing.util.EncodingManager) Inject(javax.inject.Inject) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Profile(com.graphhopper.config.Profile) TraversalMode(com.graphhopper.routing.util.TraversalMode) Graph(com.graphhopper.storage.Graph) OutputStreamWriter(java.io.OutputStreamWriter) NODE_BASED(com.graphhopper.routing.util.TraversalMode.NODE_BASED) GraphHopper(com.graphhopper.GraphHopper) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) LocationIndex(com.graphhopper.storage.index.LocationIndex) RouteResource.errorIfLegacyParameters(com.graphhopper.resources.RouteResource.errorIfLegacyParameters) BufferedWriter(java.io.BufferedWriter) LongParam(io.dropwizard.jersey.params.LongParam) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) NotNull(javax.validation.constraints.NotNull) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GHPointParam(com.graphhopper.http.GHPointParam) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) NodeAccess(com.graphhopper.storage.NodeAccess) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Weighting(com.graphhopper.routing.weighting.Weighting) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) Writer(java.io.Writer) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) Snap(com.graphhopper.storage.index.Snap) UriInfo(javax.ws.rs.core.UriInfo) EDGE_BASED(com.graphhopper.routing.util.TraversalMode.EDGE_BASED) GHPoint(com.graphhopper.util.shapes.GHPoint) RouteResource.removeLegacyParameters(com.graphhopper.resources.RouteResource.removeLegacyParameters) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) NodeAccess(com.graphhopper.storage.NodeAccess) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) StreamingOutput(javax.ws.rs.core.StreamingOutput) TraversalMode(com.graphhopper.routing.util.TraversalMode) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile) BufferedWriter(java.io.BufferedWriter) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) IOException(java.io.IOException) LocationIndex(com.graphhopper.storage.index.LocationIndex) Graph(com.graphhopper.storage.Graph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) OutputStreamWriter(java.io.OutputStreamWriter) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 47 with Context

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();
}
Also used : com.graphhopper.routing.ev(com.graphhopper.routing.ev) IGeometryFilter(com.wdtinc.mapbox_vector_tile.adapt.jts.IGeometryFilter) JtsAdapter(com.wdtinc.mapbox_vector_tile.adapt.jts.JtsAdapter) LoggerFactory(org.slf4j.LoggerFactory) VectorTile(com.wdtinc.mapbox_vector_tile.VectorTile) Coordinate(org.locationtech.jts.geom.Coordinate) HashMap(java.util.HashMap) MvtLayerBuild(com.wdtinc.mapbox_vector_tile.build.MvtLayerBuild) MvtLayerParams(com.wdtinc.mapbox_vector_tile.build.MvtLayerParams) EncodingManager(com.graphhopper.routing.util.EncodingManager) Inject(javax.inject.Inject) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) GraphHopper(com.graphhopper.GraphHopper) com.graphhopper.util(com.graphhopper.util) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) BBox(com.graphhopper.util.shapes.BBox) UserDataKeyValueMapConverter(com.wdtinc.mapbox_vector_tile.adapt.jts.UserDataKeyValueMapConverter) TileGeomResult(com.wdtinc.mapbox_vector_tile.adapt.jts.TileGeomResult) LineString(org.locationtech.jts.geom.LineString) List(java.util.List) NodeAccess(com.graphhopper.storage.NodeAccess) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) MvtLayerProps(com.wdtinc.mapbox_vector_tile.build.MvtLayerProps) UriInfo(javax.ws.rs.core.UriInfo) Envelope(org.locationtech.jts.geom.Envelope) NodeAccess(com.graphhopper.storage.NodeAccess) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) HashMap(java.util.HashMap) LineString(org.locationtech.jts.geom.LineString) Envelope(org.locationtech.jts.geom.Envelope) TileGeomResult(com.wdtinc.mapbox_vector_tile.adapt.jts.TileGeomResult) UserDataKeyValueMapConverter(com.wdtinc.mapbox_vector_tile.adapt.jts.UserDataKeyValueMapConverter) MvtLayerProps(com.wdtinc.mapbox_vector_tile.build.MvtLayerProps) VectorTile(com.wdtinc.mapbox_vector_tile.VectorTile) IGeometryFilter(com.wdtinc.mapbox_vector_tile.adapt.jts.IGeometryFilter) VectorTile(com.wdtinc.mapbox_vector_tile.VectorTile) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Coordinate(org.locationtech.jts.geom.Coordinate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LineString(org.locationtech.jts.geom.LineString) BBox(com.graphhopper.util.shapes.BBox) MvtLayerParams(com.wdtinc.mapbox_vector_tile.build.MvtLayerParams)

Example 48 with Context

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;
}
Also used : Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ApiParam(io.swagger.annotations.ApiParam) StoreListingDTO(org.commonjava.indy.model.core.dto.StoreListingDTO) ApiOperation(io.swagger.annotations.ApiOperation) Consumes(javax.ws.rs.Consumes) Response.noContent(javax.ws.rs.core.Response.noContent) Response.status(javax.ws.rs.core.Response.status) URI(java.net.URI) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) StoreKey(org.commonjava.indy.model.core.StoreKey) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) ResponseHelper(org.commonjava.indy.bind.jaxrs.util.ResponseHelper) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) StoreType(org.commonjava.indy.model.core.StoreType) IndyResources(org.commonjava.indy.bind.jaxrs.IndyResources) Response.notModified(javax.ws.rs.core.Response.notModified) IOUtils(org.apache.commons.io.IOUtils) ApplicationContent(org.commonjava.indy.util.ApplicationContent) List(java.util.List) Response(javax.ws.rs.core.Response) Response.ok(javax.ws.rs.core.Response.ok) ApplicationScoped(javax.enterprise.context.ApplicationScoped) UriInfo(javax.ws.rs.core.UriInfo) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) HEAD(javax.ws.rs.HEAD) PathParam(javax.ws.rs.PathParam) REST(org.commonjava.indy.bind.jaxrs.util.REST) GET(javax.ws.rs.GET) ApplicationContent.application_json(org.commonjava.indy.util.ApplicationContent.application_json) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) ApiResponses(io.swagger.annotations.ApiResponses) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) MavenPackageTypeDescriptor(org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) SecurityManager(org.commonjava.indy.bind.jaxrs.SecurityManager) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) METADATA_CHANGELOG(org.commonjava.indy.model.core.ArtifactStore.METADATA_CHANGELOG) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Paths(java.nio.file.Paths) ApiResponse(io.swagger.annotations.ApiResponse) AdminController(org.commonjava.indy.core.ctl.AdminController) PUT(javax.ws.rs.PUT) InputStream(java.io.InputStream) StoreType(org.commonjava.indy.model.core.StoreType) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) IOException(java.io.IOException) Path(javax.ws.rs.Path) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT)

Example 49 with Context

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;
}
Also used : Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ApiParam(io.swagger.annotations.ApiParam) StoreListingDTO(org.commonjava.indy.model.core.dto.StoreListingDTO) ApiOperation(io.swagger.annotations.ApiOperation) Consumes(javax.ws.rs.Consumes) Response.noContent(javax.ws.rs.core.Response.noContent) Response.status(javax.ws.rs.core.Response.status) URI(java.net.URI) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) StoreKey(org.commonjava.indy.model.core.StoreKey) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) ResponseHelper(org.commonjava.indy.bind.jaxrs.util.ResponseHelper) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) StoreType(org.commonjava.indy.model.core.StoreType) IndyResources(org.commonjava.indy.bind.jaxrs.IndyResources) Response.notModified(javax.ws.rs.core.Response.notModified) IOUtils(org.apache.commons.io.IOUtils) ApplicationContent(org.commonjava.indy.util.ApplicationContent) List(java.util.List) Response(javax.ws.rs.core.Response) Response.ok(javax.ws.rs.core.Response.ok) ApplicationScoped(javax.enterprise.context.ApplicationScoped) UriInfo(javax.ws.rs.core.UriInfo) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) HEAD(javax.ws.rs.HEAD) PathParam(javax.ws.rs.PathParam) REST(org.commonjava.indy.bind.jaxrs.util.REST) GET(javax.ws.rs.GET) ApplicationContent.application_json(org.commonjava.indy.util.ApplicationContent.application_json) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) ApiResponses(io.swagger.annotations.ApiResponses) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) MavenPackageTypeDescriptor(org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) SecurityManager(org.commonjava.indy.bind.jaxrs.SecurityManager) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) METADATA_CHANGELOG(org.commonjava.indy.model.core.ArtifactStore.METADATA_CHANGELOG) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Paths(java.nio.file.Paths) ApiResponse(io.swagger.annotations.ApiResponse) AdminController(org.commonjava.indy.core.ctl.AdminController) PUT(javax.ws.rs.PUT) InputStream(java.io.InputStream) StoreType(org.commonjava.indy.model.core.StoreType) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) IOException(java.io.IOException) StoreKey(org.commonjava.indy.model.core.StoreKey) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 50 with Context

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;
}
Also used : Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ApiParam(io.swagger.annotations.ApiParam) StoreListingDTO(org.commonjava.indy.model.core.dto.StoreListingDTO) ApiOperation(io.swagger.annotations.ApiOperation) Consumes(javax.ws.rs.Consumes) Response.noContent(javax.ws.rs.core.Response.noContent) Response.status(javax.ws.rs.core.Response.status) URI(java.net.URI) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) StoreKey(org.commonjava.indy.model.core.StoreKey) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) ResponseHelper(org.commonjava.indy.bind.jaxrs.util.ResponseHelper) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) StoreType(org.commonjava.indy.model.core.StoreType) IndyResources(org.commonjava.indy.bind.jaxrs.IndyResources) Response.notModified(javax.ws.rs.core.Response.notModified) IOUtils(org.apache.commons.io.IOUtils) ApplicationContent(org.commonjava.indy.util.ApplicationContent) List(java.util.List) Response(javax.ws.rs.core.Response) Response.ok(javax.ws.rs.core.Response.ok) ApplicationScoped(javax.enterprise.context.ApplicationScoped) UriInfo(javax.ws.rs.core.UriInfo) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) HEAD(javax.ws.rs.HEAD) PathParam(javax.ws.rs.PathParam) REST(org.commonjava.indy.bind.jaxrs.util.REST) GET(javax.ws.rs.GET) ApplicationContent.application_json(org.commonjava.indy.util.ApplicationContent.application_json) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) ApiResponses(io.swagger.annotations.ApiResponses) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) MavenPackageTypeDescriptor(org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) SecurityManager(org.commonjava.indy.bind.jaxrs.SecurityManager) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) METADATA_CHANGELOG(org.commonjava.indy.model.core.ArtifactStore.METADATA_CHANGELOG) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Paths(java.nio.file.Paths) ApiResponse(io.swagger.annotations.ApiResponse) AdminController(org.commonjava.indy.core.ctl.AdminController) PUT(javax.ws.rs.PUT) InputStream(java.io.InputStream) StoreType(org.commonjava.indy.model.core.StoreType) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) IOException(java.io.IOException) URI(java.net.URI) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Context (javax.ws.rs.core.Context)73 Response (javax.ws.rs.core.Response)54 Path (javax.ws.rs.Path)49 PathParam (javax.ws.rs.PathParam)42 GET (javax.ws.rs.GET)40 List (java.util.List)39 MediaType (javax.ws.rs.core.MediaType)36 POST (javax.ws.rs.POST)35 Produces (javax.ws.rs.Produces)32 UriInfo (javax.ws.rs.core.UriInfo)32 Inject (javax.inject.Inject)29 PUT (javax.ws.rs.PUT)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)27 QueryParam (javax.ws.rs.QueryParam)27 Collectors (java.util.stream.Collectors)26 Map (java.util.Map)25 Api (io.swagger.annotations.Api)24 ApiOperation (io.swagger.annotations.ApiOperation)24 IOException (java.io.IOException)24 URI (java.net.URI)24