Source Code Cross Referenced for HttpRequestDomSerializer.java in  » Web-Framework » argun » biz » hammurapi » web » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » argun » biz.hammurapi.web.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.