Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e16a60970e9f1e42c536 to your computer and use it in GitHub Desktop.
Save anonymous/e16a60970e9f1e42c536 to your computer and use it in GitHub Desktop.
// BEGIN src/main/java/com/example/MyResource.java
package com.example;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource
{
/* @Inject
public HttpSession session;*/
@Context
private UriInfo uriInfo;
@Inject HttpSession session;
/**
* Method handling HTTP GET requests. The returned object will be sent to
* the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt()
{
// System.out.println(request.getSession().getId());
return "Got it!";
}
}
// END src/main/java/com/example/MyResource.java
// BEGIN src/main/java/com/example/MyResourceConfig.java
package com.example;
import org.glassfish.hk2.api.InjectionResolver;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import javax.inject.Singleton;
import javax.servlet.http.HttpSession;
/**
* Created by IntelliJ IDEA.
* <p/>
* Created : 18/02/16 3:08 PM MST
* <p/>
* Modified : $Date$ UTC
* <p/>
* Revision : $Revision$
*
* @author Trenton D. Adams
*/
public class MyResourceConfig extends ResourceConfig
{
public MyResourceConfig()
{
register(new AbstractBinder()
{
@Override
protected void configure()
{
bindFactory(HttpSessionFactory.class).to(HttpSession.class);
}
});
}
}
// END src/main/java/com/example/MyResourceConfig.java
// BEGIN src/main/java/com/example/Main.java
package com.example;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
/**
* Main class.
*/
public class Main
{
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:9090/myapp/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this
* application.
*
* @return Grizzly HTTP server.
*/
public static HttpServer startServer()
{
// create a resource config that scans for JAX-RS resources and providers
// in com.example package
final ResourceConfig rc = new MyResourceConfig().packages("com.example");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI),
rc);
}
/**
* Main method.
*
* @param args
*
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
final HttpServer server = startServer();
System.out.println(
String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}
// END src/main/java/com/example/Main.java
// BEGIN src/main/java/com/example/SessionInject.java
package com.example;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by IntelliJ IDEA.
* <p/>
* Created : 18/02/16 4:50 PM MST
* <p/>
* Modified : $Date$ UTC
* <p/>
* Revision : $Revision$
*
* @author Trenton D. Adams
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SessionInject { }// END src/main/java/com/example/SessionInject.java
// BEGIN src/main/java/com/example/HttpSessionFactory.java
package com.example;
import org.glassfish.hk2.api.Factory;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Created by IntelliJ IDEA.
* <p/>
* Created : 18/02/16 3:23 PM MST
* <p/>
* Modified : $Date$ UTC
* <p/>
* Revision : $Revision$
*
* @author Trenton D. Adams
*/
public class HttpSessionFactory implements Factory<HttpSession>
{
private final HttpServletRequest request;
@Inject
public HttpSessionFactory(HttpServletRequest request)
{
this.request = request;
}
@Override
public HttpSession provide()
{
return request.getSession();
}
@Override
public void dispose(HttpSession t)
{
}
}
// END src/main/java/com/example/HttpSessionFactory.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment