001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.core;
016:
017: import java.io.IOException;
018: import java.io.OutputStream;
019: import java.io.PrintWriter;
020: import java.util.HashMap;
021: import java.util.Locale;
022: import java.util.Map;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import org.araneaframework.InputData;
026: import org.araneaframework.core.Assert;
027: import org.araneaframework.core.NoCurrentInputDataSetException;
028: import org.araneaframework.core.NoSuchNarrowableException;
029: import org.araneaframework.http.HttpOutputData;
030:
031: /**
032: * A implementation of ServletOutputData, MimeOutputData and ServletOverridableOutputData using
033: * StandardPath for scoping.
034: *
035: * @author "Toomas Römer" <toomas@webmedia.ee>
036: */
037: public class StandardServletOutputData implements HttpOutputData {
038: private HttpServletRequest req;
039: private HttpServletResponse res;
040:
041: private Map extensions = new HashMap();
042:
043: /**
044: * Constructs a StandardServletOutputData with the request and response.
045: */
046: public StandardServletOutputData(HttpServletRequest request,
047: HttpServletResponse response) {
048: Assert.notNullParam(request, "request");
049: Assert.notNullParam(response, "response");
050:
051: this .req = request;
052: this .res = response;
053:
054: extend(HttpServletResponse.class, res);
055: }
056:
057: public void extend(Class interfaceClass, Object implementation) {
058: if (HttpServletResponse.class.equals(interfaceClass))
059: setResponse((HttpServletResponse) implementation);
060:
061: extensions.put(interfaceClass, implementation);
062: }
063:
064: public Object narrow(Class interfaceClass) {
065: if (!extensions.containsKey(interfaceClass))
066: throw new NoSuchNarrowableException(interfaceClass);
067: return extensions.get(interfaceClass);
068: }
069:
070: public OutputStream getOutputStream() throws IOException {
071: return res.getOutputStream();
072: }
073:
074: public void setResponse(HttpServletResponse res) {
075: this .res = res;
076: }
077:
078: public InputData getInputData() {
079: InputData inputData = (InputData) req
080: .getAttribute(InputData.INPUT_DATA_KEY);
081: if (inputData == null)
082: throw new NoCurrentInputDataSetException(
083: "No InputData set in the request.");
084: else
085: return inputData;
086: }
087:
088: public String encodeURL(String url) {
089: return res.encodeURL(url);
090: }
091:
092: public String getCharacterEncoding() {
093: return res.getCharacterEncoding();
094: }
095:
096: public Locale getLocale() {
097: return res.getLocale();
098: }
099:
100: public PrintWriter getWriter() throws IOException {
101: return res.getWriter();
102: }
103:
104: public void sendRedirect(String location) throws IOException {
105: res.sendRedirect(location);
106: }
107:
108: public void setContentType(String type) {
109: res.setContentType(type);
110: }
111:
112: public void setCharacterEncoding(String encoding) {
113: res.setCharacterEncoding(encoding);
114: }
115: }
|