Example usage for org.springframework.http HttpStatus FORBIDDEN

List of usage examples for org.springframework.http HttpStatus FORBIDDEN

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus FORBIDDEN.

Prototype

HttpStatus FORBIDDEN

To view the source code for org.springframework.http HttpStatus FORBIDDEN.

Click Source Link

Document

403 Forbidden .

Usage

From source file:org.finra.dm.service.helper.DmErrorInformationExceptionHandler.java

/**
 * Handle access denied exceptions./*from   ww  w  .j a  v a 2  s.c o m*/
 *
 * @param exception the exception.
 *
 * @return the error information.
 */
@ExceptionHandler(value = AccessDeniedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public ErrorInformation handleAccessDeniedException(Exception exception) {
    return getErrorInformation(HttpStatus.FORBIDDEN, exception);
}

From source file:org.geoserver.importer.rest.ImportTaskController.java

@PostMapping(consumes = { MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public Object taskPost(@PathVariable Long id, @RequestParam(required = false) String expand,
        HttpServletRequest request, HttpServletResponse response) {
    ImportData data = null;//w  ww  . j  a  va  2s  .c  o  m

    LOGGER.info("Handling POST of " + request.getContentType());
    //file posted from form
    MediaType mimeType = MediaType.valueOf(request.getContentType());
    if (request.getContentType().startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) {
        data = handleMultiPartFormUpload(request, context(id));
    } else if (request.getContentType().startsWith(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
        try {
            data = handleFormPost(request);
        } catch (IOException | ServletException e) {
            throw new RestException(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e);
        }
    }

    if (data == null) {
        throw new RestException("Unsupported POST", HttpStatus.FORBIDDEN);
    }

    //Construct response
    return acceptData(data, context(id), response, expand);
}

From source file:org.geoserver.rest.catalog.StyleController.java

@PostMapping(value = { "/styles", "/workspaces/{workspaceName}/styles" }, consumes = { SLDHandler.MIMETYPE_11,
        SLDHandler.MIMETYPE_10 })//from w  w w  .j ava  2  s. c  om
public ResponseEntity<String> styleSLDPost(@RequestBody Style style,
        @PathVariable(required = false) String workspaceName, @RequestParam(required = false) String name,
        @RequestHeader("Content-Type") String contentType, UriComponentsBuilder builder) {

    if (workspaceName != null && catalog.getWorkspaceByName(workspaceName) == null) {
        throw new ResourceNotFoundException("Workspace " + workspaceName + " not found");
    }
    checkFullAdminRequired(workspaceName);

    StyleHandler handler = org.geoserver.catalog.Styles.handler(contentType);
    if (name == null) {
        name = findNameFromObject(style);
    }

    //ensure that the style does not already exist
    if (catalog.getStyleByName(workspaceName, name) != null) {
        throw new RestException("Style " + name + " already exists.", HttpStatus.FORBIDDEN);
    }

    StyleInfo sinfo = catalog.getFactory().createStyle();
    sinfo.setName(name);
    sinfo.setFilename(name + "." + handler.getFileExtension());
    sinfo.setFormat(handler.getFormat());
    sinfo.setFormatVersion(handler.versionForMimeType(contentType));

    if (workspaceName != null) {
        sinfo.setWorkspace(catalog.getWorkspaceByName(workspaceName));
    }

    // ensure that a existing resource does not already exist, because we may not want to overwrite it
    GeoServerDataDirectory dataDir = new GeoServerDataDirectory(catalog.getResourceLoader());
    if (dataDir.style(sinfo).getType() != Resource.Type.UNDEFINED) {
        String msg = "Style resource " + sinfo.getFilename() + " already exists.";
        throw new RestException(msg, HttpStatus.FORBIDDEN);
    }

    ResourcePool resourcePool = catalog.getResourcePool();
    try {
        if (style instanceof Style) {
            resourcePool.writeStyle(sinfo, (Style) style);
        } else {
            resourcePool.writeStyle(sinfo, (InputStream) style);
        }
    } catch (IOException e) {
        throw new RestException("Error writing style", HttpStatus.INTERNAL_SERVER_ERROR, e);
    }

    catalog.add(sinfo);
    LOGGER.info("POST Style " + name);
    //build the new path
    UriComponents uriComponents = getUriComponents(name, workspaceName, builder);
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uriComponents.toUri());
    return new ResponseEntity<String>(name, headers, HttpStatus.CREATED);
}

From source file:org.geoserver.rest.catalog.StyleController.java

@DeleteMapping(path = { "/styles/{styleName}", "/workspaces/{workspaceName}/styles/{styleName}" })
protected void styleDelete(@PathVariable String styleName, @PathVariable(required = false) String workspaceName,
        @RequestParam(required = false, defaultValue = "false") boolean recurse,
        @RequestParam(required = false, defaultValue = "false") boolean purge) throws IOException {

    if (workspaceName != null && catalog.getWorkspaceByName(workspaceName) == null) {
        throw new ResourceNotFoundException("Workspace " + workspaceName + " not found");
    }//from   w ww. j  a  va2s  .c  om

    StyleInfo style = workspaceName != null ? catalog.getStyleByName(workspaceName, styleName)
            : catalog.getStyleByName(styleName);

    if (style == null) {
        throw new ResourceNotFoundException("Style " + styleName + " not found");
    }

    if (recurse) {
        new CascadeDeleteVisitor(catalog).visit(style);
    } else {
        // ensure that no layers reference the style
        List<LayerInfo> layers = catalog.getLayers(style);
        if (!layers.isEmpty()) {
            throw new RestException("Can't delete style referenced by existing layers.", HttpStatus.FORBIDDEN);
        }
        catalog.remove(style);
    }

    catalog.getResourcePool().deleteStyle(style, purge);

    LOGGER.info("DELETE style " + styleName);
}

From source file:org.geoserver.rest.catalog.StyleController.java

@PostMapping(value = { "/styles", "/workspaces/{workspaceName}/styles" }, consumes = {
        MediaTypeExtensions.APPLICATION_ZIP_VALUE })
public ResponseEntity<String> stylePost(InputStream stream, @RequestParam(required = false) String name,
        @PathVariable(required = false) String workspaceName, UriComponentsBuilder builder) throws IOException {

    if (workspaceName != null && catalog.getWorkspaceByName(workspaceName) == null) {
        throw new ResourceNotFoundException("Workspace " + workspaceName + " not found");
    }/*from   ww w. j ava  2  s . c om*/
    checkFullAdminRequired(workspaceName);

    File directory = unzipSldPackage(stream);
    File uploadedFile = retrieveSldFile(directory);

    Style styleSld = parseSld(uploadedFile);

    if (name == null) {
        name = findNameFromObject(styleSld);
    }

    //ensure that the style does not already exist
    if (catalog.getStyleByName(workspaceName, name) != null) {
        throw new RestException("Style " + name + " already exists.", HttpStatus.FORBIDDEN);
    }

    // save image resources
    saveImageResources(directory, workspaceName);

    //create a style info object
    StyleInfo styleInfo = catalog.getFactory().createStyle();
    styleInfo.setName(name);
    styleInfo.setFilename(name + ".sld");

    if (workspaceName != null) {
        styleInfo.setWorkspace(catalog.getWorkspaceByName(workspaceName));
    }

    Resource style = dataDir.style(styleInfo);
    // ensure that a existing resource does not already exist, because we may not want to overwrite it
    if (dataDir.style(styleInfo).getType() != Resource.Type.UNDEFINED) {
        String msg = "Style resource " + styleInfo.getFilename() + " already exists.";
        throw new RestException(msg, HttpStatus.FORBIDDEN);
    }

    serializeSldFileInCatalog(style, uploadedFile);

    catalog.add(styleInfo);

    LOGGER.info("POST Style Package: " + name + ", workspace: " + workspaceName);
    UriComponents uriComponents = getUriComponents(name, workspaceName, builder);
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uriComponents.toUri());
    return new ResponseEntity<>(name, headers, HttpStatus.CREATED);
}

From source file:org.geoserver.rest.catalog.StyleController.java

@PutMapping(value = { "/styles/{styleName}", "/workspaces/{workspaceName}/styles/{styleName}" }, consumes = {
        MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
        MediaTypeExtensions.TEXT_JSON_VALUE })
public void stylePut(@RequestBody StyleInfo info, @PathVariable String styleName,
        @PathVariable(required = false) String workspaceName) {

    if (workspaceName != null && catalog.getWorkspaceByName(workspaceName) == null) {
        throw new ResourceNotFoundException("Workspace " + workspaceName + " not found");
    }/*w  w  w.j  a  v  a 2  s .  com*/
    checkFullAdminRequired(workspaceName);

    StyleInfo original = catalog.getStyleByName(workspaceName, styleName);

    //ensure no workspace change
    if (info.getWorkspace() != null) {
        if (!info.getWorkspace().equals(original.getWorkspace())) {
            throw new RestException("Can't change the workspace of a style, instead "
                    + "DELETE from existing workspace and POST to new workspace", HttpStatus.FORBIDDEN);
        }
    }

    new CatalogBuilder(catalog).updateStyle(original, info);
    catalog.save(original);
}

From source file:org.geoserver.rest.catalog.StyleController.java

/**
 * Returns the sld file in the given directory. If no sld file, throws an exception
 *
 * @param directory/*w  w  w  . j  a v a2s  .  c  o  m*/
 *
 */
private File retrieveSldFile(File directory) {
    File[] matchingFiles = directory.listFiles((dir, name) -> name.endsWith("sld"));

    if (matchingFiles.length == 0) {
        throw new RestException("No sld file provided:", HttpStatus.FORBIDDEN);
    }

    LOGGER.fine("retrieveSldFile (sldFile): " + matchingFiles[0].getAbsolutePath());

    return matchingFiles[0];
}

From source file:org.geoserver.rest.catalog.StyleController.java

private void putZipInternal(InputStream is, String workspace, String name, String style) {
    if (workspace != null && catalog.getWorkspaceByName(workspace) == null) {
        throw new ResourceNotFoundException("Workspace " + workspace + " not found");
    }/*  w  ww.ja va2 s .  co  m*/
    checkFullAdminRequired(workspace);

    File directory = null;
    try {
        directory = unzipSldPackage(is);
        File uploadedFile = retrieveSldFile(directory);

        Style styleSld = parseSld(uploadedFile);

        if (name == null) {
            name = findNameFromObject(styleSld);
        }

        if (name == null) {
            throw new RestException("Style must have a name.", HttpStatus.BAD_REQUEST);
        }

        //ensure that the style does already exist
        if (!existsStyleInCatalog(workspace, name)) {
            throw new RestException("Style " + name + " doesn't exists.", HttpStatus.FORBIDDEN);
        }

        // save image resources
        saveImageResources(directory, workspace);

        // Save the style: serialize the style out into the data directory
        StyleInfo styleInfo = catalog.getStyleByName(workspace, style);
        serializeSldFileInCatalog(dataDir.style(styleInfo), uploadedFile);

        LOGGER.info("PUT Style Package: " + name + ", workspace: " + workspace);

    } catch (Exception e) {
        LOGGER.severe("Error processing the style package (PUT): " + e.getMessage());
        throw new RestException("Error processing the style", HttpStatus.INTERNAL_SERVER_ERROR, e);
    } finally {
        FileUtils.deleteQuietly(directory);
    }
}

From source file:org.geoserver.rest.security.AbstractAclController.java

protected void checkUserIsAdmin() {
    if (!getManager().checkAuthenticationForAdminRole()) {
        throw new RestException("Amdinistrative priveleges required", HttpStatus.FORBIDDEN);
    }/*ww w. j ava2  s .  c o m*/
}

From source file:org.geoserver.rest.security.MasterPasswordController.java

@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaTypeExtensions.TEXT_JSON_VALUE,
        MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
public NamedMap<String, String> masterPasswordGet() throws IOException {

    if (!getManager().checkAuthenticationForAdminRole()) {
        throw new RestException("Amdinistrative privelges required", HttpStatus.FORBIDDEN);
    }/*from   www  . j a  v a  2  s . com*/

    char[] masterpw = getManager().getMasterPasswordForREST();

    NamedMap<String, String> m = new NamedMap<>(XML_ROOT_ELEM);
    m.put(MP_CURRENT_KEY, new String(masterpw));

    getManager().disposePassword(masterpw);
    return m;
}