| 1 | |
package org.restafarian.core.security; |
| 2 | |
|
| 3 | |
import javax.servlet.ServletContext; |
| 4 | |
import javax.servlet.http.Cookie; |
| 5 | |
import javax.servlet.http.HttpServletRequest; |
| 6 | |
import javax.servlet.http.HttpServletResponse; |
| 7 | |
|
| 8 | |
import org.apache.commons.betwixt.io.BeanReader; |
| 9 | |
import org.apache.commons.configuration.Configuration; |
| 10 | |
import org.apache.commons.httpclient.HttpClient; |
| 11 | |
import org.apache.commons.httpclient.cookie.CookiePolicy; |
| 12 | |
import org.apache.commons.httpclient.methods.GetMethod; |
| 13 | |
import org.apache.commons.lang.StringUtils; |
| 14 | |
import org.apache.commons.logging.Log; |
| 15 | |
import org.apache.commons.logging.LogFactory; |
| 16 | |
import org.restafarian.core.beans.Person; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | 0 | public class ServiceBasedUserManager implements UserManager { |
| 23 | 0 | private Log log = LogFactory.getLog(getClass()); |
| 24 | 0 | private String serviceURL = null; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public void init(ServletContext context, Configuration config) { |
| 33 | 0 | log.info("Initializing the ServiceBasedUserManager."); |
| 34 | 0 | serviceURL = config.getString("userManagerServiceURL"); |
| 35 | 0 | log.info("Configured service URL: " + serviceURL); |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public Person getAuthenticatedUser(HttpServletRequest req) { |
| 46 | 0 | Person user = null; |
| 47 | |
|
| 48 | 0 | if (StringUtils.isNotEmpty(serviceURL)) { |
| 49 | 0 | HttpClient client = new HttpClient(); |
| 50 | 0 | GetMethod get = new GetMethod(serviceURL); |
| 51 | 0 | Cookie[] cookies = req.getCookies(); |
| 52 | 0 | if (cookies != null && cookies.length > 0) { |
| 53 | 0 | get.getParams().setCookiePolicy(CookiePolicy.RFC_2109); |
| 54 | 0 | for (int x=0; x<cookies.length; x++) { |
| 55 | 0 | if (!"JSESSIONID".equalsIgnoreCase(cookies[x].getName())) { |
| 56 | 0 | get.setRequestHeader("Cookie", cookies[x].getName() + "=" + cookies[x].getValue()); |
| 57 | |
} |
| 58 | |
} |
| 59 | |
} |
| 60 | |
try { |
| 61 | 0 | int statusCode = client.executeMethod(get); |
| 62 | 0 | if (statusCode == 200) { |
| 63 | 0 | user = createUserFromXML(get.getResponseBodyAsString()); |
| 64 | |
} else { |
| 65 | 0 | log.error("Invalid HTTP status returned while attempting to access URL \"" + serviceURL + "\": " + statusCode); |
| 66 | |
} |
| 67 | 0 | } catch (Exception e) { |
| 68 | 0 | log.error("Exception occurred while attempting to access URL \"" + serviceURL + "\": " + e, e); |
| 69 | 0 | } |
| 70 | |
} |
| 71 | |
|
| 72 | 0 | return user; |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public void setAuthenticatedUser(HttpServletRequest req, HttpServletResponse res, Person user) { |
| 83 | |
|
| 84 | 0 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private Person createUserFromXML(String xml) { |
| 93 | 0 | Person user = null; |
| 94 | |
|
| 95 | 0 | BeanReader beanReader = new BeanReader(); |
| 96 | 0 | beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); |
| 97 | 0 | beanReader.getBindingConfiguration().setMapIDs(false); |
| 98 | |
try { |
| 99 | 0 | beanReader.registerBeanClass("user", Person.class); |
| 100 | 0 | user = (Person) beanReader.parse(xml); |
| 101 | 0 | } catch (Exception e) { |
| 102 | 0 | log.error("Exception occurred while attemption to parse user XML:\n" + xml + "\n\nException: " + e, e); |
| 103 | 0 | } |
| 104 | |
|
| 105 | 0 | return user; |
| 106 | |
} |
| 107 | |
} |