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

The following examples show how to use org.springframework.web.util.UriComponents#getPort() . 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
private void ports(UriComponents uriComponents, MockHttpServletRequest request) {
	int serverPort = uriComponents.getPort();
	request.setServerPort(serverPort);
	if (serverPort == -1) {
		int portConnection = this.webRequest.getUrl().getDefaultPort();
		request.setLocalPort(serverPort);
		request.setRemotePort(portConnection);
	}
	else {
		request.setRemotePort(serverPort);
	}
}
 
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
private void ports(UriComponents uriComponents, MockHttpServletRequest request) {
	int serverPort = uriComponents.getPort();
	request.setServerPort(serverPort);
	if (serverPort == -1) {
		int portConnection = this.webRequest.getUrl().getDefaultPort();
		request.setLocalPort(serverPort);
		request.setRemotePort(portConnection);
	}
	else {
		request.setRemotePort(serverPort);
	}
}
 
Example 12
Source File: ProxyRedirectFilter.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldFilter() {
	RequestContext ctx = RequestContext.getCurrentContext();
	boolean isRedirect = ctx.getResponseStatusCode() == 301 || ctx.getResponseStatusCode() == 302;
	if (!isRedirect)
		return false;

	boolean hasCorrectLocation = false;

	List<Pair<String, String>> zuulResponseHeaders = ctx.getZuulResponseHeaders();
	for (Pair<String, String> zuulResponseHeader : zuulResponseHeaders) {
		if ("Location".equalsIgnoreCase(zuulResponseHeader.first())) {
			HttpServletRequest request = ctx.getRequest();
			String path = urlPathHelper.getPathWithinApplication(request);
			Route route = routeLocator.getMatchingRoute(path);
			UriComponents redirectTo = ServletUriComponentsBuilder
					.fromHttpUrl(zuulResponseHeader.second()).build();
			UriComponents routeLocation = ServletUriComponentsBuilder
					.fromHttpUrl(route.getLocation()).build();

			if (redirectTo.getHost().equalsIgnoreCase(routeLocation.getHost())
					&& redirectTo.getPort() == routeLocation.getPort()) {
				String toLocation = ServletUriComponentsBuilder
						.fromHttpUrl(zuulResponseHeader.second())
						.host(request.getServerName())
						.port(request.getServerPort())
						.replacePath(
								buildRoutePath(route, zuulResponseHeader.second()))
						.build().toUriString();

				ctx.put(REDIRECT_TO_URL, toLocation);
				hasCorrectLocation = true;
				break;
			}
		}
	}

	return hasCorrectLocation;
}
 
Example 13
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void ports(UriComponents uriComponents, MockHttpServletRequest request) {
	int serverPort = uriComponents.getPort();
	request.setServerPort(serverPort);
	if (serverPort == -1) {
		int portConnection = this.webRequest.getUrl().getDefaultPort();
		request.setLocalPort(serverPort);
		request.setRemotePort(portConnection);
	}
	else {
		request.setRemotePort(serverPort);
	}
}
 
Example 14
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;
}