]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KernelUtils.java
1d7e0868e1e6102bce439f7d7ad31fb3c3aec64a
[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.Dictionary;
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9 import java.util.Properties;
10
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;
15
16 import org.apache.commons.logging.Log;
17 import org.argeo.cms.CmsException;
18 import org.argeo.cms.KernelHeader;
19
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";
24
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));
29 }
30 return hashtable;
31 }
32
33 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
34 Properties props = new Properties();
35 try {
36 props.load(cl.getResourceAsStream(resource));
37 } catch (IOException e) {
38 throw new CmsException("Cannot load " + resource
39 + " from classpath", e);
40 }
41 return asDictionary(props);
42 }
43
44 static File getOsgiInstanceDir() {
45 return new File(Activator.getBundleContext()
46 .getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
47 .getAbsoluteFile();
48 }
49
50 static File getOsgiConfigurationFile(String relativePath) {
51 try {
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 "
57 + relativePath, e);
58 }
59 }
60
61 static String getFrameworkProp(String key, String def) {
62 String value = Activator.getBundleContext().getProperty(key);
63 if (value == null)
64 return def;
65 return value;
66 }
67
68 static String getFrameworkProp(String key) {
69 return getFrameworkProp(key, null);
70 }
71
72 // Security
73 static Subject anonymousLogin() {
74 Subject subject = new Subject();
75 LoginContext lc;
76 try {
77 lc = new LoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject);
78 lc.login();
79 return subject;
80 } catch (LoginException e) {
81 throw new CmsException("Cannot login as anonymous", e);
82 }
83 }
84
85 // @Deprecated
86 // static void anonymousLogin(AuthenticationManager authenticationManager) {
87 // try {
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);
102 // }
103 // }
104
105 // HTTP
106 static void logRequestHeaders(Log log, HttpServletRequest request) {
107 if (!log.isDebugEnabled())
108 return;
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);
114 }
115 }
116
117 private KernelUtils() {
118
119 }
120 }