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 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
00090 if (req.getLocale() != null) {
00091 templateData.put("locale", new SimpleScalar(req.getLocale().toString()));
00092 }
00093
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
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
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 }