1 25 package org.snipsnap.net; 26 27 import org.radeox.util.Service; 28 import org.snipsnap.app.Application; 29 import org.snipsnap.graph.ContentRenderer; 30 import org.snipsnap.graph.HorizontalContentRenderer; 31 import org.snipsnap.graph.builder.StringTreeBuilder; 32 import org.snipsnap.graph.builder.TreeBuilder; 33 import org.snipsnap.graph.context.UrlContext; 34 import org.snipsnap.graph.renderer.HtmlMapRenderer; 35 import org.snipsnap.graph.renderer.Renderer; 36 37 import javax.servlet.ServletException ; 38 import javax.servlet.http.HttpServlet ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.io.ByteArrayOutputStream ; 42 import java.io.IOException ; 43 import java.io.UnsupportedEncodingException ; 44 import java.util.Collections ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.Map ; 48 49 57 public class RenderServlet extends HttpServlet { 58 private final static String RENDER_ID = "__render_id"; 59 60 private static Map contentMap = Collections.synchronizedMap(new HashMap ()); 61 private static Map handlers = new HashMap (); 62 private final static ContentRenderer DEFAULT_HANDLER = new HorizontalContentRenderer(); 63 64 private final static Map CTOR = new HashMap (); 65 68 static { 69 Iterator contentRenderer = 70 Service.providers(org.snipsnap.graph.ContentRenderer.class); 71 while (contentRenderer.hasNext()) { 72 ContentRenderer renderer = (ContentRenderer) contentRenderer.next(); 73 handlers.put(renderer.getName(), renderer); 74 } 75 } 76 77 78 90 public static String addContent(String name, String content) { 91 Application app = Application.get(); 92 String baseId = RENDER_ID + name; 93 String renderId = null; 94 synchronized (contentMap) { 95 String key = null; 96 int add = 0; 97 do { 98 key = String.valueOf(baseId + add++); 99 } while (app.getObject(key) != null); 100 app.storeObject(key, ""); 102 renderId = Integer.toHexString(key.hashCode()); 104 contentMap.put(renderId, content); 105 } 106 return renderId; 107 } 108 109 public static String getImageMap(String renderId, String handler) { 110 HtmlMapRenderer mapRenderer = new HtmlMapRenderer(); 111 TreeBuilder builder = new StringTreeBuilder((String ) contentMap.get(renderId)); 112 ByteArrayOutputStream out = new ByteArrayOutputStream (); 113 114 Renderer renderer = ((ContentRenderer) handlers.get(handler)).getRenderer(); 115 if (null != renderer) { 116 UrlContext context = new UrlContext(renderId, renderer); 117 mapRenderer.render(builder.build(), out, context); 118 try { 119 out.flush(); 120 out.close(); 121 } catch (IOException e) { 122 e.printStackTrace(); 124 } 125 try { 126 return out.toString(Application.get().getConfiguration().getEncoding()); 127 } catch (UnsupportedEncodingException e) { 128 return out.toString(); 129 } 130 } else { 131 return "<!-- image map not possible, missing renderer for handler: " + handler + " -->"; 133 } 134 } 135 136 137 public void doGet(HttpServletRequest request, HttpServletResponse response) 138 throws IOException , ServletException { 139 140 String handler = request.getParameter("handler"); 141 String id = request.getParameter("id"); 142 String content = (String ) contentMap.get(id); 143 144 ContentRenderer renderer = (ContentRenderer) handlers.get(handler); 145 if (null == renderer) { 146 renderer = DEFAULT_HANDLER; 147 } 148 renderer.render(request, response, content); 149 } 150 } | Popular Tags |