1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
6 import java
.util
.Collections
;
7 import java
.util
.Dictionary
;
8 import java
.util
.Enumeration
;
9 import java
.util
.Hashtable
;
10 import java
.util
.List
;
11 import java
.util
.Properties
;
13 import javax
.servlet
.http
.HttpServletRequest
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.argeo
.cms
.CmsException
;
17 import org
.argeo
.cms
.KernelHeader
;
18 import org
.argeo
.cms
.internal
.auth
.GrantedAuthorityPrincipal
;
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";
29 private final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
31 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
32 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
33 for (Object key
: props
.keySet()) {
34 hashtable
.put(key
.toString(), props
.get(key
));
39 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
40 Properties props
= new Properties();
42 props
.load(cl
.getResourceAsStream(resource
));
43 } catch (IOException e
) {
44 throw new CmsException("Cannot load " + resource
45 + " from classpath", e
);
47 return asDictionary(props
);
50 static File
getOsgiInstanceDir() {
51 return new File(Activator
.getBundleContext()
52 .getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
56 static File
getOsgiConfigurationFile(String relativePath
) {
58 return new File(new URI(Activator
.getBundleContext().getProperty(
59 OSGI_CONFIGURATION_AREA
)
60 + relativePath
)).getCanonicalFile();
61 } catch (Exception e
) {
62 throw new CmsException("Cannot get configuration file for "
67 static String
getFrameworkProp(String key
, String def
) {
68 String value
= Activator
.getBundleContext().getProperty(key
);
74 static String
getFrameworkProp(String key
) {
75 return getFrameworkProp(key
, null);
80 static void anonymousLogin(AuthenticationManager authenticationManager
) {
82 List
<GrantedAuthorityPrincipal
> anonAuthorities
= Collections
83 .singletonList(new GrantedAuthorityPrincipal(
84 KernelHeader
.ROLE_ANONYMOUS
));
85 UserDetails anonUser
= new User(KernelHeader
.USERNAME_ANONYMOUS
,
86 "", true, true, true, true, anonAuthorities
);
87 AnonymousAuthenticationToken anonToken
= new AnonymousAuthenticationToken(
88 DEFAULT_SECURITY_KEY
, anonUser
, anonAuthorities
);
89 Authentication authentication
= authenticationManager
90 .authenticate(anonToken
);
91 SecurityContextHolder
.getContext()
92 .setAuthentication(authentication
);
93 } catch (Exception e
) {
94 throw new CmsException("Cannot authenticate", e
);
99 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
100 if (!log
.isDebugEnabled())
102 for (Enumeration
<String
> headerNames
= request
.getHeaderNames(); headerNames
103 .hasMoreElements();) {
104 String headerName
= headerNames
.nextElement();
105 Object headerValue
= request
.getHeader(headerName
);
106 log
.debug(headerName
+ ": " + headerValue
);
110 private KernelUtils() {