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.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;
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
52 public String getContentType()
53 {
54
55
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 String namespace = NamespaceMapperAccess.getNamespaceMapper().encode(getInternalPortletWindow().getId(), "");
74
75
76
77 if (namespace.indexOf('-') != -1) {
78 namespace = namespace.replace('-', '_');
79 }
80
81 return namespace;
82 }
83
84 public void setTitle(String title)
85 {
86 DynamicTitle.setDynamicTitle(getInternalPortletWindow(),
87 getHttpServletRequest(),
88 title);
89 }
90
91 public void setContentType(String type)
92 {
93 String mimeType = stripCharacterEncoding(type);
94 if (!isValidContentType(mimeType)) {
95 throw new IllegalArgumentException(mimeType);
96 }
97 this._getHttpServletResponse().setContentType(mimeType);
98 currentContentType = mimeType;
99 }
100
101 public String getCharacterEncoding()
102 {
103 return this._getHttpServletResponse().getCharacterEncoding();
104 }
105
106 public PrintWriter getWriter() throws IOException, IllegalStateException {
107 if (currentContentType == null) {
108 throw new java.lang.IllegalStateException(illegalStateExceptionText);
109 }
110
111 return super.getWriter();
112 }
113
114 public java.util.Locale getLocale()
115 {
116 return this.getHttpServletRequest().getLocale();
117 }
118
119 public void setBufferSize(int size)
120 {
121 throw new IllegalStateException("portlet container does not support buffering");
122 }
123
124 public int getBufferSize()
125 {
126
127 return 0;
128 }
129
130 public void flushBuffer() throws java.io.IOException
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 getPortletOutputStream() throws java.io.IOException,java.lang.IllegalStateException
151 {
152 if (currentContentType == null) {
153 throw new java.lang.IllegalStateException(illegalStateExceptionText);
154 }
155 return getOutputStream();
156 }
157
158
159
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 type)
169 {
170 type = stripCharacterEncoding(type);
171 PortletEntity entity = portletWindow.getPortletEntity();
172 PortletDefinition def = entity.getPortletDefinition();
173 ContentTypeSet contentTypes = def.getContentTypeSet();
174 Iterator it = contentTypes.iterator();
175 while(it.hasNext()) {
176 ContentType ct = (ContentType)it.next();
177 String supportedType = ct.getContentType();
178 if (supportedType.equals(type)) {
179 return true;
180 } else if (supportedType.indexOf("*") >= 0) {
181
182 int index = supportedType.indexOf("/");
183 String supportedPrefix = supportedType.substring(0, index);
184 String supportedSuffix = supportedType.substring(index + 1, supportedType.length());
185
186 index = type.indexOf("/");
187 String typePrefix = type.substring(0, index);
188 String typeSuffix = type.substring(index + 1, type.length());
189
190 if (supportedPrefix.equals("*") || supportedPrefix.equals(typePrefix)) {
191
192 if (supportedSuffix.equals("*") || supportedSuffix.equals(typeSuffix)) {
193
194 return true;
195 }
196 }
197 }
198 }
199 return false;
200 }
201
202 private String stripCharacterEncoding(String type)
203 {
204 int xs = type.indexOf(';');
205 String strippedType;
206 if (xs == -1) {
207 strippedType = type;
208 } else {
209 strippedType = type.substring(0,xs);
210 }
211 return strippedType.trim();
212 }
213
214 }