public void testGenericProperties() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    String[] value = new String[] {"123.12", "123.45"};
    stack.setValue("doubles", value);
    assertEquals(2, gb.getDoubles().size());
    assertEquals(Double.class, gb.getDoubles().get(0).getClass());
    assertEquals(new Double(123.12), gb.getDoubles().get(0));
    assertEquals(new Double(123.45), gb.getDoubles().get(1));
  }
  // FIXME: Implement nested Generics such as: List of Generics List, Map of Generic keys/values,
  // etc...
  public void no_testGenericPropertiesWithNestedGenerics() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    stack.setValue("extendedMap[123.12]", new String[] {"1", "2", "3", "4"});
    stack.setValue("extendedMap[456.12]", new String[] {"5", "6", "7", "8", "9"});

    System.out.println("gb.getExtendedMap(): " + gb.getExtendedMap());

    assertEquals(2, gb.getExtendedMap().size());
    System.out.println(stack.findValue("extendedMap"));
    assertEquals(4, stack.findValue("extendedMap.get(123.12).size"));
    assertEquals(5, stack.findValue("extendedMap.get(456.12).size"));

    assertEquals("1", stack.findValue("extendedMap.get(123.12).get(0)"));
    assertEquals("5", stack.findValue("extendedMap.get(456.12).get(0)"));
    assertEquals(Integer.class, stack.findValue("extendedMap.get(123.12).get(0).class"));
    assertEquals(Integer.class, stack.findValue("extendedMap.get(456.12).get(0).class"));

    assertEquals(List.class, stack.findValue("extendedMap.get(123.12).class"));
    assertEquals(List.class, stack.findValue("extendedMap.get(456.12).class"));
  }
  /**
   * @param invocation
   * @param dataName
   * @param data
   */
  protected void prepareImportData(
      ActionInvocation invocation, String dataName, List<Map<String, String>> data) {

    ValueStack stack = invocation.getStack();

    // constructor all the parameters from csv data
    for (int i = 0; i < data.size(); i++) {

      Map<String, String> line = (Map<String, String>) data.get(i);

      for (Map.Entry<String, String> entry : line.entrySet()) {
        String key = dataName + "[" + i + "]." + entry.getKey();
        stack.setValue(key, entry.getValue());
      }
    }
  }
  // TODO: Fixme... This test does not work with GenericsObjectDeterminer!
  public void testStringToCollectionConversion() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    AnnotationUser user = new AnnotationUser();
    stack.push(user);

    stack.setValue("list", "asdf");
    assertNotNull(user.getList());
    assertEquals(1, user.getList().size());
    assertEquals(String.class, user.getList().get(0).getClass());
    assertEquals("asdf", user.getList().get(0));
  }
  public void testGenericPropertiesFromSetter() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    stack.setValue("genericMap[123.12]", "66");
    stack.setValue("genericMap[456.12]", "42");

    assertEquals(2, gb.getGenericMap().size());
    assertEquals("66", stack.findValue("genericMap.get(123.12).toString()"));
    assertEquals("42", stack.findValue("genericMap.get(456.12).toString()"));
    assertEquals(66, stack.findValue("genericMap.get(123.12)"));
    assertEquals(42, stack.findValue("genericMap.get(456.12)"));
    assertEquals(true, stack.findValue("genericMap.containsValue(66)"));
    assertEquals(true, stack.findValue("genericMap.containsValue(42)"));
    assertEquals(true, stack.findValue("genericMap.containsKey(123.12)"));
    assertEquals(true, stack.findValue("genericMap.containsKey(456.12)"));
  }
Beispiel #6
0
  /** Test that type conversion is performed on indexed collection properties. */
  public void testSetIndexedValue() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    User user = new User();
    stack.push(user);

    // indexed string w/ existing array
    user.setList(new ArrayList<String>());
    user.getList().add("");

    String[] foo = new String[] {"asdf"};
    stack.setValue("list[0]", foo);
    assertNotNull(user.getList());
    assertEquals(1, user.getList().size());
    assertEquals(String.class, user.getList().get(0).getClass());
    assertEquals("asdf", user.getList().get(0));
  }
  @Override
  public String intercept(ActionInvocation invocation) throws Exception {

    if (log.isDebugEnabled()) {
      log.debug("*******************************************************************************");
    }

    HttpServletRequest request =
        (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);

    List<Article> articles = generateArticles(request);
    log.info("共有" + articles.size() + "个图文消息");

    int i = 0;
    for (Iterator<Article> it = articles.iterator(); it.hasNext(); ) {
      log.info("********图文消息" + i + "********");
      Article temp = it.next();
      if (temp != null) {
        log.info("缩略图:" + temp.getThumb_media_id());
        log.info("标题:" + temp.getTitle());
        log.info("作者:" + temp.getAuthor());
        log.info("阅读原文URL:" + temp.getContent_source_url());
        log.info("备注:" + temp.getDigest());
        log.info("是否显示封面图:" + temp.getShow_cover_pic());
        log.info("内容:" + temp.getContent());
      }
      i++;
    }

    if (log.isDebugEnabled()) {
      log.info("*******************************************************************************");
    }

    ValueStack vs = invocation.getStack();
    vs.setValue("articles", articles);

    String result = invocation.invoke();
    log.info(result);
    return result;
  }
Beispiel #8
0
  /** XW-281 */
  public void testSetBigIndexedValue() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.FALSE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    User user = new User();
    stack.push(user);

    // indexed string w/ existing array
    user.setList(new ArrayList());

    String[] foo = new String[] {"asdf"};
    ((OgnlValueStack) stack).setDevMode("true");
    try {
      stack.setValue("list.1114778947765", foo);
      fail("non-valid expression: list.1114778947765");
    } catch (RuntimeException ex) {; // it's oke
    }

    try {
      stack.setValue("1114778947765", foo);
      fail("non-valid expression: 1114778947765");
    } catch (RuntimeException ex) {;
    }

    try {
      stack.setValue("1234", foo);
      fail("non-valid expression: 1114778947765");
    } catch (RuntimeException ex) {;
    }

    ((OgnlValueStack) stack).setDevMode("false");
    stack.setValue("list.1114778947765", foo);
    stack.setValue("1114778947765", foo);
    stack.setValue("1234", foo);
  }
Beispiel #9
0
  @Override
  public String intercept(ActionInvocation invocation) throws Exception {

    ActionConfig config = invocation.getProxy().getConfig();
    ActionContext ac = invocation.getInvocationContext();
    Object action = invocation.getAction();

    // get the action's parameters
    final Map<String, String> parameters = config.getParams();

    if (parameters.containsKey(aliasesKey)) {

      String aliasExpression = parameters.get(aliasesKey);
      ValueStack stack = ac.getValueStack();
      Object obj = stack.findValue(aliasExpression);

      if (obj != null && obj instanceof Map) {
        // get secure stack
        ValueStack newStack = valueStackFactory.createValueStack(stack);
        boolean clearableStack = newStack instanceof ClearableValueStack;
        if (clearableStack) {
          // if the stack's context can be cleared, do that to prevent OGNL
          // from having access to objects in the stack, see XW-641
          ((ClearableValueStack) newStack).clearContextValues();
          Map<String, Object> context = newStack.getContext();
          ReflectionContextState.setCreatingNullObjects(context, true);
          ReflectionContextState.setDenyMethodExecution(context, true);
          ReflectionContextState.setReportingConversionErrors(context, true);

          // keep locale from original context
          context.put(ActionContext.LOCALE, stack.getContext().get(ActionContext.LOCALE));
        }

        // override
        Map aliases = (Map) obj;
        for (Object o : aliases.entrySet()) {
          Map.Entry entry = (Map.Entry) o;
          String name = entry.getKey().toString();
          String alias = (String) entry.getValue();
          Object value = stack.findValue(name);
          if (null == value) {
            // workaround
            Map<String, Object> contextParameters = ActionContext.getContext().getParameters();

            if (null != contextParameters) {
              value = contextParameters.get(name);
            }
          }
          if (null != value) {
            try {
              newStack.setValue(alias, value);
            } catch (RuntimeException e) {
              if (devMode) {
                String developerNotification =
                    LocalizedTextUtil.findText(
                        ParametersInterceptor.class,
                        "devmode.notification",
                        ActionContext.getContext().getLocale(),
                        "Developer Notification:\n{0}",
                        new Object[] {
                          "Unexpected Exception caught setting '"
                              + entry.getKey()
                              + "' on '"
                              + action.getClass()
                              + ": "
                              + e.getMessage()
                        });
                LOG.error(developerNotification);
                if (action instanceof ValidationAware) {
                  ((ValidationAware) action).addActionMessage(developerNotification);
                }
              }
            }
          }
        }

        if (clearableStack && (stack.getContext() != null) && (newStack.getContext() != null))
          stack
              .getContext()
              .put(
                  ActionContext.CONVERSION_ERRORS,
                  newStack.getContext().get(ActionContext.CONVERSION_ERRORS));
      } else {
        LOG.debug("invalid alias expression:" + aliasesKey);
      }
    }

    return invocation.invoke();
  }
  protected void setParameters(
      Object action, ValueStack stack, final Map<String, Object> parameters) {
    ParameterNameAware parameterNameAware =
        (action instanceof ParameterNameAware) ? (ParameterNameAware) action : null;

    Map<String, Object> params;
    Map<String, Object> acceptableParameters;
    if (ordered) {
      params = new TreeMap<String, Object>(getOrderedComparator());
      acceptableParameters = new TreeMap<String, Object>(getOrderedComparator());
      params.putAll(parameters);
    } else {
      params = new TreeMap<String, Object>(parameters);
      acceptableParameters = new TreeMap<String, Object>();
    }

    for (Map.Entry<String, Object> entry : params.entrySet()) {
      String name = entry.getKey();

      boolean acceptableName =
          acceptableName(name)
              && (parameterNameAware == null || parameterNameAware.acceptableParameterName(name));

      if (acceptableName) {
        acceptableParameters.put(name, entry.getValue());
      }
    }

    ValueStack newStack = valueStackFactory.createValueStack(stack);
    boolean clearableStack = newStack instanceof ClearableValueStack;
    if (clearableStack) {
      // if the stack's context can be cleared, do that to prevent OGNL
      // from having access to objects in the stack, see XW-641
      ((ClearableValueStack) newStack).clearContextValues();
      Map<String, Object> context = newStack.getContext();
      ReflectionContextState.setCreatingNullObjects(context, true);
      ReflectionContextState.setDenyMethodExecution(context, true);
      ReflectionContextState.setReportingConversionErrors(context, true);

      // keep locale from original context
      context.put(ActionContext.LOCALE, stack.getContext().get(ActionContext.LOCALE));
    }

    boolean memberAccessStack = newStack instanceof MemberAccessValueStack;
    if (memberAccessStack) {
      // block or allow access to properties
      // see WW-2761 for more details
      MemberAccessValueStack accessValueStack = (MemberAccessValueStack) newStack;
      accessValueStack.setAcceptProperties(acceptParams);
      accessValueStack.setExcludeProperties(excludeParams);
    }

    for (Map.Entry<String, Object> entry : acceptableParameters.entrySet()) {
      String name = entry.getKey();
      Object value = entry.getValue();
      try {
        newStack.setValue(name, value);
      } catch (RuntimeException e) {
        if (devMode) {
          String developerNotification =
              LocalizedTextUtil.findText(
                  ParametersInterceptor.class,
                  "devmode.notification",
                  ActionContext.getContext().getLocale(),
                  "Developer Notification:\n{0}",
                  new Object[] {
                    "Unexpected Exception caught setting '"
                        + name
                        + "' on '"
                        + action.getClass()
                        + ": "
                        + e.getMessage()
                  });
          LOG.error(developerNotification);
          if (action instanceof ValidationAware) {
            ((ValidationAware) action).addActionMessage(developerNotification);
          }
        }
      }
    }

    if (clearableStack && (stack.getContext() != null) && (newStack.getContext() != null))
      stack
          .getContext()
          .put(
              ActionContext.CONVERSION_ERRORS,
              newStack.getContext().get(ActionContext.CONVERSION_ERRORS));

    addParametersToContext(ActionContext.getContext(), acceptableParameters);
  }