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.window.PortletWindow;
32  import org.apache.pluto.om.entity.PortletEntity;
33  import org.apache.pluto.om.portlet.PortletDefinition;
34  import org.apache.pluto.om.portlet.ContentTypeSet;
35  import org.apache.pluto.om.portlet.ContentType;
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 String currentContentType = null;   // needed as servlet 2.3 does not have a response.getContentType
43      
44      public RenderResponseImpl(PortletWindow portletWindow,
45                                javax.servlet.http.HttpServletRequest servletRequest,
46                                javax.servlet.http.HttpServletResponse servletResponse)
47      {
48          super(portletWindow, servletRequest, servletResponse);
49      }
50  
51      // javax.portlet.RenderResponse ---------------------------------------------------------------
52      public String getContentType()
53      {
54          // in servlet 2.4 we could simply use this:
55          // return this._getHttpServletResponse().getContentType();
56          return currentContentType;
57      }
58      
59      public PortletURL createRenderURL()
60      {
61          PortletURL url = createURL(false);
62          return url;
63      }
64      
65      public PortletURL createActionURL()
66      {
67          PortletURL url = createURL(true);
68          return url;
69      }
70      
71      public String getNamespace()
72      {
73          return NamespaceMapperAccess.getNamespaceMapper().encode(getInternalPortletWindow().getId(), "");
74      }
75      
76      public void setTitle(String title)
77      {
78          DynamicTitle.setDynamicTitle(getInternalPortletWindow(),
79                                       getHttpServletRequest(),
80                                       title);
81      }
82      
83      public void setContentType(String type)
84      {
85          String mimeType = stripCharacterEncoding(type);
86          if (!isValidContentType(mimeType)) {
87              throw new IllegalArgumentException(mimeType);
88          }
89          this._getHttpServletResponse().setContentType(mimeType);
90          currentContentType = mimeType;
91      }
92  
93      public String getCharacterEncoding()
94      {
95          return this._getHttpServletResponse().getCharacterEncoding();
96      }
97      
98      public PrintWriter getWriter() throws IOException, IllegalStateException {
99          if (currentContentType == null) {
100             throw new java.lang.IllegalStateException(illegalStateExceptionText);
101         }
102 
103         return super.getWriter();
104     }
105 
106     public java.util.Locale getLocale()
107     {
108         return this.getHttpServletRequest().getLocale();
109     }
110 
111     public void setBufferSize(int size)
112     {
113         throw new IllegalStateException("portlet container does not support buffering");
114     }
115     
116     public int getBufferSize()
117     {
118         //return this._getHttpServletResponse().getBufferSize();
119         return 0;
120     }
121     
122     public void flushBuffer() throws java.io.IOException
123     {
124         this._getHttpServletResponse().flushBuffer();
125     }
126 
127     public void resetBuffer()
128     {
129         this._getHttpServletResponse().resetBuffer();
130     }
131 
132     public boolean isCommitted()
133     {
134         return this._getHttpServletResponse().isCommitted();
135     }
136 
137     public void reset()
138     {
139         this._getHttpServletResponse().reset();
140     }
141     
142     public OutputStream getPortletOutputStream() throws java.io.IOException,java.lang.IllegalStateException
143     {
144         if (currentContentType == null) {
145             throw new java.lang.IllegalStateException(illegalStateExceptionText);
146         }
147         return getOutputStream(); 
148     }
149     // --------------------------------------------------------------------------------------------
150 
151     // internal methods ---------------------------------------------------------------------------
152     private PortletURL createURL(boolean isAction)
153     {
154         return PortletObjectAccess.getPortletURL(getInternalPortletWindow(),
155                                                  getHttpServletRequest(),
156                                                  _getHttpServletResponse(),
157                                                  isAction);
158     }
159 
160     private boolean isValidContentType(String type)
161     {
162         type = stripCharacterEncoding(type);
163         String wildcard = null;
164         int index = type.indexOf("/");
165         if(index > -1) {
166             wildcard = type.substring(0, index);
167         }
168 
169         PortletEntity entity = portletWindow.getPortletEntity();
170         PortletDefinition def = entity.getPortletDefinition();
171         ContentTypeSet contentTypes = def.getContentTypeSet();
172         Iterator it = contentTypes.iterator();
173         while(it.hasNext()) {
174             ContentType ct = (ContentType)it.next();
175             if(ct.getContentType().equals(type)) {
176                 return true;
177             }
178             else if(ct.getContentType().equals(wildcard)) {
179                 return true;
180             }
181         }
182         return false;
183     }
184 
185     private String stripCharacterEncoding(String type)
186     {
187         int xs = type.indexOf(';');
188         String strippedType;
189         if (xs == -1) {
190             strippedType = type;
191         } else {
192             strippedType = type.substring(0,xs);
193         }
194         return strippedType.trim();
195     }
196     // --------------------------------------------------------------------------------------------
197 }