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
;
8 import javax
.security
.auth
.Subject
;
9 import javax
.security
.auth
.x500
.X500Principal
;
11 import org
.argeo
.api
.acr
.ContentSession
;
12 import org
.argeo
.api
.acr
.spi
.ProvidedRepository
;
13 import org
.argeo
.cms
.acr
.fs
.FsContentProvider
;
14 import org
.argeo
.util
.naming
.LdapAttrs
;
17 * A standalone {@link ProvidedRepository} with a single {@link Subject} (which
18 * also provides the system session).
20 public class SingleUserContentRepository
extends AbstractContentRepository
{
21 private final Subject subject
;
22 private final Locale locale
;
25 private CmsContentSession contentSession
;
27 public SingleUserContentRepository(Subject subject
) {
28 this(subject
, Locale
.getDefault());
31 public SingleUserContentRepository(Subject subject
, Locale locale
) {
32 Objects
.requireNonNull(subject
);
33 Objects
.requireNonNull(locale
);
35 this.subject
= subject
;
41 Objects
.requireNonNull(subject
);
42 Objects
.requireNonNull(locale
);
45 initRootContentProvider(null);
46 if (contentSession
!= null)
47 throw new IllegalStateException("Repository is already started, stop it first.");
48 contentSession
= new CmsContentSession(this, subject
, locale
);
53 if (contentSession
!= null)
54 contentSession
.close();
55 contentSession
= null;
60 public ContentSession
get(Locale locale
) {
61 if (!this.locale
.equals(locale
))
62 throw new UnsupportedOperationException("This repository does not support multi-locale sessions");
63 return contentSession
;
67 public ContentSession
get() {
68 return contentSession
;
72 protected CmsContentSession
newSystemSession() {
73 return new CmsContentSession(this, subject
, Locale
.getDefault());
76 public static void main(String
... args
) {
77 Path homePath
= Paths
.get(System
.getProperty("user.home"));
78 String username
= System
.getProperty("user.name");
79 X500Principal principal
= new X500Principal(LdapAttrs
.uid
+ "=" + username
+ ",dc=localhost");
80 Subject subject
= new Subject();
81 subject
.getPrincipals().add(principal
);
83 SingleUserContentRepository contentRepository
= new SingleUserContentRepository(subject
);
84 contentRepository
.start();
85 FsContentProvider homeContentProvider
= new FsContentProvider("/home", homePath
);
86 contentRepository
.addProvider(homeContentProvider
);
87 Runtime
.getRuntime().addShutdownHook(new Thread(() -> contentRepository
.stop(), "Shutdown content repository"));
89 ContentSession contentSession
= contentRepository
.get();
90 ContentUtils
.traverse(contentSession
.get("/"), (c
, depth
) -> ContentUtils
.print(c
, System
.out
, depth
, false),