KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > ThemeImageServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.net.admin;
27
28 import org.dom4j.Document;
29 import org.dom4j.Element;
30 import org.radeox.util.logging.Logger;
31 import org.snipsnap.app.Application;
32 import org.snipsnap.config.Configuration;
33 import org.snipsnap.snip.Snip;
34 import org.snipsnap.snip.attachment.Attachment;
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.codec.BinaryDecoder;
37
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.http.HttpServlet JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.ByteArrayInputStream JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Map JavaDoc;
50
51 public class ThemeImageServlet extends HttpServlet JavaDoc {
52   protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
53     String JavaDoc name = request.getParameter("name");
54
55     Configuration config = Application.get().getConfiguration();
56     Map JavaDoc installedThemes = ThemeHelper.getInstalledThemes();
57     if(installedThemes.containsKey(name)) {
58       Snip themeSnip = (Snip)installedThemes.get(name);
59       Attachment att = themeSnip.getAttachments().getAttachment("screenshot.png");
60       if(att != null) {
61         sendImage(response, new FileInputStream JavaDoc(new File JavaDoc(config.getFilePath(), att.getLocation())),
62                   (int) att.getSize(), att.getContentType());
63         return;
64       }
65     } else {
66       Map JavaDoc themeDocs = ThemeHelper.getThemeDocuments(config, ThemeHelper.DOCUMENTS);
67       Document themeDoc = (Document)themeDocs.get(name);
68       if(null != themeDoc) {
69         Element attEl = getThemeElement(themeDoc, name);
70         if(attEl.element("data") != null) {
71           try {
72             sendImage(response, getImageStream(attEl.elementText("data")),
73                       Integer.parseInt(attEl.elementText("size")), attEl.elementText("contentType"));
74             return;
75           } catch (Exception JavaDoc e) {
76             Logger.warn("unable to read image stream", e);
77           }
78         }
79       }
80     }
81     response.sendError(HttpServletResponse.SC_NOT_FOUND);
82   }
83
84   private InputStream JavaDoc getImageStream(String JavaDoc base64str) throws Exception JavaDoc {
85     byte buffer[] = Base64.decodeBase64(base64str.getBytes("UTF-8"));
86     return new ByteArrayInputStream JavaDoc(buffer);
87   }
88
89   private Element getThemeElement(Document doc, String JavaDoc name) {
90     Iterator JavaDoc it = doc.getRootElement().elementIterator("snip");
91     while (it.hasNext()) {
92       Element element = (Element) it.next();
93       String JavaDoc snipName = element.elementText("name");
94       if(null != snipName && snipName.endsWith(name)) {
95         Iterator JavaDoc attIt = element.element("attachments").elementIterator("attachment");
96         while (attIt.hasNext()) {
97           Element attEl = (Element) attIt.next();
98           if(attEl.elementText("name").equals("screenshot.png")) {
99             return attEl;
100           }
101         }
102       }
103     }
104     return null;
105   }
106
107   private void sendImage(HttpServletResponse JavaDoc response, InputStream JavaDoc imageStream,
108                          int contentLength, String JavaDoc contentType) throws IOException JavaDoc {
109     response.setContentType(contentType);
110     response.setContentLength(contentLength);
111
112     OutputStream JavaDoc out = response.getOutputStream();
113     byte[] buffer = new byte[4096];
114     int n = 0;
115     while((n = imageStream.read(buffer)) != -1) {
116       out.write(buffer, 0, n);
117     }
118     out.close();
119   }
120
121 }
122
Popular Tags