Java Code Examples for org.springframework.util.ResourceUtils#isFileURL()

The following examples show how to use org.springframework.util.ResourceUtils#isFileURL() . 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: AbstractFileResolvingResource.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public long contentLength() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution
		File file = getFile();
		long length = file.length();
		if (length == 0L && !file.exists()) {
			throw new FileNotFoundException(getDescription() +
					" cannot be resolved in the file system for checking its content length");
		}
		return length;
	}
	else {
		// Try a URL connection content-length header
		URLConnection con = url.openConnection();
		customizeConnection(con);
		return con.getContentLengthLong();
	}
}
 
Example 2
Source File: FileUrlResource.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public boolean isWritable() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			File file = getFile();
			return (file.canWrite() && !file.isDirectory());
		}
		else {
			return true;
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 3
Source File: ElementSteps.java    From vividus with Apache License 2.0 6 votes vote down vote up
/**
 * This step uploads a file with the given relative path
 * <p>A <b>relative path</b> starts from some given working directory,
 * avoiding the need to provide the full absolute path
 * (i.e. <i>'about.jpeg'</i> is in the root directory
 * or <i>'/story/uploadfiles/about.png'</i>)</p>
 * @param locator The locator for the upload element
 * @param filePath relative path to the file to be uploaded
 * @see <a href="https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths"> <i>Absolute and
 * relative paths</i></a>
 * @throws IOException If an input or output exception occurred
 */
@When("I select element located `$locator` and upload file `$filePath`")
public void uploadFile(SearchAttributes locator, String filePath) throws IOException
{
    Resource resource = resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + filePath);
    if (!resource.exists())
    {
        resource = resourceLoader.getResource(ResourceUtils.FILE_URL_PREFIX + filePath);
    }
    File fileForUpload = ResourceUtils.isFileURL(resource.getURL()) ? resource.getFile()
            : unpackFile(resource, filePath);
    if (highlightingSoftAssert.assertTrue("File " + filePath + " exists", fileForUpload.exists()))
    {
        String fullFilePath = fileForUpload.getAbsolutePath();
        if (isRemoteExecution())
        {
            webDriverProvider.getUnwrapped(RemoteWebDriver.class).setFileDetector(new LocalFileDetector());
        }
        locator.getSearchParameters().setVisibility(Visibility.ALL);
        WebElement browse = baseValidations.assertIfElementExists(AN_ELEMENT, locator);
        if (browse != null)
        {
            browse.sendKeys(fullFilePath);
        }
    }
}
 
Example 4
Source File: AbstractFileResolvingResource.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long lastModified() throws IOException {
    URL url = getURL();
    if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
        // Proceed with file system resolution...
        return super.lastModified();
    } else {
        // Try a URL connection last-modified header...
        URLConnection con = url.openConnection();
        customizeConnection(con);
        return con.getLastModified();
    }
}
 
Example 5
Source File: AbstractFileResolvingResource.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long contentLength() throws IOException {
    URL url = getURL();
    if (ResourceUtils.isFileURL(url)) {
        // Proceed with file system resolution...
        return getFile().length();
    } else {
        // Try a URL connection content-length header...
        URLConnection con = url.openConnection();
        customizeConnection(con);
        return con.getContentLength();
    }
}
 
Example 6
Source File: AbstractFileResolvingResource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long contentLength() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution
		return getFile().length();
	}
	else {
		// Try a URL connection content-length header
		URLConnection con = url.openConnection();
		customizeConnection(con);
		return con.getContentLength();
	}
}
 
Example 7
Source File: ServletContextResource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation resolves "file:" URLs or alternatively delegates to
 * {@code ServletContext.getRealPath}, throwing a FileNotFoundException
 * if not found or not resolvable.
 * @see javax.servlet.ServletContext#getResource(String)
 * @see javax.servlet.ServletContext#getRealPath(String)
 */
@Override
public File getFile() throws IOException {
	URL url = this.servletContext.getResource(this.path);
	if (url != null && ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution...
		return super.getFile();
	}
	else {
		String realPath = WebUtils.getRealPath(this.servletContext, this.path);
		return new File(realPath);
	}
}
 
Example 8
Source File: ServletContextResource.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * This implementation resolves "file:" URLs or alternatively delegates to
 * {@code ServletContext.getRealPath}, throwing a FileNotFoundException
 * if not found or not resolvable.
 * @see javax.servlet.ServletContext#getResource(String)
 * @see javax.servlet.ServletContext#getRealPath(String)
 */
@Override
public File getFile() throws IOException {
	URL url = this.servletContext.getResource(this.path);
	if (url != null && ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution...
		return super.getFile();
	}
	else {
		String realPath = WebUtils.getRealPath(this.servletContext, this.path);
		return new File(realPath);
	}
}
 
Example 9
Source File: AbstractFileResolvingResource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public long contentLength() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution...
		return getFile().length();
	}
	else {
		// Try a URL connection content-length header...
		URLConnection con = url.openConnection();
		customizeConnection(con);
		return con.getContentLength();
	}
}
 
Example 10
Source File: DefaultResourceLoader.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Resource getResource(String location) {
	Assert.notNull(location, "Location must not be null");

	for (ProtocolResolver protocolResolver : this.protocolResolvers) {
		Resource resource = protocolResolver.resolve(location, this);
		if (resource != null) {
			return resource;
		}
	}

	if (location.startsWith("/")) {
		return getResourceByPath(location);
	}
	else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
		return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
	}
	else {
		try {
			// Try to parse the location as a URL...
			URL url = new URL(location);
			return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
		}
		catch (MalformedURLException ex) {
			// No URL -> resolve as resource path.
			return getResourceByPath(location);
		}
	}
}
 
Example 11
Source File: AbstractFileResolvingResource.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean exists() {
    try {
        URL url = getURL();
        if (ResourceUtils.isFileURL(url)) {
            // Proceed with file system resolution...
            return getFile().exists();
        } else {
            // Try a URL connection content-length header...
            URLConnection con = url.openConnection();
            customizeConnection(con);
            HttpURLConnection httpCon = (con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
            if (httpCon != null) {
                int code = httpCon.getResponseCode();
                if (code == HttpURLConnection.HTTP_OK) {
                    return true;
                } else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
                    return false;
                }
            }
            if (con.getContentLength() >= 0) {
                return true;
            }
            if (httpCon != null) {
                // no HTTP OK status, and no content-length header: give up
                httpCon.disconnect();
                return false;
            } else {
                // Fall back to stream existence: can we open the stream?
                try (InputStream is = getInputStream()) {
                    return true;
                }
            }
        }
    } catch (IOException ex) {
        return false;
    }
}
 
Example 12
Source File: AbstractFileResolvingResource.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean isReadable() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			File file = getFile();
			return (file.canRead() && !file.isDirectory());
		}
		else {
			// Try InputStream resolution for jar resources
			URLConnection con = url.openConnection();
			customizeConnection(con);
			if (con instanceof HttpURLConnection) {
				HttpURLConnection httpCon = (HttpURLConnection) con;
				int code = httpCon.getResponseCode();
				if (code != HttpURLConnection.HTTP_OK) {
					httpCon.disconnect();
					return false;
				}
			}
			long contentLength = con.getContentLengthLong();
			if (contentLength > 0) {
				return true;
			}
			else if (contentLength == 0) {
				// Empty file or directory -> not considered readable...
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				getInputStream().close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 13
Source File: ServletContextResource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation resolves "file:" URLs or alternatively delegates to
 * {@code ServletContext.getRealPath}, throwing a FileNotFoundException
 * if not found or not resolvable.
 * @see javax.servlet.ServletContext#getResource(String)
 * @see javax.servlet.ServletContext#getRealPath(String)
 */
@Override
public File getFile() throws IOException {
	URL url = this.servletContext.getResource(this.path);
	if (url != null && ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution...
		return super.getFile();
	}
	else {
		String realPath = WebUtils.getRealPath(this.servletContext, this.path);
		return new File(realPath);
	}
}
 
Example 14
Source File: AbstractFileResolvingResource.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isReadable() {
    try {
        URL url = getURL();
        if (ResourceUtils.isFileURL(url)) {
            // Proceed with file system resolution...
            File file = getFile();
            return (file.canRead() && !file.isDirectory());
        } else {
            return true;
        }
    } catch (IOException ex) {
        return false;
    }
}
 
Example 15
Source File: ServletContextResource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean isFile() {
	try {
		URL url = this.servletContext.getResource(this.path);
		if (url != null && ResourceUtils.isFileURL(url)) {
			return true;
		}
		else {
			return (this.servletContext.getRealPath(this.path) != null);
		}
	}
	catch (MalformedURLException ex) {
		return false;
	}
}
 
Example 16
Source File: DefaultResourceLoader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Resource getResource(String location) {
	Assert.notNull(location, "Location must not be null");

	for (ProtocolResolver protocolResolver : this.protocolResolvers) {
		Resource resource = protocolResolver.resolve(location, this);
		if (resource != null) {
			return resource;
		}
	}

	if (location.startsWith("/")) {
		return getResourceByPath(location);
	}
	else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
		return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
	}
	else {
		try {
			// Try to parse the location as a URL...
			URL url = new URL(location);
			return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
		}
		catch (MalformedURLException ex) {
			// No URL -> resolve as resource path.
			return getResourceByPath(location);
		}
	}
}
 
Example 17
Source File: PortletContextResource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation resolves "file:" URLs or alternatively delegates to
 * {@code PortletContext.getRealPath}, throwing a FileNotFoundException
 * if not found or not resolvable.
 * @see javax.portlet.PortletContext#getResource(String)
 * @see javax.portlet.PortletContext#getRealPath(String)
 */
@Override
public File getFile() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution...
		return super.getFile();
	}
	else {
		String realPath = PortletUtils.getRealPath(this.portletContext, this.path);
		return new File(realPath);
	}
}
 
Example 18
Source File: AbstractFileResolvingResource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean exists() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			return getFile().exists();
		}
		else {
			// Try a URL connection content-length header
			URLConnection con = url.openConnection();
			customizeConnection(con);
			HttpURLConnection httpCon =
					(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
			if (httpCon != null) {
				int code = httpCon.getResponseCode();
				if (code == HttpURLConnection.HTTP_OK) {
					return true;
				}
				else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
					return false;
				}
			}
			if (con.getContentLength() >= 0) {
				return true;
			}
			if (httpCon != null) {
				// no HTTP OK status, and no content-length header: give up
				httpCon.disconnect();
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				InputStream is = getInputStream();
				is.close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 19
Source File: AbstractFileResolvingResource.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean exists() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution...
			return getFile().exists();
		}
		else {
			// Try a URL connection content-length header...
			URLConnection con = url.openConnection();
			customizeConnection(con);
			HttpURLConnection httpCon =
					(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
			if (httpCon != null) {
				int code = httpCon.getResponseCode();
				if (code == HttpURLConnection.HTTP_OK) {
					return true;
				}
				else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
					return false;
				}
			}
			if (con.getContentLength() >= 0) {
				return true;
			}
			if (httpCon != null) {
				// no HTTP OK status, and no content-length header: give up
				httpCon.disconnect();
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				InputStream is = getInputStream();
				is.close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 20
Source File: AbstractFileResolvingResource.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean exists() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			return getFile().exists();
		}
		else {
			// Try a URL connection content-length header
			URLConnection con = url.openConnection();
			customizeConnection(con);
			HttpURLConnection httpCon =
					(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
			if (httpCon != null) {
				int code = httpCon.getResponseCode();
				if (code == HttpURLConnection.HTTP_OK) {
					return true;
				}
				else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
					return false;
				}
			}
			if (con.getContentLengthLong() > 0) {
				return true;
			}
			if (httpCon != null) {
				// No HTTP OK status, and no content-length header: give up
				httpCon.disconnect();
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				getInputStream().close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}