Navigation Menu

Skip to content

Commit

Permalink
start parsing web.xml for the test web app config
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Jun 11, 2013
1 parent ff8760b commit a573afe
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 78 deletions.
@@ -1,18 +1,24 @@

package org.fcrepo.auth.oauth.integration.api;

import static java.util.Collections.emptyList;
import static org.slf4j.LoggerFactory.getLogger;

import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
//import com.sun.grizzly.servlet.ServletRegistration;
//import org.glassfish.grizzly.servlet.WebappContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.fcrepo.auth.oauth.integration.api.bind.ContextParam;
import org.fcrepo.auth.oauth.integration.api.bind.Filter;
import org.fcrepo.auth.oauth.integration.api.bind.FilterMapping;
import org.fcrepo.auth.oauth.integration.api.bind.InitParam;
import org.fcrepo.auth.oauth.integration.api.bind.Listener;
import org.fcrepo.auth.oauth.integration.api.bind.Servlet;
import org.fcrepo.auth.oauth.integration.api.bind.ServletMapping;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.FilterRegistration;
import org.glassfish.grizzly.servlet.ServletRegistration;
Expand All @@ -34,73 +40,71 @@ public class ContainerWrapper implements ApplicationContextAware {
private int port;

private HttpServer server;

private String configLocation;

private List<Filter> filters = emptyList();

public void setPackagesToScan(final String packagesToScan) {
}

public void setContextConfigLocation(final String contextConfigLocation) {
public void setConfigLocation(final String configLocation) {
this.configLocation = configLocation.replaceFirst("^classpath:", "/");
}

public void setPort(final int port) {
this.port = port;
}

public void start() throws Exception {

final JerseyTest jt;


JAXBContext context = JAXBContext.newInstance(WebAppConfig.class);
Unmarshaller u = context.createUnmarshaller();
WebAppConfig o = (WebAppConfig) u.unmarshal(getClass().getResource(this.configLocation));

final URI uri = URI.create("http://localhost:" + port + "/");

final Map<String, String> initParams = new HashMap<String, String>();

server = GrizzlyWebContainerFactory.create(uri, initParams);

final WebappContext wac = new WebappContext("test", "");

wac.addContextInitParameter("contextConfigLocation",
"classpath:spring-test/master.xml");

wac.addListener("org.springframework.web.context.ContextLoaderListener");
wac.addListener("org.springframework.web.context.request.RequestContextListener");

final ServletRegistration servlet =
wac.addServlet("jersey-servlet", SpringServlet.class);

servlet.addMapping("/*");

servlet.setInitParameter("com.sun.jersey.config.property.packages",
"org.fcrepo");

servlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature",
"true");

final FilterRegistration wrapFilter =
wac.addFilter("WrapFilter", DelegatingFilterProxy.class);

wrapFilter.setInitParameter("targetBeanName", "wrapFilter");

wrapFilter.addMappingForUrlPatterns(null,
"/rest/objects/authenticated/*");
wrapFilter
.addMappingForUrlPatterns(null, "/rest/objects/authenticated");

final FilterRegistration opFilter =
wac.addFilter("OpFilter", DelegatingFilterProxy.class);

opFilter.setInitParameter("targetBeanName", "oauthFilter");

opFilter.addMappingForUrlPatterns(null, "/rest/objects/authenticated/*");
opFilter.addMappingForUrlPatterns(null, "/rest/objects/authenticated");

final FilterRegistration tokenFilter =
wac.addFilter("TokenFilter", DelegatingFilterProxy.class);

tokenFilter.setInitParameter("targetBeanName", "authNFilter");

tokenFilter.addMappingForUrlPatterns(null, "/token");


// create a "root" web application
WebappContext wac = new WebappContext(o.displayName(), "");

for (ContextParam p: o.contextParams()) {
wac.addContextInitParameter(p.name(), p.value());
}

for (Listener l: o.listeners) {
wac.addListener(l.className());
}

for (Servlet s: o.servlets) {
ServletRegistration servlet = wac.addServlet(s.servletName(), s.servletClass());

Collection<ServletMapping> mappings = o.servletMappings(s.servletName());
for (ServletMapping sm: mappings) {
servlet.addMapping(sm.urlPattern());
}
for (InitParam p: s.initParams()) {
servlet.setInitParameter(p.name(), p.value());
}
}

for (Filter f: o.filters) {
FilterRegistration filter = wac.addFilter(f.filterName(), f.filterClass());

Collection<FilterMapping> mappings = o.filterMappings(f.filterName());
for (FilterMapping sm: mappings) {
String urlPattern = sm.urlPattern();
String servletName = sm.servletName();
if (urlPattern != null) {
filter.addMappingForUrlPatterns(null, urlPattern);
} else {
filter.addMappingForServletNames(null, servletName);
}

}
for (InitParam p: f.initParams()) {
filter.setInitParameter(p.name(), p.value());
}
}

wac.deploy(server);

final URL webXml = this.getClass().getResource("/web.xml");
Expand All @@ -114,14 +118,11 @@ public void stop() throws Exception {
server.stop();
}

public void setFilters(final List<Filter> filters) {
this.filters = filters;
}

@Override
public void setApplicationContext(
final ApplicationContext applicationContext) throws BeansException {

}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
//this.applicationContext = applicationContext;

}

}
@@ -0,0 +1,60 @@
package org.fcrepo.auth.oauth.integration.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.fcrepo.auth.oauth.integration.api.bind.ContextParam;
import org.fcrepo.auth.oauth.integration.api.bind.FilterMapping;
import org.fcrepo.auth.oauth.integration.api.bind.Listener;
import org.fcrepo.auth.oauth.integration.api.bind.ServletMapping;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestBinding {

private final Logger LOGGER = LoggerFactory.getLogger(TestBinding.class);

@Test
public void testBinding() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(WebAppConfig.class);
Unmarshaller u = context.createUnmarshaller();
WebAppConfig o = (WebAppConfig) u.unmarshal(getClass().getResourceAsStream("/web.xml"));
assertEquals("Fedora-on-ModeShape", o.displayName());
assertTrue(o.contextParams.contains(
new ContextParam(
"contextConfigLocation",
"classpath:spring-test/rest.xml; " +
"classpath:spring-test/repo.xml; " +
"classpath:spring-test/security.xml")));
assertTrue(o.contextParams.contains(
new ContextParam(
"org.modeshape.jcr.RepositoryName",
"repo")));
assertTrue(o.contextParams.contains(
new ContextParam(
"org.modeshape.jcr.URL",
"/test_repository.json")));
assertTrue(o.listeners.contains(
new Listener(null, "org.springframework.web.context.ContextLoaderListener")));
assertTrue(o.listeners.contains(
new Listener(null, "org.modeshape.web.jcr.ModeShapeJcrDeployer")));
ServletMapping sm = o.servletMappings("jersey-servlet").iterator().next();
assertNotNull(sm);
assertEquals("/*", sm.urlPattern());

FilterMapping fm = o.filterMappings("TokenFilter").iterator().next();
assertNotNull(fm);
assertEquals("/token", fm.urlPattern());

fm = o.filterMappings("OpFilter").iterator().next();
assertNotNull(fm);
assertEquals("/rest/objects/authenticated/*", fm.urlPattern());

}
}
@@ -0,0 +1,86 @@
package org.fcrepo.auth.oauth.integration.api;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

import org.fcrepo.auth.oauth.integration.api.bind.ContextParam;
import org.fcrepo.auth.oauth.integration.api.bind.Displayable;
import org.fcrepo.auth.oauth.integration.api.bind.Filter;
import org.fcrepo.auth.oauth.integration.api.bind.FilterMapping;
import org.fcrepo.auth.oauth.integration.api.bind.Listener;
import org.fcrepo.auth.oauth.integration.api.bind.Servlet;
import org.fcrepo.auth.oauth.integration.api.bind.ServletMapping;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

@XmlRootElement(namespace="http://java.sun.com/xml/ns/javaee", name="web-app")
public class WebAppConfig extends Displayable {

@XmlElements(value = {@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="context-param")})
List<ContextParam> contextParams;

@XmlElements(value = {@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="listener")})
List<Listener> listeners;

@XmlElements(value = {@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="servlet")})
List<Servlet> servlets;

@XmlElements(value = {@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="filter")})
List<Filter> filters;

@XmlElements(value = {@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="servlet-mapping")})
List<ServletMapping> servletMappings;

@XmlElements(value = {@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="filter-mapping")})
List<FilterMapping> filterMappings;

public Collection<ServletMapping> servletMappings(String servletName) {
return Collections2.filter(servletMappings, new SMapFilter(servletName));
}

public Collection<FilterMapping> filterMappings(String filterName) {
return Collections2.filter(filterMappings, new FMapFilter(filterName));
}

@SuppressWarnings("unchecked")
public Collection<ContextParam> contextParams() {
return (contextParams != null) ? contextParams : Collections.EMPTY_LIST;
}

private static class SMapFilter implements Predicate<ServletMapping> {
String servletName;

SMapFilter(String sName) {
servletName = sName;
}

@Override
public boolean apply(ServletMapping input) {
return (servletName == null) ? input.servletName() == null :
servletName.equals(input.servletName());
}


}
private static class FMapFilter implements Predicate<FilterMapping> {
String filterName;

FMapFilter(String sName) {
filterName = sName;
}

@Override
public boolean apply(FilterMapping input) {
return (filterName == null) ? input.filterName() == null :
filterName.equals(input.filterName());
}


}
}
@@ -0,0 +1,17 @@
package org.fcrepo.auth.oauth.integration.api.bind;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(namespace="http://java.sun.com/xml/ns/javaee", name="context-param")
public class ContextParam extends Param {

public ContextParam() {
super();
}

public ContextParam(String name, String value) {
super(name, value);
}

}
@@ -0,0 +1,15 @@
package org.fcrepo.auth.oauth.integration.api.bind;

import javax.xml.bind.annotation.XmlElement;


public abstract class Describable {

@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="description")
String description;

public String getDescription() {
return description;
}

}
@@ -0,0 +1,14 @@
package org.fcrepo.auth.oauth.integration.api.bind;

import javax.xml.bind.annotation.XmlElement;


public abstract class Displayable extends Describable {

@XmlElement(namespace="http://java.sun.com/xml/ns/javaee", name="display-name")
String displayName;

public String displayName() {
return this.displayName;
}
}

0 comments on commit a573afe

Please sign in to comment.