use of spark.Request in project gocd by gocd.
the class ConfigReposControllerV4 method index.
String index(Request req, Response res) {
ConfigReposConfig repos = allRepos();
ConfigReposConfig userSpecificRepos = repos.stream().filter(configRepoConfig -> authHelper.doesUserHasPermissions(currentUsername(), getAction(req), CONFIG_REPO, configRepoConfig.getId())).collect(toCollection(ConfigReposConfig::new));
String etag = entityHashingService.hashForEntity(userSpecificRepos);
setEtagHeader(res, etag);
if (fresh(req, etag)) {
return notModified(res);
}
return jsonizeAsTopLevelObject(req, w -> ConfigReposConfigRepresenterV4.toJSON(w, userSpecificRepos));
}
use of spark.Request in project gocd by gocd.
the class StageInstanceControllerV3 method rerunSelectedJobs.
public String rerunSelectedJobs(Request req, Response res) throws IOException {
HttpOperationResult result = new HttpOperationResult();
haltIfRequestBodyDoesNotContainPropertyJobs(req);
JsonReader requestBody = GsonTransformer.getInstance().jsonReaderFrom(req.body());
List<String> requestedJobs = requestBody.readStringArrayIfPresent(JOB_NAMES_PROPERTY).get();
Optional<Stage> optionalStage = getStageFromRequestParam(req, result);
optionalStage.ifPresent(stage -> {
HealthStateType healthStateType = HealthStateType.general(HealthStateScope.forStage(stage.getIdentifier().getPipelineName(), stage.getName()));
Set<String> jobsInStage = stage.getJobInstances().stream().map(JobInstance::getName).collect(Collectors.toSet());
List<String> unknownJobs = requestedJobs.stream().filter(jobToRun -> !jobsInStage.contains(jobToRun)).collect(Collectors.toList());
if (unknownJobs.isEmpty()) {
scheduleService.rerunJobs(stage, requestedJobs, result);
} else {
String msg = String.format("Job(s) %s does not exist in stage '%s'.", unknownJobs, stage.getIdentifier().getStageLocator());
result.notFound(msg, "", healthStateType);
}
});
return renderHTTPOperationResult(result, req, res);
}
use of spark.Request in project gocd by gocd.
the class WebhookRequestTest method mockRequest.
private Request mockRequest(String body) {
final Request req = mock(Request.class);
when(req.body()).thenReturn(body);
when(req.contentType()).thenReturn(APPLICATION_JSON_VALUE);
return req;
}
use of spark.Request in project gocd by gocd.
the class RoutesHelperTest method shouldSendHttpExceptionResponseBasedOnAcceptHeader.
@ParameterizedTest
@MethodSource("data")
void shouldSendHttpExceptionResponseBasedOnAcceptHeader(Data data) {
Request request = mock(Request.class);
Response response = mock(Response.class);
when(request.headers("Accept")).thenReturn(data.mimeType);
new RoutesHelper(mock(SparkController.class)).httpException(new RecordNotFoundException("Boom!!"), request, response);
verify(response).body(data.responseText);
}
use of spark.Request in project MtgDesktopCompanion by nicho92.
the class JSONHttpServer method start.
@Override
public void start() throws IOException {
port(getInt("SERVER-PORT"));
exception(Exception.class, new ExceptionHandler<Exception>() {
@Override
public void handle(Exception exception, Request req, Response res) {
res.status(500);
res.body("{\"error\":\"" + exception + "\"}");
}
});
notFound((req, res) -> {
res.status(404);
return "{\"error\":\"not found\"}";
});
before("/*", (request, response) -> {
response.type(getString("MIME"));
logger.info("Received api call from " + request.ip());
});
get("/cards/search/:att/:val", getString("MIME"), (request, response) -> {
return MTGControler.getInstance().getEnabledProviders().searchCardByCriteria(request.params(":att"), request.params(":val"), null, false);
}, transformer);
post("/cards/move/:from/:to/:id", getString("MIME"), (request, response) -> {
MagicCollection from = new MagicCollection(request.params(":from"));
MagicCollection to = new MagicCollection(request.params(":to"));
MagicCard mc = MTGControler.getInstance().getEnabledProviders().getCardById(request.params(":id"));
MTGControler.getInstance().getEnabledDAO().removeCard(mc, from);
MTGControler.getInstance().getEnabledDAO().saveCard(mc, to);
return "OK";
}, transformer);
get("/cards/list/:col/:idEd", getString("MIME"), (request, response) -> {
MagicCollection col = new MagicCollection(request.params(":col"));
MagicEdition ed = new MagicEdition();
ed.setId(request.params(":idEd"));
ed.setSet(request.params(":idEd"));
return MTGControler.getInstance().getEnabledDAO().listCardsFromCollection(col, ed);
}, transformer);
get("/cards/:id", getString("MIME"), (request, response) -> {
return MTGControler.getInstance().getEnabledProviders().getCardById(request.params(":id"));
}, transformer);
get("/collections/list", getString("MIME"), (request, response) -> {
return MTGControler.getInstance().getEnabledDAO().getCollections();
}, transformer);
get("/collections/:name", getString("MIME"), (request, response) -> {
return MTGControler.getInstance().getEnabledDAO().getCollection(request.params(":name"));
}, transformer);
get("/editions/list", getString("MIME"), (request, response) -> {
return MTGControler.getInstance().getEnabledProviders().loadEditions();
}, transformer);
get("/editions/:idSet", getString("MIME"), (request, response) -> {
return MTGControler.getInstance().getEnabledProviders().getSetById(request.params(":idSet"));
}, transformer);
get("/editions/list/:colName", getString("MIME"), (request, response) -> {
List<MagicEdition> eds = new ArrayList<>();
List<String> list = MTGControler.getInstance().getEnabledDAO().getEditionsIDFromCollection(new MagicCollection(request.params(":colName")));
for (String s : list) eds.add(MTGControler.getInstance().getEnabledProviders().getSetById(s));
Collections.sort(eds);
return eds;
}, transformer);
get("/prices/:idSet/:name", getString("MIME"), (request, response) -> {
MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", request.params(":name"), null, false).get(0);
MagicEdition ed = MTGControler.getInstance().getEnabledProviders().getSetById(request.params(":idSet"));
List<MagicPrice> pricesret = new ArrayList<>();
for (MTGPricesProvider prices : MTGControler.getInstance().getEnabledPricers()) pricesret.addAll(prices.getPrice(ed, mc));
return pricesret;
}, transformer);
if (getBoolean("ENABLE_GZIP")) {
after((request, response) -> {
response.header("Content-Encoding", "gzip");
});
}
Spark.init();
logger.info("Server start on port " + getString("SERVER-PORT"));
running = true;
}
Aggregations