Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using a webapp adapter to map filters; creating filters programatically
- Loading branch information
Showing
6 changed files
with
169 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/test/java/org/fcrepo/auth/oauth/integration/api/ContainerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters