1 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 ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.io.ByteArrayInputStream ; 43 import java.io.File ; 44 import java.io.FileInputStream ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 import java.util.Iterator ; 49 import java.util.Map ; 50 51 public class ThemeImageServlet extends HttpServlet { 52 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 53 String name = request.getParameter("name"); 54 55 Configuration config = Application.get().getConfiguration(); 56 Map 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 (new File (config.getFilePath(), att.getLocation())), 62 (int) att.getSize(), att.getContentType()); 63 return; 64 } 65 } else { 66 Map 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 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 getImageStream(String base64str) throws Exception { 85 byte buffer[] = Base64.decodeBase64(base64str.getBytes("UTF-8")); 86 return new ByteArrayInputStream (buffer); 87 } 88 89 private Element getThemeElement(Document doc, String name) { 90 Iterator it = doc.getRootElement().elementIterator("snip"); 91 while (it.hasNext()) { 92 Element element = (Element) it.next(); 93 String snipName = element.elementText("name"); 94 if(null != snipName && snipName.endsWith(name)) { 95 Iterator 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 response, InputStream imageStream, 108 int contentLength, String contentType) throws IOException { 109 response.setContentType(contentType); 110 response.setContentLength(contentLength); 111 112 OutputStream 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 |