Java Code Examples for org.springframework.core.io.Resource#getClass()

The following examples show how to use org.springframework.core.io.Resource#getClass() . 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
private static FileSource[] toSources(Resource[] resources) {
	FileSource[] sources = new FileSource[resources.length];
	for (int i = 0; i < resources.length; i++) {
		Resource resource = resources[i];
		if (resource instanceof ClassPathResource) {
			ClassPathResource classes = (ClassPathResource) resource;
			sources[i] = new ClasspathFileSource(classes.getPath());
		}
		else if (resource instanceof FileSystemResource) {
			FileSystemResource files = (FileSystemResource) resource;
			sources[i] = new SingleRootFileSource(files.getFile());
		}
		else if (resource instanceof UrlResource) {
			sources[i] = fileOrFallbackToClasspath(resource);
		}
		else {
			throw new IllegalArgumentException(
					"Unsupported resource type for file source: "
							+ resource.getClass());
		}
	}
	return sources;
}
 
Example 2
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath));
}
 
Example 3
private boolean isResourceUnderLocation(Resource resource) throws IOException {
	if (resource.getClass() != this.location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(this.location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(this.location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	if (!resourcePath.startsWith(locationPath)) {
		return false;
	}
	if (resourcePath.contains("%") && StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
		return false;
	}
	return true;
}
 
Example 4
/**
 * Determine, if possible, the contentLength of the given resource without reading it.
 * @param resource the resource instance
 * @return the contentLength of the resource
 */
private OptionalLong contentLength(Resource resource) {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	if (InputStreamResource.class != resource.getClass()) {
		try {
			return OptionalLong.of(resource.contentLength());
		}
		catch (IOException ignored) {
		}
	}
	return OptionalLong.empty();
}
 
Example 5
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
	}
	else if (resource instanceof ServletContextResource) {
		resourcePath = ((ServletContextResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath));
}
 
Example 6
private boolean isResourceUnderLocation(Resource resource) throws IOException {
	if (resource.getClass() != this.location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(this.location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(this.location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	if (!resourcePath.startsWith(locationPath)) {
		return false;
	}
	if (resourcePath.contains("%") && StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
		return false;
	}
	return true;
}
 
Example 7
@Override
protected Long getContentLength(Resource resource, @Nullable MediaType contentType) throws IOException {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	if (InputStreamResource.class == resource.getClass()) {
		return null;
	}
	long contentLength = resource.contentLength();
	return (contentLength < 0 ? null : contentLength);
}
 
Example 8
private static long lengthOf(Resource resource) {
	// Don't consume InputStream...
	if (InputStreamResource.class != resource.getClass()) {
		try {
			return resource.contentLength();
		}
		catch (IOException ignored) {
		}
	}
	return -1;
}
 
Example 9
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath));
}
 
Example 10
private boolean isResourceUnderLocation(Resource resource) throws IOException {
	if (resource.getClass() != this.location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(this.location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(this.location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	if (!resourcePath.startsWith(locationPath)) {
		return false;
	}
	if (resourcePath.contains("%") && StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
		return false;
	}
	return true;
}
 
Example 11
/**
 * Determine, if possible, the contentLength of the given resource without reading it.
 * @param resource the resource instance
 * @return the contentLength of the resource
 */
private OptionalLong contentLength(Resource resource) {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	if (InputStreamResource.class != resource.getClass()) {
		try {
			return OptionalLong.of(resource.contentLength());
		}
		catch (IOException ignored) {
		}
	}
	return OptionalLong.empty();
}
 
Example 12
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
	}
	else if (resource instanceof ServletContextResource) {
		resourcePath = ((ServletContextResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath));
}
 
Example 13
@Override
protected Long getContentLength(Resource resource, @Nullable MediaType contentType) throws IOException {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	if (InputStreamResource.class == resource.getClass()) {
		return null;
	}
	long contentLength = resource.contentLength();
	return (contentLength < 0 ? null : contentLength);
}
 
Example 14
private static long lengthOf(Resource resource) {
	// Don't consume InputStream...
	if (InputStreamResource.class != resource.getClass()) {
		try {
			return resource.contentLength();
		}
		catch (IOException ignored) {
		}
	}
	return -1;
}
 
Example 15
@Override
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	if (InputStreamResource.class == resource.getClass()) {
		return null;
	}
	long contentLength = resource.contentLength();
	return (contentLength < 0 ? null : contentLength);
}
 
Example 16
Source Project: spring-cloud-config   File: PathUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isResourceUnderLocation(Resource resource, Resource location)
		throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils
				.cleanPath(((ClassPathResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty()
			? locationPath : locationPath + "/");
	return (resourcePath.startsWith(locationPath)
			&& !isInvalidEncodedPath(resourcePath));
}
 
Example 17
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
	}
	else if (resource instanceof ServletContextResource) {
		resourcePath = ((ServletContextResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	if (!resourcePath.startsWith(locationPath)) {
		return false;
	}

	if (resourcePath.contains("%")) {
		// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
		if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) {
			if (logger.isTraceEnabled()) {
				logger.trace("Resolved resource path contains \"../\" after decoding: " + resourcePath);
			}
			return false;
		}
	}

	return true;
}
 
Example 18
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
	if (resource.getClass() != location.getClass()) {
		return false;
	}

	String resourcePath;
	String locationPath;

	if (resource instanceof UrlResource) {
		resourcePath = resource.getURL().toExternalForm();
		locationPath = StringUtils.cleanPath(location.getURL().toString());
	}
	else if (resource instanceof ClassPathResource) {
		resourcePath = ((ClassPathResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
	}
	else if (resource instanceof ServletContextResource) {
		resourcePath = ((ServletContextResource) resource).getPath();
		locationPath = StringUtils.cleanPath(((ServletContextResource) location).getPath());
	}
	else {
		resourcePath = resource.getURL().getPath();
		locationPath = StringUtils.cleanPath(location.getURL().getPath());
	}

	if (locationPath.equals(resourcePath)) {
		return true;
	}
	locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
	if (!resourcePath.startsWith(locationPath)) {
		return false;
	}

	if (resourcePath.contains("%")) {
		// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
		if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) {
			if (logger.isTraceEnabled()) {
				logger.trace("Resolved resource path contains \"../\" after decoding: " + resourcePath);
			}
			return false;
		}
	}

	return true;
}
 
Example 19
@Override
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	return (InputStreamResource.class == resource.getClass() ? null : resource.contentLength());
}