- static boolean logoutSession(BundleContext bc, Subject subject) {
- String httpSessionId;
- if (subject.getPrivateCredentials(HttpSessionId.class).size() == 1)
- httpSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
- else
- return false;
- Collection<ServiceReference<WebCmsSession>> srs;
- try {
- srs = bc.getServiceReferences(WebCmsSession.class,
- "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessionId + ")");
- } catch (InvalidSyntaxException e) {
- throw new CmsException("Cannot retrieve CMS session #" + httpSessionId, e);
- }
+// public static CmsSessionImpl cmsSessionFromHttpSession(BundleContext bc, String httpSessionId) {
+// Authorization authorization = null;
+// Collection<ServiceReference<CmsSession>> sr;
+// try {
+// sr = bc.getServiceReferences(CmsSession.class,
+// "(" + CmsSession.SESSION_LOCAL_ID + "=" + httpSessionId + ")");
+// } catch (InvalidSyntaxException e) {
+// throw new IllegalArgumentException("Cannot get CMS session for id " + httpSessionId, e);
+// }
+// CmsSessionImpl cmsSession;
+// if (sr.size() == 1) {
+// cmsSession = (CmsSessionImpl) bc.getService(sr.iterator().next());
+//// locale = cmsSession.getLocale();
+// authorization = cmsSession.getAuthorization();
+// if (authorization.getName() == null)
+// return null;// anonymous is not sufficient
+// } else if (sr.size() == 0)
+// return null;
+// else
+// throw new IllegalStateException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
+// return cmsSession;
+// }
+
+ public static <T extends Principal> T getSinglePrincipal(Subject subject, Class<T> clss) {
+ Set<T> principals = subject.getPrincipals(clss);
+ if (principals.isEmpty())
+ return null;
+ if (principals.size() > 1)
+ throw new IllegalStateException("Only one " + clss + " principal expected in " + subject);
+ return principals.iterator().next();
+ }