001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: TestProxyGeneratorServlet.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.cocoon.generation;
022:
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import java.util.Enumeration;
026:
027: import javax.servlet.ServletConfig;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.Cookie;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import javax.servlet.http.HttpSession;
034:
035: /**
036: * Proxy Generator Servlet Test
037: */
038: public class TestProxyGeneratorServlet extends HttpServlet {
039: /**
040: * Initialize, given a servlet configuration
041: * @param config The configuration
042: * @throws ServletException if an error occurs
043: */
044: public void init(ServletConfig config) throws ServletException {
045: super .init(config);
046: }
047:
048: /**
049: * Get a request
050: * @param request The request to get
051: * @param response The response to put it in
052: * @throws IOException if an IO error occurs
053: * @throws ServletException if an error occurs
054: */
055: public void doGet(HttpServletRequest request,
056: HttpServletResponse response) throws IOException,
057: ServletException {
058: response.setContentType("text/xml");
059:
060: PrintWriter writer = response.getWriter();
061: writer.print("<?xml version=\"1.0\"?>");
062: writer.print("<servlet class=\"" + this .getClass().getName()
063: + "\">");
064: writer.print("<request method=\"GET\">");
065: writer.print(getRequestInfo(request));
066: writer.print(getParameters(request));
067: writer.print(getSession(request));
068: writer.print(getCookies(request));
069: writer.print("</request>");
070: writer.print("</servlet>");
071: }
072:
073: /**
074: * Do a POST using the request and store the response in the response object
075: * @param request The request
076: * @param response The response
077: * @throws ServletException if an error occurs
078: * @throws IOException if an IO error occurs
079: */
080: public void doPost(HttpServletRequest request,
081: HttpServletResponse response) throws ServletException,
082: IOException {
083: response.setContentType("text/xml");
084:
085: PrintWriter writer = response.getWriter();
086: writer.print("<?xml version=\"1.0\"?>");
087: writer.print("<servlet class=\"" + this .getClass().getName()
088: + "\">");
089: writer.print("<request method=\"POST\">");
090: writer.print(getRequestInfo(request));
091: writer.print(getParameters(request));
092: writer.print(getSession(request));
093: writer.print(getCookies(request));
094: writer.print("</request>");
095: writer.print("</servlet>");
096: }
097:
098: /**
099: * Return the information about a request: URI, Server name and port
100: * @param request The request
101: * @return The information (as XML)
102: */
103: public String getRequestInfo(HttpServletRequest request) {
104: StringBuffer sb = new StringBuffer("");
105: sb.append("<URI>" + request.getRequestURI() + "</URI>");
106: sb.append("<servername>" + request.getServerName()
107: + "</servername>");
108: sb.append("<serverport>" + request.getServerPort()
109: + "</serverport>");
110:
111: return sb.toString();
112: }
113:
114: /**
115: * Get all parameters from a request
116: * @param request The request
117: * @return The parameters (as XML)
118: */
119: public String getParameters(HttpServletRequest request) {
120: StringBuffer sb = new StringBuffer("");
121: Enumeration parameters = request.getParameterNames();
122: boolean hasParameters = parameters.hasMoreElements();
123:
124: if (hasParameters) {
125: sb.append("<parameters>");
126: }
127:
128: while (parameters.hasMoreElements()) {
129: String name = (String) parameters.nextElement();
130: String[] values = request.getParameterValues(name);
131: sb.append("<parameter name=\"" + name + "\">");
132:
133: for (int i = 0; i < values.length; i++) {
134: sb.append("<value>" + values[i] + "</value>");
135: }
136:
137: sb.append("</parameter>");
138: }
139:
140: if (hasParameters) {
141: sb.append("</parameters>");
142: }
143:
144: return sb.toString();
145: }
146:
147: /**
148: * Get all session attributes of a request
149: * @param request The request
150: * @return The session attributes (in XML)
151: */
152: public String getSession(HttpServletRequest request) {
153: StringBuffer sb = new StringBuffer("");
154:
155: //HttpSession session=request.getSession(true);
156: HttpSession session = request.getSession(false);
157:
158: if (session != null) {
159: sb.append("<session>");
160:
161: Enumeration attributes = session.getAttributeNames();
162:
163: if (!attributes.hasMoreElements()) {
164: sb.append("<noattributes/>");
165: }
166:
167: while (attributes.hasMoreElements()) {
168: String attributeName = (String) attributes
169: .nextElement();
170: sb.append("<attribute name=\"" + attributeName + "\">");
171: sb.append("" + session.getAttribute(attributeName));
172: sb.append("</attribute>");
173: }
174:
175: sb.append("</session>");
176: } else {
177: sb.append("<nosession/>");
178: }
179:
180: return sb.toString();
181: }
182:
183: /**
184: * Return all cookies of a request
185: * @param request The request
186: * @return The cookies (as XML)
187: */
188: public String getCookies(HttpServletRequest request) {
189: StringBuffer sb = new StringBuffer("");
190:
191: Cookie[] cookies = request.getCookies();
192:
193: if (cookies != null) {
194: if (cookies.length > 0) {
195: sb.append("<cookies>");
196:
197: for (int i = 0; i < cookies.length; i++) {
198: sb.append("<cookie>");
199: sb.append("<comment>" + cookies[i].getComment()
200: + "</comment>");
201: sb.append("<domain>" + cookies[i].getDomain()
202: + "</domain>");
203: sb.append("<maxage>" + cookies[i].getMaxAge()
204: + "</maxage>");
205: sb.append("<name>" + cookies[i].getName()
206: + "</name>");
207: sb.append("<path>" + cookies[i].getPath()
208: + "</path>");
209: sb.append("<secure>" + cookies[i].getSecure()
210: + "</secure>");
211: sb.append("<value>" + cookies[i].getValue()
212: + "</value>");
213: sb.append("<version>" + cookies[i].getVersion()
214: + "</version>");
215: sb.append("</cookie>");
216: }
217:
218: sb.append("</cookies>");
219: } else {
220: sb.append("<nocookies/>");
221: }
222: } else {
223: sb.append("<nocookies/>");
224: }
225:
226: return sb.toString();
227: }
228: }
|