]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/SingleUserContentRepository.java
Make DOM ACR more robust when used as repository root.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / 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.ldap.LdapAttr;
14 import org.argeo.api.acr.spi.ProvidedRepository;
15 import org.argeo.api.uuid.MacAddressUuidFactory;
16 import org.argeo.api.uuid.UuidFactory;
17 import org.argeo.cms.acr.fs.FsContentProvider;
18
19 /**
20 * A standalone {@link ProvidedRepository} with a single {@link Subject} (which
21 * also provides the system session).
22 */
23 public class SingleUserContentRepository extends AbstractContentRepository {
24 private final Subject subject;
25 private final Locale locale;
26
27 private UUID uuid;
28
29 private UuidFactory uuidFactory = new MacAddressUuidFactory();
30
31 // the single session
32 private CmsContentSession contentSession;
33
34 public SingleUserContentRepository(Subject subject) {
35 this(subject, Locale.getDefault());
36 }
37
38 public SingleUserContentRepository(Subject subject, Locale locale) {
39 Objects.requireNonNull(subject);
40 Objects.requireNonNull(locale);
41
42 this.subject = subject;
43 this.locale = locale;
44
45 // TODO use an UUID factory
46 this.uuid = UUID.randomUUID();
47 }
48
49 @Override
50 public void start() {
51 Objects.requireNonNull(subject);
52 Objects.requireNonNull(locale);
53
54 super.start();
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);
59 }
60
61 @Override
62 public void stop() {
63 if (contentSession != null)
64 contentSession.close();
65 contentSession = null;
66 super.stop();
67 }
68
69 @Override
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;
74 }
75
76 @Override
77 public ContentSession get() {
78 return contentSession;
79 }
80
81 @Override
82 protected CmsContentSession newSystemSession() {
83 return new CmsContentSession(this, uuid, subject, Locale.getDefault(), uuidFactory);
84 }
85
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(LdapAttr.uid + "=" + username + ",dc=localhost");
90 Subject subject = new Subject();
91 subject.getPrincipals().add(principal);
92
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"));
98
99 ContentSession contentSession = contentRepository.get();
100 ContentUtils.traverse(contentSession.get("/"), (c, depth) -> ContentUtils.print(c, System.out, depth, false),
101 2);
102
103 }
104 }