KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > RenderResponseImpl


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 JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.Iterator JavaDoc;
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 JavaDoc illegalStateExceptionText = "No content type set.";
41
42     private String JavaDoc currentContentType = null; // needed as servlet 2.3 does not have a response.getContentType
43

44     public RenderResponseImpl(PortletWindow portletWindow,
45                               javax.servlet.http.HttpServletRequest JavaDoc servletRequest,
46                               javax.servlet.http.HttpServletResponse JavaDoc servletResponse)
47     {
48         super(portletWindow, servletRequest, servletResponse);
49     }
50
51     // javax.portlet.RenderResponse ---------------------------------------------------------------
52
public String JavaDoc 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 JavaDoc getNamespace()
72     {
73         String JavaDoc namespace = NamespaceMapperAccess.getNamespaceMapper().encode(getInternalPortletWindow().getId(), "");
74         
75         // correct all chars in the ns + name that are not valid for qualified names
76
// ECMA-262 Chap. 7.6
77
if (namespace.indexOf('-') != -1) {
78             namespace = namespace.replace('-', '_');
79         }
80         
81         return namespace;
82     }
83
84     public void setTitle(String JavaDoc title)
85     {
86         DynamicTitle.setDynamicTitle(getInternalPortletWindow(),
87                                      getHttpServletRequest(),
88                                      title);
89     }
90
91     public void setContentType(String JavaDoc type)
92     {
93         String JavaDoc mimeType = stripCharacterEncoding(type);
94         if (!isValidContentType(mimeType)) {
95             throw new IllegalArgumentException JavaDoc(mimeType);
96         }
97         this._getHttpServletResponse().setContentType(mimeType);
98         currentContentType = mimeType;
99     }
100
101     public String JavaDoc getCharacterEncoding()
102     {
103         return this._getHttpServletResponse().getCharacterEncoding();
104     }
105
106     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc, IllegalStateException JavaDoc {
107         if (currentContentType == null) {
108             throw new java.lang.IllegalStateException JavaDoc(illegalStateExceptionText);
109         }
110
111         return super.getWriter();
112     }
113
114     public java.util.Locale JavaDoc getLocale()
115     {
116         return this.getHttpServletRequest().getLocale();
117     }
118
119     public void setBufferSize(int size)
120     {
121         throw new IllegalStateException JavaDoc("portlet container does not support buffering");
122     }
123
124     public int getBufferSize()
125     {
126         //return this._getHttpServletResponse().getBufferSize();
127
return 0;
128     }
129
130     public void flushBuffer() throws java.io.IOException JavaDoc
131     {
132         this._getHttpServletResponse().flushBuffer();
133     }
134
135     public void resetBuffer()
136     {
137         this._getHttpServletResponse().resetBuffer();
138     }
139
140     public boolean isCommitted()
141     {
142         return this._getHttpServletResponse().isCommitted();
143     }
144
145     public void reset()
146     {
147         this._getHttpServletResponse().reset();
148     }
149
150     public OutputStream JavaDoc getPortletOutputStream() throws java.io.IOException JavaDoc,java.lang.IllegalStateException JavaDoc
151     {
152         if (currentContentType == null) {
153             throw new java.lang.IllegalStateException JavaDoc(illegalStateExceptionText);
154         }
155         return getOutputStream();
156     }
157     // --------------------------------------------------------------------------------------------
158

159     // internal methods ---------------------------------------------------------------------------
160
private PortletURL createURL(boolean isAction)
161     {
162         return PortletObjectAccess.getPortletURL(getInternalPortletWindow(),
163                                                  getHttpServletRequest(),
164                                                  _getHttpServletResponse(),
165                                                  isAction);
166     }
167
168     private boolean isValidContentType(String JavaDoc type)
169     {
170         type = stripCharacterEncoding(type);
171         PortletEntity entity = portletWindow.getPortletEntity();
172         PortletDefinition def = entity.getPortletDefinition();
173         ContentTypeSet contentTypes = def.getContentTypeSet();
174         Iterator JavaDoc it = contentTypes.iterator();
175         while(it.hasNext()) {
176             ContentType ct = (ContentType)it.next();
177             String JavaDoc supportedType = ct.getContentType();
178             if (supportedType.equals(type)) {
179                 return true;
180             } else if (supportedType.indexOf("*") >= 0) {
181                 // the supported type contains a wildcard
182
int index = supportedType.indexOf("/");
183                 String JavaDoc supportedPrefix = supportedType.substring(0, index);
184                 String JavaDoc supportedSuffix = supportedType.substring(index + 1, supportedType.length());
185                 
186                 index = type.indexOf("/");
187                 String JavaDoc typePrefix = type.substring(0, index);
188                 String JavaDoc typeSuffix = type.substring(index + 1, type.length());
189                                 
190                 if (supportedPrefix.equals("*") || supportedPrefix.equals(typePrefix)) {
191                     // the prefixes match
192
if (supportedSuffix.equals("*") || supportedSuffix.equals(typeSuffix)) {
193                         // the suffixes match
194
return true;
195                     }
196                 }
197             }
198         }
199         return false;
200     }
201
202     private String JavaDoc stripCharacterEncoding(String JavaDoc type)
203     {
204         int xs = type.indexOf(';');
205         String JavaDoc strippedType;
206         if (xs == -1) {
207             strippedType = type;
208         } else {
209             strippedType = type.substring(0,xs);
210         }
211         return strippedType.trim();
212     }
213     // --------------------------------------------------------------------------------------------
214
}
215
Popular Tags