Search in sources :

Example 26 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project coastal-hazards by USGS-CIDA.

the class TemplateResource method instantiateTemplate.

@POST
@Path("/item/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response instantiateTemplate(@Context HttpServletRequest request, @PathParam("id") String id, String content) {
    Response response = null;
    try (ItemManager itemMan = new ItemManager();
        LayerManager layerMan = new LayerManager()) {
        Item template = itemMan.load(id);
        if (template.getItemType() != Item.ItemType.template) {
            throw new UnsupportedOperationException("Only template items may be instantiated");
        }
        List<Item> childItems = template.getChildren();
        List<Item> newItemList = null;
        List<Item> newAndOldList = null;
        List<Item> retainedItems = new LinkedList<>();
        List<String> displayed = new LinkedList<>();
        JsonParser parser = new JsonParser();
        JsonObject parsed = parser.parse(content).getAsJsonObject();
        // TODO only supporting one level for now, bring in aggs later
        boolean allAttributes = parseAllAttribute(parsed);
        boolean retainAggregations = retainAggregations(parsed);
        if (allAttributes) {
            JsonElement layer = parsed.get("layerId");
            if (layer != null) {
                String layerId = layer.getAsString();
                try {
                    newItemList = makeItemsFromLayer(template, layerId, layerMan);
                    retainedItems = findItemsToRetain(template, retainAggregations);
                    newAndOldList = new LinkedList<>(retainedItems);
                    newAndOldList.addAll(newItemList);
                } catch (IOException ex) {
                    log.error("Cannot create items", ex);
                }
                for (Item retained : retainedItems) {
                    displayed.add(retained.getId());
                }
                List<String> displayedIdByAttr = makeDisplayedChildren(newItemList);
                displayed.addAll(displayedIdByAttr);
            }
        } else {
            Map<String, Item> childMap = makeChildItemMap(childItems);
            JsonArray children = parsed.get("children").getAsJsonArray();
            newItemList = makeItemsFromDocument(template, children, childMap, itemMan, layerMan);
            List<String> visibleItems = visibleItems(children, newItemList, childMap);
            displayed.addAll(visibleItems);
            newAndOldList = newItemList;
        }
        itemMan.persistAll(newItemList);
        template.setChildren(newAndOldList);
        template.setDisplayedChildren(displayed);
        template.setSummary(gatherTemplateSummary(template.getSummary(), newItemList));
        String mergeId = itemMan.merge(template);
        if (mergeId != null) {
            response = Response.ok().build();
            try (StatusManager statusMan = new StatusManager()) {
                Status status = new Status();
                status.setStatusName(Status.StatusName.ITEM_UPDATE);
                statusMan.save(status);
            }
        } else {
            response = Response.serverError().build();
        }
    }
    return response;
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(gov.usgs.cida.coastalhazards.model.util.Status) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) LayerManager(gov.usgs.cida.coastalhazards.jpa.LayerManager) JsonParser(com.google.gson.JsonParser) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 27 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project coastal-hazards by USGS-CIDA.

the class CacheResource method deleteCache.

@Path("/")
@DELETE
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response deleteCache() {
    Response response = null;
    try (StatusManager statusMan = new StatusManager()) {
        if (clearCache()) {
            Status clearedStatus = new Status();
            clearedStatus.setStatusName(Status.StatusName.CACHE_CLEAR);
            statusMan.save(clearedStatus);
            response = Response.ok().build();
        } else {
            response = Response.serverError().entity("Error clearing cache").build();
        }
    } catch (Exception e) {
        log.error("Unable to clear cache", e);
        response = Response.serverError().entity("Unable to clear cache").build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 28 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project coastal-hazards by USGS-CIDA.

the class DynamicRolesLoginRedirectFeature method configure.

@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) {
    AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
    // DenyAll on the method take precedence over RolesAllowed and PermitAll
    if (am.isAnnotationPresent(DenyAll.class)) {
        configuration.register(new RolesAllowedPastLoginFilter());
        return;
    }
    // RolesAllowed on the method takes precedence over PermitAll
    RolesAllowed ra = am.getAnnotation(RolesAllowed.class);
    if (ra != null) {
        configuration.register(new RolesAllowedPastLoginFilter(ra.value()));
        return;
    }
    // PermitAll takes precedence over RolesAllowed on the class
    if (am.isAnnotationPresent(PermitAll.class)) {
        // Do nothing.
        return;
    }
    // DenyAll can't be attached to classes
    // RolesAllowed on the class takes precedence over PermitAll
    ra = resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class);
    if (ra != null) {
        configuration.register(new RolesAllowedPastLoginFilter(ra.value()));
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod)

Example 29 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project OpenTripPlanner by opentripplanner.

the class Routers method putGraphId.

/**
 * Load the graph for the specified routerId from disk.
 * @param preEvict before reloading each graph, evict the existing graph. This will prevent
 * memory usage from increasing during the reload, but routing will be unavailable on this
 * routerId for the duration of the operation.
 */
@RolesAllowed({ "ROUTERS" })
@PUT
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
public Response putGraphId(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict) {
    LOG.debug("Attempting to load graph '{}' from server's local filesystem.", routerId);
    GraphService graphService = otpServer.getGraphService();
    if (graphService.getRouterIds().contains(routerId)) {
        boolean success = graphService.reloadGraph(routerId, preEvict, false);
        if (success)
            return Response.status(201).entity("graph already registered, reloaded.\n").build();
        else
            return Response.status(404).entity("graph already registered, but reload failed.\n").build();
    } else {
        boolean success = graphService.registerGraph(routerId, graphService.getGraphSourceFactory().createGraphSource(routerId));
        if (success)
            return Response.status(201).entity("graph registered.\n").build();
        else
            return Response.status(404).entity("graph not found or other error.\n").build();
    }
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 30 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project OpenTripPlanner by opentripplanner.

the class Routers method postGraphOverWire.

/**
 * Deserialize a graph sent with the HTTP request as POST data, associating it with the given
 * routerId.
 */
@RolesAllowed({ "ROUTERS" })
@POST
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response postGraphOverWire(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict, @QueryParam("loadLevel") @DefaultValue("FULL") LoadLevel level, InputStream is) {
    if (preEvict) {
        LOG.debug("pre-evicting graph");
        otpServer.getGraphService().evictRouter(routerId);
    }
    LOG.debug("deserializing graph from POST data stream...");
    Graph graph;
    try {
        graph = Graph.load(is, level);
        GraphService graphService = otpServer.getGraphService();
        graphService.registerGraph(routerId, new MemoryGraphSource(routerId, graph));
        return Response.status(Status.CREATED).entity(graph.toString() + "\n").build();
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity(e.toString() + "\n").build();
    }
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) GraphNotFoundException(org.opentripplanner.routing.error.GraphNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Aggregations

RolesAllowed (javax.annotation.security.RolesAllowed)191 Path (javax.ws.rs.Path)127 Produces (javax.ws.rs.Produces)110 Consumes (javax.ws.rs.Consumes)55 GET (javax.ws.rs.GET)54 POST (javax.ws.rs.POST)40 PUT (javax.ws.rs.PUT)35 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)32 IOException (java.io.IOException)30 ApiOperation (io.swagger.annotations.ApiOperation)29 ApiResponses (io.swagger.annotations.ApiResponses)29 Response (javax.ws.rs.core.Response)28 Adapter (nl.nn.adapterframework.core.Adapter)21 DELETE (javax.ws.rs.DELETE)19 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)19 LinkedHashMap (java.util.LinkedHashMap)16 Locale (java.util.Locale)16 Map (java.util.Map)12 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)12