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
@Path("info/{tweeter}")
@POST
@Consumes("application/x-www-form-urlencoded")
public Response postInfo(@FormParam("tweeter_id") String id, @FormParam("tweeter_email") String email){
//use the injected values from HTML form attributes tweeter_id and tweeter_email
}
@Path({id}/tweet)
@POST
@Consumes("application/json")
public Response postTweet(Tweet aTweet){
//Tweet is a custom domain object
}
@Provider
@Consumes("application/json")
public TweetReader implements MessageBodyReader<Tweet>{
@Provider
public class HeaderModifierFilter implements ContainerResponseFilter{
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException{
//logic here - add custom heasers to the response
}
}
@Provider
public CustomWriterInterceptor implements WriterInterceptor{
@Override
public void aroundWriteFrom(WriterInterceptorContext wic){
//use the methods from the WriterInterceptorContext
wic.proceed();
//invoke the proceed method to get things moving!
}
}
@Path({user_id}/tweet/{tweet_id})
@GET
@Produces("application/json")
public Tweet getTweet(@PathParam("user_id") String userid, @PathParam("tweet_id") String tweeter){
//use the userid and tweetid to fetch the Tweet (domain object) and return it - this will trigger the MessageBodyWriter implementation
}
@Provider
@Produces("application/json")
public TweetWriter implements MessageBodyWriter<Tweet>{
public class EmbeddedEJBExample{
//inject an EJB available on the classpath
@EJB
MyLocalEJB local;
//use JNDI to talk to remotely deployed EJBs (via RMI)
EJBContainer container = EJBContainer.createEJBContainer();
Context context = container.getContext();
ARemoteEJB remote = (ARemoteEJB) context.lookup(<provide JNDI name here >)
}
@abhirockzz
abhirockzz / POJO_WS.java
Last active August 29, 2015 14:14
POJO powered by @webservice
// is this robust enough ?
@WebService
public class POJO_WS{
@WebMethod
public String getDate(){
System.out.println("hashCode -- "+ this.hashCode());
return new Date().toString();
}
}
@abhirockzz
abhirockzz / POJO_EJB_and_SOAP.java
Created January 28, 2015 10:23
POJO powered by @stateless exposed as a SOAP end point via @webservice
//not perfect - but better than just @WebService
//will recieve free services from the EJB container, courtsey @Stateless !
@Stateless
@WebService
public class POJO_EJB_and_SOAP {
public String fetchDate(){
System.out.println("hashCode -- "+ this.hashCode());
return new Date().toString();
}
@abhirockzz
abhirockzz / JCacheUsage.java
Last active August 29, 2015 14:15
Using JCache API
public class JCacheUsage{
public static void main(String[] args){
//bootstrap the JCache Provider
CachingProvider jcacheProvider = Caching.getCachingProvider();
CacheManager jcacheManager = jcacheProvider.getCacheManager();
//configure cache
MutableConfiguration<String, Integer> jcacheConfig = new MutableConfiguration<>();
jcacheConfig.setTypes(String.class, MyPreciousObject.class);
//create cache
Cache<String, MyPreciousObject> cache = jcacheManager.createCache("PreciousObjectCache", jcacheConfig);
@abhirockzz
abhirockzz / Choosing JCache providers
Last active August 29, 2015 14:15
Choosing JCache providers
//set JMV level system property
-Djavax.cache.spi.cachingprovider=org.ehcache.jcache.JCacheCachingProvider
//code level config
System.setProperty("javax.cache.spi.cachingprovider","org.ehcache.jcache.JCacheCachingProvider");
//you want to choose from multiple JCache providers at runtime
CachingProvider ehcacheJCacheProvider = Caching.getCachingProvider("org.ehcache.jcache.JCacheCachingProvider");
//which JCache providers do I have on the classpath?