View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17  
18   */
19  
20  package org.apache.pluto.core.impl;
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.PrintWriter;
25  import java.util.Iterator;
26  
27  import javax.portlet.PortletURL;
28  import javax.portlet.RenderResponse;
29  
30  import org.apache.pluto.factory.PortletObjectAccess;
31  import org.apache.pluto.om.entity.PortletEntity;
32  import org.apache.pluto.om.portlet.ContentType;
33  import org.apache.pluto.om.portlet.ContentTypeSet;
34  import org.apache.pluto.om.portlet.PortletDefinition;
35  import org.apache.pluto.om.window.PortletWindow;
36  import org.apache.pluto.services.title.DynamicTitle;
37  import org.apache.pluto.util.NamespaceMapperAccess;
38  
39  public class RenderResponseImpl extends PortletResponseImpl implements RenderResponse {
40      private static final String illegalStateExceptionText = "No content type set.";
41  
42      private boolean containerSupportsBuffering;
43  
44      private String currentContentType = null;   // needed as servlet 2.3 does not have a response.getContentType
45  
46      private boolean usingWriter;
47      private boolean usingStream;
48  
49      public RenderResponseImpl(PortletWindow portletWindow,
50                                javax.servlet.http.HttpServletRequest servletRequest,
51                                javax.servlet.http.HttpServletResponse servletResponse,
52                                boolean containerSupportsBuffering)
53      {
54          super(portletWindow, servletRequest, servletResponse);
55          this.containerSupportsBuffering = containerSupportsBuffering;
56      }
57  
58      // javax.portlet.RenderResponse ---------------------------------------------------------------
59      public String getContentType()
60      {
61          // in servlet 2.4 we could simply use this:
62          // return this._getHttpServletResponse().getContentType();
63          return currentContentType;
64      }
65  
66      public PortletURL createRenderURL()
67      {
68          PortletURL url = createURL(false);
69          return url;
70      }
71  
72      public PortletURL createActionURL()
73      {
74          PortletURL url = createURL(true);
75          return url;
76      }
77  
78      public String getNamespace()
79      {
80          String namespace = NamespaceMapperAccess.getNamespaceMapper().encode(getInternalPortletWindow().getId(), "");
81  
82           // replace all characters in the 'namespace + name' that are not valid
83           // javascript variable or function name characters by '_'.
84           StringBuffer validNamespace = new StringBuffer();
85           for (int i = 0; i < namespace.length(); i++) {
86           	char ch = namespace.charAt(i);
87           	if (Character.isJavaIdentifierPart(ch)) {
88           		validNamespace.append(ch);
89           	} else {
90           		validNamespace.append('_');
91           	}
92  		}
93  
94          return validNamespace.toString();
95      }
96  
97      public void setTitle(String title)
98      {
99          DynamicTitle.setDynamicTitle(getInternalPortletWindow(),
100                                      getHttpServletRequest(),
101                                      title);
102     }
103 
104     public void setContentType(String type)
105     {
106         String mimeType = stripCharacterEncoding(type);
107         if (!isValidContentType(mimeType)) {
108             throw new IllegalArgumentException(mimeType);
109         }
110         if (!usingWriter && !usingStream) {
111         	this._getHttpServletResponse().setContentType(mimeType);
112         	currentContentType = mimeType;
113 		}
114     }
115 
116     public String getCharacterEncoding()
117     {
118         return this._getHttpServletResponse().getCharacterEncoding();
119     }
120 
121     public PrintWriter getWriter() throws IOException, IllegalStateException {
122         if (currentContentType == null) {
123             throw new java.lang.IllegalStateException(illegalStateExceptionText);
124         }
125 		usingWriter = true;
126         return super.getWriter();
127     }
128 
129     public java.util.Locale getLocale()
130     {
131         return this.getHttpServletRequest().getLocale();
132     }
133 
134     public void setBufferSize(int size)
135     {
136     	if (!containerSupportsBuffering) {
137     		// default behaviour if property pluto.allowSetBufferSize in file
138     		// ConfigService.properties wasn't set or was set to a value not equal to "yes"
139     		throw new IllegalStateException("portlet container does not support buffering");
140     	} else {
141     		this._getHttpServletResponse().setBufferSize(size);
142     	}
143     }
144 
145     public int getBufferSize()
146     {
147     	if (!containerSupportsBuffering) {
148     		return 0;
149     	} else {
150     		return this._getHttpServletResponse().getBufferSize();
151     	}
152     }
153 
154     public void flushBuffer() throws java.io.IOException
155     {
156         this._getHttpServletResponse().flushBuffer();
157     }
158 
159     public void resetBuffer()
160     {
161         this._getHttpServletResponse().resetBuffer();
162     }
163 
164     public boolean isCommitted()
165     {
166         return this._getHttpServletResponse().isCommitted();
167     }
168 
169     public void reset()
170     {
171         this._getHttpServletResponse().reset();
172     }
173 
174     public OutputStream getPortletOutputStream() throws java.io.IOException,java.lang.IllegalStateException
175     {
176         if (currentContentType == null) {
177             throw new java.lang.IllegalStateException(illegalStateExceptionText);
178         }
179 		usingStream = true;
180         return getOutputStream();
181     }
182     // --------------------------------------------------------------------------------------------
183 
184     // internal methods ---------------------------------------------------------------------------
185     private PortletURL createURL(boolean isAction)
186     {
187         return PortletObjectAccess.getPortletURL(getInternalPortletWindow(),
188                                                  getHttpServletRequest(),
189                                                  _getHttpServletResponse(),
190                                                  isAction);
191     }
192 
193     private boolean isValidContentType(String type)
194     {
195         type = stripCharacterEncoding(type);
196         PortletEntity entity = portletWindow.getPortletEntity();
197         PortletDefinition def = entity.getPortletDefinition();
198         ContentTypeSet contentTypes = def.getContentTypeSet();
199         Iterator it = contentTypes.iterator();
200         while(it.hasNext()) {
201             ContentType ct = (ContentType)it.next();
202             String supportedType = ct.getContentType();
203             if (supportedType.equals(type)) {
204                 return true;
205             } else if (supportedType.indexOf("*") >= 0) {
206                 // the supported type contains a wildcard
207                 int index = supportedType.indexOf("/");
208                 String supportedPrefix = supportedType.substring(0, index);
209                 String supportedSuffix = supportedType.substring(index + 1, supportedType.length());
210 
211                 index = type.indexOf("/");
212                 String typePrefix = type.substring(0, index);
213                 String typeSuffix = type.substring(index + 1, type.length());
214 
215                 if (supportedPrefix.equals("*") || supportedPrefix.equals(typePrefix)) {
216                     // the prefixes match
217                     if (supportedSuffix.equals("*") || supportedSuffix.equals(typeSuffix)) {
218                         // the suffixes match
219                         return true;
220                     }
221                 }
222             }
223         }
224         return false;
225     }
226 
227     private String stripCharacterEncoding(String type)
228     {
229         int xs = type.indexOf(';');
230         String strippedType;
231         if (xs == -1) {
232             strippedType = type;
233         } else {
234             strippedType = type.substring(0,xs);
235         }
236         return strippedType.trim();
237     }
238     // --------------------------------------------------------------------------------------------
239 }