use of java.net.MalformedURLException in project checkstyle by checkstyle.
the class ImportControlLoader method load.
/**
* Loads the import control file from a file.
* @param uri the uri of the file to load.
* @return the root {@link ImportControl} object.
* @throws CheckstyleException if an error occurs.
*/
public static ImportControl load(final URI uri) throws CheckstyleException {
final InputStream inputStream;
try {
inputStream = uri.toURL().openStream();
} catch (final MalformedURLException ex) {
throw new CheckstyleException("syntax error in url " + uri, ex);
} catch (final IOException ex) {
throw new CheckstyleException("unable to find " + uri, ex);
}
final InputSource source = new InputSource(inputStream);
return load(source, uri);
}
use of java.net.MalformedURLException in project cogtool by cogtool.
the class WebCrawlImportDialog method isAllowedExtension.
protected boolean isAllowedExtension(String url) {
String extension = "";
try {
String path = new URL(url).getPath();
int extPos = path.lastIndexOf('.');
if (extPos != -1) {
extension = path.substring(extPos);
}
// otherwise, no extension!
} catch (MalformedURLException e) {
// TODO: Possibly, return false to eliminate from consideration?
return true;
}
if (extension.length() == 0) {
return true;
}
for (String allowedExtension : allowedExtensions) {
if (allowedExtension.equalsIgnoreCase(extension)) {
return true;
}
}
return false;
}
use of java.net.MalformedURLException in project crate by crate.
the class PluginLoader method findImplementations.
private Collection<Class<? extends Plugin>> findImplementations() {
if (!isAccessibleDirectory(pluginsPath, logger)) {
return Collections.emptyList();
}
File[] plugins = pluginsPath.toFile().listFiles();
if (plugins == null) {
return Collections.emptyList();
}
Collection<Class<? extends Plugin>> allImplementations = new ArrayList<>();
for (File plugin : plugins) {
if (!plugin.canRead()) {
logger.debug("[{}] is not readable.", plugin.getAbsolutePath());
continue;
}
// check if its an elasticsearch plugin
Path esDescriptorFile = plugin.toPath().resolve(PluginInfo.ES_PLUGIN_PROPERTIES);
try {
if (esDescriptorFile.toFile().exists()) {
continue;
}
} catch (Exception e) {
// ignore
}
List<URL> pluginUrls = new ArrayList<>();
logger.trace("--- adding plugin [{}]", plugin.getAbsolutePath());
try {
URL pluginURL = plugin.toURI().toURL();
// jar-hell check the plugin against the parent classloader
try {
checkJarHell(pluginURL);
} catch (Exception e) {
String msg = String.format(Locale.ENGLISH, "failed to load plugin %s due to jar hell", pluginURL);
logger.error(msg, e);
throw new RuntimeException(msg, e);
}
pluginUrls.add(pluginURL);
if (!plugin.isFile()) {
// gather files to add
List<File> libFiles = Lists.newArrayList();
File[] pluginFiles = plugin.listFiles();
if (pluginFiles != null) {
libFiles.addAll(Arrays.asList(pluginFiles));
}
File libLocation = new File(plugin, "lib");
if (libLocation.exists() && libLocation.isDirectory()) {
File[] pluginLibFiles = libLocation.listFiles();
if (pluginLibFiles != null) {
libFiles.addAll(Arrays.asList(pluginLibFiles));
}
}
// if there are jars in it, add it as well
for (File libFile : libFiles) {
if (!(libFile.getName().endsWith(".jar") || libFile.getName().endsWith(".zip"))) {
continue;
}
URL libURL = libFile.toURI().toURL();
// jar-hell check the plugin lib against the parent classloader
try {
checkJarHell(libURL);
pluginUrls.add(libURL);
} catch (Exception e) {
String msg = String.format(Locale.ENGLISH, "Library %s of plugin %s already loaded", libURL, pluginURL);
logger.error(msg, e);
throw new RuntimeException(msg, e);
}
}
}
} catch (MalformedURLException e) {
String msg = String.format(Locale.ENGLISH, "failed to add plugin [%s]", plugin);
logger.error(msg, e);
throw new RuntimeException(msg, e);
}
Collection<Class<? extends Plugin>> implementations = findImplementations(pluginUrls);
if (implementations == null || implementations.isEmpty()) {
String msg = String.format(Locale.ENGLISH, "Path [%s] does not contain a valid Crate or Elasticsearch plugin", plugin.getAbsolutePath());
RuntimeException e = new RuntimeException(msg);
logger.error(msg, e);
throw e;
}
jarsToLoad.addAll(pluginUrls);
allImplementations.addAll(implementations);
}
return allImplementations;
}
use of java.net.MalformedURLException in project cucumber-jvm by cucumber.
the class Helpers method jarFilePath.
static String jarFilePath(URL jarUrl) {
String urlFile = jarUrl.getFile();
int separatorIndex = urlFile.indexOf("!/");
if (separatorIndex == -1) {
throw new CucumberException("Expected a jar URL: " + jarUrl.toExternalForm());
}
try {
URL fileUrl = new URL(urlFile.substring(0, separatorIndex));
return filePath(fileUrl);
} catch (MalformedURLException e) {
throw new CucumberException(e);
}
}
use of java.net.MalformedURLException in project SimplifyReader by chentao0707.
the class BasicNetwork method performRequest.
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
Map<String, String> responseHeaders = Collections.emptyMap();
try {
// Gather headers.
Map<String, String> headers = new HashMap<String, String>();
addCacheHeaders(headers, request.getCacheEntry());
httpResponse = mHttpStack.performRequest(request, headers);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
responseHeaders = convertHeaders(httpResponse.getAllHeaders());
// Handle cache validation.
if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// A HTTP 304 response does not have all header fields. We
// have to use the header fields from the cache entry plus
// the new ones from the response.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
entry.responseHeaders.putAll(responseHeaders);
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// Some responses such as 204s do not have content. We must check.
if (httpResponse.getEntity() != null) {
responseContents = entityToBytes(httpResponse.getEntity());
} else {
// Add 0 byte response as a way of honestly representing a
// no-content request.
responseContents = new byte[0];
}
// if the request is slow, log it.
long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
logSlowRequests(requestLifetime, request, responseContents, statusLine);
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
return new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
int statusCode = 0;
NetworkResponse networkResponse = null;
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
} else {
throw new NoConnectionError(e);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
}
}
}
}
Aggregations