Mercurial Hosting > nabble
comparison src/nabble/view/web/forum/Permalink.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 18cf4872fd7f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.view.web.forum; | |
2 | |
3 import fschmidt.util.servlet.CanonicalUrl; | |
4 import fschmidt.util.servlet.ServletUtils; | |
5 import nabble.model.ModelHome; | |
6 import nabble.model.Node; | |
7 import nabble.model.Person; | |
8 import nabble.model.Site; | |
9 import nabble.view.lib.Jtp; | |
10 import nabble.view.lib.TopicView; | |
11 import nabble.view.lib.UrlMappable; | |
12 | |
13 import javax.servlet.ServletException; | |
14 import javax.servlet.http.HttpServlet; | |
15 import javax.servlet.http.HttpServletRequest; | |
16 import javax.servlet.http.HttpServletResponse; | |
17 import java.io.IOException; | |
18 import java.util.HashMap; | |
19 import java.util.Map; | |
20 import java.util.regex.Matcher; | |
21 import java.util.regex.Pattern; | |
22 | |
23 | |
24 public class Permalink extends HttpServlet implements UrlMappable, CanonicalUrl { | |
25 | |
26 public static String url(Node topic,Node post) { | |
27 if( topic==null ) | |
28 topic = post.getTopic(); | |
29 return topic.getSite().getBaseUrl() + path(topic,post); | |
30 } | |
31 | |
32 public static String path(Node topic,Node post) { | |
33 if( topic==null ) | |
34 topic = post.getTopic(); | |
35 StringBuilder buf = new StringBuilder(); | |
36 buf.append( '/' ); | |
37 buf.append( Jtp.subjectEncode(topic.getSubject()) ); | |
38 buf.append( "-tp" ); | |
39 buf.append( topic.getId() ); | |
40 if( post != null && post != topic ) { | |
41 buf.append("p"); | |
42 buf.append(post.getId()); | |
43 } | |
44 buf.append( ".html" ); | |
45 return buf.toString(); | |
46 } | |
47 | |
48 private static final Pattern URL_PATTERN = Pattern.compile("/([^/]*)-tp(\\d+)(p(\\d+))?(ef(\\d+))?\\.html$"); | |
49 | |
50 public String getCanonicalUrl(HttpServletRequest request) { | |
51 Site site = Jtp.getSite(request); | |
52 if( site == null ) | |
53 return null; | |
54 Node topic = site.getNode( Long.parseLong( request.getParameter("root") ) ); | |
55 String postS = request.getParameter("post"); | |
56 Node post = postS==null ? null : site.getNode( Long.parseLong(postS) ); | |
57 return url(topic,post); | |
58 } | |
59 | |
60 public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) { | |
61 Matcher m = URL_PATTERN.matcher(mappedUrl); | |
62 if( !m.find() ) | |
63 throw new RuntimeException(); | |
64 Map<String,String[]> params = new HashMap<String,String[]>(); | |
65 String subject = m.group(1); | |
66 params.put("subject",new String[]{subject}); | |
67 String rootId = m.group(2); | |
68 params.put("root",new String[]{rootId}); | |
69 String postId = m.group(4); | |
70 if( postId != null ) | |
71 params.put( "post", new String[]{postId} ); | |
72 return params; | |
73 } | |
74 | |
75 public Pattern getUrlPattern() { | |
76 return URL_PATTERN; | |
77 } | |
78 | |
79 protected void service(HttpServletRequest request, HttpServletResponse response) | |
80 throws ServletException, IOException | |
81 { | |
82 String type = ServletUtils.getCookieValue(request,"tview"); | |
83 TopicView topicView = "threaded".equals(type)? TopicView.THREADED | |
84 : "list".equals(type)? TopicView.LIST : TopicView.CLASSIC; | |
85 | |
86 long rootId = Long.parseLong( request.getParameter("root") ); | |
87 Site site = Jtp.getSite(request); | |
88 if (site == null) { | |
89 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Page Not Found"); | |
90 return; | |
91 } | |
92 Node root = site.getNode(rootId); | |
93 if (root == null) { | |
94 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Node Not Found"); | |
95 return; | |
96 } | |
97 Person visitor = Jtp.getVisitor(request, response); | |
98 | |
99 String postIdS = request.getParameter("post"); | |
100 Node post = postIdS == null? root : site.getNode(Long.parseLong(postIdS)); | |
101 if (Jtp.canBeViewedBy(root, visitor)) | |
102 sendToTopicPage(root, post, topicView, response); | |
103 else if (post.getOwner().equals(visitor)) | |
104 response.sendRedirect("/template/NamlServlet.jtp?macro=print_post&node="+post.getId()); | |
105 else | |
106 sendToTopicPage(root, post, topicView, response); | |
107 } | |
108 | |
109 private void sendToTopicPage(Node root, Node post, TopicView topicView, HttpServletResponse response) throws IOException { | |
110 response.setHeader("Location", Jtp.topicViewPath(root, post.getId(), topicView)); | |
111 response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY); | |
112 } | |
113 | |
114 public static Node getPostFromUrl(String url) { | |
115 Site site = Jtp.getSiteFromUrl(url); | |
116 if( site == null ) | |
117 return null; | |
118 Matcher m = URL_PATTERN.matcher(url); | |
119 if( !m.find() ) | |
120 return null; | |
121 String post = m.group(4) == null? m.group(2) : m.group(4); | |
122 return site.getNode(Long.parseLong(post)); | |
123 } | |
124 | |
125 } |