⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 requestutils.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     */
    public static String actionURL(
            HttpServletRequest request,
            ActionConfig action,
            String pattern) {

        StringBuffer sb = new StringBuffer();
        if (pattern.endsWith("/*")) {
            sb.append(pattern.substring(0, pattern.length() - 2));
            sb.append(action.getPath());

        } else if (pattern.startsWith("*.")) {
            ModuleConfig appConfig =
                    ModuleUtils.getInstance().getModuleConfig(request);
            sb.append(appConfig.getPrefix());
            sb.append(action.getPath());
            sb.append(pattern.substring(1));

        } else {
            throw new IllegalArgumentException(pattern);
        }

        return sb.toString();

    }
  /**
     * <p>Return the context-relative URL that corresponds to the specified
     * <code>ForwardConfig</code>. The URL is calculated based on the properties
     * of the {@link ForwardConfig} instance as follows:</p>
     * <ul>
     * <li>If the <code>contextRelative</code> property is set, it is
     *     assumed that the <code>path</code> property contains a path
     *     that is already context-relative:
     *     <ul>
     *     <li>If the <code>path</code> property value starts with a slash,
     *         it is returned unmodified.</li>
     *     <li>If the <code>path</code> property value does not start
     *         with a slash, a slash is prepended.</li>
     *     </ul></li>
     * <li>Acquire the <code>forwardPattern</code> property from the
     *     <code>ControllerConfig</code> for the application module used
     *     to process this request. If no pattern was configured, default
     *     to a pattern of <code>$M$P</code>, which is compatible with the
     *     hard-coded mapping behavior in Struts 1.0.</li>
     * <li>Process the acquired <code>forwardPattern</code>, performing the
     *     following substitutions:
     *     <ul>
     *     <li><strong>$M</strong> - Replaced by the module prefix for the
     *         application module processing this request.</li>
     *     <li><strong>$P</strong> - Replaced by the <code>path</code>
     *         property of the specified {@link ForwardConfig}, prepended
     *         with a slash if it does not start with one.</li>
     *     <li><strong>$$</strong> - Replaced by a single dollar sign
     *         character.</li>
     *     <li><strong>$x</strong> (where "x" is any charater not listed
     *         above) - Silently omit these two characters from the result
     *         value.  (This has the side effect of causing all other
     *         $+letter combinations to be reserved.)</li>
     *     </ul></li>
     * </ul>
     *
     * @param request The servlet request we are processing
     * @param forward ForwardConfig to be evaluated
     *
     * @return context-relative URL
     * @since Struts 1.1
     */
    public static String forwardURL(HttpServletRequest request, ForwardConfig forward) {
         return forwardURL(request,forward,null);
    }

	/**
	 * <p>Return the context-relative URL that corresponds to the specified
	 * <code>ForwardConfig</code>. The URL is calculated based on the properties
	 * of the {@link ForwardConfig} instance as follows:</p>
	 * <ul>
	 * <li>If the <code>contextRelative</code> property is set, it is
	 *     assumed that the <code>path</code> property contains a path
	 *     that is already context-relative:
	 *     <ul>
	 *     <li>If the <code>path</code> property value starts with a slash,
	 *         it is returned unmodified.</li>
	 *     <li>If the <code>path</code> property value does not start
	 *         with a slash, a slash is prepended.</li>
	 *     </ul></li>
	 * <li>Acquire the <code>forwardPattern</code> property from the
	 *     <code>ControllerConfig</code> for the application module used
	 *     to process this request. If no pattern was configured, default
	 *     to a pattern of <code>$M$P</code>, which is compatible with the
	 *     hard-coded mapping behavior in Struts 1.0.</li>
	 * <li>Process the acquired <code>forwardPattern</code>, performing the
	 *     following substitutions:
	 *     <ul>
	 *     <li><strong>$M</strong> - Replaced by the module prefix for the
	 *         application module processing this request.</li>
	 *     <li><strong>$P</strong> - Replaced by the <code>path</code>
	 *         property of the specified {@link ForwardConfig}, prepended
	 *         with a slash if it does not start with one.</li>
	 *     <li><strong>$$</strong> - Replaced by a single dollar sign
	 *         character.</li>
	 *     <li><strong>$x</strong> (where "x" is any charater not listed
	 *         above) - Silently omit these two characters from the result
	 *         value.  (This has the side effect of causing all other
	 *         $+letter combinations to be reserved.)</li>
	 *     </ul></li>
	 * </ul>
	 *
	 * @param request The servlet request we are processing
	 * @param forward ForwardConfig to be evaluated
   * @param moduleConfig Base forward on this module config.
	 *
	 * @return context-relative URL
	 * @since Struts 1.2
	 */
	public static String forwardURL(HttpServletRequest request, ForwardConfig forward, ModuleConfig moduleConfig) {
		//load the current moduleConfig, if null
		if(moduleConfig == null) {
			moduleConfig = ModuleUtils.getInstance().getModuleConfig(request);
		}
		      
		String path = forward.getPath();
		//load default prefix
		String prefix = moduleConfig.getPrefix();
		
		//override prefix if supplied by forward
		if(forward.getModule() != null) {
			prefix = forward.getModule();
		}

		// Handle a ForwardConfig marked as context relative
		StringBuffer sb = new StringBuffer();
		if (forward.getContextRelative()) {
			if (!path.startsWith("/")) {
				sb.append("/");
			}
			sb.append(path);
			return (sb.toString());
		}

		// Calculate a context relative path for this ForwardConfig
		String forwardPattern = moduleConfig.getControllerConfig().getForwardPattern();
		if (forwardPattern == null) {
			// Performance optimization for previous default behavior
			sb.append(prefix);
			// smoothly insert a '/' if needed
			if (!path.startsWith("/")) {
				sb.append("/");
			}
			sb.append(path);

		} else {
			boolean dollar = false;
			for (int i = 0; i < forwardPattern.length(); i++) {
				char ch = forwardPattern.charAt(i);
				if (dollar) {
					switch (ch) {
						case 'M':
							sb.append(prefix);
							break;
						case 'P':
							// add '/' if needed
							if (!path.startsWith("/")) {
								sb.append("/");
							}
							sb.append(path);
							break;
						case '$':
							sb.append('$');
							break;
						default :
							; // Silently swallow
					}
					dollar = false;
					continue;
				} else if (ch == '$') {
					dollar = true;
				} else {
					sb.append(ch);
				}
			}
		}

		return (sb.toString());

	}


    /**
     * <p>Return the URL representing the current request. This is equivalent
     * to <code>HttpServletRequest.getRequestURL</code> in Servlet 2.3.</p>
     *
     * @param request The servlet request we are processing

     * @return URL representing the current request
     * @exception MalformedURLException if a URL cannot be created
     */
    public static URL requestURL(HttpServletRequest request) throws MalformedURLException {

        StringBuffer url = requestToServerUriStringBuffer(request);
        return (new URL(url.toString()));

    }


    /**
     * <p>Return the URL representing the scheme, server, and port number of
     * the current request. Server-relative URLs can be created by simply
     * appending the server-relative path (starting with '/') to this.</p>
     *
     * @param request The servlet request we are processing
     *
     * @return URL representing the scheme, server, and port number of
     *     the current request
     * @exception MalformedURLException if a URL cannot be created
     */
    public static URL serverURL(HttpServletRequest request) throws MalformedURLException {

        StringBuffer url = requestToServerStringBuffer(request);
        return (new URL(url.toString()));

    }


    /**
     * <p>Return the string representing the scheme, server, and port number of
     * the current request. Server-relative URLs can be created by simply
     * appending the server-relative path (starting with '/') to this.</p>
     *
     * @param request The servlet request we are processing

     * @return URL representing the scheme, server, and port number of
     *     the current request
     * @since Struts 1.2.0
     */
    public static StringBuffer requestToServerUriStringBuffer(HttpServletRequest request) {

        StringBuffer serverUri = createServerUriStringBuffer(request.getScheme(),request.getServerName(),
        request.getServerPort(),request.getRequestURI());
        return serverUri;

    }

    /**
     * <p>Return <code>StringBuffer</code> representing the scheme, server, and port number of
     * the current request. Server-relative URLs can be created by simply
     * appending the server-relative path (starting with '/') to this.</p>
     *
     * @param request The servlet request we are processing
     *
     * @return URL representing the scheme, server, and port number of
     *     the current request
     * @since Struts 1.2.0
     */
    public static StringBuffer requestToServerStringBuffer(HttpServletRequest request) {

        return createServerStringBuffer(request.getScheme(),request.getServerName(),request.getServerPort());

    }


    /**
     * <p>Return <code>StringBuffer</code> representing the scheme, server, and port number of
     * the current request.</p>
     *
     * @param scheme The scheme name to use
     * @param server The server name to use
     * @param port The port value to use
     *
     * @return StringBuffer in the form scheme: server: port
     * @since Struts 1.2.0
     */
    public static StringBuffer createServerStringBuffer(String scheme,String server,int port) {

        StringBuffer url = new StringBuffer();
        if (port < 0) {
            port = 80; // Work around java.net.URL bug
        }
        url.append(scheme);
        url.append("://");
        url.append(server);
        if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
            url.append(':');
            url.append(port);
        }
        return url;

    }


    /**
     * <p>Return <code>StringBuffer</code> representing the scheme, server, and port number of
     * the current request.</p>
     *
     * @param scheme The scheme name to use
     * @param server The server name to use
     * @param port The port value to use
     * @param uri The uri value to use
     *
     * @return StringBuffer in the form scheme: server: port
     * @since Struts 1.2.0
     */
    public static StringBuffer createServerUriStringBuffer(String scheme,String server,int port,String uri) {

        StringBuffer serverUri = createServerStringBuffer(scheme,server,port);
        serverUri.append(uri);
        return serverUri;

    }


    // ------------------------------------- Deprecated in favor of ModuleUtils



    /**
     * <p>Select the module to which the specified request belongs, and
     * add corresponding request attributes to this request.</p>
     *
     * @param prefix The module prefix of the desired module
     * @param request The servlet request we are processing
     * @param context The ServletContext for this web application
     *
     * @since Struts 1.1
     * @deprecated Use {@link org.apache.struts.util.ModuleUtils#selectModule(String,HttpServletRequest,ServletContext)} instead.
     * This will be removed after Struts 1.2.
     */
    public static void selectModule(
            String prefix,
            HttpServletRequest request,
            ServletContext context) {
        // :TODO: Remove after Struts 1.2

        ModuleUtils.getInstance().selectModule(prefix, request, context);

    }


    /**
     * <p>Select the module to which the specified request belongs, and

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -