View Javadoc

1   package org.restafarian.id.servlets;
2   
3   import java.io.IOException;
4   import java.io.PrintWriter;
5   
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.restafarian.core.beans.Person;
11  import org.restafarian.core.security.AuthenticatedUserManager;
12  import org.restafarian.core.servlets.RestServletBase;
13  
14  /***
15   * <p>This servlet returns information about the currenly signed on user.</p>
16   */
17  public class WhoAmIServlet extends RestServletBase {
18  	private static final long serialVersionUID = 1;
19  
20  	/***
21  	 * <p>The Servlet "doGet()" method.</p>
22  	 *
23  	 * @param req the <code>HttpServletRequest</code> object
24  	 * @param res the <code>HttpServletResponse</code> object
25  	 * @throws ServletException
26  	 * @throws IOException
27  	 */
28  	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
29  		StringBuffer buffer = new StringBuffer();
30  
31  		Person user = AuthenticatedUserManager.getAuthenticatedUser(req);
32  		if (user == null) {
33  			res.sendError(404, "The requested resource was not found on this server. If you entered the URL manually please check your spelling and try again.");
34  		} else {
35  			buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
36  			buffer.append("<?xml-stylesheet type=\"text/xsl\" href=\"");
37  			buffer.append(req.getContextPath());
38  			buffer.append("/xsl/whoami.xsl\"?>\n");
39  			buffer.append("<whoami xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n");
40  			buffer.append("  <remoteAddr>");
41  			buffer.append(req.getRemoteAddr());
42  			buffer.append("</remoteAddr>\n");
43  			buffer.append("  <remoteHost>");
44  			buffer.append(req.getRemoteHost());
45  			buffer.append("</remoteHost>\n");
46  			buffer.append("  <remoteUser id=\"");
47  			buffer.append(user.getId());
48  			buffer.append("\" xlink:href=\"");
49  			buffer.append(user.getUri());
50  			buffer.append("\">");
51  			buffer.append(user.getName());
52  			buffer.append("</remoteUser>\n");
53  			buffer.append("</whoami>");
54  			PrintWriter pw = res.getWriter();
55  			pw.print(buffer.toString());
56  		}
57  	}
58  
59  	/***
60  	 * <p>The Servlet "doPost()" method.</p>
61  	 * 
62  	 * @param req the <code>HttpServletRequest</code> object
63  	 * @param res the <code>HttpServletResponse</code> object
64  	 * @throws ServletException
65  	 * @throws IOException
66  	 */
67  	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
68  		res.sendError(405, "Method Not Allowed. Use the \"GET\" method for this URL");
69  	}
70  
71  	/***
72  	 * <p>The Servlet "doPut()" method.</p>
73  	 *
74  	 * @param req the <code>HttpServletRequest</code> object
75  	 * @param res the <code>HttpServletResponse</code> object
76  	 * @throws ServletException
77  	 * @throws IOException
78  	 */
79  	public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
80  		res.sendError(405, "Method Not Allowed. Use the \"GET\" method for this URL");
81  	}
82  
83  	/***
84  	 * <p>The Servlet "doDelete()" method.</p>
85  	 *
86  	 * @param req the <code>HttpServletRequest</code> object
87  	 * @param res the <code>HttpServletResponse</code> object
88  	 * @throws ServletException
89  	 * @throws IOException
90  	 */
91  	public void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
92  		res.sendError(405, "Method Not Allowed. Use the \"GET\" method for this URL");
93  	}
94  }