]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KernelUtils.java
579138c0f2e111965eb5b0748941322563af9c76
[lgpl/argeo-commons.git] / KernelUtils.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URI;
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;
12
13 import javax.servlet.http.HttpServletRequest;
14
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;
25
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";
30
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));
35 }
36 return hashtable;
37 }
38
39 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
40 Properties props = new Properties();
41 try {
42 props.load(cl.getResourceAsStream(resource));
43 } catch (IOException e) {
44 throw new CmsException("Cannot load " + resource
45 + " from classpath", e);
46 }
47 return asDictionary(props);
48 }
49
50 static File getOsgiInstanceDir() {
51 return new File(Activator.getBundleContext()
52 .getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
53 .getAbsoluteFile();
54 }
55
56 static File getOsgiConfigurationFile(String relativePath) {
57 try {
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 "
63 + relativePath, e);
64 }
65 }
66
67 static String getFrameworkProp(String key, String def) {
68 String value = Activator.getBundleContext().getProperty(key);
69 if (value == null)
70 return def;
71 return value;
72 }
73
74 static String getFrameworkProp(String key) {
75 return getFrameworkProp(key, null);
76 }
77
78 // Security
79 @Deprecated
80 static void anonymousLogin(AuthenticationManager authenticationManager) {
81 try {
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);
95 }
96 }
97
98 // HTTP
99 static void logRequestHeaders(Log log, HttpServletRequest request) {
100 if (!log.isDebugEnabled())
101 return;
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);
107 }
108 }
109
110 private KernelUtils() {
111
112 }
113 }