Search in sources :

Example 21 with MalformedURLException

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);
}
Also used : MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException)

Example 22 with MalformedURLException

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Point(org.eclipse.swt.graphics.Point)

Example 23 with MalformedURLException

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;
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) File(java.io.File) Plugin(io.crate.Plugin)

Example 24 with MalformedURLException

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) CucumberException(cucumber.runtime.CucumberException) URL(java.net.URL)

Example 25 with MalformedURLException

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);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) HttpResponse(org.apache.http.HttpResponse) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) StatusLine(org.apache.http.StatusLine) Entry(com.android.volley.Cache.Entry) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

MalformedURLException (java.net.MalformedURLException)1319 URL (java.net.URL)984 IOException (java.io.IOException)397 File (java.io.File)324 ArrayList (java.util.ArrayList)132 HttpURLConnection (java.net.HttpURLConnection)108 InputStream (java.io.InputStream)106 HashMap (java.util.HashMap)89 URISyntaxException (java.net.URISyntaxException)78 URI (java.net.URI)76 Map (java.util.Map)69 URLClassLoader (java.net.URLClassLoader)65 InputStreamReader (java.io.InputStreamReader)61 BufferedReader (java.io.BufferedReader)58 FileNotFoundException (java.io.FileNotFoundException)53 URLConnection (java.net.URLConnection)52 HashSet (java.util.HashSet)50 Test (org.junit.Test)45 JSONException (org.json.JSONException)38 FileInputStream (java.io.FileInputStream)33