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
.api
.uuid
.MacAddressUuidFactory
;
15 import org
.argeo
.api
.uuid
.UuidFactory
;
16 import org
.argeo
.cms
.acr
.fs
.FsContentProvider
;
17 import org
.argeo
.util
.naming
.LdapAttrs
;
20 * A standalone {@link ProvidedRepository} with a single {@link Subject} (which
21 * also provides the system session).
23 public class SingleUserContentRepository
extends AbstractContentRepository
{
24 private final Subject subject
;
25 private final Locale locale
;
29 private UuidFactory uuidFactory
= new MacAddressUuidFactory();
32 private CmsContentSession contentSession
;
34 public SingleUserContentRepository(Subject subject
) {
35 this(subject
, Locale
.getDefault());
38 public SingleUserContentRepository(Subject subject
, Locale locale
) {
39 Objects
.requireNonNull(subject
);
40 Objects
.requireNonNull(locale
);
42 this.subject
= subject
;
45 // TODO use an UUID factory
46 this.uuid
= UUID
.randomUUID();
51 Objects
.requireNonNull(subject
);
52 Objects
.requireNonNull(locale
);
55 initRootContentProvider(null);
56 if (contentSession
!= null)
57 throw new IllegalStateException("Repository is already started, stop it first.");
58 contentSession
= new CmsContentSession(this, uuid
, subject
, locale
, uuidFactory
);
63 if (contentSession
!= null)
64 contentSession
.close();
65 contentSession
= null;
70 public ContentSession
get(Locale locale
) {
71 if (!this.locale
.equals(locale
))
72 throw new UnsupportedOperationException("This repository does not support multi-locale sessions");
73 return contentSession
;
77 public ContentSession
get() {
78 return contentSession
;
82 protected CmsContentSession
newSystemSession() {
83 return new CmsContentSession(this, uuid
, subject
, Locale
.getDefault(), uuidFactory
);
86 public static void main(String
... args
) {
87 Path homePath
= Paths
.get(System
.getProperty("user.home"));
88 String username
= System
.getProperty("user.name");
89 X500Principal principal
= new X500Principal(LdapAttrs
.uid
+ "=" + username
+ ",dc=localhost");
90 Subject subject
= new Subject();
91 subject
.getPrincipals().add(principal
);
93 SingleUserContentRepository contentRepository
= new SingleUserContentRepository(subject
);
94 contentRepository
.start();
95 FsContentProvider homeContentProvider
= new FsContentProvider("/home", homePath
);
96 contentRepository
.addProvider(homeContentProvider
);
97 Runtime
.getRuntime().addShutdownHook(new Thread(() -> contentRepository
.stop(), "Shutdown content repository"));
99 ContentSession contentSession
= contentRepository
.get();
100 ContentUtils
.traverse(contentSession
.get("/"), (c
, depth
) -> ContentUtils
.print(c
, System
.out
, depth
, false),