1 package org.directwebremoting.webwork; 2 3 import java.io.UnsupportedEncodingException ; 4 import java.util.Map ; 5 6 import javax.servlet.ServletContext ; 7 import javax.servlet.ServletException ; 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 11 import org.directwebremoting.util.FakeHttpServletResponse; 12 import org.directwebremoting.util.LocalUtil; 13 import org.directwebremoting.util.Logger; 14 15 import com.opensymphony.webwork.ServletActionContext; 16 import com.opensymphony.webwork.dispatcher.DispatcherUtils; 17 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping; 18 import com.opensymphony.xwork.ActionContext; 19 import com.opensymphony.xwork.ActionInvocation; 20 import com.opensymphony.xwork.ActionProxy; 21 import com.opensymphony.xwork.ActionProxyFactory; 22 import com.opensymphony.xwork.Result; 23 import com.opensymphony.xwork.config.ConfigurationException; 24 import com.opensymphony.xwork.util.OgnlValueStack; 25 import com.opensymphony.xwork.util.XWorkContinuationConfig; 26 27 36 public class DWRAction 37 { 38 41 private static final Logger log = Logger.getLogger(DWRAction.class); 42 43 private static final String DWRACTIONPROCESSOR_INIT_PARAM = "dwrActionProcessor"; 44 45 private static DWRAction s_instance; 46 47 private DispatcherUtils m_wwDispatcher; 48 49 private IDWRActionProcessor m_actionProcessor; 50 51 private DWRAction(ServletContext servletContext) throws ServletException  52 { 53 DispatcherUtils.initialize(servletContext); 54 m_wwDispatcher = DispatcherUtils.getInstance(); 55 m_actionProcessor = loadActionProcessor(servletContext.getInitParameter(DWRACTIONPROCESSOR_INIT_PARAM)); 56 } 57 58 70 public static AjaxResult execute(ActionDefinition actionDefinition, Map params, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws ServletException  71 { 72 initialize(servletContext); 73 74 return s_instance.doExecute(actionDefinition, params, request, response, servletContext); 75 } 76 77 protected AjaxResult doExecute(ActionDefinition actionDefinition, Map params, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws ServletException  78 { 79 80 FakeHttpServletResponse actionResponse = new FakeHttpServletResponse(); 81 82 if (null != m_actionProcessor) 83 { 84 m_actionProcessor.preProcess(request, response, actionResponse, params); 85 } 86 87 m_wwDispatcher.prepare(request, actionResponse); 88 89 ActionInvocation invocation = invokeAction(m_wwDispatcher, request, actionResponse, servletContext, actionDefinition, params); 90 91 AjaxResult result = null; 92 if (actionDefinition.isExecuteResult()) 93 { 94 result = getTextResult(actionResponse); 96 } 97 else 98 { 99 result = new DefaultAjaxDataResult(invocation.getAction()); 100 } 101 102 if (null != m_actionProcessor) 103 { 104 m_actionProcessor.postProcess(request, response, actionResponse, result); 105 } 106 107 return result; 108 } 109 110 protected ActionInvocation invokeAction(DispatcherUtils du, HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionDefinition actionDefinition, Map params) throws ServletException  111 { 112 ActionMapping mapping = getActionMapping(actionDefinition, params); 113 Map extraContext = du.createContextMap(request, response, mapping, context); 114 115 OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY); 117 if (null != stack) 118 { 119 extraContext.put(ActionContext.VALUE_STACK, new OgnlValueStack(stack)); 120 } 121 122 try 123 { 124 prepareContinuationAction(request, extraContext); 125 126 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(actionDefinition.getNamespace(), actionDefinition.getAction(), extraContext, actionDefinition.isExecuteResult(), false); 127 proxy.setMethod(actionDefinition.getMethod()); 128 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack()); 129 130 if (mapping.getResult() != null) 132 { 133 Result result = mapping.getResult(); 134 result.execute(proxy.getInvocation()); 135 } 136 else 137 { 138 proxy.execute(); 139 } 140 141 return proxy.getInvocation(); 142 } 143 catch (ConfigurationException ce) 144 { 145 throw new ServletException ("Cannot invoke action '" + actionDefinition.getAction() + "' in namespace '" + actionDefinition.getNamespace() + "'", ce); 146 } 147 catch (Exception e) 148 { 149 throw new ServletException ("Cannot invoke action '" + actionDefinition.getAction() + "' in namespace '" + actionDefinition.getNamespace() + "'", e); 150 } 151 finally 152 { 153 if (null != stack) 155 { 156 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack); 157 } 158 } 159 } 160 161 protected void prepareContinuationAction(HttpServletRequest request, Map extraContext) 162 { 163 String id = request.getParameter(XWorkContinuationConfig.CONTINUE_PARAM); 164 if (null != id) 165 { 166 Map params = (Map ) extraContext.get(ActionContext.PARAMETERS); 169 params.remove(XWorkContinuationConfig.CONTINUE_PARAM); 170 171 extraContext.put(XWorkContinuationConfig.CONTINUE_KEY, id); 173 } 174 } 175 176 protected ActionMapping getActionMapping(ActionDefinition actionDefinition, Map params) 177 { 178 ActionMapping actionMapping = new ActionMapping(actionDefinition.getAction(), actionDefinition.getNamespace(), actionDefinition.getMethod(), params); 179 180 return actionMapping; 181 } 182 183 protected AjaxTextResult getTextResult(FakeHttpServletResponse response) 184 { 185 DefaultAjaxTextResult result = new DefaultAjaxTextResult(); 186 187 String text = null; 188 try 189 { 190 text = response.getContentAsString(); 191 } 192 catch (UnsupportedEncodingException uee) 193 { 194 log.warn("Cannot retrieve text output as string", uee); 195 } 196 197 if (null == text) 198 { 199 try 200 { 201 text = response.getCharacterEncoding() != null ? new String (response.getContentAsByteArray(), response.getCharacterEncoding()) : new String (response.getContentAsByteArray()); 202 } 203 catch (UnsupportedEncodingException uee) 204 { 205 log.warn("Cannot retrieve text output as encoded byte array", uee); 206 text = new String (response.getContentAsByteArray()); 207 } 208 } 209 210 result.setText(text); 211 return result; 212 } 213 214 220 private static void initialize(ServletContext servletContext) throws ServletException  221 { 222 synchronized(DWRAction.class) 223 { 224 if (null == s_instance) 225 { 226 s_instance = new DWRAction(servletContext); 227 } 228 } 229 } 230 231 238 private static IDWRActionProcessor loadActionProcessor(String actionProcessorClassName) throws ServletException  239 { 240 if (null == actionProcessorClassName || "".equals(actionProcessorClassName)) 241 { 242 return null; 243 } 244 245 try 246 { 247 Class actionProcessorClass = LocalUtil.classForName(actionProcessorClassName); 248 249 return (IDWRActionProcessor) actionProcessorClass.newInstance(); 250 } 251 catch(ClassNotFoundException cnfe) 252 { 253 throw new ServletException ("Cannot load DWRActionProcessor class '" + actionProcessorClassName + "'", cnfe); 254 } 255 catch(IllegalAccessException iae) 256 { 257 throw new ServletException ("Cannot instantiate DWRActionProcessor class '" + actionProcessorClassName + "'. Default constructor is not visible", iae); 258 } 259 catch(InstantiationException ie) 260 { 261 throw new ServletException ("Cannot instantiate DWRActionProcessor class '" + actionProcessorClassName + "'. No default constructor found", ie); 262 } 263 catch(Throwable cause) 264 { 265 throw new ServletException ("Cannot instantiate DWRActionProcessor class '" + actionProcessorClassName + "'", cause); 266 } 267 } 268 269 } 270
| Popular Tags
|