Improve remoting
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 8 Jun 2018 11:24:20 +0000 (13:24 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 8 Jun 2018 11:24:20 +0000 (13:24 +0200)
org.argeo.cms/src/org/argeo/cms/internal/auth/CmsSessionImpl.java
org.argeo.cms/src/org/argeo/cms/internal/http/CmsSessionProvider.java
org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeHttp.java
org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystemProvider.java

index 863f7c202511bee640d447c129cd3981a35b12a3..82a6972cbf0e5660de9d6cf02728ba1e72e01aae 100644 (file)
@@ -17,6 +17,7 @@ import java.util.UUID;
 
 import javax.crypto.SecretKey;
 import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.naming.InvalidNameException;
 import javax.naming.ldap.LdapName;
@@ -123,7 +124,7 @@ public class CmsSessionImpl implements CmsSession {
        private Subject getSubject() {
                return Subject.getSubject(initialContext);
        }
-       
+
        public Set<SecretKey> getSecretKeys() {
                return getSubject().getPrivateCredentials(SecretKey.class);
        }
@@ -178,6 +179,8 @@ public class CmsSessionImpl implements CmsSession {
                if (additionalDataSessions.contains(session)) {
                        JcrUtils.logoutQuietly(session);
                        additionalDataSessions.remove(session);
+                       if (log.isTraceEnabled())
+                               log.trace("Remove additional data session " + session);
                        return;
                }
                String path = cn + '/' + session.getWorkspace().getName();
@@ -187,6 +190,8 @@ public class CmsSessionImpl implements CmsSession {
                Session registeredSession = dataSessions.get(path);
                if (session != registeredSession)
                        log.warn("Data session " + path + " not consistent for " + userDn);
+               if (log.isTraceEnabled())
+                       log.trace("Released data session " + session + " for " + path);
                notifyAll();
        }
 
index 943decaf8a61e19376c7b32d8df04fa887e08d47..a1ddcb0babb26315ac569bf0297e03f7094e6a43 100644 (file)
@@ -39,7 +39,7 @@ public class CmsSessionProvider implements SessionProvider, Serializable {
                // if (cmsSession == null)
                // return anonymousSession(request, rep, workspace);
                if (log.isTraceEnabled()) {
-                       log.debug("Get JCR session from " + cmsSession);
+                       log.trace("Get JCR session from " + cmsSession);
                }
                Session session = cmsSession.getDataSession(alias, workspace, rep);
                cmsSessions.put(session, cmsSession);
index 7df6a494b8062695d5c0c6b83fbc2067a51a832b..d500627a75926647c5fd93d7fdd9478532e2258b 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.cms.internal.kernel;
 
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Properties;
@@ -326,21 +327,13 @@ public class NodeHttp implements KernelConstants {
                @Override
                protected void service(final HttpServletRequest request, final HttpServletResponse response)
                                throws ServletException, IOException {
-                       // try {
-                       // Subject subject = subjectFromRequest(request, response);
-                       // Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
-                       // @Override
-                       // public Void run() throws Exception {
+                       // request.setCharacterEncoding("US-ASCII");
+                       System.out.println("## DEFAULT CHARSET: " + Charset.defaultCharset());
+                       System.out.println("## Request CHARSET: " + request.getCharacterEncoding());
+
                        if (log.isTraceEnabled())
                                HttpUtils.logRequest(log, request);
                        RemotingServlet.super.service(request, response);
-                       // return null;
-                       // }
-                       // });
-                       // } catch (PrivilegedActionException e) {
-                       // throw new CmsException("Cannot process JCR remoting request",
-                       // e.getException());
-                       // }
                }
        }
 
index 5e3e8ca213041e7652fdf4c826e7b7c8d9882593..85c2bcfd0c7ad5552a2dadc3a492359267e49e38 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.jcr.fs;
 
 import java.io.IOException;
 import java.nio.channels.SeekableByteChannel;
+import java.nio.charset.Charset;
 import java.nio.file.AccessMode;
 import java.nio.file.CopyOption;
 import java.nio.file.DirectoryNotEmptyException;
@@ -34,11 +35,12 @@ import org.apache.commons.io.FileExistsException;
 import org.argeo.jcr.JcrUtils;
 
 public abstract class JcrFileSystemProvider extends FileSystemProvider {
+
        @Override
        public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)
                        throws IOException {
+               Node node = toNode(path);
                try {
-                       Node node = toNode(path);
                        if (node == null) {
                                Node parent = toNode(path.getParent());
                                if (parent == null)
@@ -48,7 +50,6 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                        throw new IOException(path + " parent is a file");
 
                                String fileName = path.getFileName().toString();
-                               fileName = Text.escapeIllegalJcrChars(fileName);
                                node = parent.addNode(fileName, NodeType.NT_FILE);
                                node.addMixin(NodeType.MIX_CREATED);
                                node.addMixin(NodeType.MIX_LAST_MODIFIED);
@@ -57,6 +58,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                throw new UnsupportedOperationException(node + " must be a file");
                        return new BinaryChannel(node);
                } catch (RepositoryException e) {
+                       discardChanges(node);
                        throw new IOException("Cannot read file", e);
                }
        }
@@ -73,8 +75,8 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
 
        @Override
        public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
+               Node node = toNode(dir);
                try {
-                       Node node = toNode(dir);
                        if (node == null) {
                                Node parent = toNode(dir.getParent());
                                if (parent == null)
@@ -83,7 +85,6 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                                || parent.getPrimaryNodeType().isNodeType(NodeType.NT_LINKED_FILE))
                                        throw new IOException(dir + " parent is a file");
                                String fileName = dir.getFileName().toString();
-                               fileName = Text.escapeIllegalJcrChars(fileName);
                                node = parent.addNode(fileName, NodeType.NT_FOLDER);
                                node.addMixin(NodeType.MIX_CREATED);
                                node.addMixin(NodeType.MIX_LAST_MODIFIED);
@@ -93,15 +94,15 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                        throw new FileExistsException(dir + " exists and is not a directory");
                        }
                } catch (RepositoryException e) {
+                       discardChanges(node);
                        throw new IOException("Cannot create directory " + dir, e);
                }
-
        }
 
        @Override
        public void delete(Path path) throws IOException {
+               Node node = toNode(path);
                try {
-                       Node node = toNode(path);
                        if (node == null)
                                throw new NoSuchFileException(path + " does not exist");
                        Session session = node.getSession();
@@ -114,6 +115,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        }
                        session.save();
                } catch (RepositoryException e) {
+                       discardChanges(node);
                        throw new IOException("Cannot delete " + path, e);
                }
 
@@ -121,24 +123,27 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
 
        @Override
        public void copy(Path source, Path target, CopyOption... options) throws IOException {
+               Node sourceNode = toNode(source);
+               Node targetNode = toNode(target);
                try {
-                       Node sourceNode = toNode(source);
-                       Node targetNode = toNode(target);
                        JcrUtils.copy(sourceNode, targetNode);
                        sourceNode.getSession().save();
                } catch (RepositoryException e) {
+                       discardChanges(sourceNode);
+                       discardChanges(targetNode);
                        throw new IOException("Cannot copy from " + source + " to " + target, e);
                }
        }
 
        @Override
        public void move(Path source, Path target, CopyOption... options) throws IOException {
+               Node sourceNode = toNode(source);
                try {
-                       Node sourceNode = toNode(source);
                        Session session = sourceNode.getSession();
                        session.move(sourceNode.getPath(), target.toString());
                        session.save();
                } catch (RepositoryException e) {
+                       discardChanges(sourceNode);
                        throw new IOException("Cannot move from " + source + " to " + target, e);
                }
        }
@@ -194,16 +199,12 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
        @Override
        public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options)
                        throws IOException {
-               try {
-                       // TODO check if assignable
-                       Node node = toNode(path);
-                       if (node == null) {
-                               throw new IOException("JCR node not found for " + path);
-                       }
-                       return (A) new JcrBasicfileAttributes(node);
-               } catch (RepositoryException e) {
-                       throw new IOException("Cannot read basic attributes of " + path, e);
+               // TODO check if assignable
+               Node node = toNode(path);
+               if (node == null) {
+                       throw new IOException("JCR node not found for " + path);
                }
+               return (A) new JcrBasicfileAttributes(node);
        }
 
        @Override
@@ -248,8 +249,8 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
 
        @Override
        public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
+               Node node = toNode(path);
                try {
-                       Node node = toNode(path);
                        if (value instanceof byte[]) {
                                JcrUtils.setBinaryAsBytes(node, attribute, (byte[]) value);
                        } else if (value instanceof Calendar) {
@@ -259,12 +260,31 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        }
                        node.getSession().save();
                } catch (RepositoryException e) {
+                       discardChanges(node);
                        throw new IOException("Cannot set attribute " + attribute + " on " + path, e);
                }
        }
 
-       protected Node toNode(Path path) throws RepositoryException {
-               return ((JcrPath) path).getNode();
+       protected Node toNode(Path path) {
+               try {
+                       return ((JcrPath) path).getNode();
+               } catch (RepositoryException e) {
+                       throw new JcrFsException("Cannot convert path " + path + " to JCR Node", e);
+               }
+       }
+
+       /** Discard changes in the underlying session */
+       protected void discardChanges(Node node) {
+               if (node == null)
+                       return;
+               try {
+                       // discard changes
+                       node.getSession().refresh(false);
+               } catch (RepositoryException e) {
+                       e.printStackTrace();
+                       // TODO log out session?
+                       // TODO use Commons logging?
+               }
        }
 
        /**