]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - plugins/org.argeo.slc.client.rap/src/main/java/org/argeo/slc/client/ui/specific/OpenJcrFile.java
Fix file download for rap. Add various links in the bundle detail editor.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.rap / src / main / java / org / argeo / slc / client / ui / specific / OpenJcrFile.java
index 21ed8eb088844b64498bc11317c2f3965629031f..48a0d82979020b8c0338cd7c478cbca1b4d2b6fc 100644 (file)
 package org.argeo.slc.client.ui.specific;
 
 import java.net.URL;
+import java.util.UUID;
 
+import javax.jcr.Node;
 import javax.jcr.Session;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.SlcException;
 import org.argeo.slc.client.rap.OpenJcrFileService;
 import org.argeo.slc.client.rap.SlcRapPlugin;
 import org.argeo.slc.repo.RepoService;
@@ -30,27 +30,30 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.rwt.RWT;
 import org.eclipse.rwt.service.IServiceHandler;
+import org.eclipse.rwt.service.IServiceManager;
 import org.eclipse.ui.PlatformUI;
 
 /**
  * Rap specific command handler to open a file retrieved from a distant JCR
- * Repository. It forwards the request to the correct service after encoding
- * file name and path in the request URI.
+ * Repository. It creates and register a service instantiated with the
+ * corresponding JCR node, forwards the request, and un register the service on
+ * dispose
  * 
  * This command and the corresponding service are specific for RAP version [1.3,
  * 2)
  */
 public class OpenJcrFile extends AbstractHandler {
-       private final static Log log = LogFactory.getLog(OpenJcrFile.class);
+       // private final static Log log = LogFactory.getLog(OpenJcrFile.class);
 
        public final static String ID = SlcRapPlugin.PLUGIN_ID + ".openJcrFile";
 
-       public final static String PARAM_REPO_NODE_PATH = OpenJcrFileService.PARAM_REPO_NODE_PATH;
-       public final static String PARAM_REPO_URI = OpenJcrFileService.PARAM_REPO_URI;
-       public final static String PARAM_WORKSPACE_NAME = OpenJcrFileService.PARAM_WORKSPACE_NAME;
-       public final static String PARAM_FILE_PATH = OpenJcrFileService.PARAM_FILE_PATH;
+       public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
+       public final static String PARAM_REPO_URI = "param.repoUri";
+       public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
+       public final static String PARAM_FILE_PATH = "param.filePath";
 
        private RepoService repoService;
+       private String currentServiceId;
 
        public Object execute(ExecutionEvent event) throws ExecutionException {
 
@@ -59,74 +62,55 @@ public class OpenJcrFile extends AbstractHandler {
                String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
                String filePath = event.getParameter(PARAM_FILE_PATH);
 
-               // TODO remove
-               Session session = repoService.getRemoteSession(repoNodePath, repoUri,
-                               wkspName);
-               JcrUtils.logoutQuietly(session);
-
                // TODO sanity check
                if (filePath == null || "".equals(filePath.trim()))
                        return null;
-
+               Session businessSession = null;
                try {
-                       if (log.isDebugEnabled())
-                               log.debug("URL : "
-                                               + createFullDownloadUrl(repoNodePath, repoUri,
-                                                               wkspName, filePath));
-                       // RWT.getResponse().sendRedirect(createFullDownloadUrl(repoNodePath,
-                       // repoUri,
-                       // wkspName, filePath));
-
-                       URL url = new URL(createFullDownloadUrl(repoNodePath, repoUri,
-                                       wkspName, filePath));
+                       businessSession = repoService.getRemoteSession(repoNodePath,
+                                       repoUri, wkspName);
+                       Node result = businessSession.getNode(filePath);
+
+                       // Create a temporary service. No better solution has been found
+                       // yet.
+                       currentServiceId = UUID.randomUUID().toString();
+                       OpenJcrFileService ojfs = new OpenJcrFileService(result);
+                       IServiceManager manager = RWT.getServiceManager();
+                       manager.registerServiceHandler(currentServiceId, ojfs);
+                       String urlStr = createFullDownloadUrl(currentServiceId);
+                       URL url = new URL(urlStr);
                        PlatformUI.getWorkbench().getBrowserSupport()
                                        .createBrowser("DownloadDialog").openURL(url);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       throw new SlcException("Unable to open Jcr File for path "
+                                       + filePath, e);
                }
 
                return null;
        }
 
-       private String createFullDownloadUrl(String repoNodePath, String repoUri,
-                       String wkspName, String filePath) {
+       @Override
+       public void dispose() {
+               IServiceManager manager = RWT.getServiceManager();
+               manager.unregisterServiceHandler(currentServiceId);
+               super.dispose();
+       }
+
+       private String createFullDownloadUrl(String serviceId) {
                StringBuilder url = new StringBuilder();
                url.append(RWT.getRequest().getRequestURL());
 
                StringBuilder params = new StringBuilder();
                params.append("?");
                params.append(IServiceHandler.REQUEST_PARAM).append("=");
-               params.append(OpenJcrFileService.ID);
-               if (repoNodePath != null)
-                       params.append("&").append(OpenJcrFileService.PARAM_REPO_NODE_PATH)
-                                       .append("=").append(repoNodePath);
-
-               if (repoUri != null)
-                       params.append("&").append(OpenJcrFileService.PARAM_REPO_URI)
-                                       .append("=").append(repoUri);
-
-               if (wkspName != null)
-                       params.append("&").append(OpenJcrFileService.PARAM_WORKSPACE_NAME)
-                                       .append("=").append(wkspName);
-
-               if (filePath != null)
-                       params.append("&").append(OpenJcrFileService.PARAM_FILE_PATH)
-                                       .append("=").append(filePath);
-
+               params.append(serviceId);
                String encodedURL = RWT.getResponse().encodeURL(params.toString());
                url.append(encodedURL);
-
                return url.toString();
        }
 
        /* Dependency Injection */
-       // only used as a workaround to force the service instantiation
-       public void setOpenJcrFileService(OpenJcrFileService openJcrFileService) {
-               // do nothing.
-       }
-
        public void setRepoService(RepoService repoService) {
                this.repoService = repoService;
        }
-
 }
\ No newline at end of file