1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
45
46 public RenderResponseImpl(PortletWindow portletWindow,
47 javax.servlet.http.HttpServletRequest servletRequest,
48 javax.servlet.http.HttpServletResponse servletResponse,
49 boolean containerSupportsBuffering)
50 {
51 super(portletWindow, servletRequest, servletResponse);
52 this.containerSupportsBuffering = containerSupportsBuffering;
53 }
54
55
56 public String getContentType()
57 {
58
59
60 return currentContentType;
61 }
62
63 public PortletURL createRenderURL()
64 {
65 PortletURL url = createURL(false);
66 return url;
67 }
68
69 public PortletURL createActionURL()
70 {
71 PortletURL url = createURL(true);
72 return url;
73 }
74
75 public String getNamespace()
76 {
77 String namespace = NamespaceMapperAccess.getNamespaceMapper().encode(getInternalPortletWindow().getId(), "");
78
79
80
81 StringBuffer validNamespace = new StringBuffer();
82 for (int i = 0; i < namespace.length(); i++) {
83 char ch = namespace.charAt(i);
84 if (Character.isJavaIdentifierPart(ch)) {
85 validNamespace.append(ch);
86 } else {
87 validNamespace.append('_');
88 }
89 }
90
91 return validNamespace.toString();
92 }
93
94 public void setTitle(String title)
95 {
96 DynamicTitle.setDynamicTitle(getInternalPortletWindow(),
97 getHttpServletRequest(),
98 title);
99 }
100
101 public void setContentType(String type)
102 {
103 String mimeType = stripCharacterEncoding(type);
104 if (!isValidContentType(mimeType)) {
105 throw new IllegalArgumentException(mimeType);
106 }
107 this._getHttpServletResponse().setContentType(mimeType);
108 currentContentType = mimeType;
109 }
110
111 public String getCharacterEncoding()
112 {
113 return this._getHttpServletResponse().getCharacterEncoding();
114 }
115
116 public PrintWriter getWriter() throws IOException, IllegalStateException {
117 if (currentContentType == null) {
118 throw new java.lang.IllegalStateException(illegalStateExceptionText);
119 }
120
121 return super.getWriter();
122 }
123
124 public java.util.Locale getLocale()
125 {
126 return this.getHttpServletRequest().getLocale();
127 }
128
129 public void setBufferSize(int size)
130 {
131 if (!containerSupportsBuffering) {
132
133
134 throw new IllegalStateException("portlet container does not support buffering");
135 } else {
136 this._getHttpServletResponse().setBufferSize(size);
137 }
138 }
139
140 public int getBufferSize()
141 {
142 if (!containerSupportsBuffering) {
143 return 0;
144 } else {
145 return this._getHttpServletResponse().getBufferSize();
146 }
147 }
148
149 public void flushBuffer() throws java.io.IOException
150 {
151 this._getHttpServletResponse().flushBuffer();
152 }
153
154 public void resetBuffer()
155 {
156 this._getHttpServletResponse().resetBuffer();
157 }
158
159 public boolean isCommitted()
160 {
161 return this._getHttpServletResponse().isCommitted();
162 }
163
164 public void reset()
165 {
166 this._getHttpServletResponse().reset();
167 }
168
169 public OutputStream getPortletOutputStream() throws java.io.IOException,java.lang.IllegalStateException
170 {
171 if (currentContentType == null) {
172 throw new java.lang.IllegalStateException(illegalStateExceptionText);
173 }
174 return getOutputStream();
175 }
176
177
178
179 private PortletURL createURL(boolean isAction)
180 {
181 return PortletObjectAccess.getPortletURL(getInternalPortletWindow(),
182 getHttpServletRequest(),
183 _getHttpServletResponse(),
184 isAction);
185 }
186
187 private boolean isValidContentType(String type)
188 {
189 type = stripCharacterEncoding(type);
190 PortletEntity entity = portletWindow.getPortletEntity();
191 PortletDefinition def = entity.getPortletDefinition();
192 ContentTypeSet contentTypes = def.getContentTypeSet();
193 Iterator it = contentTypes.iterator();
194 while(it.hasNext()) {
195 ContentType ct = (ContentType)it.next();
196 String supportedType = ct.getContentType();
197 if (supportedType.equals(type)) {
198 return true;
199 } else if (supportedType.indexOf("*") >= 0) {
200
201 int index = supportedType.indexOf("/");
202 String supportedPrefix = supportedType.substring(0, index);
203 String supportedSuffix = supportedType.substring(index + 1, supportedType.length());
204
205 index = type.indexOf("/");
206 String typePrefix = type.substring(0, index);
207 String typeSuffix = type.substring(index + 1, type.length());
208
209 if (supportedPrefix.equals("*") || supportedPrefix.equals(typePrefix)) {
210
211 if (supportedSuffix.equals("*") || supportedSuffix.equals(typeSuffix)) {
212
213 return true;
214 }
215 }
216 }
217 }
218 return false;
219 }
220
221 private String stripCharacterEncoding(String type)
222 {
223 int xs = type.indexOf(';');
224 String strippedType;
225 if (xs == -1) {
226 strippedType = type;
227 } else {
228 strippedType = type.substring(0,xs);
229 }
230 return strippedType.trim();
231 }
232
233 }