| package nz.co.risingstars.content;
  | 
  | import java.io.ByteArrayOutputStream;
  | import java.io.IOException;
  | import java.io.InputStream;
  | 
  | import javax.servlet.ServletException;
  | import javax.servlet.http.HttpServlet;
  | import javax.servlet.http.HttpServletRequest;
  | import javax.servlet.http.HttpServletResponse;
  | 
  | import nz.co.risingstars.entities.Image;
  | 
  | import org.jboss.seam.Component;
  | 
  | /**
  |  * Serves images and other member content
  |  * 
  |  * @author Shane Bryzak
  |  */
  | public class ContentServlet extends HttpServlet {
  | 
  |     private static final String IMAGES_PATH = "/images";
  | 
  |     private byte[] noImage;
  | 
  |     public ContentServlet() {
  |             InputStream noImageFromStorage = 
getClass().getResourceAsStream("/images/no_image.png");
  |             if (noImageFromStorage != null) {
  |                     ByteArrayOutputStream out = new ByteArrayOutputStream();
  |                     byte[] buffer = new byte[512];
  |                     try {
  |                             int read = noImageFromStorage.read(buffer);
  |                             while (read != -1) {
  |                                     out.write(buffer, 0, read);
  |                                     read = noImageFromStorage.read(buffer);
  |                             }
  | 
  |                             noImage = out.toByteArray();
  |                     } catch (IOException e) {
  |                     }
  |             }
  |     }
  | 
  |     @Override
  |     protected void doGet(HttpServletRequest request,
  |                     HttpServletResponse response) throws ServletException, 
IOException {
  | 
  |             if (IMAGES_PATH.equals(request.getPathInfo())) {
  |                     ContentController contentController = 
(ContentController) Component
  |                                     
.getInstance(ContentControllerImpl.class);
  | 
  |                     String id = request.getParameter("id");
  |                     Image imageToFetch = null;
  |                     if (id != null && !"".equals(id)) {
  |                             imageToFetch = 
contentController.getImage(Long.parseLong(id));
  |                     }
  | 
  |                     String contentType = null;
  |                     byte[] data = null;
  | 
  |                     if (imageToFetch != null && imageToFetch.getThumbnail() 
!= null && imageToFetch.getThumbnail().length > 0) {
  |                             contentType = imageToFetch.getType();
  |                             data = imageToFetch.getThumbnail();
  |                     } else if (noImage != null) {
  |                             contentType = "image/png";
  |                             data = noImage;
  |                     }
  | 
  |                     if (data != null) {
  |                             response.setContentType(contentType);
  |                     }
  |                     response.getOutputStream().write(data);
  |                     response.getOutputStream().flush();
  |             }
  |     }
  | }
  | 
  | 
  | package nz.co.risingstars.content;
  | 
  | import nz.co.risingstars.entities.Image;
  | 
  | public interface ContentController
  | {
  |   Image getImage(long imageId);
  | }
  | 
  | 
  | package nz.co.risingstars.content;
  | 
  | import javax.ejb.Stateless;
  | import javax.persistence.EntityManager;
  | import javax.persistence.PersistenceContext;
  | 
  | import nz.co.risingstars.entities.Image;
  | 
  | import org.jboss.seam.annotations.Name;
  | 
  | @Stateless
  | @Name("contentController")
  | public class ContentControllerImpl implements ContentController {
  | 
  |     @PersistenceContext
  |     private EntityManager em;
  | 
  |     public Image getImage(long imageId) {
  |             Image img = em.find(Image.class, imageId);
  | 
  |             // if (img == null || 
!Identity.instance().hasPermission("memberImage", "view", img))
  |             // return null;
  |             // else
  |             return img;
  |     }
  | }
  | 
  | 
  | 
  | 
  | 
  | 
  | 

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4016700#4016700

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4016700
_______________________________________________
jboss-user mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to