KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > generation > TestProxyGeneratorServlet


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: TestProxyGeneratorServlet.java 42625 2004-03-04 15:45:03Z egli $ */
19
20 package org.apache.lenya.cms.cocoon.generation;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.Cookie JavaDoc;
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33
34
35 public class TestProxyGeneratorServlet extends HttpServlet JavaDoc {
36     /**
37      * DOCUMENT ME!
38      *
39      * @param config DOCUMENT ME!
40      *
41      * @throws ServletException DOCUMENT ME!
42      */

43     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
44         super.init(config);
45     }
46
47     /**
48      * DOCUMENT ME!
49      *
50      * @param request DOCUMENT ME!
51      * @param response DOCUMENT ME!
52      *
53      * @throws IOException DOCUMENT ME!
54      * @throws ServletException DOCUMENT ME!
55      */

56     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57         throws IOException JavaDoc, ServletException JavaDoc {
58         response.setContentType("text/xml");
59
60         PrintWriter JavaDoc writer = response.getWriter();
61         writer.print("<?xml version=\"1.0\"?>");
62         writer.print("<servlet class=\"" + this.getClass().getName() + "\">");
63         writer.print("<request method=\"GET\">");
64         writer.print(getRequestInfo(request));
65         writer.print(getParameters(request));
66         writer.print(getSession(request));
67         writer.print(getCookies(request));
68         writer.print("</request>");
69         writer.print("</servlet>");
70     }
71
72     /**
73      * DOCUMENT ME!
74      *
75      * @param request DOCUMENT ME!
76      * @param response DOCUMENT ME!
77      *
78      * @throws ServletException DOCUMENT ME!
79      * @throws IOException DOCUMENT ME!
80      */

81     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
82         throws ServletException JavaDoc, IOException JavaDoc {
83         response.setContentType("text/xml");
84
85         PrintWriter JavaDoc writer = response.getWriter();
86         writer.print("<?xml version=\"1.0\"?>");
87         writer.print("<servlet class=\"" + this.getClass().getName() + "\">");
88         writer.print("<request method=\"POST\">");
89         writer.print(getRequestInfo(request));
90         writer.print(getParameters(request));
91         writer.print(getSession(request));
92         writer.print(getCookies(request));
93         writer.print("</request>");
94         writer.print("</servlet>");
95     }
96
97     /**
98      * DOCUMENT ME!
99      *
100      * @param request DOCUMENT ME!
101      *
102      * @return DOCUMENT ME!
103      */

104     public String JavaDoc getRequestInfo(HttpServletRequest JavaDoc request) {
105         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
106         sb.append("<URI>" + request.getRequestURI() + "</URI>");
107         sb.append("<servername>" + request.getServerName() + "</servername>");
108         sb.append("<serverport>" + request.getServerPort() + "</serverport>");
109
110         return sb.toString();
111     }
112
113     /**
114      * DOCUMENT ME!
115      *
116      * @param request DOCUMENT ME!
117      *
118      * @return DOCUMENT ME!
119      */

120     public String JavaDoc getParameters(HttpServletRequest JavaDoc request) {
121         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
122         Enumeration JavaDoc parameters = request.getParameterNames();
123         boolean hasParameters = parameters.hasMoreElements();
124
125         if (hasParameters) {
126             sb.append("<parameters>");
127         }
128
129         while (parameters.hasMoreElements()) {
130             String JavaDoc name = (String JavaDoc) parameters.nextElement();
131             String JavaDoc[] values = request.getParameterValues(name);
132             sb.append("<parameter name=\"" + name + "\">");
133
134             for (int i = 0; i < values.length; i++) {
135                 sb.append("<value>" + values[i] + "</value>");
136             }
137
138             sb.append("</parameter>");
139         }
140
141         if (hasParameters) {
142             sb.append("</parameters>");
143         }
144
145         return sb.toString();
146     }
147
148     /**
149      * DOCUMENT ME!
150      *
151      * @param request DOCUMENT ME!
152      *
153      * @return DOCUMENT ME!
154      */

155     public String JavaDoc getSession(HttpServletRequest JavaDoc request) {
156         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
157
158         //HttpSession session=request.getSession(true);
159
HttpSession JavaDoc session = request.getSession(false);
160
161         if (session != null) {
162             sb.append("<session>");
163
164             Enumeration JavaDoc attributes = session.getAttributeNames();
165
166             if (!attributes.hasMoreElements()) {
167                 sb.append("<noattributes/>");
168             }
169
170             while (attributes.hasMoreElements()) {
171                 String JavaDoc attributeName = (String JavaDoc) attributes.nextElement();
172                 sb.append("<attribute name=\"" + attributeName + "\">");
173                 sb.append("" + session.getAttribute(attributeName));
174                 sb.append("</attribute>");
175             }
176
177             sb.append("</session>");
178         } else {
179             sb.append("<nosession/>");
180         }
181
182         return sb.toString();
183     }
184
185     /**
186      * DOCUMENT ME!
187      *
188      * @param request DOCUMENT ME!
189      *
190      * @return DOCUMENT ME!
191      */

192     public String JavaDoc getCookies(HttpServletRequest JavaDoc request) {
193         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
194
195         Cookie JavaDoc[] cookies = request.getCookies();
196
197         if (cookies != null) {
198             if (cookies.length > 0) {
199                 sb.append("<cookies>");
200
201                 for (int i = 0; i < cookies.length; i++) {
202                     sb.append("<cookie>");
203                     sb.append("<comment>" + cookies[i].getComment() + "</comment>");
204                     sb.append("<domain>" + cookies[i].getDomain() + "</domain>");
205                     sb.append("<maxage>" + cookies[i].getMaxAge() + "</maxage>");
206                     sb.append("<name>" + cookies[i].getName() + "</name>");
207                     sb.append("<path>" + cookies[i].getPath() + "</path>");
208                     sb.append("<secure>" + cookies[i].getSecure() + "</secure>");
209                     sb.append("<value>" + cookies[i].getValue() + "</value>");
210                     sb.append("<version>" + cookies[i].getVersion() + "</version>");
211                     sb.append("</cookie>");
212                 }
213
214                 sb.append("</cookies>");
215             } else {
216                 sb.append("<nocookies/>");
217             }
218         } else {
219             sb.append("<nocookies/>");
220         }
221
222         return sb.toString();
223     }
224 }
225
Popular Tags