下面列出了java.net.MalformedURLException#initCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Look up the parent DirectoryEntry containing this Entry.
* If this Entry is the root of its filesystem, its parent is itself.
*/
private JSONObject getParent(String baseURLstr) throws JSONException, IOException {
try {
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(baseURLstr);
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
return fs.getParentForLocalURL(inputURL);
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
}
/**
* Creates or looks up a file.
*
* @param baseURLstr base directory
* @param path file/directory to lookup or create
* @param options specify whether to create or not
* @param directory if true look up directory, if false look up file
* @return a Entry object
* @throws FileExistsException
* @throws IOException
* @throws TypeMismatchException
* @throws EncodingException
* @throws JSONException
*/
private JSONObject getFile(String baseURLstr, String path, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
try {
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(baseURLstr);
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
return fs.getFileForLocalURL(inputURL, path, options, directory);
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
}
/**
* Returns a File that represents the current state of the file that this FileEntry represents.
*
* @return returns a JSONObject represent a W3C File object
*/
private JSONObject getFileMetadata(String baseURLstr) throws FileNotFoundException, JSONException, MalformedURLException {
try {
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(baseURLstr);
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
return fs.getFileMetadataForLocalURL(inputURL);
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
}
/**
* Deletes a directory and all of its contents, if any. In the event of an error
* [e.g. trying to delete a directory that contains a file that cannot be removed],
* some of the contents of the directory may be deleted.
* It is an error to attempt to delete the root directory of a filesystem.
*
* @return a boolean representing success of failure
* @throws FileExistsException
* @throws NoModificationAllowedException
* @throws MalformedURLException
*/
private boolean removeRecursively(String baseURLstr) throws FileExistsException, NoModificationAllowedException, MalformedURLException {
try {
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(baseURLstr);
// You can't delete the root directory.
if ("".equals(inputURL.path) || "/".equals(inputURL.path)) {
throw new NoModificationAllowedException("You can't delete the root directory");
}
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
return fs.recursiveRemoveFileAtLocalURL(inputURL);
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
}
/**
* Truncate the file to size
*/
private long truncateFile(String srcURLstr, long size) throws FileNotFoundException, IOException, NoModificationAllowedException {
try {
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(srcURLstr);
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
return fs.truncateFileAtURL(inputURL, size);
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
}
/**
* Creates or looks up a file.
*
* @param baseURLstr base directory
* @param path file/directory to lookup or create
* @param options specify whether to create or not
* @param directory if true look up directory, if false look up file
* @return a Entry object
* @throws FileExistsException
* @throws IOException
* @throws TypeMismatchException
* @throws EncodingException
* @throws JSONException
*/
private JSONObject getFile(String baseURLstr, String path, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
try {
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(baseURLstr);
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
return fs.getFileForLocalURL(inputURL, path, options, directory);
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
}
/**
* Decode a URI string (according to RFC 2396).
*
* Three-character sequences '%xy', where 'xy' is the two-digit
* hexadecimal representation of the lower 8-bits of a character,
* are decoded into the character itself.
*
* The string is subsequently converted using the specified encoding
*/
public static final String decode(String s, String enc)
throws MalformedURLException, UnsupportedEncodingException {
try {
return URLDecoder.decode(s, enc);
} catch (IllegalArgumentException iae) {
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
mue.initCause(iae);
throw mue;
}
}
/**
* Create a new {@code UrlResource} based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
* @param location the location (e.g. the file path within that protocol);
* also known as "scheme-specific part"
* @param fragment the fragment within that location (e.g. anchor on an HTML page,
* as following after a "#" separator)
* @throws MalformedURLException if the given URL specification is not valid
* @see java.net.URI#URI(String, String, String)
*/
public UrlResource(String protocol, String location, String fragment) throws MalformedURLException {
try {
this.uri = new URI(protocol, location, fragment);
this.url = this.uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString());
}
catch (URISyntaxException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
exToThrow.initCause(ex);
throw exToThrow;
}
}
/**
* Decode a URI string (according to RFC 2396).
*
* Three-character sequences '%xy', where 'xy' is the two-digit
* hexadecimal representation of the lower 8-bits of a character,
* are decoded into the character itself.
*
* The string is subsequently converted using the specified encoding
*/
public static final String decode(String s, String enc)
throws MalformedURLException, UnsupportedEncodingException {
try {
return URLDecoder.decode(s, enc);
} catch (IllegalArgumentException iae) {
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
mue.initCause(iae);
throw mue;
}
}
public static URL getEncodedURL(String urlStr) throws MalformedURLException {
URL url = new URL(urlStr);
String scheme = String.valueOf(url.getProtocol()).toLowerCase();
if (scheme.equals("http") || scheme.equals("https")) {
try {
return new URL(url.toURI().toASCIIString());
} catch (URISyntaxException e) {
MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
malformedURLException.initCause(e);
throw malformedURLException;
}
}
return url;
}
/**
* Decode a URI string (according to RFC 2396).
*
* Three-character sequences '%xy', where 'xy' is the two-digit
* hexadecimal representation of the lower 8-bits of a character,
* are decoded into the character itself.
*
* The string is subsequently converted using the specified encoding
*/
public static final String decode(String s, String enc)
throws MalformedURLException, UnsupportedEncodingException {
try {
return URLDecoder.decode(s, enc);
} catch (IllegalArgumentException iae) {
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
mue.initCause(iae);
throw mue;
}
}
/**
* Decode a URI string (according to RFC 2396).
*
* Three-character sequences '%xy', where 'xy' is the two-digit
* hexadecimal representation of the lower 8-bits of a character,
* are decoded into the character itself.
*
* The string is subsequently converted using the specified encoding
*/
public static final String decode(String s, String enc)
throws MalformedURLException, UnsupportedEncodingException {
try {
return URLDecoder.decode(s, enc);
} catch (IllegalArgumentException iae) {
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
mue.initCause(iae);
throw mue;
}
}
/**
* Allows the user to look up the Entry for a file or directory referred to by a local URI.
*
* @param uriString of the file/directory to look up
* @return a JSONObject representing a Entry from the filesystem
* @throws MalformedURLException if the url is not valid
* @throws FileNotFoundException if the file does not exist
* @throws IOException if the user can't read the file
* @throws JSONException
*/
private JSONObject resolveLocalFileSystemURI(String uriString) throws IOException, JSONException {
if (uriString == null) {
throw new MalformedURLException("Unrecognized filesystem URL");
}
Uri uri = Uri.parse(uriString);
boolean isNativeUri = false;
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(uri);
if (inputURL == null) {
/* Check for file://, content:// urls */
inputURL = resolveNativeUri(uri);
isNativeUri = true;
}
try {
Filesystem fs = this.filesystemForURL(inputURL);
if (fs == null) {
throw new MalformedURLException("No installed handlers for this URL");
}
if (fs.exists(inputURL)) {
if (!isNativeUri) {
// If not already resolved as native URI, resolve to a native URI and back to
// fix the terminating slash based on whether the entry is a directory or file.
inputURL = fs.toLocalUri(fs.toNativeUri(inputURL));
}
return fs.getEntryForLocalURL(inputURL);
}
} catch (IllegalArgumentException e) {
MalformedURLException mue = new MalformedURLException("Unrecognized filesystem URL");
mue.initCause(e);
throw mue;
}
throw new FileNotFoundException();
}
/**
* Create a new {@code UrlResource} based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
* @param location the location (e.g. the file path within that protocol);
* also known as "scheme-specific part"
* @param fragment the fragment within that location (e.g. anchor on an HTML page,
* as following after a "#" separator)
* @throws MalformedURLException if the given URL specification is not valid
* @see java.net.URI#URI(String, String, String)
*/
public UrlResource(String protocol, String location, String fragment) throws MalformedURLException {
try {
this.uri = new URI(protocol, location, fragment);
this.url = this.uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString());
}
catch (URISyntaxException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
exToThrow.initCause(ex);
throw exToThrow;
}
}
/**
* Decode a URI string (according to RFC 2396).
*
* Three-character sequences '%xy', where 'xy' is the two-digit
* hexadecimal representation of the lower 8-bits of a character,
* are decoded into the character itself.
*
* The string is subsequently converted using the specified encoding
*/
public static final String decode(String s, String enc)
throws MalformedURLException, UnsupportedEncodingException {
try {
return URLDecoder.decode(s, enc);
} catch (IllegalArgumentException iae) {
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
mue.initCause(iae);
throw mue;
}
}
/**
* Fetches a stream for the given URL, which is about to
* be loaded by the <code>setPage</code> method.
* This method is expected to have the the side effect of
* establishing the content type, and therefore setting the
* appropriate <code>EditorKit</code> to use for loading the stream.
* <p>
* If debugger is not running returns super implementation.
* <p>
* If debugger runs it will set content type to text/html.
* Forwarding is not supported is that case.
* <p>Control using sysprop org.openide.awt.SwingBrowserImpl.do-not-block-awt=true.
*
* @param page the URL of the page
*/
protected @Override InputStream getStream(URL page) throws IOException {
SwingUtilities.invokeLater(SwingBrowserImpl.this);
try {
// #53207: pre-read encoding from loaded URL
String charset = findEncodingFromURL(page.openStream());
LOG.log(Level.FINE, "Url " + page + " has charset " + charset); // NOI18N
if (charset != null) {
putClientProperty("charset", charset);
}
} catch( IllegalArgumentException iaE ) {
//#165266 - empty url
MalformedURLException e = new MalformedURLException();
e.initCause(iaE);
throw e;
}
// XXX debugger ought to set this temporarily
if (Boolean.getBoolean("org.openide.awt.SwingBrowserImpl.do-not-block-awt")) {
// try to set contentType quickly and return (don't block AWT Thread)
setContentType("text/html"); // NOI18N
return new FilteredInputStream(page.openConnection(), SwingBrowserImpl.this);
} else {
return super.getStream(page);
}
}
public static URL getEncodedURL(String urlStr) throws MalformedURLException {
URL url = new URL(urlStr);
String scheme = String.valueOf(url.getProtocol()).toLowerCase();
if (scheme.equals("http") || scheme.equals("https")) {
try {
return new URL(url.toURI().toASCIIString());
} catch (URISyntaxException e) {
MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
malformedURLException.initCause(e);
throw malformedURLException;
}
}
return url;
}
/**
* Decode a URI string (according to RFC 2396).
*
* Three-character sequences '%xy', where 'xy' is the two-digit
* hexadecimal representation of the lower 8-bits of a character,
* are decoded into the character itself.
*
* The string is subsequently converted using the specified encoding
*/
public static final String decode(String s, String enc)
throws MalformedURLException, UnsupportedEncodingException {
try {
return URLDecoder.decode(s, enc);
} catch (IllegalArgumentException iae) {
MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s);
mue.initCause(iae);
throw mue;
}
}
public static URL getEncodedURL(String urlStr) throws MalformedURLException {
URL url = new URL(urlStr);
String scheme = String.valueOf(url.getProtocol()).toLowerCase();
if (scheme.equals("http") || scheme.equals("https")) {
try {
return new URL(url.toURI().toASCIIString());
} catch (URISyntaxException e) {
MalformedURLException malformedURLException = new MalformedURLException(e.getMessage());
malformedURLException.initCause(e);
throw malformedURLException;
}
}
return url;
}
private Call buildCall() throws IOException {
if (call != null) {
return call;
}
connected = true;
if (doOutput) {
if (method.equals("GET")) {
method = "POST";
} else if (!permitsRequestBody(method)) {
throw new ProtocolException(method + " does not support writing");
}
}
if (requestHeaders.get("User-Agent") == null) {
requestHeaders.add("User-Agent", defaultUserAgent());
}
OutputStreamRequestBody requestBody = null;
if (permitsRequestBody(method)) {
String contentType = requestHeaders.get("Content-Type");
if (contentType == null) {
contentType = "application/x-www-form-urlencoded";
requestHeaders.add("Content-Type", contentType);
}
boolean stream = fixedContentLength != -1L || chunkLength > 0;
long contentLength = -1L;
String contentLengthString = requestHeaders.get("Content-Length");
if (fixedContentLength != -1L) {
contentLength = fixedContentLength;
} else if (contentLengthString != null) {
contentLength = Long.parseLong(contentLengthString);
}
requestBody = stream
? new StreamedRequestBody(contentLength)
: new BufferedRequestBody(contentLength);
requestBody.timeout.timeout(client.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
}
HttpUrl url;
try {
url = HttpUrl.get(getURL().toString());
} catch (IllegalArgumentException e) {
MalformedURLException malformedUrl = new MalformedURLException();
malformedUrl.initCause(e);
throw malformedUrl;
}
Request request = new Request.Builder()
.url(url)
.headers(requestHeaders.build())
.method(method, requestBody)
.build();
OkHttpClient.Builder clientBuilder = client.newBuilder();
//clientBuilder.interceptors().clear();
//clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);
//clientBuilder.networkInterceptors().clear();
clientBuilder.networkInterceptors().add(networkInterceptor);
// Use a separate dispatcher so that limits aren't impacted. But use the same executor service!
clientBuilder.dispatcher(new Dispatcher(client.dispatcher().executorService()));
// If we're currently not using caches, make sure the engine's client doesn't have one.
if (!getUseCaches()) {
clientBuilder.cache(null);
}
return call = clientBuilder.build().newCall(request);
}