1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
5 import java
.util
.Collections
;
6 import java
.util
.Dictionary
;
7 import java
.util
.Enumeration
;
8 import java
.util
.Hashtable
;
10 import java
.util
.Properties
;
12 import javax
.servlet
.http
.HttpServletRequest
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.argeo
.cms
.CmsException
;
16 import org
.argeo
.cms
.KernelHeader
;
17 import org
.argeo
.cms
.internal
.auth
.GrantedAuthorityPrincipal
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.springframework
.security
.authentication
.AnonymousAuthenticationToken
;
20 import org
.springframework
.security
.authentication
.AuthenticationManager
;
21 import org
.springframework
.security
.core
.Authentication
;
22 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
23 import org
.springframework
.security
.core
.userdetails
.User
;
24 import org
.springframework
.security
.core
.userdetails
.UserDetails
;
26 /** Package utilities */
27 class KernelUtils
implements KernelConstants
{
28 private final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
30 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
31 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
32 for (Object key
: props
.keySet()) {
33 hashtable
.put(key
.toString(), props
.get(key
));
38 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
39 Properties props
= new Properties();
41 props
.load(cl
.getResourceAsStream(resource
));
42 } catch (IOException e
) {
43 throw new CmsException("Cannot load " + resource
44 + " from classpath", e
);
46 return asDictionary(props
);
49 static File
getOsgiInstanceDir(BundleContext bundleContext
) {
50 return new File(bundleContext
.getProperty(OSGI_INSTANCE_AREA
)
51 .substring("file:".length())).getAbsoluteFile();
55 static void anonymousLogin(AuthenticationManager authenticationManager
) {
57 List
<GrantedAuthorityPrincipal
> anonAuthorities
= Collections
58 .singletonList(new GrantedAuthorityPrincipal(
59 KernelHeader
.ROLE_ANONYMOUS
));
60 UserDetails anonUser
= new User(KernelHeader
.USERNAME_ANONYMOUS
,
61 "", true, true, true, true, anonAuthorities
);
62 AnonymousAuthenticationToken anonToken
= new AnonymousAuthenticationToken(
63 DEFAULT_SECURITY_KEY
, anonUser
, anonAuthorities
);
64 Authentication authentication
= authenticationManager
65 .authenticate(anonToken
);
66 SecurityContextHolder
.getContext()
67 .setAuthentication(authentication
);
68 } catch (Exception e
) {
69 throw new CmsException("Cannot authenticate", e
);
74 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
75 if (!log
.isDebugEnabled())
77 for (Enumeration
<String
> headerNames
= request
.getHeaderNames(); headerNames
78 .hasMoreElements();) {
79 String headerName
= headerNames
.nextElement();
80 Object headerValue
= request
.getHeader(headerName
);
81 log
.debug(headerName
+ ": " + headerValue
);
85 private KernelUtils() {