]> git.argeo.org Git - lgpl/argeo-commons.git/blob - acr/CmsContentRepository.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.login.LoginContext;
9 import javax.security.auth.login.LoginException;
10
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.uuid.UuidFactory;
17 import org.argeo.cms.auth.CurrentUser;
18 import org.argeo.cms.internal.runtime.CmsContextImpl;
19
20 /**
21 * Multi-session {@link ProvidedRepository}, integrated with a CMS.
22 */
23 public class CmsContentRepository extends AbstractContentRepository {
24 public final static String RUN_BASE = "/run";
25
26 private Map<CmsSession, CmsContentSession> userSessions = Collections.synchronizedMap(new HashMap<>());
27
28 private CmsState cmsState;
29 private UuidFactory uuidFactory;
30
31 /*
32 * REPOSITORY
33 */
34
35 @Override
36 public ContentSession get() {
37 return get(CmsContextImpl.getCmsContext().getDefaultLocale());
38 }
39
40 @Override
41 public ContentSession get(Locale locale) {
42 // Subject subject = Subject.getSubject(AccessController.getContext());
43 CmsSession cmsSession = CurrentUser.getCmsSession();
44 CmsContentSession contentSession = userSessions.get(cmsSession);
45 if (contentSession == null) {
46 final CmsContentSession newContentSession = new CmsContentSession(this, cmsSession.getUuid(),
47 cmsSession.getSubject(), locale);
48 cmsSession.addOnCloseCallback((c) -> {
49 newContentSession.close();
50 userSessions.remove(cmsSession);
51 });
52 contentSession = newContentSession;
53 }
54 return contentSession;
55 }
56
57 @Override
58 protected CmsContentSession newSystemSession() {
59 LoginContext loginContext;
60 try {
61 loginContext = new LoginContext(CmsAuth.DATA_ADMIN.getLoginContextName());
62 loginContext.login();
63 } catch (LoginException e1) {
64 throw new RuntimeException("Could not login as data admin", e1);
65 } finally {
66 }
67 return new CmsContentSession(this, getCmsState().getUuid(), loginContext.getSubject(),
68 Locale.getDefault());
69 }
70
71 protected CmsState getCmsState() {
72 return cmsState;
73 }
74
75 public void setCmsState(CmsState cmsState) {
76 this.cmsState = cmsState;
77 }
78
79 public void setUuidFactory(UuidFactory uuidFactory) {
80 this.uuidFactory = uuidFactory;
81 }
82
83 }