Skip to content

Commit

Permalink
using a webapp adapter to map filters; creating filters programatically
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Jun 7, 2013
1 parent 96023ad commit 3204dec
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 35 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -52,6 +52,12 @@
<artifactId>grizzly-servlet-webserver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-http-servlet-deployer</artifactId>
<scope>test</scope>
<version>1.9.57</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly</artifactId>
Expand Down
@@ -0,0 +1,124 @@
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.util.List;
import java.util.Map;

import javax.servlet.Filter;

//import com.sun.grizzly.servlet.ServletRegistration;
//import org.glassfish.grizzly.servlet.WebappContext;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;

import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.grizzly.http.servlet.ServletContextImpl;
import com.sun.grizzly.http.servlet.deployer.WebAppAdapter;
import com.sun.grizzly.http.webxml.WebappLoader;
import com.sun.grizzly.http.webxml.schema.ContextParam;
import com.sun.grizzly.http.webxml.schema.FilterMapping;
import com.sun.grizzly.http.webxml.schema.ServletMapping;
import com.sun.grizzly.http.webxml.schema.WebApp;
import com.sun.jersey.api.container.grizzly.GrizzlyServerFactory;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.core.servlet.WebAppResourceConfig;
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory;
import com.sun.jersey.spi.spring.container.SpringComponentProviderFactory;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;

public class ContainerWrapper implements ApplicationContextAware {

private static final Logger logger = getLogger(ContainerWrapper.class);

private String contextConfigLocation = null;

private int port;

private SelectorThread server;

private String packagesToScan = null;

private List<Filter> filters = emptyList();

private ApplicationContext applicationContext;

public void setPackagesToScan(final String packagesToScan) {
this.packagesToScan = packagesToScan;
}

public void setContextConfigLocation(final String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}

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

public void start() throws Exception {

WebApp webApp = new WebApp();

WebAppAdapter waa = new WebAppAdapter("target", "test", webApp, getClass().getClassLoader(), null);

final URI uri = URI.create("http://localhost:" + port + "/");
final ServletAdapter adapter = waa.newServletAdapter(new SpringServlet());
if (packagesToScan != null) {
adapter.addInitParameter("com.sun.jersey.config.property.packages",
packagesToScan);
}
adapter.addInitParameter("com.sun.jersey.api.json.POJOMappingFeature",
"true");

if (contextConfigLocation != null) {
adapter.addContextParameter("contextConfigLocation",
contextConfigLocation);
}


DelegatingFilterProxy filter = new DelegatingFilterProxy();
filter.setTargetBeanName("oauthFilter");
adapter.addFilter(filter, filter.getClass().getName() + "-" +
filter.hashCode(), null);

filter = new DelegatingFilterProxy();
filter.setTargetBeanName("authNFilter");
adapter.addFilter(filter, filter.getClass().getName() + "-" +
filter.hashCode(), null);

adapter.addServletListener("org.springframework.web.context.ContextLoaderListener");

adapter.setContextPath(uri.getPath());
adapter.setProperty("load-on-startup", 1);

server = GrizzlyServerFactory.create(uri, waa);

logger.debug("started grizzly webserver endpoint at " +
server.getPort());
}

public void stop() throws Exception {
server.stopEndpoint();
}

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

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

}

}
2 changes: 2 additions & 0 deletions src/test/resources/spring-test/repo.xml
Expand Up @@ -22,5 +22,7 @@
<bean class="org.fcrepo.services.DatastreamService"/>
<bean class="org.fcrepo.services.NodeService"/>
<bean class="org.fcrepo.binary.PolicyDecisionPoint" />
<!-- mints JCR Sessions -->
<bean class="org.fcrepo.session.SessionFactory"/>

</beans>
22 changes: 0 additions & 22 deletions src/test/resources/spring-test/rest.xml
Expand Up @@ -12,32 +12,10 @@
<context:component-scan
base-package="org.fcrepo.api, org.fcrepo.serialization, org.fcrepo.responses, org.fcrepo.auth.oauth.api, org.fcrepo.url"/>

<!-- mints JCR Sessions -->
<bean class="org.fcrepo.session.SessionFactory"/>
<!-- Mints PIDs-->
<bean class="org.fcrepo.identifiers.UUIDPidMinter"/>

<!-- AuthN filters -->
<bean name="oauthFilter" class="org.fcrepo.auth.oauth.filter.OAuthFilter">
<property name="realm" value="fedora"/>
<property name="provider">
<bean class="org.fcrepo.auth.oauth.DefaultOAuthResourceProvider">
<property name="sessionFactory">
<!-- mints JCR Sessions : needs to reappear here because it can't be autowired from the enclosed contexts-->
<bean class="org.fcrepo.session.SessionFactory"/>
</property>
</bean>
</property>
<property name="parameterStyles">
<set
value-type="org.apache.oltu.oauth2.common.message.types.ParameterStyle">
<value>QUERY</value>
<value>HEADER</value>
</set>
</property>
</bean>

<bean name="authNFilter" class="org.fcrepo.auth.oauth.filter.RestrictToAuthNFilter"/>

<!-- used by (de)serialization endpoints -->
<util:map id="serializers" key-type="java.lang.String" map-class="java.util.HashMap"
Expand Down
34 changes: 34 additions & 0 deletions src/test/resources/spring-test/security.xml
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Context that supports the actual ModeShape JCR itself -->

<context:annotation-config/>

<bean name="oauthFilter" class="org.fcrepo.auth.oauth.filter.OAuthFilter">
<property name="realm" value="fedora"/>
<property name="provider">
<bean class="org.fcrepo.auth.oauth.DefaultOAuthResourceProvider">
<property name="sessionFactory">
<!-- mints JCR Sessions : needs to reappear here because it can't be autowired from the enclosed contexts-->
<bean class="org.fcrepo.session.SessionFactory"/>
</property>
</bean>
</property>
<property name="parameterStyles">
<set
value-type="org.apache.oltu.oauth2.common.message.types.ParameterStyle">
<value>QUERY</value>
<value>HEADER</value>
</set>
</property>
</bean>

<bean name="authNFilter" class="org.fcrepo.auth.oauth.filter.RestrictToAuthNFilter"/>

</beans>
16 changes: 3 additions & 13 deletions src/test/resources/spring-test/test-container.xml
Expand Up @@ -7,22 +7,12 @@

<context:property-placeholder/>

<bean id="containerWrapper" class="org.fcrepo.test.util.ContainerWrapper"
<bean id="containerWrapper" class="org.fcrepo.auth.oauth.integration.api.ContainerWrapper"
init-method="start" destroy-method="stop">
<property name="port" value="${test.port:8080}"/>
<property name="contextConfigLocations"
value="classpath:spring-test/repo.xml, classpath:spring-test/rest.xml"/>
<property name="contextConfigLocation"
value="classpath:spring-test/rest.xml; classpath:spring-test/repo.xml; classpath:spring-test/security.xml"/>
<property name="packagesToScan"
value="org.fcrepo.api, org.fcrepo.responses, org.fcrepo.auth.oauth.api"/>
<property name="filters">
<list value-type="javax.servlet.Filter">
<bean class="org.springframework.web.filter.DelegatingFilterProxy">
<property name="targetBeanName" value="oauthFilter"/>
</bean>
<bean class="org.springframework.web.filter.DelegatingFilterProxy">
<property name="targetBeanName" value="authNFilter"/>
</bean>
</list>
</property>
</bean>
</beans>

0 comments on commit 3204dec

Please sign in to comment.