Skip to content

Instantly share code, notes, and snippets.

View abhirockzz's full-sized avatar
👋
fmt.Println(hello, world!)

Abhishek Gupta abhirockzz

👋
fmt.Println(hello, world!)
View GitHub Profile
@abhirockzz
abhirockzz / Books.java
Last active August 29, 2015 14:16
A hypothetical resource of books
@Path("books")
public class Books{
@Produces("application/json")
@GET
public List<Book> findAll(){
//find all books
}
@Produces("application/json")
@GET
@Path("{id}")
@abhirockzz
abhirockzz / Movies.java
Last active August 29, 2015 14:16
Hypothetical Movies resource
@Path("movies")
public class Books{
@Produces("application/json")
@GET
public List<Movie> findAll(){
//find all movies e.g. /movies/
}
@Produces("application/json")
@GET
@Path("{name}")
@abhirockzz
abhirockzz / AsyncExceute.java
Last active August 29, 2015 14:17
JAX-RS async method
@GET
@Produces("text/plain")
public void execute(@Suspended AsyncResponse response){
System.out.println("Initially invoked on thread - "+ Thread.currentThread.getName() + ". This will free up soon !");
new Thread(){
@Override
public void run(){
response.resume("executed asynchronously on thread - "+ Thread.currentThread.getName());
}
}.start();
@abhirockzz
abhirockzz / AsyncJaxrsTimeout.java
Created March 13, 2015 10:52
JAX-RS async time out
@GET
@Produces("text/plain")
public void execute(@Suspended AsyncResponse response){
System.out.println("Initially invoked on thread - "+ Thread.currentThread.getName() + ". This will free up soon !");
//just having this would result in HTTP 503 after 10 seconds
response.setTimeout(10, TimeUnit.SECONDS);
//client will recieve a HTTP 408 (timeout error) after 10 seconds
response.setTimeoutHandler((asyncResp) -> asyncResp.resume(Response.status(Response.Status.REQUEST_TIMEOUT)).build());
new Thread(() -> {
try {
@abhirockzz
abhirockzz / SFSB_With_Timeout.java
Last active August 29, 2015 14:18
A Stateful Session bean with timeout configuration
@StatefulTimeout(15000,java.util.concurrent.TimeUnit.MILLISECONDS)
public class SFSB_With_Timeout{
public void register(){
//....business logic
}
}
@abhirockzz
abhirockzz / Singleton_With_Timeout.java
Last active August 29, 2015 14:18
A Singleton Session bean with timeout configuration
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) //this is actually by default
public class Singleton_With_Timeout{
@AccessTimeout(value = 5000, unit = java.util.concurrent.TimeUnit.MILLISECONDS)
@Lock(LockType.WRITE) //default configuration
public void find(){
//... business logic
}
}
@abhirockzz
abhirockzz / InjectHeaders.java
Last active August 29, 2015 14:19
Extract Header information by injecting the HttpHeaders interface
@Path("testinject")
public class InjectURIDetails{
//localhost:8080/<root-context>/testinject/httpheaders
@GET
@Path("httpheaders")
public void test(@Context HttpHeaders headers){
System.out.println("ALL headers -- "+ headers.getRequestHeaders().toString());
System.out.println("'Accept' header -- "+ headers.getHeaderString("Accept"));
System.out.println("'TestCookie' value -- "+ headers.getCookies().get("TestCookie").getValue());
}
@abhirockzz
abhirockzz / InjectURIDetails.java
Last active August 29, 2015 14:19
Extract URI information by injecting the UriInfo interface
@Path("testinject")
public class InjectURIDetails{
//localhost:8080/<root-context>/testinject/uriinfo
@GET
@Path("uriinfo")
public void test(@Context UriInfo uriDetails){
System.out.println("ALL query parameters -- "+ uriDetails.getQueryParameters().toString());
System.out.println("'id' query parameter -- "+ uriDetails.getQueryParameters.get("id"));
System.out.println("Complete URI -- "+ uriDetails.getRequestUri());
}
@abhirockzz
abhirockzz / InjectSecurityContext.java
Created May 2, 2015 10:15
Extract Security related information by injecting the SecurityContext interface
@Path("testinject")
public class InjectSecurityContext{
//localhost:8080/<root-context>/testinject/securitycontext
@GET
@Path("securitycontext")
public void test(@Context SecurityContext secContext){
System.out.println("Caller -- "+ secContext.getUserPrincipal()getName());
System.out.println("Authentication Scheme -- "+ secContext.getAuthenticationScheme());
System.out.println("Over HTTPS ? -- "+ secContext.isSecure());
System.out.println("Belongs to 'admin' role? -- "+ secContext.isUserInRole("admin");
@abhirockzz
abhirockzz / EmailService.java
Last active August 29, 2015 14:23
Application Managed JMS Context
@Path("email")
@Stateless
public class EmailService {
//pulls in default Conn Factory as per Java EE 7
@Resource
ConnectionFactory cf;
//application managed
JMSContext ctx;