1 16 17 package org.apache.struts.chain.legacy; 18 19 20 import java.io.IOException ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import org.apache.commons.chain.Catalog; 25 import org.apache.commons.chain.CatalogFactory; 26 import org.apache.commons.chain.Command; 27 import org.apache.commons.chain.web.servlet.ServletWebContext; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.action.ActionServlet; 31 import org.apache.struts.action.RequestProcessor; 32 import org.apache.struts.chain.Constants; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.struts.upload.MultipartRequestWrapper; 35 36 37 57 58 public class ComposableRequestProcessor extends RequestProcessor { 59 60 61 63 64 68 private static final String CATALOG_NAME = 69 "org.apache.struts.chain.CATALOG_NAME"; 70 71 72 75 private static final String COMMAND_NAME = 76 "org.apache.struts.chain.COMMAND_NAME"; 77 78 79 81 82 86 protected Catalog catalog = null; 87 88 89 92 protected Command command = null; 93 94 95 98 protected static final Log log = 99 LogFactory.getLog(ComposableRequestProcessor.class); 100 101 102 104 105 108 public void destroy() { 109 110 super.destroy(); 111 catalog = null; 112 command = null; 113 114 } 115 116 117 125 public void init(ActionServlet servlet, 126 ModuleConfig moduleConfig) 127 throws ServletException { 128 129 log.info("Initializing composable request processor for module prefix '" 130 + moduleConfig.getPrefix() + "'"); 131 super.init(servlet, moduleConfig); 132 133 String catalogName = 134 servlet.getServletContext().getInitParameter(CATALOG_NAME); 135 if (catalogName == null) { 136 catalogName = "struts"; 137 } 138 catalog = CatalogFactory.getInstance().getCatalog(catalogName); 139 if (catalog == null) { 140 throw new ServletException ("Cannot find catalog '" + 141 catalogName + "'"); 142 } 143 144 String commandName = 145 servlet.getServletContext().getInitParameter(COMMAND_NAME); 146 if (commandName == null) { 147 commandName = "servlet-standard"; 148 } 149 command = catalog.getCommand(commandName); 150 if (command == null) { 151 throw new ServletException ("Cannot find command '" + 152 commandName + "'"); 153 } 154 155 } 156 157 158 168 public void process(HttpServletRequest request, 169 HttpServletResponse response) 170 throws IOException , ServletException { 171 172 request = processMultipart(request); 174 175 ServletWebContext context = new ServletWebContext(); 177 context.initialize(getServletContext(), request, response); 178 context.put(Constants.ACTION_SERVLET_KEY, 179 this.servlet); 180 context.put(Constants.MODULE_CONFIG_KEY, 181 this.moduleConfig); 182 183 try { 185 if (log.isDebugEnabled()) { 186 log.debug("Using processing chain for this request"); 187 } 188 command.execute(context); 189 } catch (Exception e) { 190 throw new ServletException (e); 192 } 193 194 context.release(); 196 } 197 198 199 205 protected HttpServletRequest processMultipart(HttpServletRequest request) { 206 207 if (!"POST".equalsIgnoreCase(request.getMethod())) { 208 return (request); 209 } 210 211 String contentType = request.getContentType(); 212 if ((contentType != null) && 213 contentType.startsWith("multipart/form-data")) { 214 return (new MultipartRequestWrapper(request)); 215 } else { 216 return (request); 217 } 218 219 } 220 221 222 } 223 224 225 226 | Popular Tags |