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

The following examples show how to use org.springframework.web.util.UriComponents#getScheme() . 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: ForwardedHeaderFilter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelper pathHelper) {
	super(request);

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
	int port = uriComponents.getPort();

	this.scheme = uriComponents.getScheme();
	this.secure = "https".equals(this.scheme);
	this.host = uriComponents.getHost();
	this.port = (port == -1 ? (this.secure ? 443 : 80) : port);

	String baseUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port);
	Supplier<HttpServletRequest> delegateRequest = () -> (HttpServletRequest) getRequest();
	this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, pathHelper, baseUrl);
}
 
Example 2
Source File: ForwardedHeaderFilter.java    From java-technology-stack with MIT License 6 votes vote down vote up
ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelper pathHelper) {
	super(request);

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
	int port = uriComponents.getPort();

	this.scheme = uriComponents.getScheme();
	this.secure = "https".equals(this.scheme);
	this.host = uriComponents.getHost();
	this.port = (port == -1 ? (this.secure ? 443 : 80) : port);

	String baseUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port);
	Supplier<HttpServletRequest> delegateRequest = () -> (HttpServletRequest) getRequest();
	this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, pathHelper, baseUrl);
}
 
Example 3
Source File: ServletUriComponentsBuilder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Initialize a builder with a scheme, host,and port (but not path and query).
 */
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
	String scheme = uriComponents.getScheme();
	String host = uriComponents.getHost();
	int port = uriComponents.getPort();

	ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
	builder.scheme(scheme);
	builder.host(host);
	if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
		builder.port(port);
	}
	return builder;
}
 
Example 4
Source File: ForwardedHeaderFilter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelper pathHelper) {
	super(request);

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
	int port = uriComponents.getPort();

	this.scheme = uriComponents.getScheme();
	this.secure = "https".equals(scheme);
	this.host = uriComponents.getHost();
	this.port = (port == -1 ? (this.secure ? 443 : 80) : port);

	String prefix = getForwardedPrefix(request);
	this.contextPath = (prefix != null ? prefix : request.getContextPath());
	this.requestUri = this.contextPath + pathHelper.getPathWithinApplication(request);
	this.requestUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port) + this.requestUri;
}
 
Example 5
Source File: ServletUriComponentsBuilder.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize a builder with a scheme, host,and port (but not path and query).
 */
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
	String scheme = uriComponents.getScheme();
	String host = uriComponents.getHost();
	int port = uriComponents.getPort();

	ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
	builder.scheme(scheme);
	builder.host(host);
	if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
		builder.port(port);
	}
	return builder;
}
 
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: 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 10
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 11
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 12
Source File: ServletNettyChannelHandler.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
private MockHttpServletRequest createHttpServletRequest(FullHttpRequest fullHttpReq) {
	UriComponents uriComponents = UriComponentsBuilder.fromUriString(fullHttpReq.getUri()).build();

	MockHttpServletRequest servletRequest = new MockHttpServletRequest(this.servletContext);
	servletRequest.setRequestURI(uriComponents.getPath());
	servletRequest.setPathInfo(uriComponents.getPath());
	servletRequest.setMethod(fullHttpReq.getMethod().name());
	servletRequest.setCharacterEncoding(UTF_8);

	if (uriComponents.getScheme() != null) {
		servletRequest.setScheme(uriComponents.getScheme());
	}
	if (uriComponents.getHost() != null) {
		servletRequest.setServerName(uriComponents.getHost());
	}
	if (uriComponents.getPort() != -1) {
		servletRequest.setServerPort(uriComponents.getPort());
	}
	
	copyHttpHeaders(fullHttpReq, servletRequest);
	copyHttpBodyData(fullHttpReq, servletRequest);
	
	copyQueryParams(uriComponents, servletRequest);
	copyToServletCookie(fullHttpReq, servletRequest);
	
	return servletRequest;
}