Skip to content

Commit

Permalink
Add methods to SessionFactory to support workspaces and http authN
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f authored and barmintor committed Jun 11, 2013
1 parent 256ecfe commit 395186b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 36 deletions.
Expand Up @@ -14,7 +14,7 @@

<bean id="containerWrapper" class="org.fcrepo.test.util.ContainerWrapper" init-method="start" destroy-method="stop" >
<property name="port" value="${test.port:8080}"/>
<property name="contextConfigLocations" value="classpath:spring-test/generator.xml,classpath:spring-test/repo.xml" />
<property name="contextConfigLocation" value="classpath:spring-test/generator.xml,classpath:spring-test/repo.xml" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.generator, org.fcrepo.exceptionhandlers" />
</bean>

Expand Down
Expand Up @@ -9,7 +9,7 @@

<bean id="containerWrapper" class="org.fcrepo.test.util.ContainerWrapper" init-method="start" destroy-method="stop" >
<property name="port" value="${test.port:8080}"/>
<property name="contextConfigLocations" value="classpath:spring-test/master.xml" />
<property name="contextConfigLocation" value="classpath:spring-test/master.xml" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.responses, org.fcrepo.exceptionhandlers"/>
</bean>
</beans>
1 change: 1 addition & 0 deletions fcrepo-http-commons/.gitignore
@@ -0,0 +1 @@
/target
Expand Up @@ -4,7 +4,6 @@
import static org.slf4j.LoggerFactory.getLogger;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand All @@ -13,12 +12,15 @@

import org.modeshape.jcr.api.ServletCredentials;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public class SessionFactory {

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

@Inject
@Autowired
private Repository repo;

public SessionFactory() {
Expand All @@ -40,36 +42,43 @@ public void setRepository(final Repository repo) {
public Session getSession() throws RepositoryException {
return repo.login();
}

private static ServletCredentials getCredentials(final SecurityContext securityContext,
final HttpServletRequest servletRequest) {
if (securityContext.getUserPrincipal() != null) {
logger.debug("Authenticated user: " +
securityContext.getUserPrincipal().getName());
return new ServletCredentials(servletRequest);
} else {
logger.debug("No authenticated user found!");
return null;
}

public Session getSession(final String workspace)
throws RepositoryException {
return repo.login(workspace);
}

private static ServletCredentials getCredentials(
final SecurityContext securityContext,
final HttpServletRequest servletRequest) {
if (securityContext.getUserPrincipal() != null) {
logger.debug("Authenticated user: " +
securityContext.getUserPrincipal().getName());
return new ServletCredentials(servletRequest);
} else {
logger.debug("No authenticated user found!");
return null;
}
}

public Session getSession(final SecurityContext securityContext,
final HttpServletRequest servletRequest) {

try {
ServletCredentials creds = getCredentials(securityContext, servletRequest);
return (creds != null) ? repo.login(creds) : repo.login();
final ServletCredentials creds =
getCredentials(securityContext, servletRequest);
return (creds != null) ? repo.login(creds) : repo.login();
} catch (final RepositoryException e) {
throw new IllegalStateException(e);
}
}


public AuthenticatedSessionProvider getSessionProvider(
final SecurityContext securityContext,
final SecurityContext securityContext,
final HttpServletRequest servletRequest) {

ServletCredentials creds = getCredentials(securityContext, servletRequest);
return new AuthenticatedSessionProviderImpl(repo, creds);
final ServletCredentials creds =
getCredentials(securityContext, servletRequest);
return new AuthenticatedSessionProviderImpl(repo, creds);
}
}
Expand Up @@ -4,4 +4,12 @@
@SuppressWarnings("serial")
public class ServletException extends Exception {

public ServletException(final String mesg) {
super(mesg);
}

public ServletException(final Exception mesg) {
super(mesg);
}

}
@@ -1,35 +1,40 @@

package org.fcrepo.test.util;

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

import java.net.URI;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Filter;

import org.slf4j.Logger;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.jersey.api.container.grizzly.GrizzlyServerFactory;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;

public class ContainerWrapper {

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

private String contextConfigLocations;
private String contextConfigLocation = null;

private int port;

private SelectorThread server;

private String packagesToScan;
private String packagesToScan = null;

private List<Filter> filters = emptyList();

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

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

public void setPort(final int port) {
Expand All @@ -39,12 +44,20 @@ public void setPort(final int port) {
public void start() throws Exception {
final URI uri = URI.create("http://localhost:" + port + "/");
final ServletAdapter adapter = new ServletAdapter();
adapter.addInitParameter("com.sun.jersey.config.property.packages",
packagesToScan);
if (packagesToScan != null) {
adapter.addInitParameter("com.sun.jersey.config.property.packages",
packagesToScan);
}
adapter.addInitParameter("com.sun.jersey.api.json.POJOMappingFeature",
"true");
adapter.addContextParameter("contextConfigLocation",
contextConfigLocations);
if (contextConfigLocation != null) {
adapter.addContextParameter("contextConfigLocation",
contextConfigLocation);
}
for (final Filter filter : filters) {
adapter.addFilter(filter, filter.getClass().getName() + "-" +
filter.hashCode(), null);
}
adapter.addServletListener("org.springframework.web.context.ContextLoaderListener");
adapter.setServletInstance(new SpringServlet());
adapter.setContextPath(uri.getPath());
Expand All @@ -58,4 +71,8 @@ public void stop() throws Exception {
server.stopEndpoint();
}

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

}
Expand Up @@ -14,7 +14,7 @@

<bean id="containerWrapper" class="org.fcrepo.test.util.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, classpath:spring-test/eventing.xml" />
<property name="contextConfigLocation" value="classpath:spring-test/repo.xml, classpath:spring-test/rest.xml, classpath:spring-test/eventing.xml" />
<property name="packagesToScan" value="org.fcrepo.syndication, org.fcrepo.exceptionhandlers" />
</bean>

Expand Down
2 changes: 1 addition & 1 deletion fcrepo-webapp/src/main/resources/spring/rest.xml
Expand Up @@ -9,7 +9,7 @@
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!-- Context that houses JAX-RS Resources that compose the API
as well as som utility gear. -->
as well as some utility gear. -->

<context:annotation-config/>

Expand Down
Expand Up @@ -14,7 +14,7 @@

<bean id="containerWrapper" class="org.fcrepo.test.util.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,classpath:spring-test/eventing.xml" />
<property name="contextConfigLocation" value="classpath:spring-test/repo.xml,classpath:spring-test/rest.xml,classpath:spring-test/eventing.xml" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.webhooks, org.fcrepo.integration.webhooks, org.fcrepo.exceptionhandlers" />
</bean>

Expand Down

0 comments on commit 395186b

Please sign in to comment.