forked from pac4j/jax-rs-pac4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Grégoire Neuville
committed
Feb 26, 2020
1 parent
2fb9efd
commit 8244518
Showing
9 changed files
with
513 additions
and
6 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
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,87 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
<artifactId>parent</artifactId> | ||
<groupId>org.pac4j.jax-rs</groupId> | ||
<version>3.0.2-SNAPSHOT</version> | ||
</parent> | ||
<groupId>org.pac4j</groupId> | ||
<artifactId>jersey228-pac4j</artifactId> | ||
|
||
<properties> | ||
<jersey.version>2.28</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.4</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> |
32 changes: 32 additions & 0 deletions
32
...8/src/main/java/org/pac4j/jax/rs/grizzly/features/GrizzlyJaxRsContextFactoryProvider.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,32 @@ | ||
package org.pac4j.jax.rs.grizzly.features; | ||
|
||
import org.glassfish.grizzly.http.server.Request; | ||
import org.pac4j.jax.rs.features.JaxRsContextFactoryProvider; | ||
import org.pac4j.jax.rs.grizzly.pac4j.GrizzlyJaxRsContext; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
|
||
/** | ||
* | ||
* 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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
jersey228/src/main/java/org/pac4j/jax/rs/grizzly/pac4j/GrizzlyJaxRsContext.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,39 @@ | ||
package org.pac4j.jax.rs.grizzly.pac4j; | ||
|
||
import org.glassfish.grizzly.http.server.Request; | ||
import org.pac4j.core.context.session.SessionStore; | ||
import org.pac4j.jax.rs.pac4j.JaxRsContext; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.ext.Providers; | ||
|
||
/** | ||
* | ||
* 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; | ||
} | ||
|
||
public Request getRequest() { | ||
return request; | ||
} | ||
|
||
@Override | ||
public String getRemoteAddr() { | ||
return request.getRemoteAddr(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
jersey228/src/main/java/org/pac4j/jax/rs/grizzly/pac4j/GrizzlySessionStore.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,83 @@ | ||
package org.pac4j.jax.rs.grizzly.pac4j; | ||
|
||
import org.glassfish.grizzly.http.server.Session; | ||
import org.pac4j.core.context.session.SessionStore; | ||
import org.pac4j.jax.rs.pac4j.JaxRsContext; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* | ||
* @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; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.