1 package org.roller.presentation.velocity; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.apache.velocity.Template; 6 import org.apache.velocity.context.Context; 7 import org.apache.velocity.servlet.VelocityServlet; 8 import org.roller.RollerException; 9 import org.roller.model.UserManager; 10 import org.roller.pojos.PageData; 11 import org.roller.pojos.UserData; 12 import org.roller.pojos.WebsiteData; 13 import org.roller.presentation.RollerContext; 14 import org.roller.presentation.RollerRequest; 15 import org.roller.util.Utilities; 16 17 import java.io.IOException ; 18 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 24 26 46 public class FoafServlet extends VelocityServlet 47 { 48 static final long serialVersionUID = -1893244416537298619L; 49 50 private static Log mLogger = LogFactory.getFactory() 51 .getInstance(RollerRequest.class); 52 53 63 public Template handleRequest(HttpServletRequest request, 64 HttpServletResponse response, Context ctx) 65 { 66 RollerRequest rreq = null; 67 try 68 { 69 rreq = RollerRequest.getRollerRequest(request, getServletContext()); 70 } 71 catch (RollerException e) 72 { 73 if (mLogger.isDebugEnabled()) 75 { 76 mLogger.debug("RollerRequest threw Exception", e); 77 } 78 79 try 80 { 81 response.sendError(HttpServletResponse.SC_NOT_FOUND); 82 } 83 catch (IOException e1) 84 { 85 if (mLogger.isDebugEnabled()) 86 { 87 mLogger.debug("IOException sending error", e); 88 } 89 } 90 return null; 91 } 92 93 try 94 { 95 setupContext(ctx, rreq); 96 97 response.setContentType("application/rdf+xml"); 98 return getTemplate("/flavors/foaf.vm"); 99 } 100 catch (Exception e) 101 { 102 mLogger.error("ERROR in FoafServlet", e); 103 } 104 return null; 105 } 106 107 110 private void setupContext(Context ctx, RollerRequest rreq) throws RollerException 111 { 112 HttpServletRequest request = rreq.getRequest(); 113 RollerContext rollerCtx = RollerContext.getRollerContext( request ); 114 115 UserData user = rreq.getUser(); 116 ctx.put("fullName", user.getFullName()); 118 String homepage = Utilities.escapeHTML( 120 rollerCtx.getAbsoluteContextUrl(request) + 121 "/page/" + rreq.getUser().getUserName() ); 122 ctx.put("websiteURL", homepage); 124 WebsiteData website = rreq.getWebsite(); 126 UserManager usrMgr = RollerContext.getRoller(request).getUserManager(); 127 PageData weblog = usrMgr.getPageByName(website, "Weblog"); 128 if (weblog != null && !website.getDefaultPageId().equals(weblog.getId())) 130 { 131 String weblogUrl = Utilities.escapeHTML( 132 rollerCtx.getAbsoluteContextUrl(request) + 133 "/page/" + rreq.getUser().getUserName() + 134 "/" + weblog.getLink() ); 135 ctx.put("weblog", weblogUrl); 136 } 137 138 String shaEmail = Utilities.encodePassword( 140 "mailto:" + user.getEmailAddress(), "SHA"); 141 ctx.put("shaEmail", shaEmail); 142 } 143 144 148 protected void error( HttpServletRequest req, HttpServletResponse res, 149 Exception e) throws ServletException , IOException 150 { 151 mLogger.warn("ERROR in FoafServlet",e); 152 } 153 } 154 | Popular Tags |