- HttpSession httpSession = request.getSession();
- if (httpSession.getAttribute(HttpContext.AUTHORIZATION) == null) {
-
- Collection<ServiceReference<WebCmsSession>> sr;
- try {
- sr = bc.getServiceReferences(WebCmsSession.class,
- "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessId + ")");
- } catch (InvalidSyntaxException e) {
- throw new CmsException("Cannot get CMS session for id " + httpSessId, e);
+ CmsSessionImpl cmsSession;
+ CmsSessionImpl currentLocalSession = CmsSessionImpl.getByLocalId(httpSessId);
+ if (currentLocalSession != null) {
+ boolean currentLocalSessionAnonymous = currentLocalSession.getAuthorization().getName() == null;
+ if (!anonymous) {
+ if (currentLocalSessionAnonymous) {
+ currentLocalSession.close();
+ // new CMS session
+ cmsSession = new WebCmsSessionImpl(subject, authorization, locale, request);
+ } else if (!authorization.getName().equals(currentLocalSession.getAuthorization().getName())) {
+ throw new IllegalStateException("Inconsistent user " + authorization.getName()
+ + " for existing CMS session " + currentLocalSession);
+ } else {
+ // keep current session
+ cmsSession = currentLocalSession;
+ // keyring
+ subject.getPrivateCredentials().addAll(cmsSession.getSecretKeys());
+ }
+ } else {// anonymous
+ if (!currentLocalSessionAnonymous) {
+ currentLocalSession.close();
+ throw new IllegalStateException(
+ "Existing CMS session " + currentLocalSession + " was not logged out properly.");
+ }
+ // keep current session
+ cmsSession = currentLocalSession;