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