コード例 #1
0
  /**
   * 判断是否合法的提交操作
   *
   * @param tokenName 为页面对应的name
   * @return
   */
  public boolean validToken(String tokenName) {
    String sessToken = (String) session.get(tokenName);
    String tokenValue = null;
    if (TOKEN_ID.equals(tokenName)) {
      tokenValue = tokenid;
    } else if (EX_TOKEN.equals(tokenName)) {
      tokenValue = extoken;
    } else {
      LOG.warn("the tokenName is not required ");
    }

    // 解决重启服务器,刷新报空指针问题
    if (tokenValue == null) {
      LOG.warn("the tokenValue  is null.");
      return false;
    }

    if (!tokenValue.equals(sessToken)) {
      LOG.warn("Form" + tokenName + "does not match the session" + tokenName + ".");
      return false;
    }
    // remove the token so it won't be used again
    session.remove(tokenName);
    return true;
  }
コード例 #2
0
  /**
   * Get a message from the first TextProvider encountered in the stack. If the first TextProvider
   * doesn't provide the message the default message is returned.
   *
   * <p>The search for a TextProvider is iterative from the root of the stack.
   *
   * <p>This method was refactored from {@link org.apache.struts2.components.Text} to use a
   * consistent implementation across UIBean components.
   *
   * @param key the message key in the resource bundle
   * @param defaultMessage the message to return if not found (evaluated for OGNL)
   * @param args an array args to be used in a {@link java.text.MessageFormat} message
   * @param stack the value stack to use for finding the text
   * @param searchStack search stack for the key
   * @return the message if found, otherwise the defaultMessage
   */
  public static String getText(
      String key, String defaultMessage, List<Object> args, ValueStack stack, boolean searchStack) {
    String msg = null;
    TextProvider tp = null;

    for (Object o : stack.getRoot()) {
      if (o instanceof TextProvider) {
        tp = (TextProvider) o;
        msg = tp.getText(key, null, args, stack);

        break;
      }
    }

    if (msg == null) {
      // evaluate the defaultMesage as an OGNL expression
      if (searchStack) msg = stack.findString(defaultMessage);

      if (msg == null) {
        // use the defaultMessage literal value
        msg = defaultMessage;
      }

      if (LOG.isWarnEnabled()) {
        if (tp != null) {
          LOG.warn(
              "The first TextProvider in the ValueStack ("
                  + tp.getClass().getName()
                  + ") could not locate the message resource with key '"
                  + key
                  + "'");
        } else {
          LOG.warn(
              "Could not locate the message resource '"
                  + key
                  + "' as there is no TextProvider in the ValueStack.");
        }
        if (defaultMessage.equals(msg)) {
          LOG.warn(
              "The default value expression '"
                  + defaultMessage
                  + "' was evaluated and did not match a property.  The literal value '"
                  + defaultMessage
                  + "' will be used.");
        } else {
          LOG.warn(
              "The default value expression '" + defaultMessage + "' evaluated to '" + msg + "'");
        }
      }
    }
    return msg;
  }
コード例 #3
0
ファイル: Include.java プロジェクト: andrea-riboldi/GAF_2.0
  public boolean end(Writer writer, String body) {
    String page =
        findString(value, "value", "You must specify the URL to include. Example: /foo.jsp");
    StringBuilder urlBuf = new StringBuilder();

    // Add URL
    urlBuf.append(page);

    // Add request parameters
    if (parameters.size() > 0) {
      urlBuf.append('?');

      String concat = "";

      // Set parameters
      Iterator iter = parameters.entrySet().iterator();

      while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        Object name = entry.getKey();
        List values = (List) entry.getValue();

        for (int i = 0; i < values.size(); i++) {
          urlBuf.append(concat);
          urlBuf.append(name);
          urlBuf.append('=');

          try {
            urlBuf.append(URLEncoder.encode(values.get(i).toString(), "UTF-8"));
          } catch (Exception e) {
            LOG.warn("unable to url-encode " + values.get(i).toString() + ", it will be ignored");
          }

          concat = "&";
        }
      }
    }

    String result = urlBuf.toString();

    // Include
    try {
      include(result, writer, req, res);
    } catch (Exception e) {
      LOG.warn("Exception thrown during include of " + result, e);
    }

    return super.end(writer, body);
  }
コード例 #4
0
 /** Cleans up a request of thread locals */
 public void cleanupRequest(HttpServletRequest request) {
   Integer counterVal = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
   if (counterVal != null) {
     counterVal -= 1;
     request.setAttribute(CLEANUP_RECURSION_COUNTER, counterVal);
     if (counterVal > 0) {
       if (log.isDebugEnabled()) {
         log.debug("skipping cleanup counter=" + counterVal);
       }
       return;
     }
   }
   // always clean up the thread request, even if an action hasn't been executed
   try {
     dispatcher.cleanUpRequest(request);
   } catch (IOException e) {
     if (LOG.isWarnEnabled()) {
       LOG.warn(
           "Cannot clean up the request, some files can still remain in #0 after upload!",
           e,
           StrutsConstants.STRUTS_MULTIPART_SAVEDIR);
     }
   } finally {
     ActionContext.setContext(null);
     Dispatcher.setInstance(null);
   }
 }
コード例 #5
0
ファイル: Dispatcher.java プロジェクト: faithyee/Struts2
  /** Releases all instances bound to this dispatcher instance. */
  public void cleanup() {

    // clean up ObjectFactory
    ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class);
    if (objectFactory == null) {
      if (LOG.isWarnEnabled()) {
        LOG.warn(
            "Object Factory is null, something is seriously wrong, no clean up will be performed");
      }
    }
    if (objectFactory instanceof ObjectFactoryDestroyable) {
      try {
        ((ObjectFactoryDestroyable) objectFactory).destroy();
      } catch (Exception e) {
        // catch any exception that may occurred during destroy() and log it
        LOG.error(
            "exception occurred while destroying ObjectFactory [#0]", e, objectFactory.toString());
      }
    }

    // clean up Dispatcher itself for this thread
    instance.set(null);

    // clean up DispatcherListeners
    if (!dispatcherListeners.isEmpty()) {
      for (DispatcherListener l : dispatcherListeners) {
        l.dispatcherDestroyed(this);
      }
    }

    // clean up all interceptors by calling their destroy() method
    Set<Interceptor> interceptors = new HashSet<Interceptor>();
    Collection<PackageConfig> packageConfigs =
        configurationManager.getConfiguration().getPackageConfigs().values();
    for (PackageConfig packageConfig : packageConfigs) {
      for (Object config : packageConfig.getAllInterceptorConfigs().values()) {
        if (config instanceof InterceptorStackConfig) {
          for (InterceptorMapping interceptorMapping :
              ((InterceptorStackConfig) config).getInterceptors()) {
            interceptors.add(interceptorMapping.getInterceptor());
          }
        }
      }
    }
    for (Interceptor interceptor : interceptors) {
      interceptor.destroy();
    }

    // Clear container holder when application is unloaded / server shutdown
    ContainerHolder.clear();

    // cleanup action context
    ActionContext.setContext(null);

    // clean up configuration
    configurationManager.destroyConfiguration();
    configurationManager = null;
  }
コード例 #6
0
  protected void process(
      InputStream is, String path, HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    if (is != null) {
      Calendar cal = Calendar.getInstance();

      // check for if-modified-since, prior to any other headers
      long ifModifiedSince = 0;
      try {
        ifModifiedSince = request.getDateHeader("If-Modified-Since");
      } catch (Exception e) {
        log.warn(
            "Invalid If-Modified-Since header value: '"
                + request.getHeader("If-Modified-Since")
                + "', ignoring");
      }
      long lastModifiedMillis = lastModifiedCal.getTimeInMillis();
      long now = cal.getTimeInMillis();
      cal.add(Calendar.DAY_OF_MONTH, 1);
      long expires = cal.getTimeInMillis();

      if (ifModifiedSince > 0 && ifModifiedSince <= lastModifiedMillis) {
        // not modified, content is not sent - only basic
        // headers and status SC_NOT_MODIFIED
        response.setDateHeader("Expires", expires);
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        is.close();
        return;
      }

      // set the content-type header
      String contentType = getContentType(path);
      if (contentType != null) {
        response.setContentType(contentType);
      }

      if (serveStaticBrowserCache) {
        // set heading information for caching static content
        response.setDateHeader("Date", now);
        response.setDateHeader("Expires", expires);
        response.setDateHeader("Retry-After", expires);
        response.setHeader("Cache-Control", "public");
        response.setDateHeader("Last-Modified", lastModifiedMillis);
      } else {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");
      }

      try {
        copy(is, response.getOutputStream());
      } finally {
        is.close();
      }
      return;
    }
  }
コード例 #7
0
 /**
  * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
  * multipart classes (see class description).
  *
  * @param saveDir the directory to save off the file
  * @param request the request containing the multipart
  * @throws java.io.IOException is thrown if encoding fails.
  */
 public void parse(HttpServletRequest request, String saveDir) throws IOException {
   try {
     processUpload(request, saveDir);
   } catch (FileUploadException e) {
     if (LOG.isWarnEnabled()) {
       LOG.warn("Unable to parse request", e);
     }
     errors.add(e.getMessage());
   }
 }
コード例 #8
0
ファイル: UIBean.java プロジェクト: angelowolf/SEW
 protected void enableAncestorFormCustomOnsubmit() {
   Form form = (Form) findAncestor(Form.class);
   if (form != null) {
     form.addParameter("customOnsubmitEnabled", Boolean.TRUE);
   } else {
     if (LOG.isWarnEnabled()) {
       LOG.warn("Cannot find an Ancestor form, custom onsubmit is NOT enabled");
     }
   }
 }
コード例 #9
0
  public int doStartTag() throws JspException {

    // value
    Object value = findValue(valueAttr);

    // separator
    String separator = DEFAULT_SEPARATOR;
    if (separatorAttr != null && separatorAttr.length() > 0) {
      separator = findString(separatorAttr);
    }

    // TODO: maybe this could be put into an Util class, or there is already one?
    // count
    int count = 0;
    if (countAttr != null && countAttr.length() > 0) {
      Object countObj = findValue(countAttr);
      if (countObj instanceof Number) {
        count = ((Number) countObj).intValue();
      } else if (countObj instanceof String) {
        try {
          count = Integer.parseInt((String) countObj);
        } catch (NumberFormatException e) {
          LOG.warn(
              "unable to convert count attribute ["
                  + countObj
                  + "] to number, ignore count attribute",
              e);
        }
      }
    }

    // converter
    Converter converter = null;
    if (converterAttr != null && converterAttr.length() > 0) {
      converter = (Converter) findValue(converterAttr);
    }

    iteratorGenerator = new IteratorGenerator();
    iteratorGenerator.setValues(value);
    iteratorGenerator.setCount(count);
    iteratorGenerator.setSeparator(separator);
    iteratorGenerator.setConverter(converter);

    iteratorGenerator.execute();

    // Push resulting iterator on stack and put into
    // stack context if we have a "var" specified.
    getStack().push(iteratorGenerator);
    if (var != null && var.length() > 0) {
      getStack().getContext().put(var, iteratorGenerator);
    }

    return EVAL_BODY_INCLUDE;
  }
コード例 #10
0
ファイル: JSONWriter.java プロジェクト: freiby/nirvana
  /** Add map to buffer */
  protected void map(Map map, Method method) throws JSONException {
    this.add("{");

    Iterator it = map.entrySet().iterator();

    boolean warnedNonString = false; // one report per map
    boolean hasData = false;
    while (it.hasNext()) {
      Map.Entry entry = (Map.Entry) it.next();
      if (excludeNullProperties && entry.getValue() == null) {
        continue;
      }

      Object key = entry.getKey();
      if (key == null) {
        LOG.error("Cannot build expression for null key in #0", exprStack);
        continue;
      }

      String expr = null;
      if (this.buildExpr) {
        expr = this.expandExpr(key.toString());
        if (this.shouldExcludeProperty(expr)) {
          continue;
        }
        expr = this.setExprStack(expr);
      }
      if (hasData) {
        this.add(',');
      }
      hasData = true;
      if (!warnedNonString && !(key instanceof String)) {
        if (LOG.isWarnEnabled()) {
          LOG.warn(
              "JavaScript doesn't support non-String keys, using toString() on #0",
              key.getClass().getName());
        }
        warnedNonString = true;
      }
      this.value(key.toString(), method);
      this.add(":");
      this.value(entry.getValue(), method);
      if (this.buildExpr) {
        this.setExprStack(expr);
      }
    }

    this.add("}");
  }
コード例 #11
0
ファイル: Dispatcher.java プロジェクト: faithyee/Struts2
  /**
   * Return the path to save uploaded files to (this is configurable).
   *
   * @return the path to save uploaded files to
   * @param servletContext Our ServletContext
   */
  private String getSaveDir(ServletContext servletContext) {
    String saveDir = multipartSaveDir.trim();

    if (saveDir.equals("")) {
      File tempdir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
      if (LOG.isInfoEnabled()) {
        LOG.info(
            "Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir");
      }

      if (tempdir != null) {
        saveDir = tempdir.toString();
        setMultipartSaveDir(saveDir);
      }
    } else {
      File multipartSaveDir = new File(saveDir);

      if (!multipartSaveDir.exists()) {
        if (!multipartSaveDir.mkdirs()) {
          String logMessage;
          try {
            logMessage =
                "Could not find create multipart save directory '"
                    + multipartSaveDir.getCanonicalPath()
                    + "'.";
          } catch (IOException e) {
            logMessage =
                "Could not find create multipart save directory '"
                    + multipartSaveDir.toString()
                    + "'.";
          }
          if (devMode) {
            LOG.error(logMessage);
          } else {
            if (LOG.isWarnEnabled()) {
              LOG.warn(logMessage);
            }
          }
        }
      }
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug("saveDir=" + saveDir);
    }

    return saveDir;
  }
コード例 #12
0
ファイル: UIBean.java プロジェクト: angelowolf/SEW
  protected Map getTooltipConfig(UIBean component) {
    Object tooltipConfigObj = component.getParameters().get("tooltipConfig");
    Map<String, String> tooltipConfig = new LinkedHashMap<String, String>();

    if (tooltipConfigObj instanceof Map) {
      // we get this if its configured using
      // 1] UI component's tooltipConfig attribute  OR
      // 2] <param name="tooltip" value="" /> param tag value attribute

      tooltipConfig = new LinkedHashMap<String, String>((Map) tooltipConfigObj);
    } else if (tooltipConfigObj instanceof String) {

      // we get this if its configured using
      // <param name="tooltipConfig"> ... </param> tag's body
      String tooltipConfigStr = (String) tooltipConfigObj;
      String[] tooltipConfigArray = tooltipConfigStr.split("\\|");

      for (String aTooltipConfigArray : tooltipConfigArray) {
        String[] configEntry = aTooltipConfigArray.trim().split("=");
        String key = configEntry[0].trim();
        String value;
        if (configEntry.length > 1) {
          value = configEntry[1].trim();
          tooltipConfig.put(key, value);
        } else {
          if (LOG.isWarnEnabled()) {
            LOG.warn(
                "component "
                    + component
                    + " tooltip config param "
                    + key
                    + " has no value defined, skipped");
          }
        }
      }
    }
    if (component.javascriptTooltip != null)
      tooltipConfig.put("jsTooltipEnabled", component.javascriptTooltip);
    if (component.tooltipIconPath != null)
      tooltipConfig.put("tooltipIcon", component.tooltipIconPath);
    if (component.tooltipDelay != null) tooltipConfig.put("tooltipDelay", component.tooltipDelay);
    return tooltipConfig;
  }
コード例 #13
0
ファイル: UIBean.java プロジェクト: angelowolf/SEW
  public void evaluateParams() {
    String templateDir = getTemplateDir();
    String theme = getTheme();

    addParameter("templateDir", templateDir);
    addParameter("theme", theme);
    addParameter("template", template != null ? findString(template) : getDefaultTemplate());
    addParameter("dynamicAttributes", dynamicAttributes);
    addParameter("themeExpansionToken", uiThemeExpansionToken);
    addParameter("expandTheme", uiThemeExpansionToken + theme);

    String name = null;
    String providedLabel = null;

    if (this.key != null) {

      if (this.name == null) {
        this.name = key;
      }

      if (this.label == null) {
        // lookup the label from a TextProvider (default value is the key)
        providedLabel = TextProviderHelper.getText(key, key, stack);
      }
    }

    if (this.name != null) {
      name = findString(this.name);
      addParameter("name", name);
    }

    if (label != null) {
      addParameter("label", findString(label));
    } else {
      if (providedLabel != null) {
        // label found via a TextProvider
        addParameter("label", providedLabel);
      }
    }

    if (labelSeparator != null) {
      addParameter("labelseparator", findString(labelSeparator));
    }

    if (labelPosition != null) {
      addParameter("labelposition", findString(labelPosition));
    }

    if (requiredPosition != null) {
      addParameter("requiredPosition", findString(requiredPosition));
    }

    if (errorPosition != null) {
      addParameter("errorposition", findString(errorPosition));
    }

    if (requiredLabel != null) {
      addParameter("required", findValue(requiredLabel, Boolean.class));
    }

    if (disabled != null) {
      addParameter("disabled", findValue(disabled, Boolean.class));
    }

    if (tabindex != null) {
      addParameter("tabindex", findString(tabindex));
    }

    if (onclick != null) {
      addParameter("onclick", findString(onclick));
    }

    if (ondblclick != null) {
      addParameter("ondblclick", findString(ondblclick));
    }

    if (onmousedown != null) {
      addParameter("onmousedown", findString(onmousedown));
    }

    if (onmouseup != null) {
      addParameter("onmouseup", findString(onmouseup));
    }

    if (onmouseover != null) {
      addParameter("onmouseover", findString(onmouseover));
    }

    if (onmousemove != null) {
      addParameter("onmousemove", findString(onmousemove));
    }

    if (onmouseout != null) {
      addParameter("onmouseout", findString(onmouseout));
    }

    if (onfocus != null) {
      addParameter("onfocus", findString(onfocus));
    }

    if (onblur != null) {
      addParameter("onblur", findString(onblur));
    }

    if (onkeypress != null) {
      addParameter("onkeypress", findString(onkeypress));
    }

    if (onkeydown != null) {
      addParameter("onkeydown", findString(onkeydown));
    }

    if (onkeyup != null) {
      addParameter("onkeyup", findString(onkeyup));
    }

    if (onselect != null) {
      addParameter("onselect", findString(onselect));
    }

    if (onchange != null) {
      addParameter("onchange", findString(onchange));
    }

    if (accesskey != null) {
      addParameter("accesskey", findString(accesskey));
    }

    if (cssClass != null) {
      addParameter("cssClass", findString(cssClass));
    }

    if (cssStyle != null) {
      addParameter("cssStyle", findString(cssStyle));
    }

    if (cssErrorClass != null) {
      addParameter("cssErrorClass", findString(cssErrorClass));
    }

    if (cssErrorStyle != null) {
      addParameter("cssErrorStyle", findString(cssErrorStyle));
    }

    if (title != null) {
      addParameter("title", findString(title));
    }

    // see if the value was specified as a parameter already
    if (parameters.containsKey("value")) {
      parameters.put("nameValue", parameters.get("value"));
    } else {
      if (evaluateNameValue()) {
        final Class valueClazz = getValueClassType();

        if (valueClazz != null) {
          if (value != null) {
            addParameter("nameValue", findValue(value, valueClazz));
          } else if (name != null) {
            String expr = completeExpressionIfAltSyntax(name);

            addParameter("nameValue", findValue(expr, valueClazz));
          }
        } else {
          if (value != null) {
            addParameter("nameValue", findValue(value));
          } else if (name != null) {
            addParameter("nameValue", findValue(name));
          }
        }
      }
    }

    final Form form = (Form) findAncestor(Form.class);

    // create HTML id element
    populateComponentHtmlId(form);

    if (form != null) {
      addParameter("form", form.getParameters());

      if (name != null) {
        // list should have been created by the form component
        List<String> tags = (List<String>) form.getParameters().get("tagNames");
        tags.add(name);
      }
    }

    // tooltip & tooltipConfig
    if (tooltipConfig != null) {
      addParameter("tooltipConfig", findValue(tooltipConfig));
    }
    if (tooltip != null) {
      addParameter("tooltip", findString(tooltip));

      Map tooltipConfigMap = getTooltipConfig(this);

      if (form != null) { // inform the containing form that we need tooltip javascript included
        form.addParameter("hasTooltip", Boolean.TRUE);

        // tooltipConfig defined in component itseilf will take precedence
        // over those defined in the containing form
        Map overallTooltipConfigMap = getTooltipConfig(form);
        overallTooltipConfigMap.putAll(tooltipConfigMap); // override parent form's tooltip config

        for (Object o : overallTooltipConfigMap.entrySet()) {
          Map.Entry entry = (Map.Entry) o;
          addParameter((String) entry.getKey(), entry.getValue());
        }
      } else {
        if (LOG.isWarnEnabled()) {
          LOG.warn(
              "No ancestor Form found, javascript based tooltip will not work, however standard HTML tooltip using alt and title attribute will still work ");
        }
      }

      // TODO: this is to keep backward compatibility, remove once when tooltipConfig is dropped
      String jsTooltipEnabled = (String) getParameters().get("jsTooltipEnabled");
      if (jsTooltipEnabled != null) this.javascriptTooltip = jsTooltipEnabled;

      // TODO: this is to keep backward compatibility, remove once when tooltipConfig is dropped
      String tooltipIcon = (String) getParameters().get("tooltipIcon");
      if (tooltipIcon != null) this.addParameter("tooltipIconPath", tooltipIcon);
      if (this.tooltipIconPath != null)
        this.addParameter("tooltipIconPath", findString(this.tooltipIconPath));

      // TODO: this is to keep backward compatibility, remove once when tooltipConfig is dropped
      String tooltipDelayParam = (String) getParameters().get("tooltipDelay");
      if (tooltipDelayParam != null) this.addParameter("tooltipDelay", tooltipDelayParam);
      if (this.tooltipDelay != null)
        this.addParameter("tooltipDelay", findString(this.tooltipDelay));

      if (this.javascriptTooltip != null) {
        Boolean jsTooltips = (Boolean) findValue(this.javascriptTooltip, Boolean.class);
        // TODO use a Boolean model when tooltipConfig is dropped
        this.addParameter("jsTooltipEnabled", jsTooltips.toString());

        if (form != null) form.addParameter("hasTooltip", jsTooltips);
        if (this.tooltipCssClass != null)
          this.addParameter("tooltipCssClass", findString(this.tooltipCssClass));
      }
    }

    evaluateExtraParams();
  }
コード例 #14
0
  public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext ac = invocation.getInvocationContext();

    HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);

    if (!(request instanceof MultiPartRequestWrapper)) {
      if (LOG.isDebugEnabled()) {
        ActionProxy proxy = invocation.getProxy();
        LOG.debug(
            getTextMessage(
                "struts.messages.bypass.request",
                new String[] {proxy.getNamespace(), proxy.getActionName()}));
      }

      return invocation.invoke();
    }

    ValidationAware validation = null;

    Object action = invocation.getAction();

    if (action instanceof ValidationAware) {
      validation = (ValidationAware) action;
    }

    MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;

    if (multiWrapper.hasErrors()) {
      for (String error : multiWrapper.getErrors()) {
        if (validation != null) {
          validation.addActionError(error);
        }

        if (LOG.isWarnEnabled()) {
          LOG.warn(error);
        }
      }
    }

    // bind allowed Files
    Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
    while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
      // get the value of this input tag
      String inputName = (String) fileParameterNames.nextElement();

      // get the content type
      String[] contentType = multiWrapper.getContentTypes(inputName);

      if (isNonEmpty(contentType)) {
        // get the name of the file from the input tag
        String[] fileName = multiWrapper.getFileNames(inputName);

        if (isNonEmpty(fileName)) {
          // get a File object for the uploaded File
          File[] files = multiWrapper.getFiles(inputName);
          if (files != null && files.length > 0) {
            List<File> acceptedFiles = new ArrayList<File>(files.length);
            List<String> acceptedContentTypes = new ArrayList<String>(files.length);
            List<String> acceptedFileNames = new ArrayList<String>(files.length);
            String contentTypeName = inputName + "ContentType";
            String fileNameName = inputName + "FileName";

            for (int index = 0; index < files.length; index++) {
              if (acceptFile(
                  action,
                  files[index],
                  fileName[index],
                  contentType[index],
                  inputName,
                  validation)) {
                acceptedFiles.add(files[index]);
                acceptedContentTypes.add(contentType[index]);
                acceptedFileNames.add(fileName[index]);
              }
            }

            if (!acceptedFiles.isEmpty()) {
              Map<String, Object> params = ac.getParameters();

              params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()]));
              params.put(
                  contentTypeName,
                  acceptedContentTypes.toArray(new String[acceptedContentTypes.size()]));
              params.put(
                  fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()]));
            }
          }
        } else {
          if (LOG.isWarnEnabled()) {
            LOG.warn(
                getTextMessage(action, "struts.messages.invalid.file", new String[] {inputName}));
          }
        }
      } else {
        if (LOG.isWarnEnabled()) {
          LOG.warn(
              getTextMessage(
                  action, "struts.messages.invalid.content.type", new String[] {inputName}));
        }
      }
    }

    // invoke action
    return invocation.invoke();
  }
コード例 #15
0
  /**
   * Override for added functionality. Checks if the proposed file is acceptable based on
   * contentType and size.
   *
   * @param action - uploading action for message retrieval.
   * @param file - proposed upload file.
   * @param contentType - contentType of the file.
   * @param inputName - inputName of the file.
   * @param validation - Non-null ValidationAware if the action implements ValidationAware, allowing
   *     for better logging.
   * @return true if the proposed file is acceptable by contentType and size.
   */
  protected boolean acceptFile(
      Object action,
      File file,
      String filename,
      String contentType,
      String inputName,
      ValidationAware validation) {
    boolean fileIsAcceptable = false;

    // If it's null the upload failed
    if (file == null) {
      String errMsg =
          getTextMessage(action, "struts.messages.error.uploading", new String[] {inputName});
      if (validation != null) {
        validation.addFieldError(inputName, errMsg);
      }

      if (LOG.isWarnEnabled()) {
        LOG.warn(errMsg);
      }
    } else if (maximumSize != null && maximumSize < file.length()) {
      String errMsg =
          getTextMessage(
              action,
              "struts.messages.error.file.too.large",
              new String[] {inputName, filename, file.getName(), "" + file.length()});
      if (validation != null) {
        validation.addFieldError(inputName, errMsg);
      }

      if (LOG.isWarnEnabled()) {
        LOG.warn(errMsg);
      }
    } else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
      String errMsg =
          getTextMessage(
              action,
              "struts.messages.error.content.type.not.allowed",
              new String[] {inputName, filename, file.getName(), contentType});
      if (validation != null) {
        validation.addFieldError(inputName, errMsg);
      }

      if (LOG.isWarnEnabled()) {
        LOG.warn(errMsg);
      }
    } else if ((!allowedExtensionsSet.isEmpty())
        && (!hasAllowedExtension(allowedExtensionsSet, filename))) {
      String errMsg =
          getTextMessage(
              action,
              "struts.messages.error.file.extension.not.allowed",
              new String[] {inputName, filename, file.getName(), contentType});
      if (validation != null) {
        validation.addFieldError(inputName, errMsg);
      }

      if (LOG.isWarnEnabled()) {
        LOG.warn(errMsg);
      }
    } else {
      fileIsAcceptable = true;
    }

    return fileIsAcceptable;
  }
コード例 #16
0
ファイル: Dispatcher.java プロジェクト: faithyee/Struts2
  /**
   * 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);
    }
  }
コード例 #17
0
  /*
   * (non-Javadoc)
   *
   * @see org.apache.struts2.dispatcher.mapper.ActionMapper#getMapping(javax.servlet.http.HttpServletRequest)
   */
  public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    if (!isSlashesInActionNames()) {
      throw new IllegalStateException(
          "This action mapper requires the setting 'slashesInActionNames' to be set to 'true'");
    }
    ActionMapping mapping = super.getMapping(request, configManager);

    if (mapping == null) {
      return null;
    }

    String actionName = mapping.getName();

    String id = null;

    // Only try something if the action name is specified
    if (actionName != null && actionName.length() > 0) {

      int lastSlashPos = actionName.lastIndexOf('/');
      if (lastSlashPos > -1) {
        id = actionName.substring(lastSlashPos + 1);
      }

      // If a method hasn't been explicitly named, try to guess using ReST-style patterns
      if (mapping.getMethod() == null) {

        if (lastSlashPos == actionName.length() - 1) {

          // Index e.g. foo/
          if (isGet(request)) {
            mapping.setMethod("index");

            // Creating a new entry on POST e.g. foo/
          } else if (isPost(request)) {
            mapping.setMethod("create");
          }

        } else if (lastSlashPos > -1) {
          // Viewing the form to create a new item e.g. foo/new
          if (isGet(request) && "new".equals(id)) {
            mapping.setMethod("editNew");

            // Viewing an item e.g. foo/1
          } else if (isGet(request)) {
            mapping.setMethod("view");

            // Removing an item e.g. foo/1
          } else if (isDelete(request)) {
            mapping.setMethod("remove");

            // Updating an item e.g. foo/1
          } else if (isPut(request)) {
            mapping.setMethod("update");
          }
        }

        if (idParameterName != null && lastSlashPos > -1) {
          actionName = actionName.substring(0, lastSlashPos);
        }
      }

      if (idParameterName != null && id != null) {
        if (mapping.getParams() == null) {
          mapping.setParams(new HashMap<String, Object>());
        }
        mapping.getParams().put(idParameterName, id);
      }

      // Try to determine parameters from the url before the action name
      int actionSlashPos = actionName.lastIndexOf('/', lastSlashPos - 1);
      if (actionSlashPos > 0 && actionSlashPos < lastSlashPos) {
        String params = actionName.substring(0, actionSlashPos);
        HashMap<String, String> parameters = new HashMap<String, String>();
        try {
          StringTokenizer st = new StringTokenizer(params, "/");
          boolean isNameTok = true;
          String paramName = null;
          String paramValue;

          while (st.hasMoreTokens()) {
            if (isNameTok) {
              paramName = URLDecoder.decode(st.nextToken(), "UTF-8");
              isNameTok = false;
            } else {
              paramValue = URLDecoder.decode(st.nextToken(), "UTF-8");

              if ((paramName != null) && (paramName.length() > 0)) {
                parameters.put(paramName, paramValue);
              }

              isNameTok = true;
            }
          }
          if (parameters.size() > 0) {
            if (mapping.getParams() == null) {
              mapping.setParams(new HashMap<String, Object>());
            }
            mapping.getParams().putAll(parameters);
          }
        } catch (Exception e) {
          if (LOG.isWarnEnabled()) {
            LOG.warn("Unable to determine parameters from the url", e);
          }
        }
        mapping.setName(actionName.substring(actionSlashPos + 1));
      }
    }

    return mapping;
  }