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

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#scheme() . 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: CorsFilter.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean isOriginWhitelisted( HttpServletRequest request, String origin )
{
    HttpServletRequestEncodingWrapper encodingWrapper = new HttpServletRequestEncodingWrapper( request );
    UriComponentsBuilder uriBuilder = ServletUriComponentsBuilder.fromContextPath( encodingWrapper ).replacePath( "" );

    String forwardedProto = request.getHeader( "X-Forwarded-Proto" );

    if ( !StringUtils.isEmpty( forwardedProto ) )
    {
        uriBuilder.scheme( forwardedProto );
    }

    String localUrl = uriBuilder.build().toString();

    return !StringUtils.isEmpty( origin ) && (localUrl.equals( origin ) ||
        configurationService.isCorsWhitelisted( origin ));
}
 
Example 2
Source File: VirtualRouterManagerImpl.java    From zstack with 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 3
Source File: SftpBackupStorageMetaDataMaker.java    From zstack with Apache License 2.0 6 votes vote down vote up
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 4
Source File: SftpBackupStorage.java    From zstack with 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 5
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 6
Source File: LoginUrlAuthenticationEntryPoint.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@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
Source File: LoginController.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
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 File: CephAgentUrl.java    From zstack with 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 9
Source File: CephAgentUrl.java    From zstack with 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 10
Source File: ApplianceVmBase.java    From zstack with 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 11
Source File: KVMHost.java    From zstack with 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 12
Source File: KVMHostFactory.java    From zstack with 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 13
Source File: Force10BaremetalSwitchBackend.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
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 14
Source File: HttpUtils.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String resolveRelativeUri(URL base, final String value0) {
	final String value = value0.replaceAll("^\\s+", "");

	try {
		final URI uri = new URI(value);
		final UriComponentsBuilder builder = UriComponentsBuilder.fromUri(uri);
		UriComponents components = null;

		if (uri.getScheme() == null) {
			builder.scheme(base.getProtocol());
		}

		if (uri.getHost() == null && uri.getPath() != null) {
			builder.host(base.getHost()).port(base.getPort());

			String path;

			if (value.startsWith(base.getHost() + "/")) {
				// Special case when URI starts with a hostname but has no schema
				//  so that hostname is treated as a leading part of a path.
				// It is possible to resolve that ambiguity when a base URL has the
				//  same hostname.
				path = uri.getPath().substring(base.getHost().length() - 1);
			} else {
				if (value.startsWith("/")) {
					// Base path is ignored when a URI starts with a slash
					path = uri.getPath();
				} else {
					if (base.getPath() != null) {
						path = base.getPath() + "/" + uri.getPath();
					} else {
						path = uri.getPath();
					}
				}
			}

			Deque<String> segments = new ArrayDeque<>();
			for (String segment : path.split("/")) {
				switch (segment) {
					case "":
					case ".":
						// Remove duplicating slashes and redundant "." operator
						break;

					case "..":
						// Remove previous segment if possible or append another ".." operator
						String last = segments.peekLast();
						if (last != null && !last.equals("..")) {
							segments.removeLast();
						} else {
							segments.addLast(segment);
						}
						break;

					default:
						segments.addLast(segment);
						break;
				}
			}
			components = builder.replacePath("/" + StringUtils.join(segments, "/")).build();
		}

		if (components != null) {
			return components.toString();
		}
	} catch (URISyntaxException e) {
		logger.error("Error occurred: " + e.getMessage(), e);
	}
	return value;
}
 
Example 15
Source File: XForwardedAwareRedirectStrategy.java    From graviteeio-access-management with Apache License 2.0 4 votes vote down vote up
@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);
}