TestServletService.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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 import org.openmobileis.services.common.ServiceManager;
00041 
00042 import freemarker.template.SimpleHash;
00043 import freemarker.template.SimpleList;
00044 import freemarker.template.SimpleScalar;
00045 import freemarker.template.TemplateModelRoot;
00046 
00052 public class TestServletService extends TemplateService {
00053     
00054     private static final String templateName = "testservlettemplates/testservlet.htm";
00055 
00056     public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00057         
00058         templateData.put("authType", new SimpleScalar(req.getAuthType()));
00059         
00060         Enumeration enuma = req.getAttributeNames();
00061         SimpleList attributesList = new SimpleList();
00062         while (enuma.hasMoreElements()) {
00063             SimpleHash attribute = new SimpleHash();
00064             String attributeName = (String)enuma.nextElement();
00065             String attributeValue = (String)req.getAttribute(attributeName);
00066             attribute.put("name", attributeName);
00067             attribute.put("value", attributeValue);
00068             attributesList.add(attribute);
00069         }
00070         templateData.put("attributes", attributesList);
00071         
00072         templateData.put("charEncoding", new SimpleScalar(req.getCharacterEncoding()));
00073         templateData.put("contentLength", new SimpleScalar(Integer.toString(req.getContentLength())));
00074         templateData.put("contentType", new SimpleScalar(req.getContentType()));
00075         templateData.put("contextPath", new SimpleScalar(req.getContextPath()));
00076         
00077         enuma = req.getHeaderNames();
00078         SimpleList headerList = new SimpleList();
00079         while (enuma.hasMoreElements()) {
00080             SimpleHash header = new SimpleHash();
00081             String headerName = (String)enuma.nextElement();
00082             String headerValue = req.getHeader(headerName);
00083             header.put("name", headerName);
00084             header.put("value", headerValue);
00085             headerList.add(header);
00086         }
00087         templateData.put("headers", headerList);
00088         
00089         //templateData.put("localAddr", new SimpleScalar(req.getLocalAddr()));
00090         if (req.getLocale() != null) {
00091             templateData.put("locale", new SimpleScalar(req.getLocale().toString()));
00092         }
00093         //templateData.put("localName", new SimpleScalar(req.getLocalName()));
00094         templateData.put("method", new SimpleScalar(req.getMethod()));
00095         
00096         enuma = req.getParameterNames();
00097         SimpleList parametersList = new SimpleList();
00098         while (enuma.hasMoreElements()) {
00099             SimpleHash parameter = new SimpleHash();
00100             String parameterName = (String)enuma.nextElement();
00101             String parameterValue = req.getParameter(parameterName);
00102             parameter.put("name", parameterName);
00103             parameter.put("value", parameterValue);
00104             parametersList.add(parameter);
00105         }
00106         templateData.put("parameters", parametersList);
00107         
00108         templateData.put("pathInfo", new SimpleScalar(req.getPathInfo()));
00109         templateData.put("pathTranslated", new SimpleScalar(req.getPathTranslated()));
00110         templateData.put("protocol", new SimpleScalar(req.getProtocol()));
00111         templateData.put("queryString", new SimpleScalar(req.getQueryString()));
00112         templateData.put("remoteAddr", new SimpleScalar(req.getRemoteAddr()));
00113         templateData.put("remoteHost", new SimpleScalar(req.getRemoteHost()));
00114         templateData.put("remoteUser", new SimpleScalar(req.getRemoteUser()));
00115         templateData.put("requestSessionId", new SimpleScalar(req.getRequestedSessionId()));
00116         templateData.put("requestURI", new SimpleScalar(req.getRequestURI()));
00117         //templateData.put("remotePort", new SimpleScalar(Integer.toString(req.getRemotePort())));
00118         templateData.put("requestURL", new SimpleScalar(req.getRequestURL().toString()));
00119         templateData.put("scheme", new SimpleScalar(req.getScheme()));
00120         templateData.put("serverName", new SimpleScalar(req.getServerName()));
00121         templateData.put("servletPath", new SimpleScalar(req.getServletPath()));
00122         templateData.put("serverPort", new SimpleScalar(Integer.toString(req.getServerPort())));
00123         
00124         //wait a few second
00125         try     {
00126           Thread.currentThread().sleep(10000);
00127         } catch (Throwable ex)  {
00128           ex.printStackTrace();
00129         }
00130         
00131         return templateName;
00132     }
00133         
00134     public String getServiceUri() {
00135       return ServiceManager.getManager().getServiceBaseURI()+"/testservlet";
00136     }
00137  
00138 }

Generated on Mon Dec 4 11:03:31 2006 for OpenMobileIS by  doxygen 1.5.1-p1