Source Code Cross Referenced for ServletRequestResponse.java in  » J2EE » ICEfaces-1.6.1 » com » icesoft » faces » webapp » 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 » J2EE » ICEfaces 1.6.1 » com.icesoft.faces.webapp.http.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.icesoft.faces.webapp.http.servlet;
002:
003:        import com.icesoft.faces.webapp.http.common.Request;
004:        import com.icesoft.faces.webapp.http.common.Response;
005:        import com.icesoft.faces.webapp.http.common.ResponseHandler;
006:        import com.icesoft.faces.webapp.http.portlet.PortletArtifactWrapper;
007:
008:        import javax.servlet.http.Cookie;
009:        import javax.servlet.http.HttpServletRequest;
010:        import javax.servlet.http.HttpServletResponse;
011:        import java.io.IOException;
012:        import java.io.InputStream;
013:        import java.io.OutputStream;
014:        import java.net.URI;
015:        import java.text.DateFormat;
016:        import java.text.ParseException;
017:        import java.text.SimpleDateFormat;
018:        import java.util.ArrayList;
019:        import java.util.Date;
020:        import java.util.Enumeration;
021:        import java.util.TimeZone;
022:
023:        public class ServletRequestResponse implements  Request, Response {
024:            private final static DateFormat DATE_FORMAT = new SimpleDateFormat(
025:                    "EEE, dd MMM yyyy HH:mm:ss zzz");
026:
027:            private URI requestURI;
028:
029:            static {
030:                DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
031:            }
032:
033:            protected HttpServletRequest request;
034:            protected HttpServletResponse response;
035:
036:            public ServletRequestResponse(HttpServletRequest request,
037:                    HttpServletResponse response) throws Exception {
038:                this .request = request;
039:                this .response = response;
040:
041:                //Need to determine the type of request URI we are using based on the
042:                //environment we are running in (servlet vs portlet).
043:                detectEnvironment(new Environment() {
044:                    public void servlet(Object request, Object response) {
045:                        HttpServletRequest req = (HttpServletRequest) request;
046:                        String query = req.getQueryString();
047:                        URI uri = URI.create(req.getRequestURL().toString());
048:                        requestURI = (query == null ? uri : URI.create(uri
049:                                + "?" + query));
050:                    }
051:
052:                    public void portlet(Object request, Object response,
053:                            Object portletConfig) {
054:                        javax.portlet.PortletRequest req = (javax.portlet.PortletRequest) request;
055:                        String reqURI = (String) req
056:                                .getAttribute(com.icesoft.jasper.Constants.INC_REQUEST_URI);
057:                        requestURI = URI.create(reqURI);
058:                    }
059:                });
060:            }
061:
062:            public String getMethod() {
063:                return request.getMethod();
064:            }
065:
066:            public URI getURI() {
067:                return requestURI;
068:            }
069:
070:            public String getHeader(String name) {
071:                return request.getHeader(name);
072:            }
073:
074:            public String[] getHeaderAsStrings(String name) {
075:                Enumeration e = request.getHeaders(name);
076:                ArrayList values = new ArrayList();
077:                while (e.hasMoreElements())
078:                    values.add(e.nextElement());
079:                return (String[]) values.toArray(new String[values.size()]);
080:            }
081:
082:            public Date getHeaderAsDate(String name) {
083:                try {
084:                    return DATE_FORMAT.parse(request.getHeader(name));
085:                } catch (ParseException e) {
086:                    throw new RuntimeException(e);
087:                }
088:            }
089:
090:            public int getHeaderAsInteger(String name) {
091:                return Integer.parseInt(request.getHeader(name));
092:            }
093:
094:            public boolean containsParameter(String name) {
095:                return request.getParameter(name) != null;
096:            }
097:
098:            public String getParameter(String name) {
099:                checkExistenceOf(name);
100:                return (String) request.getParameter(name);
101:            }
102:
103:            public String[] getParameterAsStrings(String name) {
104:                checkExistenceOf(name);
105:                return request.getParameterValues(name);
106:            }
107:
108:            public int getParameterAsInteger(String name) {
109:                return Integer.parseInt(getParameter(name));
110:            }
111:
112:            public boolean getParameterAsBoolean(String name) {
113:                return Boolean.valueOf(getParameter(name)).booleanValue();
114:            }
115:
116:            public String getParameter(String name, String defaultValue) {
117:                try {
118:                    return getParameter(name);
119:                } catch (Exception e) {
120:                    return defaultValue;
121:                }
122:            }
123:
124:            public int getParameterAsInteger(String name, int defaultValue) {
125:                try {
126:                    return getParameterAsInteger(name);
127:                } catch (Exception e) {
128:                    return defaultValue;
129:                }
130:            }
131:
132:            public boolean getParameterAsBoolean(String name,
133:                    boolean defaultValue) {
134:                try {
135:                    return getParameterAsBoolean(name);
136:                } catch (Exception e) {
137:                    return defaultValue;
138:                }
139:            }
140:
141:            public InputStream readBody() throws IOException {
142:                return request.getInputStream();
143:            }
144:
145:            public void readBodyInto(OutputStream out) throws IOException {
146:                copy(readBody(), out);
147:            }
148:
149:            public void respondWith(ResponseHandler handler) throws Exception {
150:                handler.respond(this );
151:            }
152:
153:            public void setStatus(int code) {
154:                response.setStatus(code);
155:            }
156:
157:            public void setHeader(String name, String value) {
158:                if ("Content-Type".equals(name)) {
159:                    response.setContentType(value);
160:                } else if ("Content-Length".equals(name)) {
161:                    response.setContentLength(Integer.parseInt(value));
162:                } else {
163:                    response.setHeader(name, value);
164:                }
165:            }
166:
167:            public void setHeader(String name, String[] values) {
168:                for (int i = 0; i < values.length; i++) {
169:                    response.addHeader(name, values[i]);
170:                }
171:            }
172:
173:            public void setHeader(String name, Date value) {
174:                response.setDateHeader(name, value.getTime());
175:            }
176:
177:            public void setHeader(String name, int value) {
178:                response.setIntHeader(name, value);
179:            }
180:
181:            public void setHeader(String name, long value) {
182:                response.setHeader(name, String.valueOf(value));
183:            }
184:
185:            public void addCookie(Cookie cookie) {
186:                response.addCookie(cookie);
187:            }
188:
189:            public OutputStream writeBody() throws IOException {
190:                return response.getOutputStream();
191:            }
192:
193:            public void writeBodyFrom(InputStream in) throws IOException {
194:                copy(in, writeBody());
195:            }
196:
197:            public void detectEnvironment(Environment environment)
198:                    throws Exception {
199:
200:                Object portletEnvironment = request
201:                        .getAttribute(PortletArtifactWrapper.PORTLET_ARTIFACT_KEY);
202:                if (portletEnvironment == null) {
203:                    environment.servlet(request, response);
204:                } else {
205:                    PortletArtifactWrapper portletArtifact = (PortletArtifactWrapper) portletEnvironment;
206:                    environment.portlet(portletArtifact.getRequest(),
207:                            portletArtifact.getResponse(), portletArtifact
208:                                    .getPortletConfig());
209:
210:                    //Due to the fact that the original portlet request used a RequestDispatcher to
211:                    //connect to the ICEfaces framework, we need to adjust the java.servlet.include*
212:                    //attributes in the original portlet request to match the dispatched request.
213:                    String[] incKeys = com.icesoft.jasper.Constants.INC_CONSTANTS;
214:                    for (int index = 0; index < incKeys.length; index++) {
215:                        String incVal = (String) request
216:                                .getAttribute(incKeys[index]);
217:                        if (incVal != null) {
218:                            portletArtifact.getRequest().setAttribute(
219:                                    incKeys[index], incVal);
220:                        } else {
221:                            portletArtifact.getRequest().removeAttribute(
222:                                    incKeys[index]);
223:                        }
224:                    }
225:                }
226:            }
227:
228:            private static void copy(InputStream input, OutputStream output)
229:                    throws IOException {
230:                byte[] buf = new byte[4096];
231:                int len = 0;
232:                while ((len = input.read(buf)) > -1)
233:                    output.write(buf, 0, len);
234:            }
235:
236:            private void checkExistenceOf(String name) {
237:                if (request.getParameter(name) == null)
238:                    throw new RuntimeException(
239:                            "Query does not contain parameter named: " + name);
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.