Java Code Examples for org.springframework.web.util.UriUtils#encodePath()

The following examples show how to use org.springframework.web.util.UriUtils#encodePath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncoderDecoderUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
private String encodePath(String path) {
    try {
        path = UriUtils.encodePath(path, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
    }
    return path;
}
 
Example 2
Source File: FormTag.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Resolve the value of the '{@code action}' attribute.
 * <p>If the user configured an '{@code action}' value then the result of
 * evaluating this value is used. If the user configured an
 * '{@code servletRelativeAction}' value then the value is prepended
 * with the context and servlet paths, and the result is used. Otherwise, the
 * {@link org.springframework.web.servlet.support.RequestContext#getRequestUri()
 * originating URI} is used.
 * @return the value that is to be used for the '{@code action}' attribute
 */
protected String resolveAction() throws JspException {
	String action = getAction();
	String servletRelativeAction = getServletRelativeAction();
	if (StringUtils.hasText(action)) {
		action = getDisplayString(evaluate(ACTION_ATTRIBUTE, action));
		return processAction(action);
	}
	else if (StringUtils.hasText(servletRelativeAction)) {
		String pathToServlet = getRequestContext().getPathToServlet();
		if (servletRelativeAction.startsWith("/") &&
				!servletRelativeAction.startsWith(getRequestContext().getContextPath())) {
			servletRelativeAction = pathToServlet + servletRelativeAction;
		}
		servletRelativeAction = getDisplayString(evaluate(ACTION_ATTRIBUTE, servletRelativeAction));
		return processAction(servletRelativeAction);
	}
	else {
		String requestUri = getRequestContext().getRequestUri();
		String encoding = this.pageContext.getResponse().getCharacterEncoding();
		try {
			requestUri = UriUtils.encodePath(requestUri, encoding);
		}
		catch (UnsupportedCharsetException ex) {
			// shouldn't happen - if it does, proceed with requestUri as-is
		}
		ServletResponse response = this.pageContext.getResponse();
		if (response instanceof HttpServletResponse) {
			requestUri = ((HttpServletResponse) response).encodeURL(requestUri);
			String queryString = getRequestContext().getQueryString();
			if (StringUtils.hasText(queryString)) {
				requestUri += "?" + HtmlUtils.htmlEscape(queryString);
			}
		}
		if (StringUtils.hasText(requestUri)) {
			return processAction(requestUri);
		}
		else {
			throw new IllegalArgumentException("Attribute 'action' is required. " +
					"Attempted to resolve against current request URI but request URI was null.");
		}
	}
}
 
Example 3
Source File: FormTag.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Resolve the value of the '{@code action}' attribute.
 * <p>If the user configured an '{@code action}' value then the result of
 * evaluating this value is used. If the user configured an
 * '{@code servletRelativeAction}' value then the value is prepended
 * with the context and servlet paths, and the result is used. Otherwise, the
 * {@link org.springframework.web.servlet.support.RequestContext#getRequestUri()
 * originating URI} is used.
 * @return the value that is to be used for the '{@code action}' attribute
 */
protected String resolveAction() throws JspException {
	String action = getAction();
	String servletRelativeAction = getServletRelativeAction();
	if (StringUtils.hasText(action)) {
		action = getDisplayString(evaluate(ACTION_ATTRIBUTE, action));
		return processAction(action);
	}
	else if (StringUtils.hasText(servletRelativeAction)) {
		String pathToServlet = getRequestContext().getPathToServlet();
		if (servletRelativeAction.startsWith("/") &&
				!servletRelativeAction.startsWith(getRequestContext().getContextPath())) {
			servletRelativeAction = pathToServlet + servletRelativeAction;
		}
		servletRelativeAction = getDisplayString(evaluate(ACTION_ATTRIBUTE, servletRelativeAction));
		return processAction(servletRelativeAction);
	}
	else {
		String requestUri = getRequestContext().getRequestUri();
		String encoding = this.pageContext.getResponse().getCharacterEncoding();
		try {
			requestUri = UriUtils.encodePath(requestUri, encoding);
		}
		catch (UnsupportedCharsetException ex) {
			// shouldn't happen - if it does, proceed with requestUri as-is
		}
		ServletResponse response = this.pageContext.getResponse();
		if (response instanceof HttpServletResponse) {
			requestUri = ((HttpServletResponse) response).encodeURL(requestUri);
			String queryString = getRequestContext().getQueryString();
			if (StringUtils.hasText(queryString)) {
				requestUri += "?" + HtmlUtils.htmlEscape(queryString);
			}
		}
		if (StringUtils.hasText(requestUri)) {
			return processAction(requestUri);
		}
		else {
			throw new IllegalArgumentException("Attribute 'action' is required. " +
					"Attempted to resolve against current request URI but request URI was null.");
		}
	}
}
 
Example 4
Source File: FormTag.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Resolve the value of the '{@code action}' attribute.
 * <p>If the user configured an '{@code action}' value then the result of
 * evaluating this value is used. If the user configured an
 * '{@code servletRelativeAction}' value then the value is prepended
 * with the context and servlet paths, and the result is used. Otherwise, the
 * {@link org.springframework.web.servlet.support.RequestContext#getRequestUri()
 * originating URI} is used.
 * @return the value that is to be used for the '{@code action}' attribute
 */
protected String resolveAction() throws JspException {
	String action = getAction();
	String servletRelativeAction = getServletRelativeAction();
	if (StringUtils.hasText(action)) {
		action = getDisplayString(evaluate(ACTION_ATTRIBUTE, action));
		return processAction(action);
	}
	else if (StringUtils.hasText(servletRelativeAction)) {
		String pathToServlet = getRequestContext().getPathToServlet();
		if (servletRelativeAction.startsWith("/") &&
				!servletRelativeAction.startsWith(getRequestContext().getContextPath())) {
			servletRelativeAction = pathToServlet + servletRelativeAction;
		}
		servletRelativeAction = getDisplayString(evaluate(ACTION_ATTRIBUTE, servletRelativeAction));
		return processAction(servletRelativeAction);
	}
	else {
		String requestUri = getRequestContext().getRequestUri();
		String encoding = this.pageContext.getResponse().getCharacterEncoding();
		try {
			requestUri = UriUtils.encodePath(requestUri, encoding);
		}
		catch (UnsupportedEncodingException ex) {
			// shouldn't happen - if it does, proceed with requestUri as-is
		}
		ServletResponse response = this.pageContext.getResponse();
		if (response instanceof HttpServletResponse) {
			requestUri = ((HttpServletResponse) response).encodeURL(requestUri);
			String queryString = getRequestContext().getQueryString();
			if (StringUtils.hasText(queryString)) {
				requestUri += "?" + HtmlUtils.htmlEscape(queryString);
			}
		}
		if (StringUtils.hasText(requestUri)) {
			return processAction(requestUri);
		}
		else {
			throw new IllegalArgumentException("Attribute 'action' is required. " +
					"Attempted to resolve against current request URI but request URI was null.");
		}
	}
}
 
Example 5
Source File: FormTag.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve the value of the '{@code action}' attribute.
 * <p>If the user configured an '{@code action}' value then the result of
 * evaluating this value is used. If the user configured an
 * '{@code servletRelativeAction}' value then the value is prepended
 * with the context and servlet paths, and the result is used. Otherwise, the
 * {@link org.springframework.web.servlet.support.RequestContext#getRequestUri()
 * originating URI} is used.
 * @return the value that is to be used for the '{@code action}' attribute
 */
protected String resolveAction() throws JspException {
	String action = getAction();
	String servletRelativeAction = getServletRelativeAction();
	if (StringUtils.hasText(action)) {
		action = getDisplayString(evaluate(ACTION_ATTRIBUTE, action));
		return processAction(action);
	}
	else if (StringUtils.hasText(servletRelativeAction)) {
		String pathToServlet = getRequestContext().getPathToServlet();
		if (servletRelativeAction.startsWith("/") &&
				!servletRelativeAction.startsWith(getRequestContext().getContextPath())) {
			servletRelativeAction = pathToServlet + servletRelativeAction;
		}
		servletRelativeAction = getDisplayString(evaluate(ACTION_ATTRIBUTE, servletRelativeAction));
		return processAction(servletRelativeAction);
	}
	else {
		String requestUri = getRequestContext().getRequestUri();
		String encoding = this.pageContext.getResponse().getCharacterEncoding();
		try {
			requestUri = UriUtils.encodePath(requestUri, encoding);
		}
		catch (UnsupportedEncodingException ex) {
			// shouldn't happen - if it does, proceed with requestUri as-is
		}
		ServletResponse response = this.pageContext.getResponse();
		if (response instanceof HttpServletResponse) {
			requestUri = ((HttpServletResponse) response).encodeURL(requestUri);
			String queryString = getRequestContext().getQueryString();
			if (StringUtils.hasText(queryString)) {
				requestUri += "?" + HtmlUtils.htmlEscape(queryString);
			}
		}
		if (StringUtils.hasText(requestUri)) {
			return processAction(requestUri);
		}
		else {
			throw new IllegalArgumentException("Attribute 'action' is required. " +
					"Attempted to resolve against current request URI but request URI was null.");
		}
	}
}
 
Example 6
Source File: EscapeUrl.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void escpae_url_with_spring () throws UnsupportedEncodingException {
	
	// If you are using Java 7 you can use StandardCharsets OR use Guava Charsets 
	
	String urlEscaped = UriUtils.encodePath(URL_TO_ESCAPE, Charsets.UTF_8.toString());
	 
	assertEquals("http://www.leveluplunch.com%3Fsomevar=abc123&someothervar", urlEscaped);
}