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
.osgi
.framework
.BundleContext
;
17 import org
.springframework
.security
.authentication
.AnonymousAuthenticationToken
;
18 import org
.springframework
.security
.authentication
.AuthenticationManager
;
19 import org
.springframework
.security
.core
.Authentication
;
20 import org
.springframework
.security
.core
.authority
.SimpleGrantedAuthority
;
21 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
22 import org
.springframework
.security
.core
.userdetails
.User
;
23 import org
.springframework
.security
.core
.userdetails
.UserDetails
;
25 class KernelUtils
implements KernelConstants
{
26 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
28 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
29 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
30 for (Object key
: props
.keySet()) {
31 hashtable
.put(key
.toString(), props
.get(key
));
36 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
37 Properties props
= new Properties();
39 props
.load(cl
.getResourceAsStream(resource
));
40 } catch (IOException e
) {
41 throw new CmsException("Cannot load " + resource
42 + " from classpath", e
);
44 return asDictionary(props
);
47 static File
getOsgiInstanceDir(BundleContext bundleContext
) {
48 return new File(bundleContext
.getProperty(OSGI_INSTANCE_AREA
)
49 .substring("file:".length())).getAbsoluteFile();
53 static void anonymousLogin(AuthenticationManager authenticationManager
) {
55 List
<SimpleGrantedAuthority
> anonAuthorities
= Collections
56 .singletonList(new SimpleGrantedAuthority(ROLE_ANONYMOUS
));
57 UserDetails anonUser
= new User(ANONYMOUS_USER
, "", true, true,
58 true, true, anonAuthorities
);
59 AnonymousAuthenticationToken anonToken
= new AnonymousAuthenticationToken(
60 DEFAULT_SECURITY_KEY
, anonUser
, anonAuthorities
);
61 Authentication authentication
= authenticationManager
62 .authenticate(anonToken
);
63 SecurityContextHolder
.getContext()
64 .setAuthentication(authentication
);
65 } catch (Exception e
) {
66 throw new CmsException("Cannot authenticate", e
);
71 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
72 if (!log
.isDebugEnabled())
74 for (Enumeration
<String
> headerNames
= request
.getHeaderNames(); headerNames
75 .hasMoreElements();) {
76 String headerName
= headerNames
.nextElement();
77 Object headerValue
= request
.getHeader(headerName
);
78 log
.debug(headerName
+ ": " + headerValue
);
82 private KernelUtils() {