1 package org
.argeo
.cms
.acr
;
3 import java
.util
.Collections
;
4 import java
.util
.HashMap
;
5 import java
.util
.Locale
;
8 import javax
.security
.auth
.Subject
;
9 import javax
.security
.auth
.login
.LoginContext
;
10 import javax
.security
.auth
.login
.LoginException
;
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
;
24 * Multi-session {@link ProvidedRepository}, integrated with a CMS.
26 public class CmsContentRepository
extends AbstractContentRepository
{
27 public final static String RUN_BASE
= "/run";
28 public final static String DIRECTORY_BASE
= "/directory";
30 private Map
<CmsSession
, CmsContentSession
> userSessions
= Collections
.synchronizedMap(new HashMap
<>());
32 private CmsState cmsState
;
33 private UuidFactory uuidFactory
;
40 public ContentSession
get() {
41 return get(CmsContextImpl
.getCmsContext().getDefaultLocale());
45 public ContentSession
get(Locale locale
) {
46 Subject subject
= CurrentSubject
.current();
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();
54 throw new IllegalStateException("Caller must be authenticated");
57 CmsSession cmsSession
= CurrentUser
.getCmsSession();
58 CmsContentSession contentSession
= userSessions
.get(cmsSession
);
59 if (contentSession
== null) {
60 final CmsContentSession newContentSession
= new CmsContentSession(this, cmsSession
.uuid(),
61 cmsSession
.getSubject(), locale
, uuidFactory
);
62 cmsSession
.addOnCloseCallback((c
) -> {
63 newContentSession
.close();
64 userSessions
.remove(cmsSession
);
66 contentSession
= newContentSession
;
68 return contentSession
;
72 protected CmsContentSession
newSystemSession() {
73 LoginContext loginContext
;
75 loginContext
= new LoginContext(CmsAuth
.DATA_ADMIN
.getLoginContextName());
77 } catch (LoginException e1
) {
78 throw new RuntimeException("Could not login as data admin", e1
);
81 return new CmsContentSession(this, getCmsState().getUuid(), loginContext
.getSubject(), Locale
.getDefault(),
85 protected CmsState
getCmsState() {
89 public void setCmsState(CmsState cmsState
) {
90 this.cmsState
= cmsState
;
93 public void setUuidFactory(UuidFactory uuidFactory
) {
94 this.uuidFactory
= uuidFactory
;