Java Code Examples for org.springframework.web.util.UriComponents#getQueryParams()

The following examples show how to use org.springframework.web.util.UriComponents#getQueryParams() . 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: AbstractFlashMapManager.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Whether the given FlashMap matches the current request.
 * Uses the expected request path and query parameters saved in the FlashMap.
 */
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
	String expectedPath = flashMap.getTargetRequestPath();
	if (expectedPath != null) {
		String requestUri = getUrlPathHelper().getOriginatingRequestUri(request);
		if (!requestUri.equals(expectedPath) && !requestUri.equals(expectedPath + "/")) {
			return false;
		}
	}
	UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(request).build();
	MultiValueMap<String, String> actualParams = uriComponents.getQueryParams();
	MultiValueMap<String, String> expectedParams = flashMap.getTargetRequestParams();
	for (String expectedName : expectedParams.keySet()) {
		List<String> actualValues = actualParams.get(expectedName);
		if (actualValues == null) {
			return false;
		}
		for (String expectedValue : expectedParams.get(expectedName)) {
			if (!actualValues.contains(expectedValue)) {
				return false;
			}
		}
	}
	return true;
}
 
Example 2
Source File: MvcUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fromMethodNameWithPathVarAndRequestParam() {
	UriComponents uriComponents = fromMethodName(
			ControllerWithMethods.class, "methodForNextPage", "1", 10, 5).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));
	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("offset"), contains("10"));
}
 
Example 3
Source File: MvcUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fromMethodCallWithPathVariableAndRequestParams() {
	UriComponents uriComponents = fromMethodCall(
			on(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));

	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("offset"), contains("10"));
}
 
Example 4
Source File: MvcUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fromMethodCallWithPathVariableAndMultiValueRequestParams() {
	UriComponents uriComponents = fromMethodCall(
			on(ControllerWithMethods.class).methodWithMultiValueRequestParams("1", Arrays.asList(3, 7), 5)).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));

	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("items"), containsInAnyOrder("3", "7"));
}
 
Example 5
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fromMethodNameWithPathVarAndRequestParam() {
	UriComponents uriComponents = fromMethodName(
			ControllerWithMethods.class, "methodForNextPage", "1", 10, 5).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));
	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("offset"), contains("10"));
}
 
Example 6
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fromMethodCallWithPathVariableAndRequestParams() {
	UriComponents uriComponents = fromMethodCall(
			on(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));

	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("offset"), contains("10"));
}
 
Example 7
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fromMethodCallWithPathVariableAndMultiValueRequestParams() {
	UriComponents uriComponents = fromMethodCall(
			on(ControllerWithMethods.class).methodWithMultiValueRequestParams("1", Arrays.asList(3, 7), 5)).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));

	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("items"), containsInAnyOrder("3", "7"));
}
 
Example 8
Source File: MvcUriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMethodNameWithPathVarAndRequestParam() throws Exception {
	UriComponents uriComponents = fromMethodName(
			ControllerWithMethods.class, "methodForNextPage", "1", 10, 5).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));
	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("offset"), contains("10"));
}
 
Example 9
Source File: MvcUriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMethodCallWithPathVarAndRequestParams() {
	UriComponents uriComponents = fromMethodCall(on(
			ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));

	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("offset"), contains("10"));
}
 
Example 10
Source File: MvcUriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMethodCallWithPathVarAndMultiValueRequestParams() {
	UriComponents uriComponents = fromMethodCall(on(
			ControllerWithMethods.class).methodWithMultiValueRequestParams("1", Arrays.asList(3, 7), 5)).build();

	assertThat(uriComponents.getPath(), is("/something/1/foo"));

	MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
	assertThat(queryParams.get("limit"), contains("5"));
	assertThat(queryParams.get("items"), containsInAnyOrder("3", "7"));
}
 
Example 11
Source File: WsTtyHandler.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void afterConnectionEstablished(WebSocketSession session) {
    URI uri = session.getUri();
    try {
        UriComponents uc = UriComponentsBuilder.fromUri(uri).build();
        MultiValueMap<String, String> params = uc.getQueryParams();
        String containerId = params.getFirst("container");
        try(TempAuth ta = withAuth(session)) {
            connectToContainer(session, containerId);
        }
    } catch (Exception e) {
        log.error("Can not establish connection for '{}' due to error:", uri, e);
    }
}
 
Example 12
Source File: JWTAuthenticationProvider.java    From airsonic-advanced with GNU General Public License v3.0 4 votes vote down vote up
private static boolean roughlyEqual(String expectedRaw, String requestedPathRaw) {
    LOG.debug("Comparing expected [{}] vs requested [{}]", expectedRaw, requestedPathRaw);
    if (StringUtils.isEmpty(expectedRaw)) {
        LOG.debug("False: empty expected");
        return false;
    }
    try {
        UriComponents expected = UriComponentsBuilder.fromUriString(expectedRaw).build();
        UriComponents requested = UriComponentsBuilder.fromUriString(requestedPathRaw).build();

        if (!Objects.equals(expected.getPath(), requested.getPath())) {
            LOG.debug("False: expected path [{}] does not match requested path [{}]",
                    expected.getPath(), requested.getPath());
            return false;
        }

        Map<String, List<String>> left = new HashMap<>(expected.getQueryParams());
        Map<String, List<String>> right = new HashMap<>(requested.getQueryParams());

        /*
            If the equality test uses the size parameter on the request, it is possible that the equality test will fail because Sonos
            changes the size parameter according to the client.

            All parameters should be removed, but this would require too much retrofit work throughout the code.
         */
        left.remove("size");
        right.remove("size");

        MapDifference<String, List<String>> difference = Maps.difference(left, right);

        if (difference.entriesDiffering().isEmpty() || difference.entriesOnlyOnLeft().isEmpty()
                || (difference.entriesOnlyOnRight().size() == 1 && difference.entriesOnlyOnRight().get(JWTSecurityService.JWT_PARAM_NAME) != null)) {
            return true;
        }

        LOG.debug("False: expected query params [{}] do not match requested query params [{}]", expected.getQueryParams(), requested.getQueryParams());
        return false;

    } catch (Exception e) {
        LOG.warn("Exception encountered while comparing paths", e);
        return false;
    }
}