]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/CmsContentRepository.java
Make DOM ACR more robust when used as repository root.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / CmsContentRepository.java
1 package org.argeo.cms.acr;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Locale;
6 import java.util.Map;
7
8 import javax.security.auth.Subject;
9 import javax.security.auth.login.LoginContext;
10 import javax.security.auth.login.LoginException;
11
12 import org.argeo.api.acr.ContentSession;
13 import org.argeo.api.acr.spi.ProvidedRepository;
14 import org.argeo.api.cms.CmsAuth;
15 import org.argeo.api.cms.CmsSession;
16 import org.argeo.api.cms.CmsState;
17 import org.argeo.api.cms.DataAdminPrincipal;
18 import org.argeo.api.uuid.UuidFactory;
19 import org.argeo.cms.CurrentUser;
20 import org.argeo.cms.internal.runtime.CmsContextImpl;
21 import org.argeo.cms.util.CurrentSubject;
22
23 /**
24 * Multi-session {@link ProvidedRepository}, integrated with a CMS.
25 */
26 public class CmsContentRepository extends AbstractContentRepository {
27 public final static String RUN_BASE = "/run";
28 public final static String DIRECTORY_BASE = "/directory";
29
30 private Map<CmsSession, CmsContentSession> userSessions = Collections.synchronizedMap(new HashMap<>());
31
32 private CmsState cmsState;
33 private UuidFactory uuidFactory;
34
35 /*
36 * REPOSITORY
37 */
38
39 @Override
40 public ContentSession get() {
41 return get(CmsContextImpl.getCmsContext().getDefaultLocale());
42 }
43
44 @Override
45 public ContentSession get(Locale locale) {
46 Subject subject = CurrentSubject.current();
47 if (subject == null)
48 throw new IllegalStateException("Caller must be authenticated");
49 if (!CmsSession.hasCmsSession(subject)) {
50 if (DataAdminPrincipal.isDataAdmin(subject)) {
51 // TODO open multiple data admin sessions?
52 return getSystemSession();
53 }
54 throw new IllegalStateException("Caller must be authenticated");
55 }
56
57 CmsSession cmsSession = CurrentUser.getCmsSession();
58 CmsContentSession contentSession = userSessions.get(cmsSession);
59 if (contentSession == null) {
60 final CmsContentSession newContentSession = new CmsContentSession(this, cmsSession.getUuid(),
61 cmsSession.getSubject(), locale, uuidFactory);
62 cmsSession.addOnCloseCallback((c) -> {
63 newContentSession.close();
64 userSessions.remove(cmsSession);
65 });
66 contentSession = newContentSession;
67 }
68 return contentSession;
69 }
70
71 @Override
72 protected CmsContentSession newSystemSession() {
73 LoginContext loginContext;
74 try {
75 loginContext = new LoginContext(CmsAuth.DATA_ADMIN.getLoginContextName());
76 loginContext.login();
77 } catch (LoginException e1) {
78 throw new RuntimeException("Could not login as data admin", e1);
79 } finally {
80 }
81 return new CmsContentSession(this, getCmsState().getUuid(), loginContext.getSubject(), Locale.getDefault(),
82 uuidFactory);
83 }
84
85 protected CmsState getCmsState() {
86 return cmsState;
87 }
88
89 public void setCmsState(CmsState cmsState) {
90 this.cmsState = cmsState;
91 }
92
93 public void setUuidFactory(UuidFactory uuidFactory) {
94 this.uuidFactory = uuidFactory;
95 }
96
97 }