Search in sources :

Example 1 with FreeMarkerEngine

use of spark.template.freemarker.FreeMarkerEngine 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 messages 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;
    });
}
Also used : FreeMarkerEngine(spark.template.freemarker.FreeMarkerEngine) User(com.minitwit.model.User) Message(com.minitwit.model.Message) HashMap(java.util.HashMap) LoginResult(com.minitwit.model.LoginResult) ModelAndView(spark.ModelAndView) Date(java.util.Date) MultiMap(org.eclipse.jetty.util.MultiMap)

Example 2 with FreeMarkerEngine

use of spark.template.freemarker.FreeMarkerEngine 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;
    });
}
Also used : FreeMarkerEngine(spark.template.freemarker.FreeMarkerEngine) User(com.yitianyigexiangfa.model.User) Message(com.yitianyigexiangfa.model.Message) HashMap(java.util.HashMap) LoginResult(com.yitianyigexiangfa.model.LoginResult) ModelAndView(spark.ModelAndView) Date(java.util.Date) MultiMap(org.eclipse.jetty.util.MultiMap)

Example 3 with FreeMarkerEngine

use of spark.template.freemarker.FreeMarkerEngine in project searchcode-server by boyter.

the class App method main.

public static void main(String[] args) {
    // Database migrations happen before we start
    preStart();
    Singleton.getLogger().info("Starting searchcode server on port " + getServerPort());
    if (getOnlyLocalhost()) {
        Singleton.getLogger().info("Only listening on 127.0.0.1 ");
        Spark.ipAddress("127.0.0.1");
    }
    Spark.port(getServerPort());
    Spark.staticFileLocation("/public");
    Singleton.getJobService().initialJobs();
    ////////////////////////////////////////////////////
    //          Search/Code Routes Below
    ////////////////////////////////////////////////////
    get("/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(codeRouteService.root(request, response));
    });
    get("/html/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(codeRouteService.html(request, response));
    });
    get("/literal/", (request, response) -> {
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.literalSearch(request, response), "index.ftl"));
    });
    get("/file/:codeid/:reponame/*", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getCode(request, response), "coderesult.ftl"));
    });
    get("/repository/overview/:reponame/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getProject(request, response), "repository_overview.ftl"));
    });
    get("/repository/list/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getRepositoryList(request, response), "repository_list.ftl"));
    });
    ////////////////////////////////////////////////////
    //              Page Routes Below
    ////////////////////////////////////////////////////
    get("/documentation/", (request, response) -> {
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new FreeMarkerEngine().render(new ModelAndView(map, "documentation.ftl"));
    });
    get("/404/", (request, response) -> {
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new FreeMarkerEngine().render(new ModelAndView(map, "404.ftl"));
    });
    ////////////////////////////////////////////////////
    //              API Routes Below
    ////////////////////////////////////////////////////
    path("/api", () -> {
        get("/codesearch/", (request, response) -> {
            addJsonHeaders(response);
            SearchRouteService searchRouteService = new SearchRouteService();
            return new JsonTransformer().render(searchRouteService.codeSearch(request, response));
        });
        get("/codesearch/rss/", (request, response) -> {
            addXmlHeaders(response);
            SearchRouteService searchRouteService = new SearchRouteService();
            Map<String, Object> map = new HashMap<>();
            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);
            TimeSearchRouteService ars = new TimeSearchRouteService();
            return new JsonTransformer().render(ars.getTimeSearch(request, response));
        });
        path("/repo", () -> {
            get("/add/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repoAdd(request, response));
            });
            get("/delete/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repoDelete(request, response));
            });
            get("/list/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repoList(request, response));
            });
            get("/reindex/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repositoryReindex(request, response));
            });
            ////////////////////////////////////////////////////
            //          Unsecured API Routes Below
            ////////////////////////////////////////////////////
            get("/index/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repositoryIndex(request, response));
            });
            get("/filecount/", "application/json", (request, response) -> {
                ApiRouteService apiRouteService = new ApiRouteService();
                return apiRouteService.getFileCount(request, response);
            });
            get("/indextime/", "application/json", (request, response) -> {
                ApiRouteService apiRouteService = new ApiRouteService();
                return apiRouteService.getIndexTime(request, response);
            });
            get("/indextimeseconds/", "application/json", (request, response) -> {
                ApiRouteService apiRouteService = new ApiRouteService();
                return apiRouteService.getAverageIndexTimeSeconds(request, response);
            });
            get("/repo/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.getRepo(request, response));
            });
        });
    });
    ////////////////////////////////////////////////////
    //              Admin Routes Below
    ////////////////////////////////////////////////////
    get("/login/", (request, response) -> {
        if (getAuthenticatedUser(request) != null) {
            response.redirect("/admin/");
            halt();
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        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();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        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);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminPage(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin.ftl"));
        });
        get("/repo/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminRepo(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo.ftl"));
        });
        post("/repo/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            ValidatorResult validatorResult = adminRouteService.postRepo(request, response);
            if (!validatorResult.isValid) {
                Map<String, Object> map = adminRouteService.adminRepo(request, response);
                map.put("validatorResult", validatorResult);
                return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo.ftl"));
            }
            String[] returns = request.queryParamsValues("return");
            if (returns != null) {
                response.redirect("/admin/repo/");
            } else {
                response.redirect("/admin/repolist/");
            }
            halt();
            return null;
        });
        get("/repolist/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminRepo(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repolist.ftl"));
        });
        get("/bulk/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = new HashMap<>();
            map.put("logoImage", CommonRouteService.getLogo());
            map.put("isCommunity", ISCOMMUNITY);
            map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
            map.put("repoCount", adminRouteService.getStat("repoCount"));
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_bulk.ftl"));
        });
        post("/bulk/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            List<ValidatorResult> validatorResults = adminRouteService.postBulk(request, response);
            if (!validatorResults.isEmpty()) {
                Map<String, Object> map = new HashMap<>();
                map.put("logoImage", CommonRouteService.getLogo());
                map.put("isCommunity", ISCOMMUNITY);
                map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
                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);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminSettings(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_settings.ftl"));
        });
        get("/logs/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminLogs(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_logs.ftl"));
        });
        post("/settings/", (request, response) -> {
            checkLoggedIn(request, response);
            if (ISCOMMUNITY) {
                response.redirect("/admin/settings/");
                halt();
            }
            AdminRouteService adminRouteService = new AdminRouteService();
            adminRouteService.postSettings(request, response);
            response.redirect("/admin/settings/");
            halt();
            return null;
        });
        get("/delete/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            adminRouteService.deleteRepo(request, response);
            return new JsonTransformer().render(true);
        });
        post("/rebuild/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            boolean result = Singleton.getJobService().rebuildAll();
            if (result) {
                Singleton.getJobService().forceEnqueue();
            }
            return new JsonTransformer().render(result);
        });
        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.getSharedService().setPauseBackgroundJobs(!Singleton.getSharedService().getPauseBackgroundJobs());
            return new JsonTransformer().render(Singleton.getSharedService().getPauseBackgroundJobs());
        });
        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);
            AdminRouteService adminRouteService = new AdminRouteService();
            return adminRouteService.checkVersion();
        });
        path("/api", () -> {
            get("/", (request, response) -> {
                checkLoggedIn(request, response);
                AdminRouteService adminRouteService = new AdminRouteService();
                Map<String, Object> 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);
                AdminRouteService adminRouteService = new AdminRouteService();
                return adminRouteService.getStat(request, response);
            });
            get("/getstatjson/", "application/json", (request, response) -> {
                checkLoggedIn(request, response);
                AdminRouteService adminRouteService = new AdminRouteService();
                return new JsonTransformer().render(adminRouteService.getStat(request, response));
            });
            get("/checkindexstatus/", "application/json", (request, response) -> {
                // TODO move this to unsecured routes
                AdminRouteService adminRouteService = new AdminRouteService();
                return adminRouteService.checkIndexStatus(request, response);
            });
        });
    });
}
Also used : FreeMarkerEngine(spark.template.freemarker.FreeMarkerEngine) HashMap(java.util.HashMap) ValidatorResult(com.searchcode.app.model.ValidatorResult) ModelAndView(spark.ModelAndView) JsonTransformer(com.searchcode.app.util.JsonTransformer)

Aggregations

HashMap (java.util.HashMap)3 ModelAndView (spark.ModelAndView)3 FreeMarkerEngine (spark.template.freemarker.FreeMarkerEngine)3 Date (java.util.Date)2 MultiMap (org.eclipse.jetty.util.MultiMap)2 LoginResult (com.minitwit.model.LoginResult)1 Message (com.minitwit.model.Message)1 User (com.minitwit.model.User)1 ValidatorResult (com.searchcode.app.model.ValidatorResult)1 JsonTransformer (com.searchcode.app.util.JsonTransformer)1 LoginResult (com.yitianyigexiangfa.model.LoginResult)1 Message (com.yitianyigexiangfa.model.Message)1 User (com.yitianyigexiangfa.model.User)1