Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30 Duplicate the jersey module to "jersey" for 2.25 and "jersey-226" for 2.26 #41

Merged
merged 2 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions jersey226/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.pac4j.jax-rs</groupId>
<artifactId>parent</artifactId>
<version>2.2.0-SNAPSHOT</version>
</parent>
<groupId>org.pac4j</groupId>
<artifactId>jersey226-pac4j</artifactId>

<properties>
<jersey.version>2.26</jersey.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<!-- optional dependencies -->
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<!-- same version as in org.glassfish.jersey:project -->
<version>2.4.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>testing</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<exclusions>
<exclusion><!-- Conflicts with jersey-hk2 -->
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<scope>test</scope>
<exclusions>
<exclusion><!-- Conflicts with jersey-test-framework-provider-grizzly2 -->
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.pac4j.jax.rs.grizzly.features;

import javax.inject.Inject;
import javax.inject.Provider;

import org.glassfish.grizzly.http.server.Request;
import org.pac4j.jax.rs.features.JaxRsContextFactoryProvider;
import org.pac4j.jax.rs.grizzly.pac4j.GrizzlyJaxRsContext;

/**
*
* Extends {@link JaxRsContextFactoryProvider} to support the Grizzly container (without the need for servlet support)
* and its session manager (i.e., pac4j indirect clients will work, contrary than with
* {@link JaxRsContextFactoryProvider}).
*
* @see JaxRsContextFactoryProvider
* @author Victor Noel - Linagora
* @since 1.0.0
*
*/
public class GrizzlyJaxRsContextFactoryProvider extends JaxRsContextFactoryProvider {

@Inject
protected Provider<Request> requestProvider;

@Override
public JaxRsContextFactory getContext(Class<?> type) {
Request request = requestProvider.get();
assert request != null;
return context -> new GrizzlyJaxRsContext(getProviders(), context, getConfig().getSessionStore(), request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.pac4j.jax.rs.grizzly.pac4j;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.ext.Providers;

import org.glassfish.grizzly.http.server.Request;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.jax.rs.pac4j.JaxRsContext;

/**
*
* Notice: there is often chances that the JAX-RS implementation will read the input stream of the request when it
* arrives, and after that, it becomes impossible for Grizzly to read it. In particular this means that
* {@link Request#getParameter(String)} won't be able to return FORM parameters. This is why we don't override
* {@link JaxRsContext#getRequestParameter(String)} to use the Grizzly implementation.
*
* @author Victor Noel - Linagora
* @since 1.0.0
*
*/
public class GrizzlyJaxRsContext extends JaxRsContext {

private final Request request;

public GrizzlyJaxRsContext(Providers providers, ContainerRequestContext requestContext,
SessionStore sessionStore, Request request) {
super(providers, requestContext, sessionStore != null ? sessionStore : new GrizzlySessionStore());
this.request = request;
}

@Override
public void setSessionStore(SessionStore sessionStore) {
if (sessionStore == null) {
super.setSessionStore(new GrizzlySessionStore());
} else {
super.setSessionStore(sessionStore);
}
}

public Request getRequest() {
return request;
}

@Override
public String getRemoteAddr() {
return request.getRemoteAddr();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.pac4j.jax.rs.grizzly.pac4j;

import java.util.HashMap;
import java.util.Map;

import org.glassfish.grizzly.http.server.Session;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.jax.rs.pac4j.JaxRsContext;

/**
*
* @author Victor Noel - Linagora
* @since 1.0.0
*
*/
public class GrizzlySessionStore implements SessionStore<JaxRsContext> {

public Session getSession(final JaxRsContext context) {
assert context instanceof GrizzlyJaxRsContext;
return ((GrizzlyJaxRsContext) context).getRequest().getSession();
}

@Override
public String getOrCreateSessionId(JaxRsContext context) {
return getSession(context).getIdInternal();
}

@Override
public Object get(JaxRsContext context, String key) {
return getSession(context).getAttribute(key);
}

@Override
public void set(JaxRsContext context, String key, Object value) {
if (value == null) {
getSession(context).removeAttribute(key);
} else {
getSession(context).setAttribute(key, value);
}
}

@Override
public boolean destroySession(JaxRsContext context) {
final Session session = getSession(context);

session.setValid(false);

return true;
}

@Override
public Object getTrackableSession(JaxRsContext context) {
return getSession(context);
}

@Override
public boolean renewSession(JaxRsContext context) {
final Session session = getSession(context);
final Map<String, Object> attributes = new HashMap<>();
attributes.putAll(session.attributes());

session.setValid(false);

// let's recreate the session from zero
// (Grizzly reuse the same object, but that could change in the future...)
final Session newSession = getSession(context);
attributes.forEach(newSession::setAttribute);

return true;
}



@Override
public SessionStore<JaxRsContext> buildFromTrackableSession(JaxRsContext context, Object trackableSession) {
return new GrizzlySessionStore() {
@Override
public Session getSession(JaxRsContext context) {
return (Session) trackableSession;
}
};
}
}
Loading