Hi Team,
Is there a programmatic way to determine whether an URL with ‘knime:’ protocol is pointing to a knime server or not, basically to check if it is pointing to a non-local file?
Thanks
Ravikiran
gab1one
November 20, 2019, 10:09am
2
Hi @ravikiran ,
take a look at the following method in the FileUtil:
/**
* Tries to resolve the given URL into a local path. The method can handle all URLs that are backed by local files
* especially the 'knime' protocol. If the URL does not denote a local path, <code>null</code> is returned.
*
* @param url any URL
* @return the path or <code>null</code>
* @throws IOException if an I/O error occurs while resolving the URL
* @throws URISyntaxException if the passed URL does not conform with RFC2396 for URIs
* @since 2.11
*/
public static Path resolveToPath(final URL url) throws IOException, URISyntaxException {
if (looksLikeUNC(url)) {
return Paths.get(url.toURI());
} else {
URL resolvedUrl = url.openConnection().getURL();
if (resolvedUrl.getProtocol().equalsIgnoreCase("file")) {
String pathString = resolvedUrl.getPath();
if (pathString.contains(" ")) {
// fix non-encoded URL path
URL fixedUrl = new URL(url.toString().replace(" ", "%20"));
best,
Gabriel
5 Likes
Hi Gabriel,
Another follow up question:
If I specify my URL to point to a server like, knime://knime-server/path/to/file and if the code gets executed within the server, by any chance will resolveToPath() be able to resolve to the local file in my server?
Thanks
Ravikiran
gab1one
November 21, 2019, 10:26am
4
You are not able to directly resolve local files on the server for security reasons as well as practical ones, if you are using distributed executors, the workflow may run on a different machine than the KNIME Server. You can however read and write files on the server (and locally) by using the following methods:
conn.setReadTimeout(timeout);
return conn.getInputStream();
}
/** Opens a buffered input stream for the location (file path or URL).
* @param loc the location; can be both a file path or URL.
* @return a buffered input stream.
* @throws IOException Forwarded from file input stream or url.openStream.
* @throws InvalidSettingsException If the argument is invalid or null.
* @since 2.6 */
public static InputStream openInputStream(final String loc)
throws IOException, InvalidSettingsException {
return openInputStream(loc, urlTimeout);
}
/** Opens a buffered input stream for the location (file path or URL).
* @param loc the location; can be both a file path or URL.
* @param timeoutInMilliseconds The timeout in milliseconds ({@code >0}).
* @return a buffered input stream.
* @throws IOException Forwarded from file input stream or url.openStream.
* @throws InvalidSettingsException If the argument is invalid or null.
* Tries to open an output URL connection to the given URL. If the URL is an http URL the given HTTP method is set
* and the response code following the initial request is evaluated. Otherwise the connection is simply configured
* for output.
*
* @param url any URL
* @param httpMethod the HTTP method in case the url is http
* @return a URLConnection configured for output
* @throws IOException if an I/O error occurs
* @since 2.11
*/
public static URLConnection openOutputConnection(final URL url, final String httpMethod) throws IOException {
return openOutputConnection(url, httpMethod, Collections.emptyMap());
}
/**
* Tries to open an output URL connection to the given URL. If the URL is an http URL the given HTTP method is set
* and the response code following the initial request is evaluated. Otherwise the connection is simply configured
* for output. You can pass optional properties (or headers) that are set before the connection is opened, e.g.
* a "Content-Type" for the data you are going to send.
*
* @param url any URL
Note that you can only write to and read from the workflow repository on the server.
best,
Gabriel
1 Like
system
Closed
November 28, 2019, 10:31am
5
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.