├── .idea
├── compiler.xml
├── libraries
│ ├── Maven__com_sparkjava_spark_core_1_1_1.xml
│ ├── Maven__commons_codec_commons_codec_1_7.xml
│ ├── Maven__org_apache_commons_commons_lang3_3_1.xml
│ ├── Maven__org_eclipse_jetty_jetty_http_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_io_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_security_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_server_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_servlet_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_util_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_webapp_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_jetty_xml_9_0_2_v20130417.xml
│ ├── Maven__org_eclipse_jetty_orbit_javax_servlet_3_0_0_v201112011016.xml
│ ├── Maven__org_freemarker_freemarker_2_3_19.xml
│ ├── Maven__org_mongodb_bson_3_2_2.xml
│ ├── Maven__org_mongodb_mongodb_driver_3_2_2.xml
│ ├── Maven__org_mongodb_mongodb_driver_core_3_2_2.xml
│ └── Maven__org_slf4j_slf4j_api_1_7_2.xml
├── misc.xml
├── modules.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── blogcast.iml
├── dump
└── New Text Document.txt
├── pom.xml
├── src
└── main
│ ├── java
│ ├── BlogController.java
│ ├── BlogPostDAO.java
│ ├── SessionDAO.java
│ └── UserDAO.java
│ └── resources
│ └── freemarker
│ ├── blog_template.ftl
│ ├── entry_template.ftl
│ ├── error_template.ftl
│ ├── login.ftl
│ ├── newpost_template.ftl
│ ├── post_not_found.ftl
│ ├── signup.ftl
│ └── welcome.ftl
└── target
└── classes
├── BlogController$1.class
├── BlogController$10.class
├── BlogController$11.class
├── BlogController$12.class
├── BlogController$13.class
├── BlogController$14.class
├── BlogController$15.class
├── BlogController$2.class
├── BlogController$3.class
├── BlogController$4.class
├── BlogController$5.class
├── BlogController$6.class
├── BlogController$7.class
├── BlogController$8.class
├── BlogController$9.class
├── BlogController$FreemarkerBasedRoute.class
├── BlogController.class
├── BlogPostDAO.class
├── SessionDAO.class
├── UserDAO.class
└── freemarker
├── blog_template.ftl
├── entry_template.ftl
├── error_template.ftl
├── login.ftl
├── newpost_template.ftl
├── post_not_found.ftl
├── signup.ftl
└── welcome.ftl
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
133 |
135 |
148 |
156 |
245 |
252 |
300 |
302 |
451 |
453 |
463 |
465 |
474 |
476 |
486 |
488 |
498 |
501 |
511 |
513 |
523 |
525 |
for the paragraph breaks 247 | post = post.replaceAll("\\r?\\n", "
");
248 |
249 | String permalink = blogPostDAO.addPost(title, post, tagsArray, username);
250 |
251 | // now redirect to the blog permalink
252 | response.redirect("/post/" + permalink);
253 | }
254 | }
255 | });
256 |
257 | get(new FreemarkerBasedRoute("/welcome", "welcome.ftl") {
258 | @Override
259 | protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException {
260 |
261 | String cookie = getSessionCookie(request);
262 | String username = sessionDAO.findUserNameBySessionId(cookie);
263 |
264 | if (username == null) {
265 | System.out.println("welcome() can't identify the user, redirecting to signup");
266 | response.redirect("/signup");
267 |
268 | }
269 | else {
270 | SimpleHash root = new SimpleHash();
271 |
272 | root.put("username", username);
273 |
274 | template.process(root, writer);
275 | }
276 | }
277 | });
278 |
279 | // process a new comment
280 | post(new FreemarkerBasedRoute("/newcomment", "entry_template.ftl") {
281 | @Override
282 | protected void doHandle(Request request, Response response, Writer writer)
283 | throws IOException, TemplateException {
284 | String name = StringEscapeUtils.escapeHtml4(request.queryParams("commentName"));
285 | String email = StringEscapeUtils.escapeHtml4(request.queryParams("commentEmail"));
286 | String body = StringEscapeUtils.escapeHtml4(request.queryParams("commentBody"));
287 | String permalink = request.queryParams("permalink");
288 |
289 | Document post = blogPostDAO.findByPermalink(permalink);
290 | if (post == null) {
291 | response.redirect("/post_not_found");
292 | }
293 | // check that comment is good
294 | else if (name.equals("") || body.equals("")) {
295 | // bounce this back to the user for correction
296 | SimpleHash root = new SimpleHash();
297 | SimpleHash comment = new SimpleHash();
298 |
299 | comment.put("name", name);
300 | comment.put("email", email);
301 | comment.put("body", body);
302 | root.put("comment", comment);
303 | root.put("post", post);
304 | root.put("errors", "Post must contain your name and an actual comment");
305 |
306 | template.process(root, writer);
307 | }
308 | else {
309 | blogPostDAO.addPostComment(name, email, body, permalink);
310 | response.redirect("/post/" + permalink);
311 | }
312 | }
313 | });
314 |
315 | // present the login page
316 | get(new FreemarkerBasedRoute("/login", "login.ftl") {
317 | @Override
318 | protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException {
319 | SimpleHash root = new SimpleHash();
320 |
321 | root.put("username", "");
322 | root.put("login_error", "");
323 |
324 | template.process(root, writer);
325 | }
326 | });
327 |
328 | // process output coming from login form. On success redirect folks to the welcome page
329 | // on failure, just return an error and let them try again.
330 | post(new FreemarkerBasedRoute("/login", "login.ftl") {
331 | @Override
332 | protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException {
333 |
334 | String username = request.queryParams("username");
335 | String password = request.queryParams("password");
336 |
337 | System.out.println("Login: User submitted: " + username + " " + password);
338 |
339 | Document user = userDAO.validateLogin(username, password);
340 |
341 | if (user != null) {
342 |
343 | // valid user, let's log them in
344 | String sessionID = sessionDAO.startSession(user.get("_id").toString());
345 |
346 | if (sessionID == null) {
347 | response.redirect("/internal_error");
348 | }
349 | else {
350 | // set the cookie for the user's browser
351 | response.raw().addCookie(new Cookie("session", sessionID));
352 |
353 | response.redirect("/welcome");
354 | }
355 | }
356 | else {
357 | SimpleHash root = new SimpleHash();
358 |
359 |
360 | root.put("username", StringEscapeUtils.escapeHtml4(username));
361 | root.put("password", "");
362 | root.put("login_error", "Invalid Login");
363 | template.process(root, writer);
364 | }
365 | }
366 | });
367 |
368 | // Show the posts filed under a certain tag
369 | get(new FreemarkerBasedRoute("/tag/:thetag", "blog_template.ftl") {
370 | @Override
371 | protected void doHandle(Request request, Response response, Writer writer)
372 | throws IOException, TemplateException {
373 |
374 | String username = sessionDAO.findUserNameBySessionId(getSessionCookie(request));
375 | SimpleHash root = new SimpleHash();
376 |
377 | String tag = StringEscapeUtils.escapeHtml4(request.params(":thetag"));
378 | List
12 | #if>
13 |
14 |
31 |
32 |
33 | Filed Under:
34 | <#if post["tags"]??>
35 | <#list post["tags"] as tag>
36 | ${tag}
37 | #list>
38 | #if>
39 |
40 |
41 | #list>
42 | <#else>
43 |
13 | #if>
14 |
15 | Blog Home
22 | Filed Under:
23 | <#if post["tags"]??>
24 | <#list post["tags"] as tag>
25 | ${tag}
26 | #list>
27 | #if>
28 |
29 | Comments:
30 | My Blog
15 |
16 | <#if myposts?has_content>
17 | <#list myposts as post>
18 | ${post["title"]}
19 | Posted ${post["date"]?datetime} By ${post["author"]}
20 | Comments:
21 | <#if post["comments"]??>
22 | <#assign numComments = post["comments"]?size>
23 | <#else>
24 | <#assign numComments = 0>
25 | #if>
26 |
27 | ${numComments}
28 |
29 | ${post["body"]!""}
30 | There are no posts yet. Please add a post!
44 | #if>
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/main/resources/freemarker/entry_template.ftl:
--------------------------------------------------------------------------------
1 |
2 |
4 |
16 |
17 | ${post["title"]}
18 | Posted ${post["date"]?datetime} By ${post["author"]}
19 |
20 | ${post["body"]}
21 |
31 | <#if post["comments"]??>
32 | <#assign numComments = post["comments"]?size>
33 | <#else>
34 | <#assign numComments = 0>
35 | #if>
36 | <#if (numComments > 0)>
37 | <#list 0 .. (numComments -1) as i>
38 |
43 |
44 | ${post["comments"][i]["body"]}
45 |
46 | #list>
47 | #if>
48 | Add a comment
49 |
50 |
61 |
62 |
63 |