KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > FoafServlet


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 JavaDoc;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23
24 //////////////////////////////////////////////////////////////////////////////
25

26 /**
27   * <p>Responsible for rendering FOAF feed. This servlet requires
28   * that the RequestFilter is in place for it, and should also
29   * have the IfModifiedFilter configured.</p>
30   *
31   * <p>Resources:<br />
32   * <a HREF="http://xmlns.com/foaf/0.1/"
33   * >FOAF Vocabulary Specification</a><br />
34   * <a HREF="http://www.xml.com/lpt/a/2004/02/04/foaf.html"
35   * >An Introduction to FOAF</a></p>
36   *
37   * <p>FOAF Autodiscovery: <br />
38   * <link rel="meta" type="application/rdf+xml" title="FOAF"
39   * HREF="$absBaseURL/foaf/$userName" /> </p>
40   *
41   * @author Lance Lavandowska
42   *
43   * @web.servlet name="FoafServlet"
44   * @web.servlet-mapping url-pattern="/foaf/*"
45   */

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     /**
54      * This Velocity servlet does not make use of ContextLoader and associated
55      * classes (as do FlavorServlet and PageServlet) because that is more
56      * work than is really necessary. It implements its own setupContext()
57      * to load necessary values into the Velocity Context.
58      *
59      * @param ctx
60      * @param rreq
61      * @throws RollerException
62      */

63     public Template handleRequest(HttpServletRequest JavaDoc request,
64                                   HttpServletResponse JavaDoc response, Context ctx)
65     {
66         RollerRequest rreq = null;
67         try
68         {
69             rreq = RollerRequest.getRollerRequest(request, getServletContext());
70         }
71         catch (RollerException e)
72         {
73             // An error initializing the request is considered to be a 404
74
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 JavaDoc 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 JavaDoc e)
101         {
102             mLogger.error("ERROR in FoafServlet", e);
103         }
104         return null;
105     }
106
107     /**
108      * @param ctx
109      */

110     private void setupContext(Context ctx, RollerRequest rreq) throws RollerException
111     {
112         HttpServletRequest JavaDoc request = rreq.getRequest();
113         RollerContext rollerCtx = RollerContext.getRollerContext( request );
114         
115         UserData user = rreq.getUser();
116         ctx.put("fullName", user.getFullName()); // name for FlavorServlet compatibility
117

118         // foaf:homepage to equal base URL for user
119
String JavaDoc homepage = Utilities.escapeHTML(
120                 rollerCtx.getAbsoluteContextUrl(request) +
121                     "/page/" + rreq.getUser().getUserName() );
122         ctx.put("websiteURL", homepage); // name for FlavorServlet compatibility
123

124         // see if foaf:weblog is different Page
125
WebsiteData website = rreq.getWebsite();
126         UserManager usrMgr = RollerContext.getRoller(request).getUserManager();
127         PageData weblog = usrMgr.getPageByName(website, "Weblog");
128         // if weblog != homepage, add to context
129
if (weblog != null && !website.getDefaultPageId().equals(weblog.getId()))
130         {
131             String JavaDoc weblogUrl = Utilities.escapeHTML(
132                     rollerCtx.getAbsoluteContextUrl(request) +
133                         "/page/" + rreq.getUser().getUserName() +
134                             "/" + weblog.getLink() );
135             ctx.put("weblog", weblogUrl);
136         }
137         
138         // use SHA1 encrypted email address, including mailto: prefix
139
String JavaDoc shaEmail = Utilities.encodePassword(
140                 "mailto:" + user.getEmailAddress(), "SHA");
141         ctx.put("shaEmail", shaEmail);
142     }
143
144     //------------------------------------------------------------------------
145
/**
146      * Handle error in Velocity processing.
147      */

148     protected void error( HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res,
149         Exception JavaDoc e) throws ServletException JavaDoc, IOException JavaDoc
150     {
151         mLogger.warn("ERROR in FoafServlet",e);
152     }
153 }
154
Popular Tags