1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
6 import java
.util
.Dictionary
;
7 import java
.util
.Enumeration
;
8 import java
.util
.Hashtable
;
9 import java
.util
.Properties
;
11 import javax
.security
.auth
.Subject
;
12 import javax
.security
.auth
.login
.LoginContext
;
13 import javax
.security
.auth
.login
.LoginException
;
14 import javax
.servlet
.http
.HttpServletRequest
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.argeo
.cms
.CmsException
;
18 import org
.argeo
.cms
.auth
.AuthConstants
;
20 /** Package utilities */
21 class KernelUtils
implements KernelConstants
{
22 private final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
23 private final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
25 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
26 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
27 for (Object key
: props
.keySet()) {
28 hashtable
.put(key
.toString(), props
.get(key
));
33 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
34 Properties props
= new Properties();
36 props
.load(cl
.getResourceAsStream(resource
));
37 } catch (IOException e
) {
38 throw new CmsException("Cannot load " + resource
39 + " from classpath", e
);
41 return asDictionary(props
);
44 static File
getOsgiInstanceDir() {
45 return new File(Activator
.getBundleContext()
46 .getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
50 static File
getOsgiConfigurationFile(String relativePath
) {
52 return new File(new URI(Activator
.getBundleContext().getProperty(
53 OSGI_CONFIGURATION_AREA
)
54 + relativePath
)).getCanonicalFile();
55 } catch (Exception e
) {
56 throw new CmsException("Cannot get configuration file for "
61 static String
getFrameworkProp(String key
, String def
) {
62 String value
= Activator
.getBundleContext().getProperty(key
);
68 static String
getFrameworkProp(String key
) {
69 return getFrameworkProp(key
, null);
73 static Subject
anonymousLogin() {
74 Subject subject
= new Subject();
77 lc
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
80 } catch (LoginException e
) {
81 throw new CmsException("Cannot login as anonymous", e
);
86 // static void anonymousLogin(AuthenticationManager authenticationManager) {
88 // List<GrantedAuthorityPrincipal> anonAuthorities = Collections
89 // .singletonList(new GrantedAuthorityPrincipal(
90 // KernelHeader.ROLE_ANONYMOUS));
91 // UserDetails anonUser = new User(KernelHeader.USERNAME_ANONYMOUS,
92 // "", true, true, true, true, anonAuthorities);
93 // AnonymousAuthenticationToken anonToken = new
94 // AnonymousAuthenticationToken(
95 // DEFAULT_SECURITY_KEY, anonUser, anonAuthorities);
96 // Authentication authentication = authenticationManager
97 // .authenticate(anonToken);
98 // SecurityContextHolder.getContext()
99 // .setAuthentication(authentication);
100 // } catch (Exception e) {
101 // throw new CmsException("Cannot authenticate", e);
106 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
107 if (!log
.isDebugEnabled())
109 for (Enumeration
<String
> headerNames
= request
.getHeaderNames(); headerNames
110 .hasMoreElements();) {
111 String headerName
= headerNames
.nextElement();
112 Object headerValue
= request
.getHeader(headerName
);
113 log
.debug(headerName
+ ": " + headerValue
);
117 private KernelUtils() {