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: }
|