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

The following examples show how to use org.springframework.web.util.UriComponents#getPath() . 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: JWTSecurityService.java    From airsonic-advanced with GNU General Public License v3.0 6 votes vote down vote up
private static String createToken(String jwtKey, String user, String path, Instant expireDate, Map<String, String> additionalClaims) {
    UriComponents components = UriComponentsBuilder.fromUriString(path).build();
    String query = components.getQuery();
    String pathClaim = components.getPath() + (!StringUtils.isBlank(query) ? "?" + components.getQuery() : "");
    Builder builder = JWT
            .create()
            .withIssuer("airsonic")
            .withSubject(user)
            .withClaim(CLAIM_PATH, pathClaim)
            .withExpiresAt(Date.from(expireDate));

    for (Entry<String, String> claim : additionalClaims.entrySet()) {
        builder = builder.withClaim(claim.getKey(), claim.getValue());
    }

    return builder.sign(getAlgorithm(jwtKey));
}
 
Example 2
Source File: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
	if (this.contextPath == null) {
		List<String> pathSegments = uriComponents.getPathSegments();
		if (pathSegments.isEmpty()) {
			request.setContextPath("");
		}
		else {
			request.setContextPath("/" + pathSegments.get(0));
		}
	}
	else {
		String path = uriComponents.getPath();
		Assert.isTrue(path != null && path.startsWith(this.contextPath),
				() -> "\"" + uriComponents.getPath() +
						"\" should start with context path \"" + this.contextPath + "\"");
		request.setContextPath(this.contextPath);
	}
}
 
Example 3
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
	if (this.contextPath == null) {
		List<String> pathSegments = uriComponents.getPathSegments();
		if (pathSegments.isEmpty()) {
			request.setContextPath("");
		}
		else {
			request.setContextPath("/" + pathSegments.get(0));
		}
	}
	else {
		String path = uriComponents.getPath();
		Assert.isTrue(path != null && path.startsWith(this.contextPath),
				() -> "\"" + uriComponents.getPath() +
						"\" should start with context path \"" + this.contextPath + "\"");
		request.setContextPath(this.contextPath);
	}
}
 
Example 4
Source File: JavametricsRestController.java    From javametrics with Apache License 2.0 6 votes vote down vote up
@RequestMapping(produces = "application/json", path = "/collections", method = RequestMethod.POST)
public ResponseEntity<?> createCollection() {
    if (!initialized) {
        init();
    }

    if (mp.getContextIds().length > 9) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    int contextId = mp.addContext();

    UriComponents uriComponents = UriComponentsBuilder.fromPath("collections/{id}").buildAndExpand(contextId);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uriComponents.toUri());

    String json = new String("{\"uri\":\"" + uriComponents.getPath() + "\"}");
    return new ResponseEntity<>(json, headers, HttpStatus.CREATED);
}
 
Example 5
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
	if (this.contextPath == null) {
		List<String> pathSegments = uriComponents.getPathSegments();
		if (pathSegments.isEmpty()) {
			request.setContextPath("");
		}
		else {
			request.setContextPath("/" + pathSegments.get(0));
		}
	}
	else {
		if (!uriComponents.getPath().startsWith(this.contextPath)) {
			throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath "
					+ this.contextPath);
		}
		request.setContextPath(this.contextPath);
	}
}
 
Example 6
Source File: UiController.java    From spring-boot-admin with Apache License 2.0 6 votes vote down vote up
@ModelAttribute(value = "baseUrl", binding = false)
public String getBaseUrl(UriComponentsBuilder uriBuilder) {
	UriComponents publicComponents = UriComponentsBuilder.fromUriString(this.publicUrl).build();
	if (publicComponents.getScheme() != null) {
		uriBuilder.scheme(publicComponents.getScheme());
	}
	if (publicComponents.getHost() != null) {
		uriBuilder.host(publicComponents.getHost());
	}
	if (publicComponents.getPort() != -1) {
		uriBuilder.port(publicComponents.getPort());
	}
	if (publicComponents.getPath() != null) {
		uriBuilder.path(publicComponents.getPath());
	}
	return uriBuilder.path("/").toUriString();
}
 
Example 7
Source File: ForwardedHeaderFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void sendRedirect(String location) throws IOException {

	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location);
	UriComponents uriComponents = builder.build();

	// Absolute location
	if (uriComponents.getScheme() != null) {
		super.sendRedirect(location);
		return;
	}

	// Network-path reference
	if (location.startsWith("//")) {
		String scheme = this.request.getScheme();
		super.sendRedirect(builder.scheme(scheme).toUriString());
		return;
	}

	String path = uriComponents.getPath();
	if (path != null) {
		// Relative to Servlet container root or to current request
		path = (path.startsWith(FOLDER_SEPARATOR) ? path :
				StringUtils.applyRelativePath(this.request.getRequestURI(), path));
	}

	String result = UriComponentsBuilder
			.fromHttpRequest(new ServletServerHttpRequest(this.request))
			.replacePath(path)
			.replaceQuery(uriComponents.getQuery())
			.fragment(uriComponents.getFragment())
			.build().normalize().toUriString();

	super.sendRedirect(result);
}
 
Example 8
Source File: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	Charset charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();
	String path = uriComponents.getPath();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
			servletContext, httpMethod, (path != null ? path : ""));
	parent(request, this.parentBuilder);
	String host = uriComponents.getHost();
	request.setServerName(host != null ? host : "");  // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset.name());
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	String scheme = uriComponents.getScheme();
	request.setScheme(scheme != null ? scheme : "");
	request.setPathInfo(null);

	return postProcess(request);
}
 
Example 9
Source File: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void servletPath(UriComponents uriComponents, MockHttpServletRequest request) {
	if ("".equals(request.getPathInfo())) {
		request.setPathInfo(null);
	}
	String path = uriComponents.getPath();
	servletPath(request, (path != null ? path : ""));
}
 
Example 10
Source File: ForwardedHeaderFilter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void sendRedirect(String location) throws IOException {

	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location);
	UriComponents uriComponents = builder.build();

	// Absolute location
	if (uriComponents.getScheme() != null) {
		super.sendRedirect(location);
		return;
	}

	// Network-path reference
	if (location.startsWith("//")) {
		String scheme = this.request.getScheme();
		super.sendRedirect(builder.scheme(scheme).toUriString());
		return;
	}

	String path = uriComponents.getPath();
	if (path != null) {
		// Relative to Servlet container root or to current request
		path = (path.startsWith(FOLDER_SEPARATOR) ? path :
				StringUtils.applyRelativePath(this.request.getRequestURI(), path));
	}

	String result = UriComponentsBuilder
			.fromHttpRequest(new ServletServerHttpRequest(this.request))
			.replacePath(path)
			.replaceQuery(uriComponents.getQuery())
			.fragment(uriComponents.getFragment())
			.build().normalize().toUriString();

	super.sendRedirect(result);
}
 
Example 11
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	Charset charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();
	String path = uriComponents.getPath();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
			servletContext, httpMethod, (path != null ? path : ""));
	parent(request, this.parentBuilder);
	String host = uriComponents.getHost();
	request.setServerName(host != null ? host : "");  // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset.name());
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	String scheme = uriComponents.getScheme();
	request.setScheme(scheme != null ? scheme : "");
	request.setPathInfo(null);

	return postProcess(request);
}
 
Example 12
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void servletPath(UriComponents uriComponents, MockHttpServletRequest request) {
	if ("".equals(request.getPathInfo())) {
		request.setPathInfo(null);
	}
	String path = uriComponents.getPath();
	servletPath(request, (path != null ? path : ""));
}
 
Example 13
Source File: DemoApplicationTests.java    From keycloak-springsecurity5-sample with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void requestAuthorizeGitHubClientWhenLinkClickedThenStatusRedirectForAuthorization() throws Exception {
	HtmlPage page = this.webClient.getPage("/");

	ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("github");

	HtmlAnchor clientAnchorElement = this.getClientAnchorElement(page, clientRegistration);
	assertThat(clientAnchorElement).isNotNull();

	WebResponse response = this.followLinkDisableRedirects(clientAnchorElement);

	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value());

	String authorizeRedirectUri = response.getResponseHeaderValue("Location");
	assertThat(authorizeRedirectUri).isNotNull();

	UriComponents uriComponents = UriComponentsBuilder.fromUri(URI.create(authorizeRedirectUri)).build();

	String requestUri = uriComponents.getScheme() + "://" + uriComponents.getHost() + uriComponents.getPath();
	assertThat(requestUri).isEqualTo(clientRegistration.getProviderDetails().getAuthorizationUri());

	Map<String, String> params = uriComponents.getQueryParams().toSingleValueMap();

	assertThat(params.get(OAuth2ParameterNames.RESPONSE_TYPE)).isEqualTo(OAuth2AuthorizationResponseType.CODE.getValue());
	assertThat(params.get(OAuth2ParameterNames.CLIENT_ID)).isEqualTo(clientRegistration.getClientId());
	String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId();
	assertThat(URLDecoder.decode(params.get(OAuth2ParameterNames.REDIRECT_URI), "UTF-8")).isEqualTo(redirectUri);
	assertThat(URLDecoder.decode(params.get(OAuth2ParameterNames.SCOPE), "UTF-8"))
		.isEqualTo(clientRegistration.getScopes().stream().collect(Collectors.joining(" ")));
	assertThat(params.get(OAuth2ParameterNames.STATE)).isNotNull();
}
 
Example 14
Source File: JWTSecurityService.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
private static String createToken(String jwtKey, String path, Date expireDate) {
    UriComponents components = UriComponentsBuilder.fromUriString(path).build();
    String query = components.getQuery();
    String claim = components.getPath() + (!StringUtils.isBlank(query) ? "?" + components.getQuery() : "");
    LOG.debug("Creating token with claim " + claim);
    return JWT.create()
            .withClaim(CLAIM_PATH, claim)
            .withExpiresAt(expireDate)
            .sign(getAlgorithm(jwtKey));
}
 
Example 15
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	String charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod,
			uriComponents.getPath());
	parent(request, this.parentBuilder);
	request.setServerName(uriComponents.getHost()); // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset);
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	request.setScheme(uriComponents.getScheme());
	pathInfo(uriComponents,request);

	return postProcess(request);
}