001 /* 002 Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.gui.web; 019 020 import java.io.IOException; 021 import java.io.PrintWriter; 022 import javax.servlet.http.HttpServletResponse; 023 import org.apache.log4j.Logger; 024 import org.mortbay.html.Tag; 025 import org.objectweb.jac.aspects.gui.GuiAC; 026 import org.objectweb.jac.aspects.gui.TableCellViewer; 027 import org.objectweb.jac.core.rtti.FieldItem; 028 import org.objectweb.jac.lib.Attachment; 029 import org.objectweb.jac.util.Strings; 030 import org.objectweb.jac.util.Thumbnail; 031 032 public class AttachmentViewer extends AbstractFieldView 033 implements HTMLViewer, AttachmentListener, TableCellViewer 034 { 035 static Logger logger = Logger.getLogger("web.attachment"); 036 037 Attachment value; 038 039 public AttachmentViewer(Object value, Object substance, FieldItem field) { 040 super(substance,field); 041 setValue(value); 042 } 043 044 public AttachmentViewer() { 045 isCellViewer = true; 046 } 047 048 public void setValue(Object value) { 049 this.value = (Attachment)value; 050 } 051 052 public void genHTML(PrintWriter out) throws IOException { 053 if (value!=null) { 054 if (value.getMimeType()==null) 055 out.print("<a href=\""+eventURL("onLoadAttachment")+"\">"+ 056 value.getName()+"</a>"); 057 else if (value.getMimeType().startsWith("image/")) { 058 Tag img = new Tag("img"); 059 if (isCellViewer) 060 img.attribute("src",eventURL("onLoadAttachment")+"&thumb=1"); 061 else 062 img.attribute("src",eventURL("onLoadAttachment")); 063 img.write(out); 064 } else 065 out.print("<a href=\""+eventURL("onLoadAttachment")+"\">"+ 066 value.getName()+"</a>"); 067 } 068 } 069 070 // AttachmentListener interface 071 072 public void onLoadAttachment() { 073 WebDisplay display = (WebDisplay)context.getDisplay(); 074 try { 075 if (value!=null) { 076 JacRequest request = WebDisplay.getRequest(); 077 HttpServletResponse response = WebDisplay.getResponse(); 078 response.setContentType(value.getMimeType()); 079 if (request.getParameter("thumb")!=null) { 080 try { 081 byte[] thumb = 082 Thumbnail.createThumbArray( 083 value.getData(), 084 GuiAC.THUMB_MAX_WIDTH,GuiAC.THUMB_MAX_HEIGHT, 085 GuiAC.THUMB_QUALITY); 086 logger.debug(this+"Writing attachment "+value+ 087 " on "+Strings.hex(response)); 088 response.getOutputStream().write(thumb); 089 } catch (Exception e) { 090 e.printStackTrace(); 091 } 092 } else { 093 response.getOutputStream().write(value.getData()); 094 } 095 } 096 } catch (IOException e) { 097 logger.error("Failed to output stream",e); 098 } finally { 099 (WebDisplay.getRequest()).setResponse(); 100 } 101 } 102 }