Mercurial Hosting > nabble
view src/nabble/view/web/forum/Permalink.java @ 19:18cf4872fd7f
remove anonymous posting
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 29 May 2020 22:58:25 -0600 |
parents | 7ecd1a4ef557 |
children |
line wrap: on
line source
package nabble.view.web.forum; import fschmidt.util.servlet.CanonicalUrl; import fschmidt.util.servlet.ServletUtils; import nabble.model.ModelHome; import nabble.model.Node; import nabble.model.User; import nabble.model.Site; import nabble.view.lib.Jtp; import nabble.view.lib.TopicView; import nabble.view.lib.UrlMappable; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Permalink extends HttpServlet implements UrlMappable, CanonicalUrl { public static String url(Node topic,Node post) { if( topic==null ) topic = post.getTopic(); return topic.getSite().getBaseUrl() + path(topic,post); } public static String path(Node topic,Node post) { if( topic==null ) topic = post.getTopic(); StringBuilder buf = new StringBuilder(); buf.append( '/' ); buf.append( Jtp.subjectEncode(topic.getSubject()) ); buf.append( "-tp" ); buf.append( topic.getId() ); if( post != null && post != topic ) { buf.append("p"); buf.append(post.getId()); } buf.append( ".html" ); return buf.toString(); } private static final Pattern URL_PATTERN = Pattern.compile("/([^/]*)-tp(\\d+)(p(\\d+))?(ef(\\d+))?\\.html$"); public String getCanonicalUrl(HttpServletRequest request) { Site site = Jtp.getSite(request); if( site == null ) return null; Node topic = site.getNode( Long.parseLong( request.getParameter("root") ) ); String postS = request.getParameter("post"); Node post = postS==null ? null : site.getNode( Long.parseLong(postS) ); return url(topic,post); } public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) { Matcher m = URL_PATTERN.matcher(mappedUrl); if( !m.find() ) throw new RuntimeException(); Map<String,String[]> params = new HashMap<String,String[]>(); String subject = m.group(1); params.put("subject",new String[]{subject}); String rootId = m.group(2); params.put("root",new String[]{rootId}); String postId = m.group(4); if( postId != null ) params.put( "post", new String[]{postId} ); return params; } public Pattern getUrlPattern() { return URL_PATTERN; } protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = ServletUtils.getCookieValue(request,"tview"); TopicView topicView = "threaded".equals(type)? TopicView.THREADED : "list".equals(type)? TopicView.LIST : TopicView.CLASSIC; long rootId = Long.parseLong( request.getParameter("root") ); Site site = Jtp.getSite(request); if (site == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Page Not Found"); return; } Node root = site.getNode(rootId); if (root == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Node Not Found"); return; } User visitor = Jtp.getUser(request); String postIdS = request.getParameter("post"); Node post = postIdS == null? root : site.getNode(Long.parseLong(postIdS)); if (Jtp.canBeViewedBy(root, visitor)) sendToTopicPage(root, post, topicView, response); else if (post.getOwner().equals(visitor)) response.sendRedirect("/template/NamlServlet.jtp?macro=print_post&node="+post.getId()); else sendToTopicPage(root, post, topicView, response); } private void sendToTopicPage(Node root, Node post, TopicView topicView, HttpServletResponse response) throws IOException { response.setHeader("Location", Jtp.topicViewPath(root, post.getId(), topicView)); response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY); } public static Node getPostFromUrl(String url) { Site site = Jtp.getSiteFromUrl(url); if( site == null ) return null; Matcher m = URL_PATTERN.matcher(url); if( !m.find() ) return null; String post = m.group(4) == null? m.group(2) : m.group(4); return site.getNode(Long.parseLong(post)); } }