Source Code Cross Referenced for HttpMarshaler.java in  » ESB » servicemix » org » apache » servicemix » components » http » 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 » ESB » servicemix » org.apache.servicemix.components.http 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        package org.apache.servicemix.components.http;
018:
019:        import java.io.IOException;
020:        import java.util.Enumeration;
021:        import java.util.HashMap;
022:        import java.util.Iterator;
023:        import java.util.Map;
024:
025:        import javax.jbi.messaging.InOut;
026:        import javax.jbi.messaging.MessageExchange;
027:        import javax.jbi.messaging.MessagingException;
028:        import javax.jbi.messaging.NormalizedMessage;
029:        import javax.servlet.http.HttpServletRequest;
030:        import javax.servlet.http.HttpServletResponse;
031:        import javax.xml.transform.Source;
032:        import javax.xml.transform.TransformerException;
033:        import javax.xml.transform.stream.StreamResult;
034:        import javax.xml.transform.stream.StreamSource;
035:
036:        import org.apache.servicemix.components.util.MarshalerSupport;
037:        import org.apache.servicemix.jbi.jaxp.StringSource;
038:
039:        /**
040:         * A class which marshalls a HTTP request to a NMS message
041:         * 
042:         * @version $Revision: 426415 $
043:         */
044:        public class HttpMarshaler extends MarshalerSupport {
045:
046:            public static final String CGI_HEADERS = "cgi.headers";
047:
048:            public static final String AUTH_TYPE = "AUTH_TYPE";
049:            public static final String CONTENT_LENGTH = "CONTENT_LENGTH";
050:            public static final String CONTENT_TYPE = "CONTENT_TYPE";
051:            public static final String DOCUMENT_ROOT = "DOCUMENT_ROOT";
052:            public static final String PATH_INFO = "PATH_INFO";
053:            public static final String PATH_TRANSLATED = "PATH_TRANSLATED";
054:            public static final String QUERY_STRING = "QUERY_STRING";
055:            public static final String REMOTE_ADDRESS = "REMOTE_ADDR";
056:            public static final String REMOTE_HOST = "REMOTE_HOST";
057:            public static final String REMOTE_USER = "REMOTE_USER";
058:            public static final String REQUEST_METHOD = "REQUEST_METHOD";
059:            public static final String REQUEST_URI = "REQUEST_URI";
060:            public static final String SCRIPT_NAME = "SCRIPT_NAME";
061:            public static final String SERVER_NAME = "SERVER_NAME";
062:            public static final String SERVER_PORT = "SERVER_PORT";
063:            public static final String SERVER_PROTOCOL = "SERVER_PROTOCOL";
064:
065:            protected static final Source EMPTY_CONTENT = new StringSource(
066:                    "<payload/>");
067:
068:            private String contentType = "text/xml";
069:
070:            public void toNMS(MessageExchange exchange,
071:                    NormalizedMessage inMessage, HttpServletRequest request)
072:                    throws IOException, MessagingException {
073:                addNmsProperties(inMessage, request);
074:                String method = request.getMethod();
075:                if (method != null && method.equalsIgnoreCase("POST")) {
076:                    /*
077:                    Source src = null;
078:                    try {
079:                        if (request.getContentType() != null) {
080:                            String charset = new MimeType(request.getContentType()).getParameter("charset");
081:                            if (charset != null) {
082:                                XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(request.getInputStream(), charset);
083:                                src = new StaxSource(xr);
084:                            }
085:                        }
086:                        if (src == null) {
087:                            XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(request.getInputStream());
088:                            src = new StaxSource(xr);
089:                        }
090:                    } catch (Exception e) {
091:                        throw new RuntimeException(e);
092:                    }
093:                    try {
094:                        src = getTransformer().toDOMSource(src);
095:                    } catch (Exception e) {
096:                        throw new RuntimeException(e);
097:                    }
098:                    inMessage.setContent(src);
099:                     */
100:                    inMessage.setContent(new StreamSource(request
101:                            .getInputStream()));
102:                } else {
103:                    Enumeration enumeration = request.getParameterNames();
104:                    while (enumeration.hasMoreElements()) {
105:                        String name = (String) enumeration.nextElement();
106:                        String value = request.getParameter(name);
107:                        inMessage.setProperty(name, value);
108:                    }
109:                    inMessage.setContent(EMPTY_CONTENT);
110:                }
111:            }
112:
113:            public void toResponse(InOut exchange, NormalizedMessage message,
114:                    HttpServletResponse response) throws IOException,
115:                    TransformerException {
116:                if (message != null) {
117:                    addHttpHeaders(response, message);
118:                }
119:
120:                response.setContentType(contentType);
121:                getTransformer().toResult(message.getContent(),
122:                        new StreamResult(response.getOutputStream()));
123:            }
124:
125:            // Properties
126:            // -------------------------------------------------------------------------
127:            public String getContentType() {
128:                return contentType;
129:            }
130:
131:            public void setContentType(String contentType) {
132:                this .contentType = contentType;
133:            }
134:
135:            // Implementation methods
136:            // -------------------------------------------------------------------------
137:            protected void addNmsProperties(NormalizedMessage message,
138:                    HttpServletRequest request) {
139:                Enumeration enumeration = request.getHeaderNames();
140:                while (enumeration.hasMoreElements()) {
141:                    String name = (String) enumeration.nextElement();
142:                    String value = request.getHeader(name);
143:                    message.setProperty(name, value);
144:                }
145:                Map cgi = new HashMap();
146:                cgi.put(AUTH_TYPE, request.getAuthType());
147:                cgi.put(CONTENT_LENGTH, String.valueOf(request
148:                        .getContentLength()));
149:                cgi.put(CONTENT_TYPE, request.getContentType());
150:                cgi.put(DOCUMENT_ROOT, request.getRealPath("/"));
151:                cgi.put(PATH_INFO, request.getPathInfo());
152:                cgi.put(PATH_TRANSLATED, request.getPathTranslated());
153:                cgi.put(QUERY_STRING, request.getQueryString());
154:                cgi.put(REMOTE_ADDRESS, request.getRemoteAddr());
155:                cgi.put(REMOTE_HOST, request.getRemoteHost());
156:                cgi.put(REMOTE_USER, request.getRemoteUser());
157:                cgi.put(REQUEST_METHOD, request.getMethod());
158:                cgi.put(REQUEST_URI, request.getRequestURL());
159:                cgi.put(SCRIPT_NAME, request.getServletPath());
160:                cgi.put(SERVER_NAME, request.getServerName());
161:                cgi.put(SERVER_PORT, String.valueOf(request.getServerPort()));
162:                cgi.put(SERVER_PROTOCOL, request.getProtocol());
163:                message.setProperty(CGI_HEADERS, cgi);
164:            }
165:
166:            protected void addHttpHeaders(HttpServletResponse response,
167:                    NormalizedMessage normalizedMessage) {
168:                for (Iterator iter = normalizedMessage.getPropertyNames()
169:                        .iterator(); iter.hasNext();) {
170:                    String name = (String) iter.next();
171:                    Object value = normalizedMessage.getProperty(name);
172:                    if (shouldIncludeHeader(normalizedMessage, name, value)) {
173:                        response.setHeader(name, value.toString());
174:                    }
175:                }
176:            }
177:
178:            /**
179:             * Decides whether or not the given header should be included in the JMS
180:             * message. By default this includes all suitable typed values
181:             */
182:            protected boolean shouldIncludeHeader(
183:                    NormalizedMessage normalizedMessage, String name,
184:                    Object value) {
185:                return value instanceof  String
186:                        && !"Content-Length".equalsIgnoreCase(name)
187:                        && !"Content-Type".equalsIgnoreCase(name);
188:            }
189:
190:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.