1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.server;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.portlet.MimeResponse;
import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceURL;
import com.vaadin.Application;
import com.vaadin.external.json.JSONException;
import com.vaadin.external.json.JSONObject;
import com.vaadin.terminal.DeploymentConfiguration;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.StreamVariable;
import com.vaadin.terminal.WrappedRequest;
import com.vaadin.terminal.WrappedResponse;
import com.vaadin.terminal.gwt.client.Connector;
import com.vaadin.ui.Root;
/**
* TODO document me!
*
* @author peholmst
*
*/
@SuppressWarnings("serial")
public class PortletCommunicationManager extends AbstractCommunicationManager {
private transient MimeResponse currentMimeResponse;
public PortletCommunicationManager(Application application) {
super(application);
}
public void handleFileUpload(Root root, WrappedRequest request,
WrappedResponse response) throws IOException {
String contentType = request.getContentType();
String name = request.getParameter("name");
String ownerId = request.getParameter("rec-owner");
Connector owner = getConnector(root, ownerId);
StreamVariable streamVariable = ownerToNameToStreamVariable.get(owner)
.get(name);
if (contentType.contains("boundary")) {
doHandleSimpleMultipartFileUpload(request, response,
streamVariable, name, owner,
contentType.split("boundary=")[1]);
} else {
doHandleXhrFilePost(request, response, streamVariable, name, owner,
request.getContentLength());
}
}
@Override
protected void postPaint(Root root) {
super.postPaint(root);
Application application = root.getApplication();
if (ownerToNameToStreamVariable != null) {
Iterator<Connector> iterator = ownerToNameToStreamVariable.keySet()
.iterator();
while (iterator.hasNext()) {
Connector owner = iterator.next();
if (getConnector(root, owner.getConnectorId()) == null) {
// Owner is no longer attached to the application
iterator.remove();
}
}
}
}
@Override
protected boolean handleApplicationRequest(WrappedRequest request,
WrappedResponse response) throws IOException {
setCurrentMimeReponse(response);
try {
return super.handleApplicationRequest(request, response);
} finally {
currentMimeResponse = null;
}
}
private void setCurrentMimeReponse(WrappedResponse response) {
PortletResponse portletResponse = ((WrappedPortletResponse) response)
.getPortletResponse();
if (portletResponse instanceof MimeResponse) {
currentMimeResponse = (MimeResponse) portletResponse;
}
}
@Override
public void handleUidlRequest(WrappedRequest request,
WrappedResponse response, Callback callback, Root root)
throws IOException, InvalidUIDLSecurityKeyException, JSONException {
setCurrentMimeReponse(response);
super.handleUidlRequest(request, response, callback, root);
currentMimeResponse = null;
}
@Override
public void handleBrowserDetailsRequest(WrappedRequest request,
WrappedResponse response, Application application)
throws IOException {
setCurrentMimeReponse(response);
super.handleBrowserDetailsRequest(request, response, application);
currentMimeResponse = null;
}
private Map<Connector, Map<String, StreamVariable>> ownerToNameToStreamVariable;
@Override
String getStreamVariableTargetUrl(Connector owner, String name,
StreamVariable value) {
if (ownerToNameToStreamVariable == null) {
ownerToNameToStreamVariable = new HashMap<Connector, Map<String, StreamVariable>>();
}
Map<String, StreamVariable> nameToReceiver = ownerToNameToStreamVariable
.get(owner);
if (nameToReceiver == null) {
nameToReceiver = new HashMap<String, StreamVariable>();
ownerToNameToStreamVariable.put(owner, nameToReceiver);
}
nameToReceiver.put(name, value);
ResourceURL resurl = createResourceURL();
resurl.setResourceID("UPLOAD");
resurl.setParameter("name", name);
resurl.setParameter("rec-owner", owner.getConnectorId());
resurl.setProperty("name", name);
resurl.setProperty("rec-owner", owner.getConnectorId());
return resurl.toString();
}
private ResourceURL createResourceURL() {
if (currentMimeResponse == null) {
throw new RuntimeException(
"No reponse object available. Cannot create a resource URL");
}
return currentMimeResponse.createResourceURL();
}
@Override
protected void cleanStreamVariable(Connector owner, String name) {
Map<String, StreamVariable> map = ownerToNameToStreamVariable
.get(owner);
map.remove(name);
if (map.isEmpty()) {
ownerToNameToStreamVariable.remove(owner);
}
}
@Override
protected BootstrapHandler createBootstrapHandler() {
return new BootstrapHandler() {
@Override
public boolean handleRequest(Application application,
WrappedRequest request, WrappedResponse response)
throws IOException {
PortletRequest portletRequest = WrappedPortletRequest.cast(
request).getPortletRequest();
if (portletRequest instanceof RenderRequest) {
return super.handleRequest(application, request, response);
} else {
return false;
}
}
@Override
protected String getApplicationId(BootstrapContext context) {
PortletRequest portletRequest = WrappedPortletRequest.cast(
context.getRequest()).getPortletRequest();
/*
* We need to generate a unique ID because some portals already
* create a DIV with the portlet's Window ID as the DOM ID.
*/
return "v-" + portletRequest.getWindowID();
}
@Override
protected String getAppUri(BootstrapContext context) {
return getRenderResponse(context).createActionURL().toString();
}
private RenderResponse getRenderResponse(BootstrapContext context) {
PortletResponse response = ((WrappedPortletResponse) context
.getResponse()).getPortletResponse();
RenderResponse renderResponse = (RenderResponse) response;
return renderResponse;
}
@Override
protected JSONObject getDefaultParameters(BootstrapContext context)
throws JSONException {
/*
* We need this in order to get uploads to work. TODO this is
* not needed for uploads anymore, check if this is needed for
* some other things
*/
JSONObject defaults = super.getDefaultParameters(context);
defaults.put("usePortletURLs", true);
ResourceURL uidlUrlBase = getRenderResponse(context)
.createResourceURL();
uidlUrlBase.setResourceID("UIDL");
defaults.put("portletUidlURLBase", uidlUrlBase.toString());
defaults.put("pathInfo", "");
return defaults;
}
@Override
protected void writeMainScriptTagContents(BootstrapContext context)
throws JSONException, IOException {
// fixed base theme to use - all portal pages with Vaadin
// applications will load this exactly once
String portalTheme = WrappedPortletRequest
.cast(context.getRequest())
.getPortalProperty(
AbstractApplicationPortlet.PORTAL_PARAMETER_VAADIN_THEME);
if (portalTheme != null
&& !portalTheme.equals(context.getThemeName())) {
String portalThemeUri = getThemeUri(context, portalTheme);
// XSS safe - originates from portal properties
context.getWriter().write(
"vaadin.loadTheme('" + portalThemeUri + "');");
}
super.writeMainScriptTagContents(context);
}
@Override
protected String getMainDivStyle(BootstrapContext context) {
DeploymentConfiguration deploymentConfiguration = context
.getRequest().getDeploymentConfiguration();
return deploymentConfiguration.getApplicationOrSystemProperty(
AbstractApplicationPortlet.PORTLET_PARAMETER_STYLE,
null);
}
@Override
protected String getInitialUIDL(WrappedRequest request, Root root)
throws PaintException, JSONException {
return PortletCommunicationManager.this.getInitialUIDL(request,
root);
}
@Override
protected JSONObject getApplicationParameters(
BootstrapContext context) throws JSONException,
PaintException {
JSONObject parameters = super.getApplicationParameters(context);
WrappedPortletResponse wrappedPortletResponse = (WrappedPortletResponse) context
.getResponse();
MimeResponse portletResponse = (MimeResponse) wrappedPortletResponse
.getPortletResponse();
ResourceURL resourceURL = portletResponse.createResourceURL();
resourceURL.setResourceID("browserDetails");
parameters.put("browserDetailsUrl", resourceURL.toString());
return parameters;
}
};
}
@Override
protected InputStream getThemeResourceAsStream(Root root, String themeName,
String resource) {
PortletApplicationContext2 context = (PortletApplicationContext2) root
.getApplication().getContext();
PortletContext portletContext = context.getPortletSession()
.getPortletContext();
return portletContext.getResourceAsStream("/"
+ AbstractApplicationPortlet.THEME_DIRECTORY_PATH + themeName
+ "/" + resource);
}
}
|