KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > AttachmentViewer


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import org.apache.log4j.Logger;
24 import org.mortbay.html.Tag;
25 import org.objectweb.jac.aspects.gui.GuiAC;
26 import org.objectweb.jac.aspects.gui.TableCellViewer;
27 import org.objectweb.jac.core.rtti.FieldItem;
28 import org.objectweb.jac.lib.Attachment;
29 import org.objectweb.jac.util.Strings;
30 import org.objectweb.jac.util.Thumbnail;
31
32 public class AttachmentViewer extends AbstractFieldView
33     implements HTMLViewer, AttachmentListener, TableCellViewer
34 {
35     static Logger logger = Logger.getLogger("web.attachment");
36
37     Attachment value;
38
39     public AttachmentViewer(Object JavaDoc value, Object JavaDoc substance, FieldItem field) {
40         super(substance,field);
41         setValue(value);
42     }
43
44     public AttachmentViewer() {
45         isCellViewer = true;
46     }
47
48     public void setValue(Object JavaDoc value) {
49         this.value = (Attachment)value;
50     }
51
52     public void genHTML(PrintWriter JavaDoc out) throws IOException JavaDoc {
53         if (value!=null) {
54             if (value.getMimeType()==null)
55                 out.print("<a HREF=\""+eventURL("onLoadAttachment")+"\">"+
56                           value.getName()+"</a>");
57             else if (value.getMimeType().startsWith("image/")) {
58                 Tag img = new Tag("img");
59                 if (isCellViewer)
60                     img.attribute("src",eventURL("onLoadAttachment")+"&amp;thumb=1");
61                 else
62                     img.attribute("src",eventURL("onLoadAttachment"));
63                 img.write(out);
64             } else
65                 out.print("<a HREF=\""+eventURL("onLoadAttachment")+"\">"+
66                           value.getName()+"</a>");
67         }
68     }
69
70     // AttachmentListener interface
71

72     public void onLoadAttachment() {
73         WebDisplay display = (WebDisplay)context.getDisplay();
74         try {
75             if (value!=null) {
76                 JacRequest request = WebDisplay.getRequest();
77                 HttpServletResponse JavaDoc response = WebDisplay.getResponse();
78                 response.setContentType(value.getMimeType());
79                 if (request.getParameter("thumb")!=null) {
80                     try {
81                         byte[] thumb =
82                             Thumbnail.createThumbArray(
83                                 value.getData(),
84                                 GuiAC.THUMB_MAX_WIDTH,GuiAC.THUMB_MAX_HEIGHT,
85                                 GuiAC.THUMB_QUALITY);
86                         logger.debug(this+"Writing attachment "+value+
87                                      " on "+Strings.hex(response));
88                         response.getOutputStream().write(thumb);
89                     } catch (Exception JavaDoc e) {
90                         e.printStackTrace();
91                     }
92                 } else {
93                     response.getOutputStream().write(value.getData());
94                 }
95             }
96         } catch (IOException JavaDoc e) {
97             logger.error("Failed to output stream",e);
98         } finally {
99             (WebDisplay.getRequest()).setResponse();
100         }
101     }
102 }
103
Popular Tags