Skip to content

Commit

Permalink
actually getting the resource to work via upgraded grizzly and jersey…
Browse files Browse the repository at this point in the history
… libs
  • Loading branch information
barmintor committed Jun 10, 2013
1 parent 74794a1 commit 3613db3
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 60 deletions.
53 changes: 40 additions & 13 deletions pom.xml
Expand Up @@ -42,26 +42,22 @@
<type>test-jar</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<scope>test</scope>
<version>2.2.21</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-http-servlet-deployer</artifactId>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet</artifactId>
<scope>test</scope>
<version>1.9.57</version>
<version>2.2.21</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly</artifactId>
<artifactId>jersey-grizzly2</artifactId>
<scope>test</scope>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
Expand Down Expand Up @@ -91,7 +87,38 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>


<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.17.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.14</version>
<scope>test</scope>
</dependency>
<!-- Playing with ordered loading for tests -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2-b04</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/fcrepo/auth/oauth/Constants.java
Expand Up @@ -5,7 +5,9 @@ public interface Constants {

public static final String OAUTH_WORKSPACE = "oauth";

public static final String CLIENT_PROPERTY = "oauth:client";
//TODO get namespaced properties to work
public static final String CLIENT_PROPERTY = "oauth-client";

public static final String PRINCIPAL_PROPERTY = "oauth:principal";
//TODO get namespaced properties to work
public static final String PRINCIPAL_PROPERTY = "oauth-principal";
}
Expand Up @@ -25,6 +25,11 @@ public class RestrictToAuthNFilter implements Filter {

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
init();
}

// used by Spring
public void init() {
LOGGER.debug("Initialized {}", this.getClass().getName());
}

Expand Down
Expand Up @@ -4,13 +4,24 @@
import static org.slf4j.LoggerFactory.getLogger;

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

import javax.servlet.Filter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

//import com.sun.grizzly.servlet.ServletRegistration;
//import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.FilterRegistration;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.grizzly2.servlet.GrizzlyWebContainerFactory;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
Expand All @@ -20,21 +31,22 @@
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.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.api.core.PackagesResourceConfig;
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.container.servlet.ServletContainer;
import com.sun.jersey.spi.spring.container.SpringComponentProviderFactory;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.sun.jersey.test.framework.WebAppDescriptor.Builder;
import com.sun.jersey.test.framework.WebAppDescriptor.FilterDescriptor;
import com.sun.jersey.test.framework.spi.container.TestContainer;
import com.sun.jersey.test.framework.spi.container.grizzly2.web.GrizzlyWebTestContainerFactory;

public class ContainerWrapper implements ApplicationContextAware {

Expand All @@ -44,7 +56,7 @@ public class ContainerWrapper implements ApplicationContextAware {

private int port;

private SelectorThread server;
private HttpServer server;

private String packagesToScan = null;

Expand All @@ -66,48 +78,57 @@ public void setPort(final int port) {

public void start() throws Exception {

WebApp webApp = new WebApp();
JerseyTest jt;

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);
}

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

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

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);
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");

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

servlet.addMapping("/*");

// servlet.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS,
// PackagesResourceConfig.class.getName());

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

servlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
//
// FilterRegistration filter = wac.addFilter("delegating-2", DelegatingFilterProxy.class);
//
// filter.setInitParameter("targetBeanName", "oauthFilter");
//
// filter.addMappingForUrlPatterns(null, "/authorization");
//
// filter = wac.addFilter("delegating-1", DelegatingFilterProxy.class);
//
// filter.setInitParameter("targetBeanName", "authNFilter");
//
// filter.addMappingForUrlPatterns(null, "/token");

wac.deploy(server);

URL webXml = this.getClass().getResource("/web.xml");
logger.error(webXml.toString());

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

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

public void setFilters(final List<Filter> filters) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/spring-test/master.xml
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Master context for the test application. -->

<import resource="classpath:/spring-test/repo.xml"/>
<import resource="classpath:/spring-test/rest.xml"/>
<import resource="classpath:/spring-test/security.xml"/>

</beans>
2 changes: 1 addition & 1 deletion src/test/resources/spring-test/repo.xml
Expand Up @@ -23,6 +23,6 @@
<bean class="org.fcrepo.services.NodeService"/>
<bean class="org.fcrepo.binary.PolicyDecisionPoint" />
<!-- mints JCR Sessions -->
<bean class="org.fcrepo.session.SessionFactory"/>
<bean class="org.fcrepo.session.SessionFactory" id="org.fcrepo.session.SessionFactory" />

</beans>
4 changes: 2 additions & 2 deletions src/test/resources/spring-test/security.xml
Expand Up @@ -16,7 +16,7 @@
<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"/>
<ref bean="org.fcrepo.session.SessionFactory"/>
</property>
</bean>
</property>
Expand All @@ -29,6 +29,6 @@
</property>
</bean>

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

</beans>
91 changes: 91 additions & 0 deletions src/test/resources/web.xml
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="false">

<display-name>Fedora-on-ModeShape</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-test/rest.xml; classpath:spring-test/repo.xml; classpath:spring-test/security.xml</param-value>
</context-param>


<!-- engaging the ModeShape REST API -->

<context-param>
<param-name>org.modeshape.jcr.RepositoryName</param-name>
<param-value>repo</param-value>
</context-param>
<context-param>
<param-name>org.modeshape.jcr.URL</param-name>
<param-value>WEB-INF/classes/config/repository.json</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Required parameter for ModeShape REST - should not be modified -->
<listener>
<listener-class>org.modeshape.web.jcr.ModeShapeJcrDeployer</listener-class>
</listener>

<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.fcrepo</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
<filter-name>delegating-1</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>oauthFilter</param-value>
</init-param>
</filter>
<filter>
<filter-name>delegating-2</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>authNFilter</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>delegating-1</filter-name>
<url-pattern>/token</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>delegating-2</filter-name>
<url-pattern>/authorization</url-pattern>
</filter-mapping>

<!-- BASIC security parameters -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>fcrepo</realm-name>
</login-config>

<security-role>
<role-name>admin</role-name>
</security-role>

</web-app>

0 comments on commit 3613db3

Please sign in to comment.