dev@jersey.java.net

Reposting to jersey alias- REST Resources talking to Session EJB

From: Ayub Khan <Ayub.Khan_at_Sun.COM>
Date: Wed, 13 Feb 2008 12:45:09 -0800

Hi Paul,

Using NB6.1 m1 (jersey 0.5), I created a JAX-WS service that talks to a
Session EJB, following the tutorial (steps 1-3, ie till testing the app)

http://www.netbeans.org/kb/60/websvc/ejb.html#Exercise_3.

Found JAX-WS to EJB working fine.

So now I wanted to test the scenario with a J2EE app 'FlowerApplication'
containing EJB module 'FlowerAlbum' and REST web project 'FlowerService2'.

Steps
1. I created a web app 'FlowerService2' with a single Resource as follows.
2. Also added library reference to the EJB module FlowerAlbum.
3. Created a new J2EE app 'FlowerApplication2' , then wrapped EJB module
'FlowerAlbum' with 'FlowerService2' REST web project.
4. Deployed J2EE app.
5. Test the url
'http://localhost:8080/FlowerService2/resources/flowerService2/

I got a NPE at line 'byte[] bytes = ejbRef.getFlower(name);', in the
getImage REST method, what
could be the problem.

flower.album.FlowerService2Resource.java
=====================================

@Path("flowerService2")
public class FlowerService2Resource {
   @HttpContext
   private UriInfo context;
     @EJB
   private FlowerRemote ejbRef;

   /** Creates a new instance of FlowerService2Resource */
   public FlowerService2Resource() {
   }

   /**
    * Retrieves representation of an instance of
flower.album.FlowerService2Resource
    * @return an instance of java.lang.String
    */
   @GET
   @ProduceMime("text/html")
   public String getImage(String name) {
       try {
           byte[] bytes = ejbRef.getFlower(name);
           Image img = getImage(bytes, false);
           return img.getSource().toString();
       } catch (IOException ex) {
           
Logger.getLogger(FlowerService2Resource.class.getName()).log(Level.SEVERE,
null, ex);
       }
       return null;
   }
     private Image getImage(byte[] bytes, boolean isThumbnail) throws
IOException {
       ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
       Iterator readers = ImageIO.getImageReadersByFormatName("png");
       ImageReader reader = (ImageReader) readers.next();
       Object source = bis; // File or InputStream
       ImageInputStream iis = ImageIO.createImageInputStream(source);
       reader.setInput(iis, true);
       ImageReadParam param = reader.getDefaultReadParam();
       if (isThumbnail) {
           param.setSourceSubsampling(4, 4, 0, 0);
       }
       return reader.read(0, param);
   }
}
=================================

Thanks
Ayub