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

The following examples show how to use org.springframework.web.util.UriComponents#getHost() . 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: SwaggerController.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Serves the Swagger description of the REST API. As host, fills in the host where the controller
 * lives. As options for the entity names, contains only those entity names that the user can
 * actually see.
 */
@GetMapping(value = "/swagger.yml", produces = "text/yaml")
public String swagger(Model model, HttpServletResponse response) {
  response.setContentType("text/yaml");
  response.setCharacterEncoding("UTF-8");
  final UriComponents uriComponents =
      ServletUriComponentsBuilder.fromCurrentContextPath().build();
  model.addAttribute("scheme", uriComponents.getScheme());
  String host = uriComponents.getHost();
  if (uriComponents.getPort() >= 0) {
    host += ":" + uriComponents.getPort();
  }
  model.addAttribute("host", host);
  model.addAttribute(
      "entityTypes",
      metaDataService
          .getEntityTypes()
          .filter(e -> !e.isAbstract())
          .map(EntityType::getId)
          .sorted()
          .collect(toList()));
  model.addAttribute("attributeTypes", AttributeType.getOptionsLowercase());
  model.addAttribute("languageCodes", getLanguageCodes().collect(toList()));
  return "view-swagger";
}
 
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: Knife4jController.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
private String hostName(UriComponents uriComponents) {
    if ("DEFAULT".equals(hostNameOverride)) {
        String host = uriComponents.getHost();
        int port = uriComponents.getPort();
        if (port > -1) {
            return String.format("%s:%d", host, port);
        }
        return host;
    }
    return hostNameOverride;
}
 
Example 10
Source File: Swagger2Controller.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
private static String getString(UriComponents uriComponents, String hostNameOverride) {
    if ("DEFAULT".equals(hostNameOverride)) {
        String host = uriComponents.getHost();
        int port = uriComponents.getPort();
        return port > -1 ? String.format("%s:%d", host, port) : host;
    } else {
        return hostNameOverride;
    }
}
 
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: 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 13
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;
}