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.pluto.internal.impl;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021:
022: import javax.portlet.PortletResponse;
023: import javax.servlet.ServletOutputStream;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026: import javax.servlet.http.HttpServletResponseWrapper;
027:
028: import org.apache.pluto.PortletContainer;
029: import org.apache.pluto.internal.InternalPortletResponse;
030: import org.apache.pluto.internal.InternalPortletWindow;
031: import org.apache.pluto.spi.ResourceURLProvider;
032: import org.apache.pluto.util.ArgumentUtility;
033: import org.apache.pluto.util.PrintWriterServletOutputStream;
034:
035: /**
036: * Abstract <code>javax.portlet.PortletResponse</code> implementation.
037: * This class also implements InternalPortletResponse.
038: *
039: */
040: public abstract class PortletResponseImpl extends
041: HttpServletResponseWrapper implements PortletResponse,
042: InternalPortletResponse {
043:
044: // Private Member Variables ------------------------------------------------
045:
046: /** The portlet container. */
047: private final PortletContainer container;
048:
049: /** The internal portlet window. */
050: private final InternalPortletWindow internalPortletWindow;
051:
052: /** The servlet request of the target/portlet's web module. */
053: private final HttpServletRequest httpServletRequest;
054:
055: private boolean usingWriter;
056: private boolean usingStream;
057:
058: private ServletOutputStream wrappedWriter;
059:
060: // Constructor -------------------------------------------------------------
061:
062: public PortletResponseImpl(PortletContainer container,
063: InternalPortletWindow internalPortletWindow,
064: HttpServletRequest servletRequest,
065: HttpServletResponse servletResponse) {
066: super (servletResponse);
067: this .container = container;
068: this .httpServletRequest = servletRequest;
069: this .internalPortletWindow = internalPortletWindow;
070: }
071:
072: // PortletResponse Impl ----------------------------------------------------
073:
074: public void addProperty(String name, String value) {
075: ArgumentUtility.validateNotNull("propertyName", name);
076: container.getRequiredContainerServices()
077: .getPortalCallbackService().addResponseProperty(
078: getHttpServletRequest(), internalPortletWindow,
079: name, value);
080: }
081:
082: public void setProperty(String name, String value) {
083: ArgumentUtility.validateNotNull("propertyName", name);
084: container.getRequiredContainerServices()
085: .getPortalCallbackService().setResponseProperty(
086: getHttpServletRequest(), internalPortletWindow,
087: name, value);
088: }
089:
090: public String encodeURL(String path) {
091: if (path.indexOf("://") == -1 && !path.startsWith("/")) {
092: throw new IllegalArgumentException(
093: "only absolute URLs or full path URIs are allowed");
094: }
095:
096: ResourceURLProvider provider = getContainer()
097: .getRequiredContainerServices()
098: .getPortalCallbackService().getResourceURLProvider(
099: httpServletRequest, internalPortletWindow);
100: if (path.indexOf("://") != -1) {
101: provider.setAbsoluteURL(path);
102: } else {
103: provider.setFullPath(path);
104: }
105: return getHttpServletResponse().encodeURL(provider.toString());
106: }
107:
108: // InternalPortletResponse impl --------------------------------------------
109:
110: public InternalPortletWindow getInternalPortletWindow() {
111: return internalPortletWindow;
112: }
113:
114: // Internal Methods --------------------------------------------------------
115:
116: /**
117: * Returns the portlet container.
118: * @return the portlet container.
119: */
120: protected PortletContainer getContainer() {
121: return container;
122: }
123:
124: /**
125: * Returns the nested HttpServletRequest instance.
126: * @return the nested HttpServletRequest instance.
127: */
128: protected HttpServletRequest getHttpServletRequest() {
129: return httpServletRequest;
130: }
131:
132: /**
133: * Returns the nested HttpServletResponse instance.
134: * @return the nested HttpServletResponse instance.
135: */
136: public HttpServletResponse getHttpServletResponse() {
137: return (HttpServletResponse) super .getResponse();
138: }
139:
140: // HttpServletResponse Methods ---------------------------------------------
141:
142: public String encodeUrl(String url) {
143: return this .encodeURL(url);
144: }
145:
146: /**
147: * TODO: javadoc about why we are using a wrapped writer here.
148: * @see org.apache.pluto.util.PrintWriterServletOutputStream
149: */
150: public ServletOutputStream getOutputStream()
151: throws IllegalStateException, IOException {
152: if (usingWriter) {
153: throw new IllegalStateException(
154: "getPortletOutputStream can't be used "
155: + "after getWriter was invoked.");
156: }
157: if (wrappedWriter == null) {
158: wrappedWriter = new PrintWriterServletOutputStream(
159: getHttpServletResponse().getWriter());
160: }
161: usingStream = true;
162: return wrappedWriter;
163: }
164:
165: public PrintWriter getWriter() throws IllegalStateException,
166: IOException {
167: if (usingStream) {
168: throw new IllegalStateException("getWriter can't be used "
169: + "after getOutputStream was invoked.");
170: }
171: usingWriter = true;
172: return getHttpServletResponse().getWriter();
173: }
174:
175: }
|