]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added rootId to stream variables URLs (#9034)
authorArtur Signell <artur@vaadin.com>
Fri, 29 Jun 2012 06:54:51 +0000 (09:54 +0300)
committerArtur Signell <artur@vaadin.com>
Fri, 29 Jun 2012 06:55:30 +0000 (09:55 +0300)
src/com/vaadin/Application.java
src/com/vaadin/terminal/AbstractClientConnector.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java
src/com/vaadin/terminal/gwt/server/ClientConnector.java
src/com/vaadin/terminal/gwt/server/CommunicationManager.java
src/com/vaadin/terminal/gwt/server/DragAndDropService.java
src/com/vaadin/terminal/gwt/server/JsonPaintTarget.java
src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java

index 84a8df50535ca0a0e30715ff1f5a7ccfde6742f1..468a7ee8befe81763aee03b939e07e51c341e148 100644 (file)
@@ -2373,4 +2373,18 @@ public class Application implements Terminal.ErrorListener, Serializable {
     private static final Logger getLogger() {
         return Logger.getLogger(Application.class.getName());
     }
+
+    /**
+     * Returns a Root with the given id.
+     * <p>
+     * This is meant for framework internal use.
+     * </p>
+     * 
+     * @param rootId
+     *            The root id
+     * @return The root with the given id or null if not found
+     */
+    public Root getRootById(int rootId) {
+        return roots.get(rootId);
+    }
 }
index 9de444d70ee65ac6d3efe0db808b5f2201aebf38..752b5326fcb99cc59c088bf26c2db6fcc93092bc 100644 (file)
@@ -322,7 +322,7 @@ public abstract class AbstractClientConnector implements ClientConnector {
      * @return the Root ancestor of this connector, or <code>null</code> if none
      *         is found.
      */
-    protected Root getRoot() {
+    public Root getRoot() {
         ClientConnector connector = this;
         while (connector != null) {
             if (connector instanceof Root) {
index c2f887674aade310528aa191f8f24ddd90443bba..acfc926d685e69427f822801c5ed0fc5d657a58e 100644 (file)
@@ -638,8 +638,10 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
 
                 /* Handle the request */
                 if (requestType == RequestType.FILE_UPLOAD) {
-                    applicationManager.handleFileUpload(root, wrappedRequest,
-                            wrappedResponse);
+                    // Root is resolved in handleFileUpload by
+                    // PortletCommunicationManager
+                    applicationManager.handleFileUpload(application,
+                            wrappedRequest, wrappedResponse);
                     return;
                 } else if (requestType == RequestType.BROWSER_DETAILS) {
                     applicationManager.handleBrowserDetailsRequest(
index 7d88b432f428e35185f61578b59a260df4f72dc0..f61f40dccc1d5ed762994c9fce8a789c5eb70dc7 100644 (file)
@@ -436,11 +436,9 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
 
             /* Handle the request */
             if (requestType == RequestType.FILE_UPLOAD) {
-                Root root = application.getRootForRequest(request);
-                if (root == null) {
-                    throw new ServletException(ERROR_NO_ROOT_FOUND);
-                }
-                applicationManager.handleFileUpload(root, request, response);
+                // Root is resolved in communication manager
+                applicationManager.handleFileUpload(application, request,
+                        response);
                 return;
             } else if (requestType == RequestType.UIDL) {
                 Root root = application.getRootForRequest(request);
index ba17822813cacde1e4fc7e22a436105482abf74e..d71ab7f892deb9bd2d68551eacce1b552b9e4c3c 100644 (file)
@@ -212,7 +212,7 @@ public abstract class AbstractCommunicationManager implements Serializable {
      */
     protected void doHandleSimpleMultipartFileUpload(WrappedRequest request,
             WrappedResponse response, StreamVariable streamVariable,
-            String variableName, Connector owner, String boundary)
+            String variableName, ClientConnector owner, String boundary)
             throws IOException {
         // multipart parsing, supports only one file for request, but that is
         // fine for our current terminal
@@ -275,14 +275,16 @@ public abstract class AbstractCommunicationManager implements Serializable {
         final String mimeType = rawMimeType;
 
         try {
-            /*
-             * safe cast as in GWT terminal all variable owners are expected to
-             * be components.
-             */
-            Component component = (Component) owner;
-            if (component.isReadOnly()) {
+            // TODO Shouldn't this check connectorEnabled?
+            if (owner == null) {
                 throw new UploadException(
-                        "Warning: file upload ignored because the componente was read-only");
+                        "File upload ignored because the connector for the stream variable was not found");
+            }
+            if (owner instanceof Component) {
+                if (((Component) owner).isReadOnly()) {
+                    throw new UploadException(
+                            "Warning: file upload ignored because the componente was read-only");
+                }
             }
             boolean forgetVariable = streamToReceiver(simpleMultiPartReader,
                     streamVariable, filename, mimeType, contentLength);
@@ -311,7 +313,7 @@ public abstract class AbstractCommunicationManager implements Serializable {
      */
     protected void doHandleXhrFilePost(WrappedRequest request,
             WrappedResponse response, StreamVariable streamVariable,
-            String variableName, Connector owner, int contentLength)
+            String variableName, ClientConnector owner, int contentLength)
             throws IOException {
 
         // These are unknown in filexhr ATM, maybe add to Accept header that
@@ -2273,10 +2275,11 @@ public abstract class AbstractCommunicationManager implements Serializable {
 
     }
 
-    abstract String getStreamVariableTargetUrl(Connector owner, String name,
-            StreamVariable value);
+    abstract String getStreamVariableTargetUrl(ClientConnector owner,
+            String name, StreamVariable value);
 
-    abstract protected void cleanStreamVariable(Connector owner, String name);
+    abstract protected void cleanStreamVariable(ClientConnector owner,
+            String name);
 
     /**
      * Gets the bootstrap handler that should be used for generating the pages
index dfdd58879d2699d4687384ab0b8a251bc32185db..ca1ad349c1ed4739b3ad0ff5a2a21f2419008bdb 100644 (file)
@@ -12,6 +12,7 @@ import com.vaadin.terminal.gwt.client.Connector;
 import com.vaadin.terminal.gwt.client.communication.SharedState;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.ComponentContainer;
+import com.vaadin.ui.Root;
 
 /**
  * Interface implemented by all connectors that are capable of communicating
@@ -136,4 +137,12 @@ public interface ClientConnector extends Connector, RpcTarget {
      *            the extension to remove.
      */
     public void removeExtension(Extension extension);
+
+    /**
+     * Returns the root this connector is attached to
+     * 
+     * @return The Root this connector is attached to or null if it is not
+     *         attached to any Root
+     */
+    public Root getRoot();
 }
index 2d2888e03476942be1a25ad81420dde09877a538..2cf3b23446cff279a68c7e0beff418f6de42ba7d 100644 (file)
@@ -20,7 +20,6 @@ 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;
 
 /**
@@ -72,12 +71,13 @@ public class CommunicationManager extends AbstractCommunicationManager {
      * @throws IOException
      * @throws InvalidUIDLSecurityKeyException
      */
-    public void handleFileUpload(Root root, WrappedRequest request,
-            WrappedResponse response) throws IOException,
-            InvalidUIDLSecurityKeyException {
+    public void handleFileUpload(Application application,
+            WrappedRequest request, WrappedResponse response)
+            throws IOException, InvalidUIDLSecurityKeyException {
 
         /*
-         * URI pattern: APP/UPLOAD/[PID]/[NAME]/[SECKEY] See #createReceiverUrl
+         * URI pattern: APP/UPLOAD/[ROOTID]/[PID]/[NAME]/[SECKEY] See
+         * #createReceiverUrl
          */
 
         String pathInfo = request.getRequestPathInfo();
@@ -86,16 +86,20 @@ public class CommunicationManager extends AbstractCommunicationManager {
                 .indexOf(AbstractApplicationServlet.UPLOAD_URL_PREFIX)
                 + AbstractApplicationServlet.UPLOAD_URL_PREFIX.length();
         String uppUri = pathInfo.substring(startOfData);
-        String[] parts = uppUri.split("/", 3); // 0 = pid, 1= name, 2 = sec key
-        String variableName = parts[1];
-        String connectorId = parts[0];
+        String[] parts = uppUri.split("/", 4); // 0= rootid, 1 = cid, 2= name, 3
+                                               // = sec key
+        String rootId = parts[0];
+        String connectorId = parts[1];
+        String variableName = parts[2];
+        Root root = application.getRootById(Integer.parseInt(rootId));
+        Root.setCurrent(root);
 
         StreamVariable streamVariable = pidToNameToStreamVariable.get(
                 connectorId).get(variableName);
         String secKey = streamVariableToSeckey.get(streamVariable);
-        if (secKey.equals(parts[2])) {
+        if (secKey.equals(parts[3])) {
 
-            Connector source = getConnector(root, connectorId);
+            ClientConnector source = getConnector(root, connectorId);
             String contentType = request.getContentType();
             if (contentType.contains("boundary")) {
                 // Multipart requests contain boundary string
@@ -143,13 +147,13 @@ public class CommunicationManager extends AbstractCommunicationManager {
     private Map<StreamVariable, String> streamVariableToSeckey;
 
     @Override
-    String getStreamVariableTargetUrl(Connector owner, String name,
+    String getStreamVariableTargetUrl(ClientConnector owner, String name,
             StreamVariable value) {
         /*
          * We will use the same APP/* URI space as ApplicationResources but
          * prefix url with UPLOAD
          * 
-         * eg. APP/UPLOAD/[PID]/[NAME]/[SECKEY]
+         * eg. APP/UPLOAD/[ROOTID]/[PID]/[NAME]/[SECKEY]
          * 
          * SECKEY is created on each paint to make URL's unpredictable (to
          * prevent CSRF attacks).
@@ -158,7 +162,8 @@ public class CommunicationManager extends AbstractCommunicationManager {
          * handling post
          */
         String paintableId = owner.getConnectorId();
-        String key = paintableId + "/" + name;
+        int rootId = owner.getRoot().getRootId();
+        String key = rootId + "/" + paintableId + "/" + name;
 
         if (pidToNameToStreamVariable == null) {
             pidToNameToStreamVariable = new HashMap<String, Map<String, StreamVariable>>();
@@ -186,7 +191,7 @@ public class CommunicationManager extends AbstractCommunicationManager {
     }
 
     @Override
-    protected void cleanStreamVariable(Connector owner, String name) {
+    protected void cleanStreamVariable(ClientConnector owner, String name) {
         Map<String, StreamVariable> nameToStreamVar = pidToNameToStreamVariable
                 .get(owner.getConnectorId());
         nameToStreamVar.remove("name");
index 0e8d1c015255eb56b3699e945477d4031bb949df..8e0346f6af249acaee07a5aa5dbf745a0a8bc77e 100644 (file)
@@ -26,6 +26,7 @@ import com.vaadin.terminal.gwt.client.communication.SharedState;
 import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager;
 import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager.DragEventType;
 import com.vaadin.ui.Component;
+import com.vaadin.ui.Root;
 
 public class DragAndDropService implements VariableOwner, ClientConnector {
 
@@ -287,4 +288,8 @@ public class DragAndDropService implements VariableOwner, ClientConnector {
     private Logger getLogger() {
         return Logger.getLogger(DragAndDropService.class.getName());
     }
+
+    public Root getRoot() {
+        return null;
+    }
 }
index 70ab452e4ea21f609e6bbb6df881a773fe2b5114..15b5ced70386845fc0b5f25d27062e9f90b8ca3b 100644 (file)
@@ -24,7 +24,6 @@ import com.vaadin.terminal.Resource;
 import com.vaadin.terminal.StreamVariable;
 import com.vaadin.terminal.ThemeResource;
 import com.vaadin.terminal.VariableOwner;
-import com.vaadin.terminal.gwt.client.Connector;
 import com.vaadin.ui.Alignment;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.CustomLayout;
@@ -992,8 +991,8 @@ public class JsonPaintTarget implements PaintTarget {
 
     public void addVariable(VariableOwner owner, String name,
             StreamVariable value) throws PaintException {
-        String url = manager.getStreamVariableTargetUrl((Connector) owner,
-                name, value);
+        String url = manager.getStreamVariableTargetUrl(
+                (ClientConnector) owner, name, value);
         if (url != null) {
             addVariable(owner, name, url);
         } // else { //NOP this was just a cleanup by component }
index 7398315ee25a08d654267e80fccf99e2259932cb..d45e652110b928d2d61f20ef85e41160e41c2403 100644 (file)
@@ -43,12 +43,19 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
         super(application);
     }
 
-    public void handleFileUpload(Root root, WrappedRequest request,
-            WrappedResponse response) throws IOException {
+    public void handleFileUpload(Application application,
+            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);
+        String rootId = request.getParameter("rootId");
+
+        Root root = application.getRootById(Integer.parseInt(rootId));
+        Root.setCurrent(root);
+
+        ClientConnector owner = getConnector(root, ownerId);
+
         StreamVariable streamVariable = ownerToNameToStreamVariable.get(owner)
                 .get(name);
 
@@ -123,7 +130,7 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
     private Map<Connector, Map<String, StreamVariable>> ownerToNameToStreamVariable;
 
     @Override
-    String getStreamVariableTargetUrl(Connector owner, String name,
+    String getStreamVariableTargetUrl(ClientConnector owner, String name,
             StreamVariable value) {
         if (ownerToNameToStreamVariable == null) {
             ownerToNameToStreamVariable = new HashMap<Connector, Map<String, StreamVariable>>();
@@ -139,8 +146,10 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
         resurl.setResourceID("UPLOAD");
         resurl.setParameter("name", name);
         resurl.setParameter("rec-owner", owner.getConnectorId());
+        resurl.setParameter("rootId", "" + owner.getRoot().getRootId());
         resurl.setProperty("name", name);
         resurl.setProperty("rec-owner", owner.getConnectorId());
+        resurl.setProperty("rootId", "" + owner.getRoot().getRootId());
         return resurl.toString();
     }
 
@@ -153,7 +162,7 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
     }
 
     @Override
-    protected void cleanStreamVariable(Connector owner, String name) {
+    protected void cleanStreamVariable(ClientConnector owner, String name) {
         Map<String, StreamVariable> map = ownerToNameToStreamVariable
                 .get(owner);
         map.remove(name);