00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00089 if (req.getLocale() != null) {
00090 templateData.put("locale", new SimpleScalar(req.getLocale().toString()));
00091 }
00092
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
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
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 }