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
.springframework
.security
.authentication
.AnonymousAuthenticationToken
;
19 import org
.springframework
.security
.authentication
.AuthenticationManager
;
20 import org
.springframework
.security
.core
.Authentication
;
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 /** Package utilities */
26 class KernelUtils
implements KernelConstants
{
27 private final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
29 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
30 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
31 for (Object key
: props
.keySet()) {
32 hashtable
.put(key
.toString(), props
.get(key
));
37 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
38 Properties props
= new Properties();
40 props
.load(cl
.getResourceAsStream(resource
));
41 } catch (IOException e
) {
42 throw new CmsException("Cannot load " + resource
43 + " from classpath", e
);
45 return asDictionary(props
);
48 static File
getOsgiInstanceDir() {
49 return new File(Activator
.getBundleContext()
50 .getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
54 static String
getFrameworkProp(String key
, String def
) {
55 String value
= Activator
.getBundleContext().getProperty(key
);
61 static String
getFrameworkProp(String key
) {
62 return getFrameworkProp(key
, null);
66 static void anonymousLogin(AuthenticationManager authenticationManager
) {
68 List
<GrantedAuthorityPrincipal
> anonAuthorities
= Collections
69 .singletonList(new GrantedAuthorityPrincipal(
70 KernelHeader
.ROLE_ANONYMOUS
));
71 UserDetails anonUser
= new User(KernelHeader
.USERNAME_ANONYMOUS
,
72 "", true, true, true, true, anonAuthorities
);
73 AnonymousAuthenticationToken anonToken
= new AnonymousAuthenticationToken(
74 DEFAULT_SECURITY_KEY
, anonUser
, anonAuthorities
);
75 Authentication authentication
= authenticationManager
76 .authenticate(anonToken
);
77 SecurityContextHolder
.getContext()
78 .setAuthentication(authentication
);
79 } catch (Exception e
) {
80 throw new CmsException("Cannot authenticate", e
);
85 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
86 if (!log
.isDebugEnabled())
88 for (Enumeration
<String
> headerNames
= request
.getHeaderNames(); headerNames
89 .hasMoreElements();) {
90 String headerName
= headerNames
.nextElement();
91 Object headerValue
= request
.getHeader(headerName
);
92 log
.debug(headerName
+ ": " + headerValue
);
96 private KernelUtils() {