Source Code Cross Referenced for AbstractReceiverServlet.java in  » ESB » mule » org » mule » transport » http » servlet » 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 » mule » org.mule.transport.http.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: AbstractReceiverServlet.java 11186 2008-03-05 23:11:10Z dandiep $
003:         * --------------------------------------------------------------------------------------
004:         * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
005:         *
006:         * The software in this package is published under the terms of the CPAL v1.0
007:         * license, a copy of which has been included with this distribution in the
008:         * LICENSE.txt file.
009:         */
010:
011:        package org.mule.transport.http.servlet;
012:
013:        import org.mule.RequestContext;
014:        import org.mule.api.MuleMessage;
015:        import org.mule.api.transport.OutputHandler;
016:        import org.mule.transport.http.HttpConnector;
017:        import org.mule.transport.http.HttpConstants;
018:        import org.mule.transport.http.HttpResponse;
019:
020:        import java.io.IOException;
021:
022:        import javax.servlet.ServletConfig;
023:        import javax.servlet.ServletException;
024:        import javax.servlet.http.HttpServlet;
025:        import javax.servlet.http.HttpServletResponse;
026:
027:        import org.apache.commons.httpclient.Header;
028:        import org.apache.commons.logging.Log;
029:        import org.apache.commons.logging.LogFactory;
030:
031:        /**
032:         * A base servlet used to receive requests from a servlet container and route
033:         * them into Mule
034:         */
035:
036:        public abstract class AbstractReceiverServlet extends HttpServlet {
037:            /**
038:             * logger used by this class
039:             */
040:            protected transient Log logger = LogFactory.getLog(getClass());
041:
042:            public static final String REQUEST_TIMEOUT_PROPERTY = "org.mule.servlet.timeout";
043:            public static final String FEEDBACK_PROPERTY = "org.mule.servlet.feedback";
044:            public static final String DEFAULT_CONTENT_TYPE_PROPERTY = "org.mule.servlet.default.content.type";
045:
046:            /** The name of the servlet connector to use with this Servlet */
047:            public static final String SERVLET_CONNECTOR_NAME_PROPERTY = "org.mule.servlet.connector.name";
048:
049:            public static final String PAYLOAD_PARAMETER_NAME = "org.mule.servlet.payload.param";
050:            public static final String DEFAULT_PAYLOAD_PARAMETER_NAME = "payload";
051:
052:            public static final long DEFAULT_GET_TIMEOUT = 10000L;
053:
054:            protected String payloadParameterName;
055:            protected long timeout = DEFAULT_GET_TIMEOUT;
056:            protected boolean feedback = true;
057:            protected String defaultContentType = HttpConstants.DEFAULT_CONTENT_TYPE;
058:
059:            public final void init() throws ServletException {
060:                doInit();
061:            }
062:
063:            public final void init(ServletConfig servletConfig)
064:                    throws ServletException {
065:                String timeoutString = servletConfig
066:                        .getInitParameter(REQUEST_TIMEOUT_PROPERTY);
067:                if (timeoutString != null) {
068:                    timeout = Long.parseLong(timeoutString);
069:                }
070:                logger.info("Default request timeout for GET methods is: "
071:                        + timeout);
072:
073:                String feedbackString = servletConfig
074:                        .getInitParameter(FEEDBACK_PROPERTY);
075:                if (feedbackString != null) {
076:                    feedback = Boolean.valueOf(feedbackString).booleanValue();
077:                }
078:                logger.info("feedback is set to: " + feedback);
079:
080:                String ct = servletConfig
081:                        .getInitParameter(DEFAULT_CONTENT_TYPE_PROPERTY);
082:                if (ct != null) {
083:                    defaultContentType = ct;
084:                }
085:                logger.info("Default content type is: " + defaultContentType);
086:
087:                payloadParameterName = servletConfig
088:                        .getInitParameter(PAYLOAD_PARAMETER_NAME);
089:                if (payloadParameterName == null) {
090:                    payloadParameterName = DEFAULT_PAYLOAD_PARAMETER_NAME;
091:                }
092:                logger
093:                        .info("Using payload param name: "
094:                                + payloadParameterName);
095:
096:                doInit(servletConfig);
097:            }
098:
099:            protected void doInit(ServletConfig servletConfig)
100:                    throws ServletException {
101:                // nothing to do
102:            }
103:
104:            protected void doInit() throws ServletException {
105:                // nothing to do
106:            }
107:
108:            protected void writeResponse(HttpServletResponse servletResponse,
109:                    MuleMessage message) throws Exception {
110:                if (message == null) {
111:                    servletResponse
112:                            .setStatus(HttpServletResponse.SC_NO_CONTENT);
113:                    if (feedback) {
114:                        servletResponse.setStatus(HttpServletResponse.SC_OK);
115:                        servletResponse
116:                                .getWriter()
117:                                .write(
118:                                        "Action was processed successfully. There was no result");
119:                    }
120:                } else {
121:                    HttpResponse httpResponse;
122:
123:                    if (message.getPayload() instanceof  HttpResponse) {
124:                        httpResponse = (HttpResponse) message.getPayload();
125:                    } else {
126:                        httpResponse = new HttpResponse();
127:                        String ct = message.getStringProperty(
128:                                HttpConstants.HEADER_CONTENT_TYPE, null);
129:                        if (ct != null) {
130:                            httpResponse.setHeader(new Header(
131:                                    HttpConstants.HEADER_CONTENT_TYPE, ct));
132:                        }
133:                        httpResponse.setStatusLine(httpResponse
134:                                .getHttpVersion(), message.getIntProperty(
135:                                HttpConnector.HTTP_STATUS_PROPERTY,
136:                                HttpServletResponse.SC_OK));
137:                        httpResponse.setBody(message);
138:
139:                    }
140:
141:                    Header contentTypeHeader = httpResponse
142:                            .getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
143:                    String contentType = null;
144:                    if (contentTypeHeader == null) {
145:                        contentType = defaultContentType;
146:                    } else {
147:                        contentType = contentTypeHeader.getValue();
148:                    }
149:
150:                    if (!servletResponse.isCommitted()) {
151:                        servletResponse.setStatus(httpResponse.getStatusCode());
152:                    }
153:                    if (!contentType.startsWith("text")) {
154:                        servletResponse.setContentType(contentType);
155:                        OutputHandler outputHandler = httpResponse.getBody();
156:
157:                        outputHandler.write(RequestContext.getEvent(),
158:                                servletResponse.getOutputStream());
159:                    } else {
160:                        servletResponse.setContentType(contentType);
161:                        // Encoding: this method will check the charset on the content type
162:                        servletResponse.getWriter().write(
163:                                httpResponse.getBodyAsString());
164:                    }
165:                }
166:                servletResponse.flushBuffer();
167:            }
168:
169:            protected void handleException(Throwable exception, String message,
170:                    HttpServletResponse response) {
171:                logger.error("message: " + exception.getMessage(), exception);
172:                response
173:                        .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
174:                try {
175:                    response.sendError(
176:                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
177:                            message + ": " + exception.getMessage());
178:                } catch (IOException e) {
179:                    logger.error("Failed to sendError on response: "
180:                            + e.getMessage(), e);
181:                }
182:            }
183:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.