This is embarrasing. It's really not much, and not very well written. On the
other hand I "ported" it fairly easy to another project by changing class
names and lookup so I suppose you could do the same. I'm not doing any
java2D magic in it though, just passing a byte[] as an image/jpeg to the out
stream from the defined object;

In my ImageEdit.html I have;

<img jwcid="@Any" src="ognl:url" width="ognl:model.width" height="ognl:
model.height"/>

And in the coresponding ImageEdit.java I have ;

public String getUrl()
   {
       Picture sp = (Picture)getModel();
       System.out.println("StoredPictureEdit getCurrentUrl pic is "+sp);
       System.out.println("StoredPictureEdit getCurrentUrl pic id is "+sp);
       Integer id = sp.getID();
       return "/Art/app?service=image&imageid=" + id ;
   }

Where model is the "current object" in trails. You could easily just change
this to any getter for the class contatining a byte[] for the pciture you
want to show. Not that I have chickened out on the link generation
department. Works great, though.

Then I define my ImageService in a hivemodule.xml I just put in a META-INF
directory directly under my /src directory;

<?xml version="1.0"?>
<module id="model" version="1.0.0">

 <service-point id="ImageService" interface="
org.apache.tapestry.engine.IEngineService">

<invoke-factory>

            <construct
class="ImageService"/>


</invoke-factory>

   </service-point>

 <contribution configuration-id="tapestry.services.ApplicationServices">
     <service name="image" object="service:ImageService"/>
 </contribution>

</module>

The comes the fairly half-and again not really longish ImageService.java;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.trails.persistence.PersistenceService;



public class ImageService implements IEngineService
{
   private HttpServletResponse response;
   private String TYPE="jpeg";
   private int imgid;

   public void setResponse(HttpServletResponse response)
   {
       this.response = response;
   }

   public String getName()
   {
       return "image";
   }

   public void service(IRequestCycle cycle) throws IOException
   {
       System.out.println("ImageService called");
       String id =  cycle.getParameter("imageid");
       imgid = Integer.parseInt(id);
       System.out.println("ImageService id == '"+imgid+"'");
       Picture sp = getImage();
       byte[] raw = sp.getPicdata();
       OutputStream os = response.getOutputStream();
       os.write(raw);
       os.close();
   }

   public Picture getImage()
   {
       Picture rv = null;
       List tmp = null;
       Class arg0 = null;
       try
       {
           arg0 = Class.forName("Picture");
       }
       catch (ClassNotFoundException e)
       {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       PersistenceService ps = PictureEdit.getRefrence
().getPersistenceService();
       Picture s = (Picture) ps.getInstance(arg0, imgid);
       System.out.println("ImageService Object from persistence is "+s);
       rv = s;
       return rv;
   }

   public ILink getLink(boolean post, Object parameter)
   {
       return null;

   }

   public ILink getLink(IRequestCycle arg0, boolean arg1, Object arg2)
   {
       // TODO Auto-generated method stub
       return null;
   }
}

And as I said, the main part to change here is the part in getImage where
you get a persisitenceService from some kind of Hibernate manager to
actually read the object referred to in the service HTTP argument.

Not much to it, really. Please use any or all if you can .

Cheers,
PS



On 6/11/06, Andreas Bulling <[EMAIL PROTECTED]> wrote:

On 11. Jun 2006 - 10:11:00, Peter Svensson wrote:
| I have a simple image service which is not very well packaged, but
depends
| on an Image class, and uses the trails frameworks Hibernate DAO to get
and
| do stuff to images stored as byte[]s in the Image class. Could any of
that
| be of use?

I'd also be interested in it ;-)

Andreas

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to