X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fkernel%2FKernelUtils.java;h=0a9e6c53e44a2c24d7b438b526ce15ddda5f8214;hb=08fac35eeedb151c2fd1cc85ed4a36adf66e02fc;hp=c3297cba87dd5d814ad4385624bf4a065e5aa73d;hpb=63446804f4954bfedd50d8c692bde0fab13aa1ec;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelUtils.java b/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelUtils.java index c3297cba8..0a9e6c53e 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelUtils.java @@ -2,15 +2,29 @@ package org.argeo.cms.internal.kernel; import java.io.File; import java.io.IOException; +import java.util.Collections; import java.util.Dictionary; +import java.util.Enumeration; import java.util.Hashtable; +import java.util.List; import java.util.Properties; +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.logging.Log; import org.argeo.cms.CmsException; -import org.osgi.framework.BundleContext; +import org.argeo.cms.KernelHeader; +import org.argeo.cms.internal.auth.GrantedAuthorityPrincipal; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; -class KernelUtils { - final static String OSGI_INSTANCE_AREA = "osgi.instance.area"; +/** Package utilities */ +class KernelUtils implements KernelConstants { + private final static String OSGI_INSTANCE_AREA = "osgi.instance.area"; static Dictionary asDictionary(Properties props) { Hashtable hashtable = new Hashtable(); @@ -20,8 +34,7 @@ class KernelUtils { return hashtable; } - static Dictionary asDictionary(ClassLoader cl, - String resource) { + static Dictionary asDictionary(ClassLoader cl, String resource) { Properties props = new Properties(); try { props.load(cl.getResourceAsStream(resource)); @@ -32,9 +45,52 @@ class KernelUtils { return asDictionary(props); } - static File getOsgiInstanceDir(BundleContext bundleContext) { - return new File(bundleContext.getProperty(OSGI_INSTANCE_AREA) - .substring("file:".length())).getAbsoluteFile(); + static File getOsgiInstanceDir() { + return new File(Activator.getBundleContext() + .getProperty(OSGI_INSTANCE_AREA).substring("file:".length())) + .getAbsoluteFile(); + } + + static String getFrameworkProp(String key, String def) { + String value = Activator.getBundleContext().getProperty(key); + if (value == null) + return def; + return value; + } + + static String getFrameworkProp(String key) { + return getFrameworkProp(key, null); + } + + // Security + static void anonymousLogin(AuthenticationManager authenticationManager) { + try { + List anonAuthorities = Collections + .singletonList(new GrantedAuthorityPrincipal( + KernelHeader.ROLE_ANONYMOUS)); + UserDetails anonUser = new User(KernelHeader.USERNAME_ANONYMOUS, + "", true, true, true, true, anonAuthorities); + AnonymousAuthenticationToken anonToken = new AnonymousAuthenticationToken( + DEFAULT_SECURITY_KEY, anonUser, anonAuthorities); + Authentication authentication = authenticationManager + .authenticate(anonToken); + SecurityContextHolder.getContext() + .setAuthentication(authentication); + } catch (Exception e) { + throw new CmsException("Cannot authenticate", e); + } + } + + // HTTP + static void logRequestHeaders(Log log, HttpServletRequest request) { + if (!log.isDebugEnabled()) + return; + for (Enumeration headerNames = request.getHeaderNames(); headerNames + .hasMoreElements();) { + String headerName = headerNames.nextElement(); + Object headerValue = request.getHeader(headerName); + log.debug(headerName + ": " + headerValue); + } } private KernelUtils() {