Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

TestServletService.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2005 Modification Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.embedded.webserver.test.services;
00031 
00032 import java.io.IOException;
00033 import java.util.Enumeration;
00034 
00035 import javax.servlet.ServletException;
00036 import javax.servlet.http.HttpServletRequest;
00037 import javax.servlet.http.HttpServletResponse;
00038 
00039 import org.openmobileis.services.TemplateService;
00040 
00041 import freemarker.template.SimpleHash;
00042 import freemarker.template.SimpleList;
00043 import freemarker.template.SimpleScalar;
00044 import freemarker.template.TemplateModelRoot;
00045 
00051 public class TestServletService extends TemplateService {
00052     
00053     private static final String templateName = "testservlettemplates/testservlet.htm";
00054 
00055     public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00056         
00057         templateData.put("authType", new SimpleScalar(req.getAuthType()));
00058         
00059         Enumeration enuma = req.getAttributeNames();
00060         SimpleList attributesList = new SimpleList();
00061         while (enuma.hasMoreElements()) {
00062             SimpleHash attribute = new SimpleHash();
00063             String attributeName = (String)enuma.nextElement();
00064             String attributeValue = (String)req.getAttribute(attributeName);
00065             attribute.put("name", attributeName);
00066             attribute.put("value", attributeValue);
00067             attributesList.add(attribute);
00068         }
00069         templateData.put("attributes", attributesList);
00070         
00071         templateData.put("charEncoding", new SimpleScalar(req.getCharacterEncoding()));
00072         templateData.put("contentLength", new SimpleScalar(Integer.toString(req.getContentLength())));
00073         templateData.put("contentType", new SimpleScalar(req.getContentType()));
00074         templateData.put("contextPath", new SimpleScalar(req.getContextPath()));
00075         
00076         enuma = req.getHeaderNames();
00077         SimpleList headerList = new SimpleList();
00078         while (enuma.hasMoreElements()) {
00079             SimpleHash header = new SimpleHash();
00080             String headerName = (String)enuma.nextElement();
00081             String headerValue = req.getHeader(headerName);
00082             header.put("name", headerName);
00083             header.put("value", headerValue);
00084             headerList.add(header);
00085         }
00086         templateData.put("headers", headerList);
00087         
00088         //templateData.put("localAddr", new SimpleScalar(req.getLocalAddr()));
00089         if (req.getLocale() != null) {
00090             templateData.put("locale", new SimpleScalar(req.getLocale().toString()));
00091         }
00092         //templateData.put("localName", new SimpleScalar(req.getLocalName()));
00093         templateData.put("method", new SimpleScalar(req.getMethod()));
00094         
00095         enuma = req.getParameterNames();
00096         SimpleList parametersList = new SimpleList();
00097         while (enuma.hasMoreElements()) {
00098             SimpleHash parameter = new SimpleHash();
00099             String parameterName = (String)enuma.nextElement();
00100             String parameterValue = req.getParameter(parameterName);
00101             parameter.put("name", parameterName);
00102             parameter.put("value", parameterValue);
00103             parametersList.add(parameter);
00104         }
00105         templateData.put("parameters", parametersList);
00106         
00107         templateData.put("pathInfo", new SimpleScalar(req.getPathInfo()));
00108         templateData.put("pathTranslated", new SimpleScalar(req.getPathTranslated()));
00109         templateData.put("protocol", new SimpleScalar(req.getProtocol()));
00110         templateData.put("queryString", new SimpleScalar(req.getQueryString()));
00111         templateData.put("remoteAddr", new SimpleScalar(req.getRemoteAddr()));
00112         templateData.put("remoteHost", new SimpleScalar(req.getRemoteHost()));
00113         templateData.put("remoteUser", new SimpleScalar(req.getRemoteUser()));
00114         templateData.put("requestSessionId", new SimpleScalar(req.getRequestedSessionId()));
00115         templateData.put("requestURI", new SimpleScalar(req.getRequestURI()));
00116         //templateData.put("remotePort", new SimpleScalar(Integer.toString(req.getRemotePort())));
00117         templateData.put("requestURL", new SimpleScalar(req.getRequestURL().toString()));
00118         templateData.put("scheme", new SimpleScalar(req.getScheme()));
00119         templateData.put("serverName", new SimpleScalar(req.getServerName()));
00120         templateData.put("servletPath", new SimpleScalar(req.getServletPath()));
00121         templateData.put("serverPort", new SimpleScalar(Integer.toString(req.getServerPort())));
00122         
00123         //wait a few second
00124         try     {
00125           Thread.currentThread().sleep(10000);
00126         } catch (Throwable ex)  {
00127           ex.printStackTrace();
00128         }
00129         
00130         return templateName;
00131     }
00132     
00133     public String getName() {
00134         return "TestServlet";
00135     }
00136 
00137 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4