1 package org
.argeo
.cms
.acr
;
3 import java
.nio
.file
.Path
;
4 import java
.nio
.file
.Paths
;
5 import java
.util
.Locale
;
6 import java
.util
.Objects
;
9 import javax
.security
.auth
.Subject
;
10 import javax
.security
.auth
.x500
.X500Principal
;
12 import org
.argeo
.api
.acr
.ContentSession
;
13 import org
.argeo
.api
.acr
.spi
.ProvidedRepository
;
14 import org
.argeo
.cms
.acr
.fs
.FsContentProvider
;
15 import org
.argeo
.util
.naming
.LdapAttrs
;
18 * A standalone {@link ProvidedRepository} with a single {@link Subject} (which
19 * also provides the system session).
21 public class SingleUserContentRepository
extends AbstractContentRepository
{
22 private final Subject subject
;
23 private final Locale locale
;
28 private CmsContentSession contentSession
;
30 public SingleUserContentRepository(Subject subject
) {
31 this(subject
, Locale
.getDefault());
34 public SingleUserContentRepository(Subject subject
, Locale locale
) {
35 Objects
.requireNonNull(subject
);
36 Objects
.requireNonNull(locale
);
38 this.subject
= subject
;
41 // TODO use an UUID factory
42 this.uuid
= UUID
.randomUUID();
47 Objects
.requireNonNull(subject
);
48 Objects
.requireNonNull(locale
);
51 initRootContentProvider(null);
52 if (contentSession
!= null)
53 throw new IllegalStateException("Repository is already started, stop it first.");
54 contentSession
= new CmsContentSession(this, uuid
, subject
, locale
);
59 if (contentSession
!= null)
60 contentSession
.close();
61 contentSession
= null;
66 public ContentSession
get(Locale locale
) {
67 if (!this.locale
.equals(locale
))
68 throw new UnsupportedOperationException("This repository does not support multi-locale sessions");
69 return contentSession
;
73 public ContentSession
get() {
74 return contentSession
;
78 protected CmsContentSession
newSystemSession() {
79 return new CmsContentSession(this, uuid
, subject
, Locale
.getDefault());
82 public static void main(String
... args
) {
83 Path homePath
= Paths
.get(System
.getProperty("user.home"));
84 String username
= System
.getProperty("user.name");
85 X500Principal principal
= new X500Principal(LdapAttrs
.uid
+ "=" + username
+ ",dc=localhost");
86 Subject subject
= new Subject();
87 subject
.getPrincipals().add(principal
);
89 SingleUserContentRepository contentRepository
= new SingleUserContentRepository(subject
);
90 contentRepository
.start();
91 FsContentProvider homeContentProvider
= new FsContentProvider("/home", homePath
);
92 contentRepository
.addProvider(homeContentProvider
);
93 Runtime
.getRuntime().addShutdownHook(new Thread(() -> contentRepository
.stop(), "Shutdown content repository"));
95 ContentSession contentSession
= contentRepository
.get();
96 ContentUtils
.traverse(contentSession
.get("/"), (c
, depth
) -> ContentUtils
.print(c
, System
.out
, depth
, false),