/** * Gets the query param, or returns default value * * @param queryParam the query parameter * @param defaultValue the default value * @return the value of the provided queryParam, or default if value is null * Example: query parameter 'id' from the following request URI: /hello?id=foo */ public String queryParamOrDefault(String queryParam, String defaultValue) { String value = queryParams(queryParam); return value != null ? value : defaultValue; }
@Override public String queryParams(String queryParam) { return delegate.queryParams(queryParam); }
@Override public Set<String> queryParams() { return delegate.queryParams(); }
private String getViewName(Request request) { final String viewName = request.queryParams(VIEW_NAME); return StringUtils.isBlank(viewName) ? DEFAULT_NAME : viewName; } }
private String getViewName(Request request) { final String viewName = request.queryParams(VIEW_NAME); return StringUtils.isBlank(viewName) ? DEFAULT_NAME : viewName; } }
private String requiredQueryParam(final Request req, final String name) { String value = req.queryParams(name); if (StringUtils.isBlank(value)) { throw HaltApiResponses.haltBecauseRequiredParamMissing(name); } return value; } }
private void checkPipelineExists(Request request, Response response) { if (isPipelineRequest(request)) { if (null == pipelineConfigService.pipelineConfigNamed(request.queryParams("pipeline_name"))) { throw halt(404, format("Cannot generate analytics. Pipeline with name: '%s' not found.", request.queryParams("pipeline_name"))); } } }
private CaseInsensitiveString getPipelineNameFromRequest(Request request) { String pipelineName = request.params("pipeline_name"); if (StringUtils.isBlank(pipelineName)) { pipelineName = request.queryParams("pipeline_name"); } return new CaseInsensitiveString(pipelineName); }
private ConfigRepoConfig repoFromRequest(Request req) { String repoId = req.queryParams("repoId"); if (null == repoId) { return null; } ConfigRepoConfig repo = service.getConfigRepo(repoId); if (null == repo) { throw HaltApiResponses.haltBecauseNotFound("Could not find a config-repo with id `%s`", repoId); } return repo; } }
private ConfigRepoPlugin pluginFromRequest(Request req) { String pluginId = req.queryParams("pluginId"); if (StringUtils.isBlank(pluginId)) { throw HaltApiResponses.haltBecauseRequiredParamMissing("pluginId"); } return (ConfigRepoPlugin) pluginService.partialConfigProviderFor(pluginId); }
public String search(Request request, Response response) throws IOException { String pipelineName = request.queryParams("pipeline_name"); String fingerprint = request.queryParams("fingerprint"); String searchText = request.queryParamOrDefault("search_text", ""); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); List<MatchedRevision> matchedRevisions = materialService.searchRevisions(pipelineName, fingerprint, searchText, currentUsername(), result); if (result.isSuccessful()) { return writerForTopLevelArray(request, response, outputListWriter -> MatchedRevisionRepresenter.toJSON(outputListWriter, matchedRevisions)); } else { return renderHTTPOperationResult(result, request, response); } } }
public String show(Request req, Response res) throws IOException { HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); String searchTerm = req.queryParams("q"); if (isBlank(searchTerm)) { throw haltBecauseOfReason("Search term not specified!"); } List<UserSearchModel> userSearchModels = userSearchService.search(searchTerm, result); if (result.isSuccessful()) { return writerForTopLevelObject(req, res, writer -> UserSearchResultsRepresenter.toJSON(writer, searchTerm, userSearchModels)); } else { return renderHTTPOperationResult(result, req, res); } }
public String index(Request req, Response res) throws InvalidPluginTypeException, IOException { String pluginType = req.queryParams("type"); RolesConfig roles = roleConfigService.getRoles().ofType(pluginType); String etag = entityHashingService.md5ForEntity(roles); if (fresh(req, etag)) { return notModified(res); } else { setEtagHeader(res, etag); return writerForTopLevelObject(req, res, writer -> RolesRepresenter.toJSON(writer, roles)); } }
public String index(Request req, Response res) throws InvalidPluginTypeException, IOException { String pluginType = req.queryParams("type"); RolesConfig roles = roleConfigService.getRoles().ofType(pluginType); String etag = entityHashingService.md5ForEntity(roles); if (fresh(req, etag)) { return notModified(res); } else { setEtagHeader(res, etag); return writerForTopLevelObject(req, res, writer -> RolesRepresenter.toJSON(writer, roles)); } }
@Override public Point apply(Request webRequest) throws BadRequestException { Point location; String lonParam = webRequest.queryParams("lon"); String latParam = webRequest.queryParams("lat"); if (!mandatory && lonParam == null && latParam == null) { return null; } try { Double lon = Double.valueOf(lonParam); if (lon > 180.0 || lon < -180.00) { throw new BadRequestException(400, "invalid search term 'lon', expected number >= -180.0 and <= 180.0"); } Double lat = Double.valueOf(latParam); if (lat > 90.0 || lat < -90.00) { throw new BadRequestException(400, "invalid search term 'lat', expected number >= -90.0 and <= 90.0"); } location = geometryFactory.createPoint(new Coordinate(lon, lat)); } catch (NullPointerException | NumberFormatException e) { throw new BadRequestException(400, "invalid search term 'lat' and/or 'lon', try instead lat=51.5&lon=8.0"); } return location; } }
public <R extends PhotonRequest> R create(Request webRequest) throws BadRequestException { for (String queryParam : webRequest.queryParams()) if (!m_hsRequestQueryParams.contains(queryParam)) throw new BadRequestException(400, "unknown query parameter '" + queryParam + "'. Allowed parameters are: " + m_hsRequestQueryParams); String language = webRequest.queryParams("lang"); language = language == null ? "en" : language; languageChecker.apply(language); String query = webRequest.queryParams("q"); if (query == null) throw new BadRequestException(400, "missing search term 'q': /?q=berlin"); Integer limit; try { limit = Integer.valueOf(webRequest.queryParams("limit")); } catch (NumberFormatException e) { limit = 15; String scaleStr = webRequest.queryParams("location_bias_scale"); if (scaleStr != null && !scaleStr.isEmpty()) try {
@Override public String handle(Request request, Response response) { R photonRequest = null; try { photonRequest = photonRequestFactory.create(request); } catch (BadRequestException e) { JSONObject json = new JSONObject(); json.put("message", e.getMessage()); halt(e.getHttpStatus(), json.toString()); } PhotonRequestHandler<R> handler = requestHandlerFactory.createHandler(photonRequest); List<JSONObject> results = handler.handle(photonRequest); JSONObject geoJsonResults = geoJsonConverter.convert(results); response.type("application/json; charset=utf-8"); response.header("Access-Control-Allow-Origin", "*"); if (request.queryParams("debug") != null) return geoJsonResults.toString(4); return geoJsonResults.toString(); } }
@Override public String handle(Request request, Response response) { R photonRequest = null; try { photonRequest = reverseRequestFactory.create(request); } catch (BadRequestException e) { JSONObject json = new JSONObject(); json.put("message", e.getMessage()); halt(e.getHttpStatus(), json.toString()); } ReverseRequestHandler<R> handler = requestHandlerFactory.createHandler(photonRequest); List<JSONObject> results = handler.handle(photonRequest); JSONObject geoJsonResults = geoJsonConverter.convert(results); response.type("application/json; charset=utf-8"); response.header("Access-Control-Allow-Origin", "*"); if (request.queryParams("debug") != null) return geoJsonResults.toString(4); return geoJsonResults.toString(); } }
if (req.queryParams("limit") != null) { limit = Integer.parseInt(req.queryParams("limit"));
/** * Creates a controller that maps requests to actions. */ public UserController(final UserService userService) { Spark.staticFileLocation("/public"); get("/api/users", (req, res) -> userService.getAllUsers(), json()); get("/api/users/:id", (req, res) -> userService.getUser(req.params(":id")), json()); post("/api/users", (req, res) -> userService.createUser(req.queryParams("name"), req.queryParams("email")), json()); put("/api/users/:id", (req, res) -> userService.updateUser( req.params(":id"), req.queryParams("name"), req.queryParams("email") ), json()); delete("/api/users/:id", (req, res) -> userService.deleteUser(req.params(":id")), json()); after((req, res) -> { res.type("application/json"); }); exception(IllegalArgumentException.class, (error, req, res) -> { res.status(400); res.body(toJson(new ResponseError(error))); }); }