Friday, June 20, 2014
Aop with juice on controller
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
public @interface Aop {
}
import java.math.BigDecimal;
import java.sql.Time;
import java.util.Date;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.codehaus.jackson.JsonNode;
import Model.Modal1;
import Model.Pojo1;
import Model.Pojo2;
import Model.Pojo3;
@Path("/test")
public class GuiceResource {
@Aop
@GET
@Produces("text/plain")
public String getIt() {
System.out.println("I am called after interception");
return "Hello From Guice: ";
}
@Aop
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Modal1 getTrackInJSON() {
System.out.println("i am json called for get ");
Modal1 model = new Modal1();
model.setVariable1(new BigDecimal(1));
model.setVariable2("string");
model.setVariable3(new Date());
model.setVariable4(new Time(1));
model.setPojo1(new Pojo1());
model.setPojo2(new Pojo2());
model.setPojo3(new Pojo3());
// List <BasicPatientInformationServiceDto> test= getAllBasicPatientInformation();
return model;
}
@Aop
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(JsonNode jsonNode) {
System.out.println("i am json called for post ");
String result = "Track saved : " + jsonNode;
return Response.status(201).entity(result).build();
}
}
import java.util.HashMap;
import java.util.Map;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
bind(GuiceResource.class);
Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.api.json.POJOMappingFeature",
"true");
// serve("/*").with(GuiceContainer.class);
bindInterceptor(Matchers.any(),Matchers.annotatedWith(Aop.class),new SecurityGuard());
serve("/*").with(
GuiceContainer.class,
initParams);
filter("/*").through(LogFilter.class);
}
});
}
}
;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class SecurityGuard implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("I am intercepted ");
// if (false) {
// return null;
// }
return invocation.proceed();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment