]> git.argeo.org Git - lgpl/argeo-commons.git/blob - acr/SingleUserContentRepository.java
Prepare next development cycle
[lgpl/argeo-commons.git] / acr / SingleUserContentRepository.java
1 package org.argeo.cms.acr;
2
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.Locale;
6 import java.util.Objects;
7 import java.util.UUID;
8
9 import javax.security.auth.Subject;
10 import javax.security.auth.x500.X500Principal;
11
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;
16
17 /**
18 * A standalone {@link ProvidedRepository} with a single {@link Subject} (which
19 * also provides the system session).
20 */
21 public class SingleUserContentRepository extends AbstractContentRepository {
22 private final Subject subject;
23 private final Locale locale;
24
25 private UUID uuid;
26
27 // the single session
28 private CmsContentSession contentSession;
29
30 public SingleUserContentRepository(Subject subject) {
31 this(subject, Locale.getDefault());
32 }
33
34 public SingleUserContentRepository(Subject subject, Locale locale) {
35 Objects.requireNonNull(subject);
36 Objects.requireNonNull(locale);
37
38 this.subject = subject;
39 this.locale = locale;
40
41 // TODO use an UUID factory
42 this.uuid = UUID.randomUUID();
43 }
44
45 @Override
46 public void start() {
47 Objects.requireNonNull(subject);
48 Objects.requireNonNull(locale);
49
50 super.start();
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);
55 }
56
57 @Override
58 public void stop() {
59 if (contentSession != null)
60 contentSession.close();
61 contentSession = null;
62 super.stop();
63 }
64
65 @Override
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;
70 }
71
72 @Override
73 public ContentSession get() {
74 return contentSession;
75 }
76
77 @Override
78 protected CmsContentSession newSystemSession() {
79 return new CmsContentSession(this, uuid, subject, Locale.getDefault());
80 }
81
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);
88
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"));
94
95 ContentSession contentSession = contentRepository.get();
96 ContentUtils.traverse(contentSession.get("/"), (c, depth) -> ContentUtils.print(c, System.out, depth, false),
97 2);
98
99 }
100 }