KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > RenderServlet


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

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 JavaDoc;
38 import javax.servlet.http.HttpServlet JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.UnsupportedEncodingException JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * Render special content added to a temporary store. This is used for the
51  * graph macro. The main use is to add content to the page that is retrieved
52  * by img tags or similar.
53  *
54  * @author Matthias L. Jugel
55  * @version $Id: RenderServlet.java 1801 2005-01-29 17:26:09Z leo $
56  */

57 public class RenderServlet extends HttpServlet JavaDoc {
58   private final static String JavaDoc RENDER_ID = "__render_id";
59
60   private static Map JavaDoc contentMap = Collections.synchronizedMap(new HashMap JavaDoc());
61   private static Map JavaDoc handlers = new HashMap JavaDoc();
62   private final static ContentRenderer DEFAULT_HANDLER = new HorizontalContentRenderer();
63
64   private final static Map JavaDoc CTOR = new HashMap JavaDoc();
65   /**
66    * Initialize the render servlet by loading the content handlers.
67    */

68   static {
69     Iterator JavaDoc 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   /**
79    * Add content to the temporary store and return an id that can be used to select
80    * the content later. The graph macro uses this to store the graph description
81    * here which is then handed over to the rendering handler to translate to an image.
82    * The id will persist until content for the same name is added.
83    * <p/>
84    * Example:
85    * &lt;img SRC="../../../../exec/render?id=XXXX&handler=YYYY"/&gt;
86    *
87    * @param content the textual content to be rendered
88    * @return an it to add to the url for retrieving the rendered content
89    */

90   public static String JavaDoc addContent(String JavaDoc name, String JavaDoc content) {
91     Application app = Application.get();
92     String JavaDoc baseId = RENDER_ID + name;
93     String JavaDoc renderId = null;
94     synchronized (contentMap) {
95       String JavaDoc key = null;
96       int add = 0;
97       do {
98         key = String.valueOf(baseId + add++);
99       } while (app.getObject(key) != null);
100       // store a dummy to ensure the id is taken
101
app.storeObject(key, "");
102       // store content with corresponding id
103
renderId = Integer.toHexString(key.hashCode());
104       contentMap.put(renderId, content);
105     }
106     return renderId;
107   }
108
109   public static String JavaDoc getImageMap(String JavaDoc renderId, String JavaDoc handler) {
110     HtmlMapRenderer mapRenderer = new HtmlMapRenderer();
111     TreeBuilder builder = new StringTreeBuilder((String JavaDoc) contentMap.get(renderId));
112     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc e) {
122         // ignore as this is unlikely to happen
123
e.printStackTrace();
124       }
125       try {
126         return out.toString(Application.get().getConfiguration().getEncoding());
127       } catch (UnsupportedEncodingException JavaDoc e) {
128         return out.toString();
129       }
130     } else {
131       // we can't render the image map, so return comment
132
return "<!-- image map not possible, missing renderer for handler: " + handler + " -->";
133     }
134   }
135
136
137   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
138           throws IOException JavaDoc, ServletException JavaDoc {
139
140     String JavaDoc handler = request.getParameter("handler");
141     String JavaDoc id = request.getParameter("id");
142     String JavaDoc content = (String JavaDoc) 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