]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelUtils.java
Remove unused directory
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / 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.auth.AuthConstants;
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 String getOsgiInstancePath(String relativePath) {
51 try {
52 if (relativePath == null)
53 return getOsgiInstanceDir().getCanonicalPath();
54 else
55 return new File(getOsgiInstanceDir(), relativePath)
56 .getCanonicalPath();
57 } catch (IOException e) {
58 throw new CmsException("Cannot get instance path for "
59 + relativePath, e);
60 }
61 }
62
63 static File getOsgiConfigurationFile(String relativePath) {
64 try {
65 return new File(new URI(Activator.getBundleContext().getProperty(
66 OSGI_CONFIGURATION_AREA)
67 + relativePath)).getCanonicalFile();
68 } catch (Exception e) {
69 throw new CmsException("Cannot get configuration file for "
70 + relativePath, e);
71 }
72 }
73
74 static String getFrameworkProp(String key, String def) {
75 String value = Activator.getBundleContext().getProperty(key);
76 if (value == null)
77 return def;
78 return value;
79 }
80
81 static String getFrameworkProp(String key) {
82 return getFrameworkProp(key, null);
83 }
84
85 // Security
86 static Subject anonymousLogin() {
87 Subject subject = new Subject();
88 LoginContext lc;
89 try {
90 lc = new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
91 subject);
92 lc.login();
93 return subject;
94 } catch (LoginException e) {
95 throw new CmsException("Cannot login as anonymous", e);
96 }
97 }
98
99 // @Deprecated
100 // static void anonymousLogin(AuthenticationManager authenticationManager) {
101 // try {
102 // List<GrantedAuthorityPrincipal> anonAuthorities = Collections
103 // .singletonList(new GrantedAuthorityPrincipal(
104 // KernelHeader.ROLE_ANONYMOUS));
105 // UserDetails anonUser = new User(KernelHeader.USERNAME_ANONYMOUS,
106 // "", true, true, true, true, anonAuthorities);
107 // AnonymousAuthenticationToken anonToken = new
108 // AnonymousAuthenticationToken(
109 // DEFAULT_SECURITY_KEY, anonUser, anonAuthorities);
110 // Authentication authentication = authenticationManager
111 // .authenticate(anonToken);
112 // SecurityContextHolder.getContext()
113 // .setAuthentication(authentication);
114 // } catch (Exception e) {
115 // throw new CmsException("Cannot authenticate", e);
116 // }
117 // }
118
119 // HTTP
120 static void logRequestHeaders(Log log, HttpServletRequest request) {
121 if (!log.isDebugEnabled())
122 return;
123 for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames
124 .hasMoreElements();) {
125 String headerName = headerNames.nextElement();
126 Object headerValue = request.getHeader(headerName);
127 log.debug(headerName + ": " + headerValue);
128 }
129 }
130
131 private KernelUtils() {
132
133 }
134 }