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

The following examples show how to use org.springframework.web.util.UriUtils#decode() . 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: DefaultServerRequestBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static MultiValueMap<String, String> parseQueryParams(URI uri) {
	MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
	String query = uri.getRawQuery();
	if (query != null) {
		Matcher matcher = QUERY_PATTERN.matcher(query);
		while (matcher.find()) {
			String name = UriUtils.decode(matcher.group(1), StandardCharsets.UTF_8);
			String eq = matcher.group(2);
			String value = matcher.group(3);
			if (value != null) {
				value = UriUtils.decode(value, StandardCharsets.UTF_8);
			}
			else {
				value = (StringUtils.hasLength(eq) ? "" : null);
			}
			queryParams.add(name, value);
		}
	}
	return queryParams;
}
 
Example 2
Source File: DefaultServerRequestBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static MultiValueMap<String, String> parseQueryParams(URI uri) {
	MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
	String query = uri.getRawQuery();
	if (query != null) {
		Matcher matcher = QUERY_PATTERN.matcher(query);
		while (matcher.find()) {
			String name = UriUtils.decode(matcher.group(1), StandardCharsets.UTF_8);
			String eq = matcher.group(2);
			String value = matcher.group(3);
			if (value != null) {
				value = UriUtils.decode(value, StandardCharsets.UTF_8);
			}
			else {
				value = (StringUtils.hasLength(eq) ? "" : null);
			}
			queryParams.add(name, value);
		}
	}
	return queryParams;
}
 
Example 3
Source File: AbstractHttpSendingTransportHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected final String getCallbackParam(ServerHttpRequest request) {
	String query = request.getURI().getQuery();
	MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
	String value = params.getFirst("c");
	if (StringUtils.isEmpty(value)) {
		return null;
	}
	try {
		String result = UriUtils.decode(value, "UTF-8");
		return (CALLBACK_PARAM_PATTERN.matcher(result).matches() ? result : null);
	}
	catch (UnsupportedEncodingException ex) {
		// should never happen
		throw new SockJsException("Unable to decode callback query parameter", null, ex);
	}
}
 
Example 4
Source File: UserfilesDownloadServlet.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
public void fileOutputStream(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
	String filepath = req.getRequestURI();
	int index = filepath.indexOf(Global.USERFILES_BASE_URL);
	if(index >= 0) {
		filepath = filepath.substring(index + Global.USERFILES_BASE_URL.length());
	}
	try {
		filepath = UriUtils.decode(filepath, "UTF-8");
	} catch (UnsupportedEncodingException e1) {
		logger.error(String.format("解释文件路径失败,URL地址为%s", filepath), e1);
	}
	File file = new File(Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + filepath);
	try {
		FileCopyUtils.copy(new FileInputStream(file), resp.getOutputStream());
		resp.setHeader("Content-Type", "application/octet-stream");
		return;
	} catch (FileNotFoundException e) {
		req.setAttribute("exception", new FileNotFoundException("请求的文件不存在"));
		req.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(req, resp);
	}
}
 
Example 5
Source File: MultigetReport.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private static boolean isDescendentOrEqual(URL collection,
                                           URL test) {
    if(collection == null || test == null){
        return false;
    }
    if (collection.toString().equals(test.toString())) {
        return true;
    }
    
    try {
        String testPathDecoded = UriUtils.decode(test.getPath(), "UTF-8");
        String collectionPathDecoded = UriUtils.decode(collection.getPath(), "UTF-8");

        return testPathDecoded.startsWith(collectionPathDecoded);
    } catch (IllegalArgumentException e) {
        return test.getPath().startsWith(collection.getPath());
    }
}
 
Example 6
Source File: ServletNettyChannelHandler.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
void copyQueryParams(UriComponents uriComponents, MockHttpServletRequest servletRequest){
	try {
		if (uriComponents.getQuery() != null) {
			String query = UriUtils.decode(uriComponents.getQuery(), UTF_8);
			servletRequest.setQueryString(query);
		}

		for (Entry<String, List<String>> entry : uriComponents.getQueryParams().entrySet()) {
			for (String value : entry.getValue()) {
				servletRequest.addParameter(
						UriUtils.decode(entry.getKey(), UTF_8),
						UriUtils.decode(value, UTF_8));
			}
		}
	}
	catch (UnsupportedEncodingException ex) {
		// shouldn't happen
	}
}
 
Example 7
Source File: PathResourceResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Find the resource under the given location.
 * <p>The default implementation checks if there is a readable
 * {@code Resource} for the given path relative to the location.
 * @param resourcePath the path to the resource
 * @param location the location to check
 * @return the resource, or empty {@link Mono} if none found
 */
protected Mono<Resource> getResource(String resourcePath, Resource location) {
	try {
		if (location instanceof ClassPathResource) {
			resourcePath = UriUtils.decode(resourcePath, StandardCharsets.UTF_8);
		}
		Resource resource = location.createRelative(resourcePath);
		if (resource.isReadable()) {
			if (checkResource(resource, location)) {
				return Mono.just(resource);
			}
			else if (logger.isWarnEnabled()) {
				Resource[] allowedLocations = getAllowedLocations();
				logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
						"but resource \"" + resource.getURL() + "\" is neither under the " +
						"current location \"" + location.getURL() + "\" nor under any of the " +
						"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
			}
		}
		return Mono.empty();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			String error = "Skip location [" + location + "] due to error";
			if (logger.isTraceEnabled()) {
				logger.trace(error, ex);
			}
			else {
				logger.debug(error + ": " + ex.getMessage());
			}
		}
		return Mono.error(ex);
	}
}
 
Example 8
Source File: AbstractHttpSendingTransportHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
protected final String getCallbackParam(ServerHttpRequest request) {
	String query = request.getURI().getQuery();
	MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
	String value = params.getFirst("c");
	if (!StringUtils.hasLength(value)) {
		return null;
	}
	String result = UriUtils.decode(value, StandardCharsets.UTF_8);
	return (CALLBACK_PARAM_PATTERN.matcher(result).matches() ? result : null);
}
 
Example 9
Source File: AbstractHttpSendingTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
protected final String getCallbackParam(ServerHttpRequest request) {
	String query = request.getURI().getQuery();
	MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
	String value = params.getFirst("c");
	if (StringUtils.isEmpty(value)) {
		return null;
	}
	String result = UriUtils.decode(value, StandardCharsets.UTF_8);
	return (CALLBACK_PARAM_PATTERN.matcher(result).matches() ? result : null);
}
 
Example 10
Source File: Servlets.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static String decodeQueryString(String query){
    try {
        return UriUtils.decode(query,"UTF-8");

    } catch (UnsupportedEncodingException e){
        return query;
    }
}
 
Example 11
Source File: RestServer.java    From zstack with Apache License 2.0 5 votes vote down vote up
private String getDecodedUrl(HttpServletRequest req) {
    try {
        if (req.getContextPath() == null) {
            return UriUtils.decode(req.getRequestURI(), "UTF-8");
        } else {
            return UriUtils.decode(StringUtils.removeStart(req.getRequestURI(), req.getContextPath()), "UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
        throw new CloudRuntimeException(e);
    }
}
 
Example 12
Source File: LoginController.java    From Parrit with MIT License 5 votes vote down vote up
@RequestMapping(path = "/login", method = RequestMethod.GET)
public String loginProject(final HttpServletRequest request, final HttpServletResponse response, Model model) {
    SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);

    //TODO: Check to make sure this isn't null -- maybe redirect to homepage if it is
    String originalRequestUrl = savedRequest.getRedirectUrl();
    String projectName = originalRequestUrl.substring(originalRequestUrl.lastIndexOf('/') + 1);
    projectName = UriUtils.decode(projectName, Charset.defaultCharset());

    model.addAttribute("projectName", projectName);
    return "login";
}
 
Example 13
Source File: EncoderDecoderUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPathSegment_thenEncodeDecode() throws UnsupportedEncodingException {
    String pathSegment = "/Path 1/Path+2";
    String encodedPathSegment = encodePath(pathSegment);
    String decodedPathSegment = UriUtils.decode(encodedPathSegment, "UTF-8");
    Assert.assertEquals("/Path%201/Path+2", encodedPathSegment);
    Assert.assertEquals("/Path 1/Path+2", decodedPathSegment);
}
 
Example 14
Source File: ServletUtil.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
public static String decode(String string) {
    return UriUtils.decode(string, StandardCharsets.UTF_8.name());
}
 
Example 15
Source File: URIUtils.java    From yue-library with Apache License 2.0 2 votes vote down vote up
/**
 * URI解码
 * @param source 要解码的字符串
 * @return 解码后的字符串
 */
public static String decode(String source) {
	return UriUtils.decode(source, DEFAULT_ENCODING);
}