001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/samples/tags/sakai_2-4-1/sample-tool-servlet/src/java/org/sakaiproject/sample/tool/ServletTool.java $
003: * $Id: ServletTool.java 9286 2006-05-11 03:17:27Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.sample.tool;
021:
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.text.DateFormat;
025: import java.util.Date;
026:
027: import javax.servlet.ServletConfig;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.sakaiproject.tool.api.Placement;
036: import org.sakaiproject.tool.api.Session;
037: import org.sakaiproject.tool.api.Tool;
038: import org.sakaiproject.tool.api.ToolSession;
039: import org.sakaiproject.tool.cover.SessionManager;
040: import org.sakaiproject.tool.cover.ToolManager;
041: import org.sakaiproject.util.Validator;
042: import org.sakaiproject.util.Web;
043:
044: /**
045: * <p>
046: * A Simple Servlet Sakai Sample tool.
047: * </p>
048: */
049: public class ServletTool extends HttpServlet {
050: /** Our log (commons). */
051: private static Log M_log = LogFactory.getLog(ServletTool.class);
052:
053: /**
054: * Access the Servlet's information display.
055: *
056: * @return servlet information.
057: */
058: public String getServletInfo() {
059: return "Simple Servlet Sakai Sample tool";
060: }
061:
062: /**
063: * Initialize the servlet.
064: *
065: * @param config
066: * The servlet config.
067: * @throws ServletException
068: */
069: public void init(ServletConfig config) throws ServletException {
070: super .init(config);
071:
072: M_log.info("init()");
073: }
074:
075: /**
076: * Shutdown the servlet.
077: */
078: public void destroy() {
079: M_log.info("destroy()");
080:
081: super .destroy();
082: }
083:
084: /**
085: * Respond to requests.
086: *
087: * @param req
088: * The servlet request.
089: * @param res
090: * The servlet response.
091: * @throws ServletException.
092: * @throws IOException.
093: */
094: protected void doGet(HttpServletRequest req, HttpServletResponse res)
095: throws ServletException, IOException {
096: // get the Sakai session
097: Session session = SessionManager.getCurrentSession();
098:
099: // get the Tool session
100: ToolSession toolSession = SessionManager
101: .getCurrentToolSession();
102:
103: // get the Tool
104: Placement placement = ToolManager.getCurrentPlacement();
105:
106: // fragment or not?
107: boolean fragment = Boolean.TRUE.toString().equals(
108: req.getAttribute(Tool.FRAGMENT));
109:
110: // our response writer
111: PrintWriter out = null;
112:
113: if (!fragment) {
114: // write a full HTML header
115: res.setContentType("text/html; charset=UTF-8");
116: out = res.getWriter();
117: out
118: .println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
119: out
120: .println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
121: out
122: .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
123: out.println("<head>");
124: out
125: .println("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />");
126:
127: // include the portal's stuff in head (css and js links)
128: String headInclude = (String) req
129: .getAttribute("sakai.html.head");
130: if (headInclude != null) {
131: out.println(headInclude);
132: }
133:
134: // use our placement title
135: out.println("<title>");
136: if (placement != null) {
137: out.println(Validator.escapeHtml(placement.getTitle()));
138: }
139: out.println("</title>");
140:
141: out.print("</head><body");
142:
143: // do the body the portal wants to do
144: String onload = (String) req
145: .getAttribute("sakai.html.body.onload");
146: if (onload != null) {
147: out.print(" onload=\"" + onload + "\"");
148: }
149: out.println(">");
150:
151: } else {
152: out = res.getWriter();
153: }
154:
155: out.println("<div class=\"portletBody\">");
156:
157: // show the current user session information (for the user across all Sakai tools)
158: if (session == null) {
159: out.println("no session established");
160: out.println("<br />");
161: } else {
162: out.println("session id: " + session.getId());
163: out.println("<br />");
164: out.println("session user id: " + session.getUserId());
165: out.println("<br />");
166: out.println("session user eid: " + session.getUserEid());
167: out.println("<br />");
168: out.println("session started: "
169: + DateFormat.getDateInstance().format(
170: new Date(session.getCreationTime())));
171: out.println("<br />");
172: out.println("session accessed: "
173: + DateFormat.getDateInstance().format(
174: new Date(session.getLastAccessedTime())));
175: out.println("<br />");
176: out.println("session inactive after: "
177: + session.getMaxInactiveInterval());
178: out.println("<br />");
179: }
180:
181: // show the tool session (for this tool placement / user)
182: if (toolSession == null) {
183: out.println("no tool session established");
184: out.println("<br />");
185: } else {
186: out.println("tool session id: " + toolSession.getId());
187: out.println("<br />");
188: out.println("tool session started: "
189: + DateFormat.getDateInstance().format(
190: new Date(toolSession.getCreationTime())));
191: out.println("<br />");
192: out.println("tool session last accessed: "
193: + DateFormat.getDateInstance()
194: .format(
195: new Date(toolSession
196: .getLastAccessedTime())));
197: out.println("<br />");
198: }
199:
200: // show the placement information
201: if (placement == null) {
202: out.println("no placement");
203: out.println("<br />");
204: } else {
205: out.println("placement id: " + placement.getId());
206: out.println("<br />");
207: out.println("placement context: " + placement.getContext());
208: out.println("<br />");
209: out.println("placement title: " + placement.getTitle());
210: out.println("<br />");
211: out.println("placement tool: " + placement.getToolId());
212: // placement.getConfig();
213: // placement.getPlacementConfig();
214: out.println("<br />");
215: }
216:
217: Web.snoop(out, true, getServletConfig(), req);
218:
219: out.println("</div>");
220:
221: if (!fragment) {
222: // end the complete document
223: out.println("</body></html>");
224: }
225: }
226: }
|