Search in sources :

Example 21 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project ff4j by ff4j.

the class FF4jAuthorizationFilter method getRoles.

private Set<String> getRoles() {
    Set<String> roles = new HashSet<String>();
    RolesAllowed ra1 = info.getResourceClass().getAnnotation(RolesAllowed.class);
    if (ra1 != null) {
        roles.addAll(Arrays.asList(ra1.value()));
    }
    RolesAllowed ra2 = info.getResourceMethod().getAnnotation(RolesAllowed.class);
    if (ra2 != null) {
        roles.addAll(Arrays.asList(ra2.value()));
    }
    return roles;
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) HashSet(java.util.HashSet)

Example 22 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project openscoring by openscoring.

the class ModelResource method undeploy.

@DELETE
@Path("{id:" + ModelRegistry.ID_REGEX + "}")
@RolesAllowed(value = { "admin" })
@Produces(MediaType.APPLICATION_JSON)
public SimpleResponse undeploy(@PathParam("id") String id) {
    Model model = this.modelRegistry.get(id);
    if (model == null) {
        throw new NotFoundException("Model \"" + id + "\" not found");
    }
    boolean success = this.modelRegistry.remove(id, model);
    if (!success) {
        throw new InternalServerErrorException("Concurrent modification");
    }
    final String prefix = createNamePrefix(id);
    MetricFilter filter = new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return name.startsWith(prefix);
        }
    };
    this.metricRegistry.removeMatching(filter);
    SimpleResponse response = new SimpleResponse();
    return response;
}
Also used : MetricFilter(com.codahale.metrics.MetricFilter) SimpleResponse(org.openscoring.common.SimpleResponse) NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Metric(com.codahale.metrics.Metric) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces)

Example 23 with RolesAllowed

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

the class ItemResource method postItem.

/**
 * Only allows one card to be posted at a time for now
 *
 * @param content Posted content as text string (should be JSON)
 * @param request passed through context of request
 * @return
 */
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postItem(String content, @Context HttpServletRequest request) {
    Response response;
    Item item = Item.fromJSON(content);
    final String id;
    try (ItemManager itemManager = new ItemManager()) {
        id = itemManager.persist(item);
    }
    if (null == id) {
        throw new BadRequestException();
    } else {
        Map<String, Object> ok = new HashMap<String, Object>() {

            private static final long serialVersionUID = 2398472L;

            {
                put("id", id);
            }
        };
        response = Response.ok(GsonUtil.getDefault().toJson(ok, HashMap.class), MediaType.APPLICATION_JSON_TYPE).build();
    }
    try (StatusManager statusMan = new StatusManager();
        ThumbnailManager thumbMan = new ThumbnailManager()) {
        Status status = new Status();
        status.setStatusName(Status.StatusName.ITEM_UPDATE);
        statusMan.save(status);
        thumbMan.updateDirtyBits(id);
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) Item(gov.usgs.cida.coastalhazards.model.Item) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ThumbnailManager(gov.usgs.cida.coastalhazards.jpa.ThumbnailManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) HashMap(java.util.HashMap) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 24 with RolesAllowed

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

the class MetadataResource method acceptMetadata.

@POST
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response acceptMetadata(@Context HttpServletRequest req) throws IOException {
    int maxFileSize = FILE_UPLOAD_MAX_SIZE;
    int fileSize = Integer.parseInt(req.getHeader("Content-Length"));
    File tempFile = File.createTempFile(UUID.randomUUID().toString(), "temp");
    Map<String, String> responseContent = new HashMap<>();
    String fileName;
    if (maxFileSize > 0 && fileSize > maxFileSize) {
        responseContent.put("message", "File too large");
        responseContent.put("success", "false");
        return Response.notAcceptable(null).entity(responseContent).build();
    }
    try {
        FileUtils.forceMkdir(UPLOAD_DIR);
    } catch (IOException ex) {
        return Response.serverError().build();
    }
    try {
        FormUploadHandler.saveFileFromRequest(req, FILENAME_PARAM, tempFile);
    } catch (FileUploadException | IOException ex) {
        responseContent.put("message", ex.getMessage());
        responseContent.put("success", "false");
        return Response.serverError().entity(responseContent).build();
    }
    try {
        fileName = StringHelper.makeSHA1Hash(IOUtils.toString(new FileInputStream(tempFile)));
    } catch (NoSuchAlgorithmException ex) {
        responseContent.put("message", ex.getMessage());
        responseContent.put("success", "false");
        return Response.serverError().entity(responseContent).build();
    }
    File savedFile = new File(UPLOAD_DIR, fileName);
    if (savedFile.exists()) {
        responseContent.put("fid", fileName);
    } else {
        FileUtils.moveFile(tempFile, savedFile);
        responseContent.put("fid", fileName);
    }
    responseContent.put("success", "true");
    return Response.ok(GsonUtil.getDefault().toJson(responseContent, HashMap.class), MediaType.APPLICATION_JSON_TYPE).build();
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException) FileInputStream(java.io.FileInputStream) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 25 with RolesAllowed

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

the class TemplateResource method instantiateStormTemplate.

@GET
@Path("/storm")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response instantiateStormTemplate(@Context HttpServletRequest request, @QueryParam("layerId") String layerId, @QueryParam("activeStorm") String active, @QueryParam("alias") String alias, @QueryParam("copyType") String copyType, @QueryParam("copyVal") String copyVal, @QueryParam("trackId") String trackId) {
    Response response;
    if (layerId != null && active != null) {
        Gson gson = GsonUtil.getDefault();
        String childJson = null;
        childJson = gson.toJson(StormUtil.createStormChildMap(layerId, Boolean.parseBoolean(active), trackId));
        if (childJson != null && childJson.length() > 0) {
            try (ItemManager itemMan = new ItemManager();
                LayerManager layerMan = new LayerManager();
                AliasManager aliasMan = new AliasManager()) {
                Layer layer = layerMan.load(layerId);
                if (layer != null) {
                    Summary summary = null;
                    if (copyType.equalsIgnoreCase("item") || copyType.equalsIgnoreCase("alias")) {
                        summary = copyExistingSummary(copyType, copyVal, itemMan, aliasMan);
                    } else {
                        summary = StormUtil.buildStormTemplateSummary(layer);
                    }
                    if (summary != null) {
                        List<Service> services = layer.getServices();
                        List<Service> serviceCopies = new LinkedList<>();
                        for (Service service : services) {
                            serviceCopies.add(Service.copyValues(service, new Service()));
                        }
                        Item baseTemplate = baseTemplateItem(Boolean.parseBoolean(active), layer.getBbox(), serviceCopies, summary);
                        String templateId = itemMan.persist(baseTemplate);
                        if (templateId != null && templateId.length() > 0) {
                            response = instantiateTemplate(request, templateId, childJson);
                            if (response.getStatus() == HttpStatus.SC_OK) {
                                Map<String, Object> ok = new HashMap<String, Object>() {

                                    private static final long serialVersionUID = 2398472L;

                                    {
                                        put("id", templateId);
                                    }
                                };
                                if (alias != null && alias.length() > 0) {
                                    Alias fullAlias = aliasMan.load(alias);
                                    if (fullAlias != null) {
                                        fullAlias.setItemId(templateId);
                                        aliasMan.update(fullAlias);
                                    } else {
                                        fullAlias = new Alias();
                                        fullAlias.setId(alias);
                                        fullAlias.setItemId(templateId);
                                        aliasMan.save(fullAlias);
                                    }
                                }
                                response = Response.ok(GsonUtil.getDefault().toJson(ok, HashMap.class), MediaType.APPLICATION_JSON_TYPE).build();
                            }
                        } else {
                            response = Response.status(500).build();
                        }
                    } else {
                        response = Response.status(400).build();
                    }
                } else {
                    response = Response.status(400).build();
                }
            } catch (Exception e) {
                log.error(e.toString());
                response = Response.status(500).build();
            }
        } else {
            response = Response.status(400).build();
        }
    } else {
        response = Response.status(400).build();
    }
    return response;
}
Also used : ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Gson(com.google.gson.Gson) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) OGCService(gov.usgs.cida.coastalhazards.util.ogc.OGCService) Service(gov.usgs.cida.coastalhazards.model.Service) Layer(gov.usgs.cida.coastalhazards.model.Layer) LinkedList(java.util.LinkedList) URISyntaxException(java.net.URISyntaxException) BadRequestException(javax.ws.rs.BadRequestException) SAXException(org.xml.sax.SAXException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Response(javax.ws.rs.core.Response) AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Item(gov.usgs.cida.coastalhazards.model.Item) Alias(gov.usgs.cida.coastalhazards.model.Alias) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) JsonObject(com.google.gson.JsonObject) LayerManager(gov.usgs.cida.coastalhazards.jpa.LayerManager) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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