001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/samples/tags/sakai_2-4-1/sample-tool-servlet/src/java/org/sakaiproject/sample/tool/ServletTool2.java $
003: * $Id: ServletTool2.java 9319 2006-05-12 02:32:47Z 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.ActiveTool;
036: import org.sakaiproject.tool.api.Placement;
037: import org.sakaiproject.tool.api.Session;
038: import org.sakaiproject.tool.api.Tool;
039: import org.sakaiproject.tool.api.ToolSession;
040: import org.sakaiproject.tool.cover.SessionManager;
041: import org.sakaiproject.tool.cover.ToolManager;
042: import org.sakaiproject.util.Validator;
043: import org.sakaiproject.util.Web;
044:
045: /**
046: * <p>
047: * A Simple Servlet Sakai Sample tool (number 2), which has some tool modes / destinations controlled by URL
048: * </p>
049: */
050: public class ServletTool2 extends HttpServlet {
051: /** Our log (commons). */
052: private static Log M_log = LogFactory.getLog(ServletTool2.class);
053:
054: /** poor man's service - obviously, no persistence. */
055: private String m_value = "unset";
056:
057: /**
058: * Access the Servlet's information display.
059: *
060: * @return servlet information.
061: */
062: public String getServletInfo() {
063: return "Simple Servlet Sakai Sample tool (2)";
064: }
065:
066: /**
067: * Initialize the servlet.
068: *
069: * @param config
070: * The servlet config.
071: * @throws ServletException
072: */
073: public void init(ServletConfig config) throws ServletException {
074: super .init(config);
075:
076: M_log.info("init()");
077: }
078:
079: /**
080: * Shutdown the servlet.
081: */
082: public void destroy() {
083: M_log.info("destroy()");
084:
085: super .destroy();
086: }
087:
088: /**
089: * Respond to requests.
090: *
091: * @param req
092: * The servlet request.
093: * @param res
094: * The servlet response.
095: * @throws ServletException.
096: * @throws IOException.
097: */
098: protected void doPost(HttpServletRequest req,
099: HttpServletResponse res) throws ServletException,
100: IOException {
101: // get the Tool session
102: ToolSession toolSession = SessionManager
103: .getCurrentToolSession();
104:
105: // take value
106: String value = req.getParameter("value");
107: if (value != null) {
108: m_value = value;
109: }
110:
111: // send redirect to our current destination
112: String current = Web
113: .returnUrl(
114: req,
115: (String) toolSession
116: .getAttribute(ActiveTool.TOOL_ATTR_CURRENT_DESTINATION));
117: res.sendRedirect(res.encodeRedirectURL(current));
118: }
119:
120: /**
121: * Respond to requests.
122: *
123: * @param req
124: * The servlet request.
125: * @param res
126: * The servlet response.
127: * @throws ServletException.
128: * @throws IOException.
129: */
130: protected void doGet(HttpServletRequest req, HttpServletResponse res)
131: throws ServletException, IOException {
132: // get the Sakai session
133: Session session = SessionManager.getCurrentSession();
134:
135: // get the Tool session
136: ToolSession toolSession = SessionManager
137: .getCurrentToolSession();
138:
139: // get the Tool
140: Placement placement = ToolManager.getCurrentPlacement();
141:
142: // dispatch our destinations
143: String path = req.getPathInfo();
144: if (path == null)
145: path = "/";
146:
147: // // deal (manually, for now) with null-path - last mode or home if new
148: // if ("/".equals(path))
149: // {
150: // String currentPath = (String) toolSession.getAttribute(ActiveTool.TOOL_ATTR_CURRENT_DESTINATION);
151: // if (currentPath == null)
152: // {
153: // currentPath = "/home";
154: // }
155: //
156: // String url = Web.returnUrl(req, currentPath);
157: // res.sendRedirect(res.encodeRedirectURL(url));
158: // return;
159: // }
160:
161: // 0 parts means the path was just "/", otherwise parts[0] = "", parts[1] = the first part, etc.
162: String[] parts = path.split("/");
163:
164: String destination = null;
165:
166: // different locations for each path
167: if (parts.length > 1) {
168: destination = parts[1];
169: }
170:
171: // fragment or not?
172: boolean fragment = Boolean.TRUE.toString().equals(
173: req.getAttribute(Tool.FRAGMENT));
174:
175: // our response writer
176: PrintWriter out = null;
177:
178: if (!fragment) {
179: // write a full HTML header
180: res.setContentType("text/html; charset=UTF-8");
181: res.addDateHeader("Expires", System.currentTimeMillis()
182: - (1000L * 60L * 60L * 24L * 365L));
183: res.addDateHeader("Last-Modified", System
184: .currentTimeMillis());
185: res
186: .addHeader("Cache-Control",
187: "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
188: res.addHeader("Pragma", "no-cache");
189:
190: out = res.getWriter();
191: out
192: .println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
193: out
194: .println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
195: out
196: .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
197: out.println("<head>");
198: out
199: .println("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />");
200:
201: // include the portal's stuff in head (css and js links)
202: String headInclude = (String) req
203: .getAttribute("sakai.html.head");
204: if (headInclude != null) {
205: out.println(headInclude);
206: }
207:
208: // use our placement title, with our destination
209: out.print("<title>");
210: if (placement != null) {
211: out.print(Validator.escapeHtml(placement.getTitle()));
212: if (destination != null) {
213: out
214: .print(" - "
215: + Validator.escapeHtml(destination));
216: }
217: }
218: out.println("</title>");
219:
220: out.print("</head><body");
221:
222: // do the body the portal wants to do
223: String onload = (String) req
224: .getAttribute("sakai.html.body.onload");
225: if (onload != null) {
226: out.print(" onload=\"" + onload + "\"");
227: }
228: out.println(">");
229:
230: } else {
231: out = res.getWriter();
232: }
233:
234: out.println("<div class=\"portletBody\">");
235:
236: // show the current user session information (for the user across all Sakai tools)
237: if (session == null) {
238: out.println("no session established");
239: out.println("<br />");
240: } else {
241: out.println("session id: " + session.getId());
242: out.println("<br />");
243: out.println("session user id: " + session.getUserId());
244: out.println("<br />");
245: out.println("session user eid: " + session.getUserEid());
246: out.println("<br />");
247: out.println("session started: "
248: + DateFormat.getDateInstance().format(
249: new Date(session.getCreationTime())));
250: out.println("<br />");
251: out.println("session accessed: "
252: + DateFormat.getDateInstance().format(
253: new Date(session.getLastAccessedTime())));
254: out.println("<br />");
255: out.println("session inactive after: "
256: + session.getMaxInactiveInterval());
257: out.println("<br />");
258: }
259:
260: // show the tool session (for this tool placement / user)
261: if (toolSession == null) {
262: out.println("no tool session established");
263: out.println("<br />");
264: } else {
265: out.println("tool session id: " + toolSession.getId());
266: out.println("<br />");
267: out.println("tool session started: "
268: + DateFormat.getDateInstance().format(
269: new Date(toolSession.getCreationTime())));
270: out.println("<br />");
271: out.println("tool session last accessed: "
272: + DateFormat.getDateInstance()
273: .format(
274: new Date(toolSession
275: .getLastAccessedTime())));
276: out.println("<br />");
277: }
278:
279: // show the placement information
280: if (placement == null) {
281: out.println("no placement");
282: out.println("<br />");
283: } else {
284: out.println("placement id: " + placement.getId());
285: out.println("<br />");
286: out.println("placement context: " + placement.getContext());
287: out.println("<br />");
288: out.println("placement title: " + placement.getTitle());
289: out.println("<br />");
290: out.println("placement tool: " + placement.getToolId());
291: // placement.getConfig();
292: // placement.getPlacementConfig();
293: out.println("<br />");
294: }
295:
296: // show value
297: out.println("<p>Value: " + m_value + "</p>");
298:
299: // different locations for each path
300: if (destination == null) {
301: out.println("<p>no destination</p>");
302: } else {
303: out.println("<p>destination: " + destination + "</p>");
304: }
305:
306: out.println("</div>");
307:
308: // link to some destinations
309: String home = Web.returnUrl(req, "/home");
310: String destA = Web.returnUrl(req, "/a");
311: String destB = Web.returnUrl(req, "/b/123-45-6789");
312: out.println("<p>navigation<ul><li><a href=\"" + home
313: + "\">home</a></li><li><a href=\"" + destA
314: + "\">a</a></li><li><a href=\"" + destB
315: + "\">b</a></li></ul></p>");
316:
317: // a data posting
318: out
319: .println("<p><form method=\"post\" action=\""
320: + destB
321: + "\">value:<input name=\"value\" id=\"value\" type=\"text\" /><input name=\"submit\" type=\"submit\" id=\"submit\" value=\"submit\" /></form></p>");
322:
323: Web.snoop(out, true, getServletConfig(), req);
324:
325: if (!fragment) {
326: // end the complete document
327: out.println("</body></html>");
328: }
329:
330: // // keep track (manually, for now) of our current destination
331: // toolSession.setAttribute(ActiveTool.TOOL_ATTR_CURRENT_DESTINATION, path);
332: }
333: }
|