예제 #1
0
  @Test
  public void testExecute() throws Exception {

    ActionMapping mapping = getActionMapping("/system/user-list");
    assertNotNull(mapping);
    assertEquals("/system", mapping.getNamespace());
    assertEquals("user-list", mapping.getName());

    log.info("Answer value is " + mapping.getName());

    ActionProxy proxy = getActionProxy("/system/user-list");

    UserListAction action = (UserListAction) proxy.getAction();

    assertNotNull(action);

    String result = proxy.execute();

    assertEquals(
        "Result of calling execute method is not success but it should be.", "success", result);
  }
예제 #2
0
  public static String buildNamespace(
      ActionMapper mapper, ValueStack stack, HttpServletRequest request) {
    ActionContext context = new ActionContext(stack.getContext());
    ActionInvocation invocation = context.getActionInvocation();

    if (invocation == null) {
      ActionMapping mapping =
          mapper.getMapping(request, Dispatcher.getInstance().getConfigurationManager());

      if (mapping != null) {
        return mapping.getNamespace();
      } else {
        // well, if the ActionMapper can't tell us, and there is no existing action invocation,
        // let's just go with a default guess that the namespace is the last the path minus the
        // last part (/foo/bar/baz.xyz -> /foo/bar)

        String path = RequestUtils.getServletPath(request);
        return path.substring(0, path.lastIndexOf("/"));
      }
    } else {
      return invocation.getProxy().getNamespace();
    }
  }
예제 #3
0
  /**
   * Load Action class for mapping and invoke the appropriate Action method, or go directly to the
   * Result.
   *
   * <p>This method first creates the action context from the given parameters, and then loads an
   * <tt>ActionProxy</tt> from the given action name and namespace. After that, the Action method is
   * executed and output channels through the response object. Actions not found are sent back to
   * the user via the {@link Dispatcher#sendError} method, using the 404 return code. All other
   * errors are reported by throwing a ServletException.
   *
   * @param request the HttpServletRequest object
   * @param response the HttpServletResponse object
   * @param mapping the action mapping object
   * @throws ServletException when an unknown error occurs (not a 404, but typically something that
   *     would end up as a 5xx by the servlet container)
   * @param context Our ServletContext object
   */
  public void serviceAction(
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext context,
      ActionMapping mapping)
      throws ServletException {

    Map<String, Object> extraContext = createContextMap(request, response, mapping, context);

    // If there was a previous value stack, then create a new copy and pass it in to be used by the
    // new Action
    ValueStack stack =
        (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
    boolean nullStack = stack == null;
    if (nullStack) {
      ActionContext ctx = ActionContext.getContext();
      if (ctx != null) {
        stack = ctx.getValueStack();
      }
    }
    if (stack != null) {
      extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
    }

    String timerKey = "Handling request from Dispatcher";
    try {
      UtilTimerStack.push(timerKey);
      String namespace = mapping.getNamespace();
      String name = mapping.getName();
      String method = mapping.getMethod();

      Configuration config = configurationManager.getConfiguration();
      ActionProxy proxy =
          config
              .getContainer()
              .getInstance(ActionProxyFactory.class)
              .createActionProxy(namespace, name, method, extraContext, true, false);

      request.setAttribute(
          ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

      // if the ActionMapping says to go straight to a result, do it!
      if (mapping.getResult() != null) {
        Result result = mapping.getResult();
        result.execute(proxy.getInvocation());
      } else {
        proxy.execute();
      }

      // If there was a previous value stack then set it back onto the request
      if (!nullStack) {
        request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
      }
    } catch (ConfigurationException e) {
      // WW-2874 Only log error if in devMode
      if (devMode) {
        String reqStr = request.getRequestURI();
        if (request.getQueryString() != null) {
          reqStr = reqStr + "?" + request.getQueryString();
        }
        LOG.error("Could not find action or result\n" + reqStr, e);
      } else {
        if (LOG.isWarnEnabled()) {
          LOG.warn("Could not find action or result", e);
        }
      }
      sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
    } catch (Exception e) {
      if (handleException || devMode) {
        sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
      } else {
        throw new ServletException(e);
      }
    } finally {
      UtilTimerStack.pop(timerKey);
    }
  }