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
.login
.LoginContext
;
9 import javax
.security
.auth
.login
.LoginException
;
11 import org
.argeo
.api
.acr
.ContentSession
;
12 import org
.argeo
.api
.acr
.spi
.ProvidedRepository
;
13 import org
.argeo
.api
.cms
.CmsAuth
;
14 import org
.argeo
.api
.cms
.CmsSession
;
15 import org
.argeo
.api
.cms
.CmsState
;
16 import org
.argeo
.api
.cms
.DataAdminPrincipal
;
17 import org
.argeo
.api
.uuid
.UuidFactory
;
18 import org
.argeo
.cms
.CurrentUser
;
19 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
20 import org
.argeo
.cms
.util
.CurrentSubject
;
23 * Multi-session {@link ProvidedRepository}, integrated with a CMS.
25 public class CmsContentRepository
extends AbstractContentRepository
{
26 public final static String RUN_BASE
= "/run";
27 public final static String DIRECTORY_BASE
= "/directory";
29 private Map
<CmsSession
, CmsContentSession
> userSessions
= Collections
.synchronizedMap(new HashMap
<>());
31 private CmsState cmsState
;
32 private UuidFactory uuidFactory
;
39 public ContentSession
get() {
40 return get(CmsContextImpl
.getCmsContext().getDefaultLocale());
44 public ContentSession
get(Locale locale
) {
45 if (!CmsSession
.hasCmsSession(CurrentSubject
.current())) {
46 if (DataAdminPrincipal
.isDataAdmin(CurrentSubject
.current())) {
47 // TODO open multiple data admin sessions?
48 return getSystemSession();
50 throw new IllegalStateException("Caller must be authenticated");
53 CmsSession cmsSession
= CurrentUser
.getCmsSession();
54 CmsContentSession contentSession
= userSessions
.get(cmsSession
);
55 if (contentSession
== null) {
56 final CmsContentSession newContentSession
= new CmsContentSession(this, cmsSession
.getUuid(),
57 cmsSession
.getSubject(), locale
, uuidFactory
);
58 cmsSession
.addOnCloseCallback((c
) -> {
59 newContentSession
.close();
60 userSessions
.remove(cmsSession
);
62 contentSession
= newContentSession
;
64 return contentSession
;
68 protected CmsContentSession
newSystemSession() {
69 LoginContext loginContext
;
71 loginContext
= new LoginContext(CmsAuth
.DATA_ADMIN
.getLoginContextName());
73 } catch (LoginException e1
) {
74 throw new RuntimeException("Could not login as data admin", e1
);
77 return new CmsContentSession(this, getCmsState().getUuid(), loginContext
.getSubject(), Locale
.getDefault(),
81 protected CmsState
getCmsState() {
85 public void setCmsState(CmsState cmsState
) {
86 this.cmsState
= cmsState
;
89 public void setUuidFactory(UuidFactory uuidFactory
) {
90 this.uuidFactory
= uuidFactory
;