1 25 package org.snipsnap.net; 26 27 import groovy.text.SimpleTemplateEngine; 28 import groovy.text.Template; 29 import org.radeox.util.logging.Logger; 30 import org.snipsnap.app.Application; 31 import org.snipsnap.container.Components; 32 import org.snipsnap.snip.Snip; 33 import org.snipsnap.snip.SnipSpace; 34 import org.snipsnap.snip.label.TypeLabel; 35 import org.snipsnap.user.Permissions; 36 import org.snipsnap.user.Roles; 37 import org.snipsnap.user.Security; 38 39 import javax.servlet.RequestDispatcher ; 40 import javax.servlet.ServletException ; 41 import javax.servlet.http.HttpServlet ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.io.BufferedReader ; 45 import java.io.BufferedWriter ; 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.io.InputStreamReader ; 49 import java.io.OutputStream ; 50 import java.io.Writer ; 51 import java.util.Collection ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 import java.util.Map ; 55 56 public class PluginServlet extends HttpServlet { 57 private Map extTypeMap = new HashMap (); 58 private Map servletCache = new HashMap (); 59 60 private final static Roles EXEC_ROLES = new Roles(Roles.ADMIN); 61 62 SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(); 63 64 public void init() throws ServletException { 65 super.init(); 66 67 extTypeMap.put(".gsp", "text/gsp"); 69 extTypeMap.put(".groovy", "text/groovy"); 70 } 71 72 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 73 String pluginName = (String ) request.getAttribute("javax.servlet.include.path_info"); 75 if (null == pluginName) { 76 pluginName = request.getPathInfo(); 77 } 78 79 if (pluginName.startsWith("/")) { 80 pluginName = pluginName.substring(1); 81 } 82 83 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 85 if (space.exists(pluginName)) { 86 Snip snip = space.load(pluginName); 87 if (Security.existsPermission(Permissions.EDIT_SNIP, snip, EXEC_ROLES)) { 89 String mimeType = getMIMEType(snip); 90 if ("text/gsp".equalsIgnoreCase(mimeType)) { 91 BufferedWriter writer = new BufferedWriter (response.getWriter()); 92 try { 93 handleGroovyTemplate(snip.getContent(), writer); 94 } catch (Exception e) { 95 writer.write("<span class=\"error\">" + e.getLocalizedMessage() + "</span>"); 96 } 97 writer.flush(); 98 } else { 99 RequestDispatcher dispatcher = request.getRequestDispatcher("/raw/" + pluginName); 101 dispatcher.include(request, response); 102 } 103 return; 104 } 105 } 106 107 Map plugins = ServletPluginLoader.getPlugins(); 109 if (plugins.containsKey(pluginName)) { 110 String handlerMIMEType = (String ) plugins.get(pluginName); 111 112 if (null == handlerMIMEType) { 114 int extIndex = pluginName.indexOf("."); 115 if (extIndex != -1) { 116 handlerMIMEType = (String ) extTypeMap.get(pluginName.substring(extIndex)); 117 } 118 } 119 120 if ("text/gsp".equalsIgnoreCase(handlerMIMEType)) { 121 BufferedWriter writer = new BufferedWriter (response.getWriter()); 122 try { 123 handleGroovyTemplate(getTemplateSource(pluginName), writer); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 writer.write("<span class=\"error\">" + e.getLocalizedMessage() + "</span>"); 127 } 128 writer.flush(); 129 return; 130 } else { 131 ServletPlugin servletPlugin = (ServletPlugin) servletCache.get(pluginName); 133 if (null == servletPlugin) { 134 try { 135 servletPlugin = getServletPlugin(pluginName); 136 servletCache.put(pluginName, servletPlugin); 137 } catch (Exception e) { 138 } 140 } 141 142 if (null != servletPlugin) { 144 try { 145 servletPlugin.service(request, response); 146 } catch (Exception e) { 147 Logger.warn("error while executing servlet plugin", e); 148 throw new ServletException ("error while executing servlet plugin", e); 149 } 150 } else { 151 if (null != handlerMIMEType) { 152 response.setContentType(handlerMIMEType); 153 } 154 OutputStream out = response.getOutputStream(); 155 InputStream fileIs = PluginServlet.class.getResourceAsStream("/" + pluginName); 156 if (null != fileIs) { 157 byte[] buffer = new byte[1024]; 158 int bytes = 0; 159 while ((bytes = fileIs.read(buffer)) != -1) { 160 out.write(buffer, 0, bytes); 161 } 162 out.flush(); 163 } else { 164 throw new ServletException ("unable to load servlet plugin: not found"); 165 } 166 } 167 return; 168 } 169 } 170 171 response.sendError(HttpServletResponse.SC_FORBIDDEN); 172 } 173 174 private ServletPlugin getServletPlugin(String pluginName) throws Exception { 175 Class pluginClass = Class.forName(pluginName); 176 return (ServletPlugin) pluginClass.newInstance(); 177 } 178 179 private String getMIMEType(Snip snip) { 180 Collection mimeTypes = snip.getLabels().getLabels("TypeLabel"); 181 if (!mimeTypes.isEmpty()) { 182 Iterator handlerIt = mimeTypes.iterator(); 183 while (handlerIt.hasNext()) { 184 TypeLabel mimeType = (TypeLabel) handlerIt.next(); 185 return mimeType.getTypeValue(); 186 } 187 } 188 return null; 189 } 190 191 private void handleGroovyTemplate(String source, Writer out) throws Exception { 192 try { 193 Template groovyTemplate = templateEngine.createTemplate(source); 194 groovyTemplate.make(Application.get().getParameters()).writeTo(out); 195 } catch (Error e) { 196 e.printStackTrace(); 197 throw new ServletException ("groovy error", e); 198 } 199 } 200 201 209 private String getTemplateSource(String name) throws IOException { 210 InputStream resource = getClass().getResourceAsStream("/" + name); 211 if (null != resource) { 212 BufferedReader reader = new BufferedReader (new InputStreamReader (resource)); 214 StringBuffer content = new StringBuffer (); 215 char buffer[] = new char[1024]; 216 int length = 0; 217 while ((length = reader.read(buffer)) != -1) { 218 content.append(buffer, 0, length); 219 } 220 return content.toString(); 221 } 222 223 throw new IOException ("unable to load template source: '" + name + "'"); 224 } 225 } 226 | Popular Tags |