protected RestException createRestException(Exception ex) { if (ex instanceof RestException) { return (RestException) ex; // do nothing } else { return new RestException("", HttpStatus.INTERNAL_SERVER_ERROR, ex); } } }
protected void validateMin(Integer value, int min, String name) { if (value != null && value < min) { throw new RestException( "Invalid parameter " + name + ", should be at least " + min, HttpStatus.BAD_REQUEST); } }
protected void validateMax(Integer value, int max, String name) { if (value != null && value > max) { throw new RestException( "Invalid parameter " + name + ", should be at most " + max, HttpStatus.BAD_REQUEST); } }
@GetMapping() public void passwordGet() { throw new RestException("You can not request the password!", HttpStatus.METHOD_NOT_ALLOWED); }
/** * Returns the sld file in the given directory. If no sld file, throws an appropriate exception * * @param directory The directory containing the sld file * @throws RestException it the sld file does not exist */ private File getSldFileFromDirectory(File directory) throws RestException { File[] matchingFiles = directory.listFiles((dir, name) -> name.endsWith("sld")); if (matchingFiles == null || matchingFiles.length == 0) { throw new RestException("No sld file provided:", HttpStatus.FORBIDDEN); } LOGGER.fine("getSldFileFromDirectory (sldFile): " + matchingFiles[0].getAbsolutePath()); return matchingFiles[0]; }
/** * Validates the string representation of rule keys and values * * @param ruleMap */ protected void validateMap(Map<String, String> ruleMap) { for (Entry<String, String> entry : ruleMap.entrySet()) { String msg = validateRule(entry.getKey(), entry.getValue()); if (msg != null) { throw new RestException(msg, HttpStatus.UNPROCESSABLE_ENTITY); } } }
/** * Validates the current user can edit the resource (full admin required if workspaceName is * null) * * @param workspaceName */ protected void checkFullAdminRequired(String workspaceName) { // global workspaces/styles can only be edited by a full admin if (workspaceName == null && !isAuthenticatedAsAdmin()) { throw new RestException( "Cannot edit global resource , full admin credentials required", HttpStatus.METHOD_NOT_ALLOWED); } } }
@GetMapping(value = "/{target}") protected RestWrapper<ModuleStatus> statusGet(@PathVariable String target) throws Exception { List<ModuleStatus> applicationStatus = GeoServerExtensions.extensions(ModuleStatus.class) .stream() .map(ModuleStatusImpl::new) .filter(getModule(target)) .collect(Collectors.toList()); if (applicationStatus.isEmpty()) { throw new RestException("No such module: " + target, HttpStatus.NOT_FOUND); } return wrapList(applicationStatus, ModuleStatus.class); }
private GranuleRemovalPolicy mapRemovalPolicy(String key) { try { return GranuleRemovalPolicy.valueOf(key.toUpperCase()); } catch (Exception e) { throw new RestException( "Invalid purge value " + key + ", allowed values are " + Arrays.toString(GranuleRemovalPolicy.values()), HttpStatus.BAD_REQUEST); } }
protected SimpleFeature parseGeoJSONFeature(String fileReference, final byte[] payload) { try { SimpleFeature jsonFeature = new FeatureJSON().readFeature(new ByteArrayInputStream(payload)); return jsonFeature; } catch (IOException e) { throw new RestException( fileReference + " contains invalid GeoJSON: " + e.getMessage(), HttpStatus.BAD_REQUEST, e); } }
protected void checkUserIsAdmin() { if (!getManager().checkAuthenticationForAdminRole()) { throw new RestException("Amdinistrative priveleges required", HttpStatus.FORBIDDEN); } }
protected void checkUserIsAdmin() { if (!getManager().checkAuthenticationForAdminRole()) { throw new RestException("Amdinistrative priveleges required", HttpStatus.FORBIDDEN); } }
private Hints getQueryHints(String viewParams) { if (viewParams != null && !viewParams.isEmpty()) { FormatOptionsKvpParser parser = new FormatOptionsKvpParser(); Map<String, String> params; try { params = (Map<String, String>) parser.parse(viewParams); return new Hints(Hints.VIRTUAL_TABLE_PARAMETERS, params); } catch (Exception e) { throw new RestException("Invalid viewparams", HttpStatus.INTERNAL_SERVER_ERROR); } } return null; }
protected void postMap(Map map) throws Exception { validateMap(map); Set<Object> commonKeys = intersection(map); if (!commonKeys.isEmpty()) { String msg = "Already existing rules: " + StringUtils.join(commonKeys.iterator(), ","); throw new RestException(msg, HttpStatus.CONFLICT); } for (Object entry : map.entrySet()) { Comparable rule = convertEntryToRule((Entry<String, String>) entry); ruleDAO.addRule(rule); } ruleDAO.storeRules(); }
@DeleteMapping(value = "/workspaces/{workspaceName}/settings") public void serviceDelete(@PathVariable String workspaceName) { WorkspaceInfo ws = geoServer.getCatalog().getWorkspaceByName(workspaceName); if (ws == null) { throw new RestException( "Workspace " + workspaceName + " does not exist", HttpStatus.NOT_FOUND); } ServiceInfo serviceInfo = geoServer.getService(ws, clazz); if (serviceInfo != null) { geoServer.remove(serviceInfo); } }
protected void putMap(Map map) throws Exception { validateMap(map); Set<Object> nonExisting = nonExistingKeys(map); if (!nonExisting.isEmpty()) { String msg = "Unknown rules: " + StringUtils.join(nonExisting.iterator(), ","); throw new RestException(msg, HttpStatus.CONFLICT); } for (Object entry : map.entrySet()) { Comparable rule = convertEntryToRule((Entry<String, String>) entry); // TODO, will not work for REST ruleDAO.removeRule(rule); ruleDAO.addRule(rule); } ruleDAO.storeRules(); }
@GetMapping public void handleGet( @RequestParam(name = "message", required = false) String message, @RequestParam(name = "code", required = false) Integer code) { throw new RestException( message != null ? message : "Unknown error", code != null ? HttpStatus.valueOf(code) : HttpStatus.INTERNAL_SERVER_ERROR); } }
protected ServiceInfo getServiceInfo(String workspaceName) { ServiceInfo service; if (workspaceName != null) { WorkspaceInfo ws = geoServer.getCatalog().getWorkspaceByName(workspaceName); if (ws == null) { throw new RestException( "Workspace " + workspaceName + " does not exist", HttpStatus.NOT_FOUND); } service = geoServer.getService(ws, WMSInfo.class); } else { service = geoServer.getService(WMSInfo.class); } return service; }
protected WFSInfo getServiceInfo(String workspaceName) { WFSInfo service; if (workspaceName != null) { WorkspaceInfo ws = geoServer.getCatalog().getWorkspaceByName(workspaceName); if (ws == null) { throw new RestException( "Workspace " + workspaceName + " does not exist", HttpStatus.NOT_FOUND); } service = geoServer.getService(ws, WFSInfo.class); } else { service = geoServer.getService(WFSInfo.class); } return service; }
/** Delete resource */ @DeleteMapping public void resourceDelete(HttpServletRequest request) { Resource resource = resource(request); if (Type.UNDEFINED.equals(resource.getType())) { throw new ResourceNotFoundException("Resource '" + resource.path() + "' not found"); } boolean removed = resource.delete(); if (!removed) { throw new RestException( "Resource '" + resource.path() + "' not removed", HttpStatus.INTERNAL_SERVER_ERROR); } }