001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.util;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.security.Principal;
028: import java.util.Enumeration;
029:
030: import javax.servlet.ServletRequest;
031: import javax.servlet.http.Cookie;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpSession;
034: import javax.xml.parsers.FactoryConfigurationError;
035: import javax.xml.parsers.ParserConfigurationException;
036: import javax.xml.transform.TransformerException;
037:
038: import org.w3c.dom.Element;
039:
040: import biz.hammurapi.web.ActionsBase;
041: import biz.hammurapi.xml.dom.AbstractDomObject;
042: import biz.hammurapi.xml.dom.CompositeDomSerializer;
043: import biz.hammurapi.xml.dom.DOMUtils;
044: import biz.hammurapi.xml.dom.DomSerializable;
045:
046: /**
047: * @author Pavel Vlasov
048: * @version $Revision$
049: */
050: public class HttpRequestDomSerializer implements
051: CompositeDomSerializer.Member {
052:
053: private CompositeDomSerializer owner;
054: private boolean withAttributes;
055:
056: /**
057: * Same as HttpRequestDomSerializer(false);
058: *
059: */
060: public HttpRequestDomSerializer() {
061: this (false);
062: }
063:
064: /**
065: *
066: * @param withAttributes If true request and session attributes are serialized to xml along with other
067: * request parameters.
068: */
069: public HttpRequestDomSerializer(boolean withAttributes) {
070: // Default constructor
071: this .withAttributes = withAttributes;
072: }
073:
074: /* (non-Javadoc)
075: * @see biz.hammurapi.xml.dom.CompositeDomSerializer.Member#setOwner(biz.hammurapi.xml.dom.CompositeDomSerializer)
076: */
077: public void setOwner(CompositeDomSerializer owner) {
078: this .owner = owner;
079: }
080:
081: public DomSerializable toDomSerializable(
082: final ServletRequest request) {
083: return new DomSerializable() {
084:
085: public void toDom(Element holder) {
086: populate(request, holder);
087: }
088:
089: };
090: }
091:
092: /**
093: * @param request
094: * @param holder
095: */
096: protected void populate(ServletRequest request, Element holder) {
097: setAttribute(holder, "character-encoding", request
098: .getCharacterEncoding());
099: setAttribute(holder, "content-length", request
100: .getContentLength());
101: setAttribute(holder, "content-type", request.getContentType());
102: //setAttribute(holder, "local-address", request.getLocalAddr());
103:
104: Enumeration locales = request.getLocales();
105: if (locales != null) {
106: owner.toDomSerializable(locales).toDom(
107: AbstractDomObject.addElement(holder, "locales"));
108: }
109:
110: //setAttribute(holder, "local-name", request.getLocalName());
111: //setAttribute(holder, "local-port", request.getLocalPort());
112:
113: Enumeration pEnum = request.getParameterNames();
114: while (pEnum != null && pEnum.hasMoreElements()) {
115: String pName = (String) pEnum.nextElement();
116: Element pe = AbstractDomObject.addElement(holder,
117: "parameter");
118: pe.setAttribute("name", pName);
119: owner.toDomSerializable(request.getParameterValues(pName))
120: .toDom(pe);
121: }
122:
123: setAttribute(holder, "protocol", request.getProtocol());
124: setAttribute(holder, "remote-address", request.getRemoteAddr());
125: setAttribute(holder, "remote-host", request.getRemoteHost());
126: //setAttribute(holder, "remote-port", request.getRemotePort());
127: setAttribute(holder, "scheme", request.getScheme());
128: setAttribute(holder, "server-name", request.getServerName());
129: setAttribute(holder, "server-port", request.getServerPort());
130: setAttribute(holder, "secure", request.isSecure());
131:
132: if (withAttributes) {
133: Enumeration ae = request.getAttributeNames();
134: while (ae.hasMoreElements()) {
135: String attribute = (String) ae.nextElement();
136: Object value = request.getAttribute(attribute);
137: if (value != null) {
138: Element attributeElement = AbstractDomObject
139: .addTextElement(holder, "attribute", value
140: .toString());
141: attributeElement.setAttribute("type", value
142: .getClass().getName());
143: attributeElement.setAttribute("name", attribute);
144: }
145: }
146: }
147: }
148:
149: /**
150: * Sets attribute if value is not null.
151: * @param holder
152: * @param name
153: * @param value
154: */
155: private void setAttribute(Element holder, String name, String value) {
156: if (value != null) {
157: holder.setAttribute(name, value);
158: }
159: }
160:
161: private void setAttribute(Element holder, String name, int value) {
162: holder.setAttribute(name, String.valueOf(value));
163: }
164:
165: private void setAttribute(Element holder, String name, boolean value) {
166: holder.setAttribute(name, value ? "yes" : "no");
167: }
168:
169: public DomSerializable toDomSerializable(
170: final HttpServletRequest request) {
171: return new DomSerializable() {
172:
173: public void toDom(Element holder) {
174: populate(request, holder);
175:
176: setAttribute(holder, "authorization-type", request
177: .getAuthType());
178: setAttribute(holder, "context-path", request
179: .getContextPath());
180: Element ce = AbstractDomObject.addElement(holder,
181: "cookies");
182: Cookie[] cookies = request.getCookies();
183: if (cookies != null) {
184: owner.toDomSerializable(cookies).toDom(ce);
185: }
186:
187: Enumeration hEnum = request.getHeaderNames();
188: while (hEnum != null && hEnum.hasMoreElements()) {
189: String hName = (String) hEnum.nextElement();
190: Element he = AbstractDomObject.addElement(holder,
191: "header");
192: he.setAttribute("name", hName);
193: owner.toDomSerializable(request.getHeaders(hName))
194: .toDom(he);
195: }
196:
197: setAttribute(holder, "method", request.getMethod());
198: setAttribute(holder, "path-info", request.getPathInfo());
199: setAttribute(holder, "path-translated", request
200: .getPathTranslated());
201: setAttribute(holder, "query-string", request
202: .getQueryString());
203: setAttribute(holder, "remote-user", request
204: .getRemoteUser());
205: setAttribute(holder, "requested-session-id", request
206: .getRequestedSessionId());
207: setAttribute(holder, "request-uri", request
208: .getRequestURI());
209: setAttribute(holder, "servlet-path", request
210: .getServletPath());
211:
212: Principal principal = request.getUserPrincipal();
213: if (principal != null) {
214: setAttribute(holder, "principal", principal
215: .getName());
216: }
217:
218: setAttribute(holder,
219: "requested-session-id-from-cookie", request
220: .isRequestedSessionIdFromCookie());
221: setAttribute(holder, "requested-session-id-from-url",
222: request.isRequestedSessionIdFromURL());
223: setAttribute(holder, "requested-session-id-valid",
224: request.isRequestedSessionIdValid());
225:
226: if (withAttributes) {
227: HttpSession session = request.getSession(false);
228: if (session != null) {
229: Enumeration ae = session.getAttributeNames();
230: while (ae.hasMoreElements()) {
231: String attribute = (String) ae
232: .nextElement();
233: Object value = session
234: .getAttribute(attribute);
235: if (value != null) {
236: Element attributeElement = AbstractDomObject
237: .addTextElement(holder,
238: "session-attribute",
239: value.toString());
240: attributeElement.setAttribute("type",
241: value.getClass().getName());
242: attributeElement.setAttribute("name",
243: attribute);
244: }
245: }
246: }
247: }
248:
249: String referrer = ActionsBase.getReferrer(request);
250: if (referrer != null) {
251: AbstractDomObject.addTextElement(holder,
252: "referrer", referrer);
253: }
254: }
255:
256: };
257: }
258:
259: private static CompositeDomSerializer REQUEST_DOM_SERIALIZER = new CompositeDomSerializer();
260:
261: static {
262: REQUEST_DOM_SERIALIZER
263: .addDomSerializer(new HttpRequestDomSerializer());
264: }
265:
266: /**
267: * Converts request to XML string.
268: * @param request
269: * @return
270: * @throws FactoryConfigurationError
271: * @throws ParserConfigurationException
272: * @throws TransformerException
273: * @throws IOException
274: */
275: public static String toXML(ServletRequest request)
276: throws IOException, TransformerException,
277: ParserConfigurationException, FactoryConfigurationError {
278: CompositeDomSerializer
279: .pushThreadSerializer(REQUEST_DOM_SERIALIZER);
280: try {
281: return DOMUtils.toXmlString(request, "request");
282: } finally {
283: CompositeDomSerializer.popThreadSerializer();
284: }
285: }
286:
287: public static void dump(ServletRequest request, File out)
288: throws IOException, ParserConfigurationException,
289: FactoryConfigurationError, TransformerException {
290: CompositeDomSerializer
291: .pushThreadSerializer(REQUEST_DOM_SERIALIZER);
292: try {
293: DOMUtils.serialize(request, "request", out);
294: } finally {
295: CompositeDomSerializer.popThreadSerializer();
296: }
297: }
298:
299: public static DomSerializable requestToDomSerializable(
300: ServletRequest request) {
301: return REQUEST_DOM_SERIALIZER.toDomSerializable(request);
302: }
303:
304: }
|