1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
5 import java
.nio
.file
.FileSystem
;
6 import java
.nio
.file
.FileSystemAlreadyExistsException
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.HashMap
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.RepositoryFactory
;
14 import javax
.jcr
.Session
;
16 import org
.argeo
.cms
.CmsException
;
17 import org
.argeo
.cms
.auth
.CurrentUser
;
18 import org
.argeo
.jackrabbit
.fs
.AbstractJackrabbitFsProvider
;
19 import org
.argeo
.jcr
.JcrUtils
;
20 import org
.argeo
.jcr
.fs
.JcrFileSystem
;
21 import org
.argeo
.jcr
.fs
.JcrFsException
;
22 import org
.argeo
.node
.NodeConstants
;
23 import org
.argeo
.node
.NodeUtils
;
24 import org
.osgi
.framework
.BundleContext
;
25 import org
.osgi
.framework
.FrameworkUtil
;
27 public class CmsFsProvider
extends AbstractJackrabbitFsProvider
{
28 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
29 private BundleContext bc
= FrameworkUtil
.getBundle(CmsFsProvider
.class).getBundleContext();
32 public String
getScheme() {
33 return NodeConstants
.SCHEME_NODE
;
37 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
38 String username
= CurrentUser
.getUsername();
39 if (username
== null) {
40 // TODO deal with anonymous
43 if (fileSystems
.containsKey(username
))
44 throw new FileSystemAlreadyExistsException("CMS file system already exists for user " + username
);
47 String host
= uri
.getHost();
48 if (host
!= null && !host
.trim().equals("")) {
49 URI repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), "/jcr/node", null, null);
50 RepositoryFactory repositoryFactory
= bc
.getService(bc
.getServiceReference(RepositoryFactory
.class));
51 Repository repository
= NodeUtils
.getRepositoryByUri(repositoryFactory
, repoUri
.toString());
52 Session session
= repository
.login("main");
53 JcrFileSystem fileSystem
= new JcrFileSystem(this, session
);
54 fileSystems
.put(username
, fileSystem
);
57 Repository repository
= bc
.getService(
58 bc
.getServiceReferences(Repository
.class, "(cn=" + NodeConstants
.HOME
+ ")").iterator().next());
59 Session session
= repository
.login();
60 JcrFileSystem fileSystem
= new JcrFileSystem(this, session
);
61 fileSystems
.put(username
, fileSystem
);
64 } catch (Exception e
) {
65 throw new CmsException("Cannot open file system " + uri
+ " for user " + username
, e
);
70 public FileSystem
getFileSystem(URI uri
) {
71 return currentUserFileSystem();
75 public Path
getPath(URI uri
) {
76 JcrFileSystem fileSystem
= currentUserFileSystem();
77 String path
= uri
.getPath();
78 if (fileSystem
== null)
80 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
81 } catch (IOException e
) {
82 throw new JcrFsException("Could not autocreate file system", e
);
84 return fileSystem
.getPath(path
);
87 protected JcrFileSystem
currentUserFileSystem() {
88 String username
= CurrentUser
.getUsername();
89 return fileSystems
.get(username
);
92 public Node
getUserHome(Session session
) {
93 return NodeUtils
.getUserHome(session
);