Java Code Examples for org.springframework.web.util.UriComponentsBuilder#port()

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#port() . These examples are extracted from open source projects. 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 Project: zstack   File: VirtualRouterManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String buildUrl(String mgmtNicIp, String subPath) {
       UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
       ub.scheme(VirtualRouterGlobalProperty.AGENT_URL_SCHEME);

       if (CoreGlobalProperty.UNIT_TEST_ON) {
           ub.host("localhost");
       } else {
           ub.host(mgmtNicIp);
       }

       ub.port(VirtualRouterGlobalProperty.AGENT_PORT);
       if (!"".equals(VirtualRouterGlobalProperty.AGENT_URL_ROOT_PATH)) {
           ub.path(VirtualRouterGlobalProperty.AGENT_URL_ROOT_PATH);
       }
       ub.path(subPath);

       return ub.build().toUriString();
   }
 
Example 2
private String buildUrl(String subPath, String hostName) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme(SftpBackupStorageGlobalProperty.AGENT_URL_SCHEME);
    if (CoreGlobalProperty.UNIT_TEST_ON) {
        ub.host("localhost");
    } else {
        ub.host(hostName);
    }

    ub.port(SftpBackupStorageGlobalProperty.AGENT_PORT);
    if (!"".equals(SftpBackupStorageGlobalProperty.AGENT_URL_ROOT_PATH)) {
        ub.path(SftpBackupStorageGlobalProperty.AGENT_URL_ROOT_PATH);
    }
    ub.path(subPath);
    return ub.build().toUriString();
}
 
Example 3
Source Project: zstack   File: SftpBackupStorage.java    License: Apache License 2.0 6 votes vote down vote up
public String buildUrl(String subPath) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme(SftpBackupStorageGlobalProperty.AGENT_URL_SCHEME);
    if (CoreGlobalProperty.UNIT_TEST_ON) {
        ub.host("localhost");
    } else {
        ub.host(getSelf().getHostname());
    }

    ub.port(SftpBackupStorageGlobalProperty.AGENT_PORT);
    if (!"".equals(SftpBackupStorageGlobalProperty.AGENT_URL_ROOT_PATH)) {
        ub.path(SftpBackupStorageGlobalProperty.AGENT_URL_ROOT_PATH);
    }
    ub.path(subPath);
    return ub.build().toUriString();
}
 
Example 4
protected URI getManagementUrl(ServiceInstance instance) {
	URI serviceUrl = this.getServiceUrl(instance);
	String managementScheme = this.getManagementScheme(instance);
	String managementHost = this.getManagementHost(instance);
	int managementPort = this.getManagementPort(instance);

	UriComponentsBuilder builder;
	if (serviceUrl.getHost().equals(managementHost) && serviceUrl.getScheme().equals(managementScheme)
			&& serviceUrl.getPort() == managementPort) {
		builder = UriComponentsBuilder.fromUri(serviceUrl);
	}
	else {
		builder = UriComponentsBuilder.newInstance().scheme(managementScheme).host(managementHost);
		if (managementPort != -1) {
			builder.port(managementPort);
		}
	}

	return builder.path("/").path(getManagementPath(instance)).build().toUri();
}
 
Example 5
Source Project: spring-boot-admin   File: UiController.java    License: 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 6
@Override
protected String buildRedirectUrlToLoginPage(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
    String url = super.buildRedirectUrlToLoginPage(request, response, authException);

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);

    String scheme = request.getHeader(HttpHeaders.X_FORWARDED_PROTO);
    if (scheme != null && !scheme.isEmpty()) {
        builder.scheme(scheme);
    }

    String host = request.getHeader(HttpHeaders.X_FORWARDED_HOST);
    if (host != null && !host.isEmpty()) {
        if (host.contains(":")) {
            // Forwarded host contains both host and port
            String [] parts = host.split(":");
            builder.host(parts[0]);
            builder.port(parts[1]);
        } else {
            builder.host(host);
        }
    }

    // handle forwarded path
    String forwardedPath = request.getHeader(X_FORWARDED_PREFIX);
    if (forwardedPath != null && !forwardedPath.isEmpty()) {
        String path = builder.build().getPath();
        // remove trailing slash
        forwardedPath = forwardedPath.substring(0, forwardedPath.length() - (forwardedPath.endsWith("/") ? 1 : 0));
        builder.replacePath(forwardedPath + path);
    }

    return builder.toUriString();
}
 
Example 7
private String buildRedirectUri(HttpServletRequest request, String identity) {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();

    String scheme = request.getHeader(HttpHeaders.X_FORWARDED_PROTO);
    if (scheme != null && !scheme.isEmpty()) {
        builder.scheme(scheme);
    } else {
        builder.scheme(request.getScheme());
    }

    String host = request.getHeader(HttpHeaders.X_FORWARDED_HOST);
    if (host != null && !host.isEmpty()) {
        if (host.contains(":")) {
            // Forwarded host contains both host and port
            String[] parts = host.split(":");
            builder.host(parts[0]);
            builder.port(parts[1]);
        } else {
            builder.host(host);
        }
    } else {
        builder.host(request.getServerName());
        if (request.getServerPort() != 80 && request.getServerPort() != 443) {
            builder.port(request.getServerPort());
        }
    }
    // append context path
    builder.path(request.getContextPath());
    builder.pathSegment("auth/login/callback");

    // append identity provider id
    builder.queryParam("provider", identity);

    return builder.build().toUriString();
}
 
Example 8
Source Project: modeldb   File: NFSService.java    License: Apache License 2.0 5 votes vote down vote up
private String getUrl(String artifactPath, String endpoint, String scheme) {

    String host =
        ModelDBAuthInterceptor.METADATA_INFO
            .get()
            .get(Metadata.Key.of("x-forwarded-host", Metadata.ASCII_STRING_MARSHALLER));
    if (host == null || host.isEmpty() || app.getPickNFSHostFromConfig()) {
      host = app.getNfsServerHost();
    }

    String[] hostArr = host.split(":");
    String finalHost = hostArr[0];

    UriComponentsBuilder uriComponentsBuilder =
        ServletUriComponentsBuilder.newInstance()
            .scheme(scheme)
            .host(finalHost)
            .path(endpoint)
            .queryParam("artifact_path", artifactPath);

    if (hostArr.length > 1) {
      String finalPort = hostArr[1];
      uriComponentsBuilder.port(finalPort);
    }

    return uriComponentsBuilder.toUriString();
  }
 
Example 9
Source Project: zstack   File: CephAgentUrl.java    License: Apache License 2.0 5 votes vote down vote up
public static String primaryStorageUrl(String ip, String path) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme("http");
    ub.host(ip);
    ub.port(CephGlobalProperty.PRIMARY_STORAGE_AGENT_PORT);
    if (!"".equals(CephGlobalProperty.PRIMARY_STORAGE_AGENT_URL_ROOT_PATH)) {
        ub.path(CephGlobalProperty.PRIMARY_STORAGE_AGENT_URL_ROOT_PATH);
    }
    ub.path(path);
    return ub.build().toUriString();
}
 
Example 10
Source Project: zstack   File: CephAgentUrl.java    License: Apache License 2.0 5 votes vote down vote up
public static String backupStorageUrl(String ip, String path) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme("http");
    ub.host(ip);
    ub.port(CephGlobalProperty.BACKUP_STORAGE_AGENT_PORT);
    if (!"".equals(CephGlobalProperty.BACKUP_STORAGE_AGENT_URL_ROOT_PATH)) {
        ub.path(CephGlobalProperty.BACKUP_STORAGE_AGENT_URL_ROOT_PATH);
    }
    ub.path(path);
    return ub.build().toUriString();
}
 
Example 11
Source Project: zstack   File: ApplianceVmBase.java    License: Apache License 2.0 5 votes vote down vote up
public static String buildAgentUrl(String hostname, String subPath, int port) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme(ApplianceVmGlobalProperty.AGENT_URL_SCHEME);
    if (CoreGlobalProperty.UNIT_TEST_ON) {
        ub.host("localhost");
    } else {
        ub.host(hostname);
    }
    ub.port(port);
    if (!"".equals(ApplianceVmGlobalProperty.AGENT_URL_ROOT_PATH)) {
        ub.path(ApplianceVmGlobalProperty.AGENT_URL_ROOT_PATH);
    }
    ub.path(subPath);
    return ub.build().toUriString();
}
 
Example 12
Source Project: zstack   File: KVMHost.java    License: Apache License 2.0 5 votes vote down vote up
private String buildUrl(String path) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme(KVMGlobalProperty.AGENT_URL_SCHEME);
    ub.host(self.getManagementIp());
    ub.port(KVMGlobalProperty.AGENT_PORT);
    if (!"".equals(KVMGlobalProperty.AGENT_URL_ROOT_PATH)) {
        ub.path(KVMGlobalProperty.AGENT_URL_ROOT_PATH);
    }
    ub.path(path);
    return ub.build().toUriString();
}
 
Example 13
Source Project: zstack   File: KVMHostFactory.java    License: Apache License 2.0 5 votes vote down vote up
public KVMHostContext createHostContext(KVMHostVO vo) {
    UriComponentsBuilder ub = UriComponentsBuilder.newInstance();
    ub.scheme(KVMGlobalProperty.AGENT_URL_SCHEME);
    ub.host(vo.getManagementIp());
    ub.port(KVMGlobalProperty.AGENT_PORT);
    if (!"".equals(KVMGlobalProperty.AGENT_URL_ROOT_PATH)) {
        ub.path(KVMGlobalProperty.AGENT_URL_ROOT_PATH);
    }
    String baseUrl = ub.build().toUriString();

    KVMHostContext context = new KVMHostContext();
    context.setInventory(KVMHostInventory.valueOf(vo));
    context.setBaseUrl(baseUrl);
    return context;
}
 
Example 14
private String buildLink(String switchIp, String path) {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
    builder.scheme("http");
    builder.host(switchIp);
    builder.port(8008);
    builder.path(path);
    return builder.build().toUriString();
}
 
Example 15
@Override
public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
    String redirectUrl = calculateRedirectUrl(request.getContextPath(), url);

    UriComponentsBuilder builder;
    if (UrlUtils.isAbsoluteUrl(redirectUrl)) {
        builder = UriComponentsBuilder.fromHttpUrl(redirectUrl);
    } else {
        builder = UriComponentsBuilder.fromUriString(redirectUrl);
    }

    String scheme = request.getHeader(HttpHeaders.X_FORWARDED_PROTO);
    if (scheme != null && !scheme.isEmpty()) {
        builder.scheme(scheme);
    }

    String host = request.getHeader(HttpHeaders.X_FORWARDED_HOST);
    if (host != null && !host.isEmpty()) {
        if (host.contains(":")) {
            // Forwarded host contains both host and port
            String [] parts = host.split(":");
            builder.host(parts[0]);
            builder.port(parts[1]);
        } else {
            builder.host(host);
        }
    }

    // handle forwarded path
    String forwardedPath = request.getHeader(X_FORWARDED_PREFIX);
    if (forwardedPath != null && !forwardedPath.isEmpty()) {
        String path = builder.build().getPath();
        // remove trailing slash
        forwardedPath = forwardedPath.substring(0, forwardedPath.length() - (forwardedPath.endsWith("/") ? 1 : 0));
        builder.replacePath(forwardedPath + path);
    }

    redirectUrl = response.encodeRedirectURL(builder.build(false).toUriString());

    if (logger.isDebugEnabled()) {
        logger.debug("Redirecting to '{}'", redirectUrl);
    }

    response.sendRedirect(redirectUrl);
}