1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.IOException ; 14 import java.util.Map ; 15 import java.util.WeakHashMap ; 16 17 import javax.servlet.ServletException ; 18 import javax.servlet.http.HttpServlet ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import javax.xml.transform.TransformerException ; 22 23 import org.eclipse.help.internal.HelpPlugin; 24 import org.eclipse.help.internal.dynamic.DocumentWriter; 25 import org.eclipse.help.internal.extension.ContentExtension; 26 import org.eclipse.help.internal.webapp.data.UrlUtil; 27 28 34 public class ExtensionServlet extends HttpServlet { 35 36 private static final long serialVersionUID = 1L; 37 private Map responseByLocale; 38 private DocumentWriter writer; 39 40 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 41 throws ServletException , IOException { 42 String locale = UrlUtil.getLocale(req, resp); 43 req.setCharacterEncoding("UTF-8"); resp.setContentType("application/xml; charset=UTF-8"); 46 if (responseByLocale == null) { 47 responseByLocale = new WeakHashMap (); 48 } 49 String response = (String )responseByLocale.get(locale); 50 if (response == null) { 51 ContentExtension[] extensions = HelpPlugin.getContentExtensionManager().getExtensions(locale); 52 try { 53 response = serialize(extensions); 54 } 55 catch (TransformerException e) { 56 throw new ServletException (e); 57 } 58 responseByLocale.put(locale, response); 59 } 60 resp.getWriter().write(response); 61 } 62 63 private String serialize(ContentExtension[] extensions) throws TransformerException { 64 StringBuffer buf = new StringBuffer (); 65 buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); buf.append("<contentExtensions>\n"); for (int i = 0; i < extensions.length; ++i) { 68 if (writer == null) { 69 writer = new DocumentWriter(); 70 } 71 buf.append(writer.writeString(extensions[i], false)); 72 } 73 buf.append("</contentExtensions>\n"); return buf.toString(); 75 } 76 } 77 | Popular Tags |