/** * Returns a {@link javax.servlet.http.HttpServletResponse} by looking it up through WebWork's ActionContext Map. * We could override this method to return the desired Mock HttpServletResponse when writing testcases. * * @param invocation * @return {@link javax.servlet.http.HttpServletResponse} */ protected HttpServletResponse getServletResponse(ActionInvocation invocation) { return (HttpServletResponse) invocation.getInvocationContext().getContextMap().get(ServletActionContext.HTTP_RESPONSE); } }
public void execute(ActionInvocation invocation) throws IOException, IllegalArgumentException { HttpServletResponse response = ServletActionContext.getResponse(); // Read captcha image bytes byte[] image = (byte[]) ActionContext.getContext().getContextMap().get("captchaImage"); // Send response response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); response.setContentLength(image.length); try { response.getOutputStream().write(image); response.getOutputStream().flush(); } catch (IOException e) { throw e; } } }
protected Object resolveModel(ObjectFactory factory, ActionContext actionContext, String modelClassName, String modelScope, String modelName) throws Exception { Object model = null; Map<String, Object> scopeMap = actionContext.getContextMap(); if ("session".equals(modelScope)) { scopeMap = actionContext.getSession(); } model = scopeMap.get(modelName); if (model == null) { model = factory.buildBean(modelClassName, null); scopeMap.put(modelName, model); } return model; }
private Object convert(Object element) { if ((element != null) && !clazz.isAssignableFrom(element.getClass())) { // convert to correct type if (LOG.isDebugEnabled()) { LOG.debug("Converting from " + element.getClass().getName() + " to " + clazz.getName()); } TypeConverter conv = getTypeConverter(); Map<String, Object> context = ActionContext.getContext().getContextMap(); element = conv.convertValue(context, null, null, null, element, clazz); } return element; }
public void parseValidatorDefinitions(Map<String, String> validators, InputStream is, String resourceName) { InputSource in = new InputSource(is); in.setSystemId(resourceName); Map<String, String> dtdMappings = new HashMap<String, String>(); dtdMappings.put("-//Apache Struts//XWork Validator Config 1.0//EN", "xwork-validator-config-1.0.dtd"); dtdMappings.put("-//Apache Struts//XWork Validator Definition 1.0//EN", "xwork-validator-definition-1.0.dtd"); Document doc = DomHelper.parse(in, dtdMappings); if (doc != null) { NodeList nodes = doc.getElementsByTagName("validator"); for (int i = 0; i < nodes.getLength(); i++) { Element validatorElement = (Element) nodes.item(i); String name = validatorElement.getAttribute("name"); String className = validatorElement.getAttribute("class"); try { // catch any problems here objectFactory.buildValidator(className, new HashMap<String, Object>(), ActionContext.getContext().getContextMap()); validators.put(name, className); } catch (Exception e) { throw new ConfigurationException("Unable to load validator class " + className, e, validatorElement); } } } }
protected AgiTemplate buildTemplateInstance(ActionContext context, Class<?> c) throws Exception { Container container = context.getContainer(); ObjectFactory factory = container.getInstance(ObjectFactory.class); AgiTemplate template = (AgiTemplate) factory.buildBean(c, context.getContextMap()); return template; }
private static void reloadBundles() { reloadBundles(ActionContext.getContext() != null ? ActionContext.getContext().getContextMap() : null); }
@Override public String intercept(ActionInvocation invocation) throws Exception { ValueStack stack = invocation.getStack(); CompoundRoot root = stack.getRoot(); if (root.size() >= this.compoundRootMinSize && isChainResult(invocation)) { List<CompoundRoot> list = new ArrayList<CompoundRoot>(root); list.remove(0); Collections.reverse(list); Map<String, Object> ctxMap = invocation.getInvocationContext().getContextMap(); Iterator<CompoundRoot> iterator = list.iterator(); int index = 1; // starts with 1, 0 has been removed while (iterator.hasNext()) { index = index + 1; Object o = iterator.next(); if (o != null) { if (!(o instanceof Unchainable)) { reflectionProvider.copy(o, invocation.getAction(), ctxMap, excludes, includes); } } else { LOG.warn("compound root element at index " + index + " is null"); } } } return invocation.invoke(); }
@Override public Object evaluate(String expression, Request request) { try { HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); ActionContext ctx = ServletActionContext.getActionContext(httpRequest); if (ctx == null) { LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); throw new ConfigurationException("There is no ActionContext for current request!"); } OgnlUtil ognlUtil = ctx.getContainer().getInstance(OgnlUtil.class); LOG.debug("Trying evaluate expression [{}] using OgnlUtil's getValue", expression); Object result = ognlUtil.getValue(expression, ctx.getContextMap(), ctx.getValueStack().getRoot()); LOG.debug("Final result of evaluating expression [{}] is: {}", expression, result); return result; } catch (OgnlException e) { throw new EvaluationException(e); } }
protected Result buildResult(String path, String resultCode, ResultTypeConfig config, ActionContext invocationContext) { String resultClass = config.getClassName(); Map<String, String> params = new LinkedHashMap<>(); if (config.getParams() != null) { params.putAll(config.getParams()); } params.put(config.getDefaultResultParam(), path); ResultConfig resultConfig = new ResultConfig.Builder(resultCode, resultClass).addParams(params).build(); try { return objectFactory.buildResult(resultConfig, invocationContext.getContextMap()); } catch (Exception e) { throw new XWorkException("Unable to build convention result", e, resultConfig); } }
@Override protected ViewPreparer createPreparer(String name) { ActionContext actionContext = ActionContext.getContext(); if (actionContext == null) { LOG.warn("Action context not initialised, request has omitted an action? Fallback to super.createPreparer!"); return super.createPreparer(name); } try { ObjectFactory factory = actionContext.getContainer().getInstance(ObjectFactory.class); return (ViewPreparer) factory.buildBean(name, ActionContext.getContext().getContextMap()); } catch (Exception e) { LOG.error(new ParameterizedMessage("Cannot create a ViewPreparer [{}], fallback to super.createPreparer!", name), e); return super.createPreparer(name); } } }
private void copyStack(ActionInvocation invocation, CompoundRoot root) { List list = prepareList(root); Map<String, Object> ctxMap = invocation.getInvocationContext().getContextMap(); for (Object object : list) { if (shouldCopy(object)) { reflectionProvider.copy(object, invocation.getAction(), ctxMap, prepareExcludes(), includes); } } }
this.result = objectFactory.buildResult(resultConfig, invocationContext.getContextMap()); LOG.debug("Found default error result.");
/** * Returns the element at the specified position in this list. * <p/> * An object is guaranteed to be returned since it will create empty beans to fill the gap * between the current list size and the requested index. * * @param index index of element to return. * @return the element at the specified position in this list. */ @Override public synchronized Object get(int index) { while (index >= this.size()) { try { this.add(getObjectFactory().buildBean(clazz, ActionContext.getContext().getContextMap())); } catch (Exception e) { throw new XWorkException(e); } } return super.get(index); }
return objectFactory.buildResult(resultConfig, invocationContext.getContextMap()); } catch (Exception e) { if (LOG.isErrorEnabled()) {
Map<String, Object> contextMap = ac.getContextMap(); try { ReflectionContextState.setCreatingNullObjects(contextMap, true);
public Validator getValidator(ValidatorConfig cfg) { String className = lookupRegisteredValidatorType(cfg.getType()); Validator validator; try { // instantiate the validator, and set configured parameters //todo - can this use the ThreadLocal? validator = objectFactory.buildValidator(className, cfg.getParams(), ActionContext.getContext().getContextMap()); } catch (Exception e) { final String msg = "There was a problem creating a Validator of type " + className + " : caused by " + e.getMessage(); throw new XWorkException(msg, e, cfg); } // set other configured properties validator.setMessageKey(cfg.getMessageKey()); validator.setDefaultMessage(cfg.getDefaultMessage()); validator.setMessageParameters(cfg.getMessageParams()); if (validator instanceof ShortCircuitableValidator) { ((ShortCircuitableValidator) validator).setShortCircuit(cfg.isShortCircuit()); } return validator; }
invocationContext.getContextMap().put(ServletActionContext.ACTION_MAPPING, mapping); invocationContext.setParameters(HttpParameters.create(request.getParameterMap()).build());
@Override public String doIntercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (!(action instanceof NoParameters)) { ActionContext ac = invocation.getInvocationContext(); final Map<String, Object> parameters = retrieveParameters(ac); if (LOG.isDebugEnabled()) { LOG.debug("Setting params " + getParameterLogMap(parameters)); } if (parameters != null) { Map<String, Object> contextMap = ac.getContextMap(); try { ReflectionContextState.setCreatingNullObjects(contextMap, true); ReflectionContextState.setDenyMethodExecution(contextMap, true); ReflectionContextState.setReportingConversionErrors(contextMap, true); ValueStack stack = ac.getValueStack(); setParameters(action, stack, parameters); } finally { ReflectionContextState.setCreatingNullObjects(contextMap, false); ReflectionContextState.setDenyMethodExecution(contextMap, false); ReflectionContextState.setReportingConversionErrors(contextMap, false); } } } return invocation.invoke(); }