Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Focusing

Abhishek Gupta abhirockzz

:octocat:
Focusing
View GitHub Profile
View UsingObjects.java
package com.abhirockzz.wordpress.npesaviors;
import java.util.Map;
import java.util.Objects;
public class UsingObjects {
String getValSafe(Map<String, String> aMap, String key) {
Map<String, String> safeMap = Objects.requireNonNull(aMap,
"Map is null");
View gist:d05db7a2f6911cd22cad
@Path("{k}={v}")
@GET
public void test(@PathParam("k") String key, @PathParam("v") String val){
System.out.println("Key == "+ key);
System.out.println("Val == "+ val);
}
View MethodInterceptorExample.java
public class MethodInterceptor{
@AroundInvoke
public Object interceptorMethod(InvocationContext ictx) throws Exception{
//logic goes here
}
}
@Stateless
public class AnEJB{
@Interceptors(MethodInterceptor.class)
@abhirockzz
abhirockzz / ConstructorInterceptorExample.java
Last active Aug 29, 2015
Lifecycle Callback interceptors @AroundConstruct
View ConstructorInterceptorExample.java
public class ConstructorInterceptor{
@AroundConstruct
public Object interceptorMethod(InvocationContext ictx) throws Exception{
//logic goes here
}
}
public class APOJO{
@Interceptors(ConstructorInterceptor.class)
public APOJO(){
View PostConstructInterceptorExample.java
public class PostConstructInterceptor{
@PostConstruct
public void interceptorMethod(InvocationContext ictx) throws Exception{
//logic goes here
}
}
@Interceptors(PostConstructInterceptor.class)
public class APOJO{
@PostConstruct
View IntercerptorBindingExample.java
@InterceptorBinding
@Target({TYPE, METHOD, CONSTRUCTOR})
@Retention(RUNTIME)
public @interface @Auditable {
}
@Auditable
@Interceptor
public class AuditInterceptor {
View LoggingFilter.java
@Provider
public class LoggingFilter implements ContainerRequestFilter{
public void filter(ContainerRequestContext requestContext){
//logic here - logging etc
}
}
View HTTPParamInjectionExample.java
@Path("info")
@GET
@Produces("application/json")
public Response getTweeterInfo(@QueryParam("tweeter") String tweeterId){
//fetch details for abhi_tweeter
}
@Path("info/{tweeter}")
@GET
@Produces("application/json")
View Header_CookieParamInjectionExample.java
@Path("info/{tweeter}")
@GET
@Produces("application/json")
public Response getTweeterInfo(@HeaderParam("referrer") String refURL, @CookieParam("id") String id){
//use the injected values of refURL, id from header attribute called header and Cookie named id respectively
}
@Path("info/{tweeter}")
@POST
@Consumes("application/x-www-form-urlencoded")
View BeanParamInjectionExample.java
public class ClientRequestBean{
@FormParam("tweeter_id")
private String id;
@FormParam("tweeter_email")
private String email;
@HeaderParam("referrer")
private String referrer;