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

The following examples show how to use org.springframework.core.io.Resource#getClass() . 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: ResourcesFileSource.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
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
Source File: ResourceRegionEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 3
Source File: PathUtils.java    From spring-cloud-config with 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 4
Source File: ResourceHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 5
Source File: ResourceHttpMessageWriter.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 6
Source File: ResourceHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 7
Source File: PathResourceResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 8
Source File: PathResourceResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 9
Source File: PathResourceLookupFunction.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 10
Source File: PathResourceResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 11
Source File: ResourceHttpMessageWriter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 12
Source File: ResourceHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 13
Source File: PathResourceLookupFunction.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 14
Source File: PathResourceResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 15
Source File: ResourceRegionEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 16
Source File: PathResourceLookupFunction.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 17
Source File: PathResourceResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: PathResourceResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
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
Source File: ResourceHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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());
}