use of spark.ModelAndView in project newts by OpenNMS.
the class Web method main.
public static void main(String... args) {
staticFileLocation("/static");
get("/stations", new TemplateViewRoute() {
@Override
public ModelAndView handle(Request request, Response response) {
Map<String, Object> model = Maps.newHashMap();
model.put("stationsMap", STATION_NAMES);
return new ModelAndView(model, "index.wm");
}
}, new VelocityTemplateEngine());
get("/summer88", new TemplateViewRoute() {
@Override
public ModelAndView handle(Request request, Response response) {
Map<String, Object> model = Maps.newHashMap();
model.put("stationIds", STATION_IDS);
return new ModelAndView(model, "summer.wm");
}
}, new VelocityTemplateEngine());
get("/stations/:stationName", new TemplateViewRoute() {
@Override
public ModelAndView handle(Request request, Response response) {
String stationName = request.params(":stationName");
String id = STATION_IDS.get(stationName);
if (id == null) {
halt(404, "No such station");
}
Map<String, String> model = Maps.newHashMap();
model.put("location", STATION_NAMES.get(stationName));
model.put("id", id);
model.put("start", request.queryParams("start"));
model.put("end", request.queryParams("end"));
model.put("resolution", request.queryParams("resolution"));
return new ModelAndView(model, "station.wm");
}
}, new VelocityTemplateEngine());
}
use of spark.ModelAndView in project searchcode-server by boyter.
the class CodeRouteServiceTest method testRootQueryString.
public void testRootQueryString() {
CodeRouteService codeRouteService = new CodeRouteService();
Request request = Mockito.mock(Request.class);
Set<String> hashSet = new HashSet<>();
hashSet.add("q");
when(request.queryParams()).thenReturn(hashSet);
when(request.queryParams("q")).thenReturn("test");
ModelAndView modelAndView = codeRouteService.root(request, null);
Map<String, Object> model = (Map<String, Object>) modelAndView.getModel();
String viewName = modelAndView.getViewName();
assertThat(model.get("searchValue")).isEqualTo("test");
assertThat(model.get("searchResultJson")).isNotNull();
assertThat(model.get("logoImage")).isNotNull();
assertThat(model.get("isCommunity")).isEqualTo(App.IS_COMMUNITY);
assertThat(viewName).isEqualTo("search_ajax.ftl");
}
use of spark.ModelAndView in project searchcode-server by boyter.
the class ServerRoutes method RegisterServerRoutes.
public static void RegisterServerRoutes() {
get("/", (request, response) -> {
var codeRouteService = new CodeRouteService();
return new FreeMarkerEngine().render(codeRouteService.root(request, response));
});
get("/healthcheck/", (request, response) -> new JsonTransformer().render(true));
get("/health-check/", (request, response) -> new JsonTransformer().render(true));
get("/file/:codeid/:reponame/*", (request, response) -> {
var codeRouteService = new CodeRouteService();
return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getCode(request, response), "coderesult.ftl"));
});
get("/repository/overview/:reponame/", (request, response) -> {
var codeRouteService = new CodeRouteService();
return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getProject(request, response), "repository_overview.ftl"));
});
get("/repository/list/", (request, response) -> {
var codeRouteService = new CodeRouteService();
return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getRepositoryList(request, response), "repository_list.ftl"));
});
// //////////////////////////////////////////////////
// Page Routes Below
// //////////////////////////////////////////////////
get("/documentation/", (request, response) -> {
var map = getDefaultMap();
return new FreeMarkerEngine().render(new ModelAndView(map, "documentation.ftl"));
});
get("/404/", (request, response) -> {
var map = getDefaultMap();
return new FreeMarkerEngine().render(new ModelAndView(map, "404.ftl"));
});
// //////////////////////////////////////////////////
// API Routes Below
// //////////////////////////////////////////////////
path("/api", () -> {
// All new API endpoints should go in here to allow public exposure and versioning
path("/v1", () -> {
get("/version/", (request, response) -> {
addJsonHeaders(response);
return new JsonTransformer().render(new VersionResponse().setVersion(App.VERSION));
});
get("/health-check/", (request, response) -> {
addJsonHeaders(response);
return new JsonTransformer().render(new VersionResponse().setVersion(App.VERSION));
});
});
get("/codesearch/", (request, response) -> {
addJsonHeaders(response);
var searchRouteService = new SearchRouteService();
return new JsonTransformer().render(searchRouteService.codeSearch(request, response));
});
get("/literalcodesearch/", (request, response) -> {
addJsonHeaders(response);
var searchRouteService = new SearchRouteService();
return new JsonTransformer().render(searchRouteService.literalCodeSearch(request, response));
});
get("/codesearch/rss/", (request, response) -> {
addXmlHeaders(response);
var searchRouteService = new SearchRouteService();
var map = new HashMap<String, Object>();
map.put("result", searchRouteService.codeSearch(request, response));
map.put("hostname", Properties.getProperties().getProperty(Values.HOST_NAME, Values.DEFAULT_HOST_NAME));
return new FreeMarkerEngine().render(new ModelAndView(map, "codesearchrss.ftl"));
});
get("/timecodesearch/", (request, response) -> {
addJsonHeaders(response);
var ars = new TimeSearchRouteService();
return new JsonTransformer().render(ars.getTimeSearch(request, response));
});
path("/repo", () -> {
get("/add/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.repoAdd(request, response));
});
get("/delete/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.repoDelete(request, response));
});
get("/list/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.repoList(request, response));
});
get("/reindex/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.repositoryReindex(request, response));
});
// //////////////////////////////////////////////////
// Unsecured API Routes Below
// //////////////////////////////////////////////////
get("/index/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.repositoryIndex(request, response));
});
get("/filecount/", "application/json", (request, response) -> {
var apiRouteService = new ApiRouteService();
return apiRouteService.getFileCount(request, response);
});
get("/indextime/", "application/json", (request, response) -> {
var apiRouteService = new ApiRouteService();
return apiRouteService.getIndexTime(request, response);
});
get("/indextimeseconds/", "application/json", (request, response) -> {
var apiRouteService = new ApiRouteService();
return apiRouteService.getAverageIndexTimeSeconds(request, response);
});
get("/repo/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.getRepo(request, response));
});
get("/repotree/", "application/json", (request, response) -> {
addJsonHeaders(response);
var apiRouteService = new ApiRouteService();
return new JsonTransformer().render(apiRouteService.repoTree(request, response));
});
});
});
// //////////////////////////////////////////////////
// Admin Routes Below
// //////////////////////////////////////////////////
get("/login/", (request, response) -> {
if (getAuthenticatedUser(request) != null) {
response.redirect("/admin/");
halt();
return null;
}
var map = getDefaultMap();
return new FreeMarkerEngine().render(new ModelAndView(map, "login.ftl"));
});
post("/login/", (request, response) -> {
if (request.queryParams().contains("password") && request.queryParams("password").equals(com.searchcode.app.util.Properties.getProperties().getProperty("password"))) {
addAuthenticatedUser(request);
response.redirect("/admin/");
halt();
}
var map = getDefaultMap();
if (request.queryParams().contains("password")) {
map.put("passwordInvalid", true);
}
return new FreeMarkerEngine().render(new ModelAndView(map, "login.ftl"));
});
get("/logout/", (req, res) -> {
removeAuthenticatedUser(req);
res.redirect("/");
return null;
});
path("/admin", () -> {
get("/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminPage(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin.ftl"));
});
get("/repo/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminRepo(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo.ftl"));
});
post("/repo/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var validatorResult = adminRouteService.postRepo(request, response, false);
if (!validatorResult.isValid) {
var map = adminRouteService.adminRepo(request, response);
map.put("validatorResult", validatorResult);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo.ftl"));
}
var returns = request.queryParamsValues("return");
if (returns != null) {
response.redirect("/admin/repo/");
} else {
response.redirect("/admin/repolist/");
}
halt();
return null;
});
get("/repo/edit/:reponame/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminGetRepo(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo_edit.ftl"));
});
post("/repo/edit/:reponame/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var validatorResult = adminRouteService.postRepo(request, response, true);
if (!validatorResult.isValid) {
Map<String, Object> map = adminRouteService.adminRepo(request, response);
map.put("validatorResult", validatorResult);
map.put("repoResult", validatorResult.getRepoResult());
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo_edit.ftl"));
}
response.redirect(request.url());
halt();
return null;
});
get("/repolist/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminRepo(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repolist.ftl"));
});
get("/repo/error/:reponame/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminGetRepo(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo_error.ftl"));
});
get("/bulk/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = getDefaultMap();
map.put("repoCount", adminRouteService.getStat("repoCount"));
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_bulk.ftl"));
});
post("/bulk/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var validatorResults = adminRouteService.postBulk(request, response);
if (!validatorResults.isEmpty()) {
var map = getDefaultMap();
map.put("repoCount", adminRouteService.getStat("repoCount"));
map.put("validatorResults", validatorResults);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_bulk.ftl"));
}
response.redirect("/admin/repolist/");
halt();
return null;
});
get("/settings/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminSettings(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_settings.ftl"));
});
get("/logs/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminLogs(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_logs.ftl"));
});
post("/settings/", (request, response) -> {
checkLoggedIn(request, response);
if (IS_COMMUNITY) {
response.redirect("/admin/settings/");
halt();
}
var adminRouteService = new AdminRouteService();
adminRouteService.postSettings(request, response);
response.redirect("/admin/settings/");
halt();
return null;
});
get("/delete/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
adminRouteService.deleteRepo(request, response);
return new JsonTransformer().render(true);
});
get("/reindex/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
adminRouteService.reindexRepo(request, response);
return new JsonTransformer().render(true);
});
post("/enableadder/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getIndexService().setRepoAdderPause(false);
Singleton.getIndexService().resetReindexingAll();
return new JsonTransformer().render(true);
});
post("/rebuild/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getIndexService().reindexAll();
return new JsonTransformer().render(true);
});
post("/deleteindex/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getIndexService().deleteAll();
return new JsonTransformer().render(true);
});
post("/forcequeue/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
return Singleton.getJobService().forceEnqueue();
}, new JsonTransformer());
post("/togglepause/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getIndexService().toggleRepoAdderPause();
return new JsonTransformer().render(Singleton.getIndexService().getRepoAdderPause());
});
post("/flipreadindex/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getIndexService().flipReadIndex();
return new JsonTransformer().render(Values.EMPTYSTRING);
});
post("/resetindexlinescount/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getIndexService().setCodeIndexLinesCount(0);
return new JsonTransformer().render(Values.EMPTYSTRING);
});
post("/clearsearchcount/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getStatsService().clearSearchCount();
return new JsonTransformer().render(Values.EMPTYSTRING);
});
post("/resetspellingcorrector/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getSpellingCorrector().reset();
return new JsonTransformer().render(Values.EMPTYSTRING);
});
get("/checkversion/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
return adminRouteService.checkVersion();
});
path("/api", () -> {
get("/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
var map = adminRouteService.adminApi(request, response);
return new FreeMarkerEngine().render(new ModelAndView(map, "admin_api.ftl"));
});
post("/", (request, response) -> {
checkLoggedIn(request, response);
Singleton.getApiService().createKeys();
response.redirect("/admin/api/");
halt();
return null;
});
get("/delete/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
if (getAuthenticatedUser(request) == null || !request.queryParams().contains("publicKey")) {
response.redirect("/login/");
halt();
return new JsonTransformer().render(false);
}
String publicKey = request.queryParams("publicKey");
Singleton.getApiService().deleteKey(publicKey);
return new JsonTransformer().render(true);
});
get("/getstat/", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
return adminRouteService.getStat(request, response);
});
get("/getstatjson/", "application/json", (request, response) -> {
checkLoggedIn(request, response);
var adminRouteService = new AdminRouteService();
return new JsonTransformer().render(adminRouteService.getStat(request, response));
});
get("/checkindexstatus/", "application/json", (request, response) -> {
// TODO move this to unsecured routes
var adminRouteService = new AdminRouteService();
return adminRouteService.checkIndexStatus(request, response);
});
});
});
}
use of spark.ModelAndView in project SpringStepByStep by JavaProgrammerLB.
the class WebConfig method setupRoutes.
private void setupRoutes() {
/*
* Shows a users timeline or if no user is logged in,
* it will redirect to the public timeline.
* This timeline shows the user's messages as well
* as all the message of followed users.
*/
get("/", (req, res) -> {
User user = getAuthenticatedUser(req);
Map<String, Object> map = new HashMap<>();
map.put("pageTitle", "Timeline");
map.put("user", user);
List<Message> messages = service.getUserFullTimelineMessages(user);
map.put("messages", messages);
return new ModelAndView(map, "timeline.ftl");
}, new FreeMarkerEngine());
before("/", (req, res) -> {
User user = getAuthenticatedUser(req);
if (user == null) {
res.redirect("/public");
halt();
}
});
/*
* Displays the latest messages of all users.
*/
get("/public", (req, res) -> {
User user = getAuthenticatedUser(req);
Map<String, Object> map = new HashMap<>();
map.put("pageTitle", "Public Timeline");
map.put("user", user);
List<Message> messages = service.getPublicTimelineMessages();
map.put("messages", messages);
return new ModelAndView(map, "timeline.ftl");
}, new FreeMarkerEngine());
/*
* Displays a user's tweets.
*/
get("/t/:username", (req, res) -> {
String username = req.params(":username");
User profileUser = service.getUserbyUsername(username);
User authUser = getAuthenticatedUser(req);
boolean followed = false;
if (authUser != null) {
followed = service.isUserFollower(authUser, profileUser);
}
List<Message> messages = service.getUserTimelineMessages(profileUser);
Map<String, Object> map = new HashMap<>();
map.put("pageTitle", username + "'s Timeline");
map.put("user", authUser);
map.put("profileUser", profileUser);
map.put("followed", followed);
map.put("messages", messages);
return new ModelAndView(map, "timeline.ftl");
}, new FreeMarkerEngine());
/*
* Checks if the user exists
*/
before("/t/:username", (req, res) -> {
String username = req.params(":username");
User profileUser = service.getUserbyUsername(username);
if (profileUser == null) {
halt(404, "User not Found");
}
});
/*
* Adds the current user as follower of the given user
*/
get("/t/:username/follow", (req, res) -> {
String username = req.params(":username");
User profileUser = service.getUserbyUsername(username);
User authUser = getAuthenticatedUser(req);
service.followUser(authUser, profileUser);
res.redirect("/t/" + username);
return null;
});
/*
* Checks if the user is authenticated and the user to follow exists
*/
before("/t/:username/follow", (req, res) -> {
String username = req.params(":username");
User authUser = getAuthenticatedUser(req);
User profileUser = service.getUserbyUsername(username);
if (authUser == null) {
res.redirect("/login");
halt();
} else if (profileUser == null) {
halt(404, "User not Found");
}
});
/*
* Removes the current user as follower of the given user.
*/
get("/t/:username/unfollow", (req, res) -> {
String username = req.params(":username");
User profileUser = service.getUserbyUsername(username);
User authUser = getAuthenticatedUser(req);
service.unfollowUser(authUser, profileUser);
res.redirect("/t/" + username);
return null;
});
/*
* Checks if the user is authenticated and the user to unfollow exists
*/
before("/t/:username/unfollow", (req, res) -> {
String username = req.params(":username");
User authUser = getAuthenticatedUser(req);
User profileUser = service.getUserbyUsername(username);
if (authUser == null) {
res.redirect("/login");
halt();
} else if (profileUser == null) {
halt(404, "User not Found");
}
});
/*
* Presents the login form or redirect the user to
* her timeline if it's already logged in
*/
get("/login", (req, res) -> {
Map<String, Object> map = new HashMap<>();
if (req.queryParams("r") != null) {
map.put("message", "You were successfully registered and can login now");
}
return new ModelAndView(map, "login.ftl");
}, new FreeMarkerEngine());
/*
* Logs the user in.
*/
post("/login", (req, res) -> {
Map<String, Object> map = new HashMap<>();
User user = new User();
try {
MultiMap<String> params = new MultiMap<String>();
UrlEncoded.decodeTo(req.body(), params, "UTF-8", -1);
BeanUtils.populate(user, params);
} catch (Exception e) {
halt(501);
return null;
}
LoginResult result = service.checkUser(user);
if (result.getUser() != null) {
addAuthenticatedUser(req, result.getUser());
res.redirect("/");
halt();
} else {
map.put("error", result.getError());
}
map.put("username", user.getUsername());
return new ModelAndView(map, "login.ftl");
}, new FreeMarkerEngine());
/*
* Checks if the user is already authenticated
*/
before("/login", (req, res) -> {
User authUser = getAuthenticatedUser(req);
if (authUser != null) {
res.redirect("/");
halt();
}
});
/*
* Presents the register form or redirect the user to
* her timeline if it's already logged in
*/
get("/register", (req, res) -> {
Map<String, Object> map = new HashMap<>();
return new ModelAndView(map, "register.ftl");
}, new FreeMarkerEngine());
/*
* Registers the user.
*/
post("/register", (req, res) -> {
Map<String, Object> map = new HashMap<>();
User user = new User();
try {
MultiMap<String> params = new MultiMap<String>();
UrlEncoded.decodeTo(req.body(), params, "UTF-8", -1);
BeanUtils.populate(user, params);
} catch (Exception e) {
halt(501);
return null;
}
String error = user.validate();
if (StringUtils.isEmpty(error)) {
User existingUser = service.getUserbyUsername(user.getUsername());
if (existingUser == null) {
service.registerUser(user);
res.redirect("/login?r=1");
halt();
} else {
error = "The username is already taken";
}
}
map.put("error", error);
map.put("username", user.getUsername());
map.put("email", user.getEmail());
return new ModelAndView(map, "register.ftl");
}, new FreeMarkerEngine());
/*
* Checks if the user is already authenticated
*/
before("/register", (req, res) -> {
User authUser = getAuthenticatedUser(req);
if (authUser != null) {
res.redirect("/");
halt();
}
});
/*
* Registers a new message for the user.
*/
post("/message", (req, res) -> {
User user = getAuthenticatedUser(req);
MultiMap<String> params = new MultiMap<String>();
UrlEncoded.decodeTo(req.body(), params, "UTF-8", -1);
Message m = new Message();
m.setUserId(user.getId());
m.setPubDate(new Date());
BeanUtils.populate(m, params);
service.addMessage(m);
res.redirect("/");
return null;
});
/*
* Checks if the user is authenticated
*/
before("/message", (req, res) -> {
User authUser = getAuthenticatedUser(req);
if (authUser == null) {
res.redirect("/login");
halt();
}
});
/*
* Logs the user out and redirects to the public timeline
*/
get("/logout", (req, res) -> {
removeAuthenticatedUser(req);
res.redirect("/public");
return null;
});
}
use of spark.ModelAndView in project gocd by gocd.
the class PipelineActivityController method index.
public ModelAndView index(Request request, Response response) {
String pipelineName = request.params("pipeline_name");
Map<Object, Object> object = new HashMap<Object, Object>() {
{
put("viewTitle", "Pipeline Activity");
put("meta", meta(pipelineName));
}
};
return new ModelAndView(object, null);
}
Aggregations