Search in sources :

Example 51 with MalformedURLException

use of java.net.MalformedURLException in project druid by druid-io.

the class LookupCoordinatorManager method deleteAllOnTier.

void deleteAllOnTier(final String tier, final Collection<String> dropLookups) throws ExecutionException, InterruptedException, IOException {
    if (dropLookups.isEmpty()) {
        LOG.debug("Nothing to drop");
        return;
    }
    final Collection<URL> urls = getAllHostsAnnounceEndpoint(tier);
    final List<ListenableFuture<?>> futures = new ArrayList<>(urls.size());
    for (final URL url : urls) {
        futures.add(executorService.submit(new Runnable() {

            @Override
            public void run() {
                for (final String drop : dropLookups) {
                    final URL lookupURL;
                    try {
                        lookupURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), String.format("%s/%s", url.getFile(), drop));
                    } catch (MalformedURLException e) {
                        throw new ISE(e, "Error creating url for [%s]/[%s]", url, drop);
                    }
                    try {
                        deleteOnHost(lookupURL);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        LOG.warn("Delete [%s] interrupted", lookupURL);
                        throw Throwables.propagate(e);
                    } catch (IOException | ExecutionException e) {
                        // Don't raise as ExecutionException. Just log and continue
                        LOG.makeAlert(e, "Error deleting [%s]", lookupURL).emit();
                    }
                }
            }
        }));
    }
    final ListenableFuture allFuture = Futures.allAsList(futures);
    try {
        allFuture.get(lookupCoordinatorManagerConfig.getUpdateAllTimeout().getMillis(), TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        // This should cause Interrupted exceptions on the offending ones
        allFuture.cancel(true);
        throw new ExecutionException("Timeout in updating hosts! Attempting to cancel", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ISE(io.druid.java.util.common.ISE) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 52 with MalformedURLException

use of java.net.MalformedURLException in project checkstyle by checkstyle.

the class CommonUtils method getUriByFilename.

/**
     * Resolve the specified filename to a URI.
     * @param filename name os the file
     * @return resolved header file URI
     * @throws CheckstyleException on failure
     */
public static URI getUriByFilename(String filename) throws CheckstyleException {
    // figure out if this is a File or a URL
    URI uri;
    try {
        final URL url = new URL(filename);
        uri = url.toURI();
    } catch (final URISyntaxException | MalformedURLException ignored) {
        uri = null;
    }
    if (uri == null) {
        final File file = new File(filename);
        if (file.exists()) {
            uri = file.toURI();
        } else {
            // check to see if the file is in the classpath
            try {
                final URL configUrl = CommonUtils.class.getResource(filename);
                if (configUrl == null) {
                    throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename);
                }
                uri = configUrl.toURI();
            } catch (final URISyntaxException ex) {
                throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename, ex);
            }
        }
    }
    return uri;
}
Also used : MalformedURLException(java.net.MalformedURLException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) URL(java.net.URL)

Example 53 with MalformedURLException

use of java.net.MalformedURLException in project deeplearning4j by deeplearning4j.

the class ClassPathResource method extractActualUrl.

/**
     * Extracts parent Jar URL from original ClassPath entry URL.
     *
     * @param jarUrl Original URL of the resource
     * @return URL of the Jar file, containing requested resource
     * @throws MalformedURLException
     */
private URL extractActualUrl(URL jarUrl) throws MalformedURLException {
    String urlFile = jarUrl.getFile();
    int separatorIndex = urlFile.indexOf("!/");
    if (separatorIndex != -1) {
        String jarFile = urlFile.substring(0, separatorIndex);
        try {
            return new URL(jarFile);
        } catch (MalformedURLException var5) {
            if (!jarFile.startsWith("/")) {
                jarFile = "/" + jarFile;
            }
            return new URL("file:" + jarFile);
        }
    } else {
        return jarUrl;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Example 54 with MalformedURLException

use of java.net.MalformedURLException in project openhab1-addons by openhab.

the class HttpTransport method execute.

public String execute(String request, int connectTimeout, int readTimeout) {
    if (request != null && !request.trim().equals("")) {
        HttpURLConnection connection = null;
        StringBuilder response = new StringBuilder();
        BufferedReader in = null;
        try {
            URL url = new URL(this.uri + request);
            connection = (HttpURLConnection) url.openConnection();
            int responseCode = -1;
            if (connection != null) {
                connection.setConnectTimeout(connectTimeout);
                connection.setReadTimeout(readTimeout);
                try {
                    connection.connect();
                    responseCode = connection.getResponseCode();
                } catch (SocketTimeoutException e) {
                    logger.warn(e.getMessage() + " : " + request);
                    return null;
                }
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    String inputLine = null;
                    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                } else {
                    response = null;
                }
            }
            if (response != null) {
                return response.toString();
            }
        } catch (MalformedURLException e) {
            logger.error("MalformedURLException by executing jsonRequest: " + request + " ; " + e.getLocalizedMessage());
        } catch (IOException e) {
            logger.error("IOException by executing jsonRequest: " + request + " ; " + e.getLocalizedMessage());
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) SocketTimeoutException(java.net.SocketTimeoutException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL)

Example 55 with MalformedURLException

use of java.net.MalformedURLException in project neo4j by neo4j.

the class URLTypeCaster method getList.

@Override
Object[] getList(GraphDatabaseAPI graphDb, ParameterList parameters, String name) throws BadInputException {
    URI[] uris = parameters.getUriList(name);
    URL[] urls = new URL[uris.length];
    try {
        for (int i = 0; i < urls.length; i++) {
            urls[i] = uris[i].toURL();
        }
    } catch (MalformedURLException e) {
        throw new BadInputException(e);
    }
    return urls;
}
Also used : MalformedURLException(java.net.MalformedURLException) BadInputException(org.neo4j.server.rest.repr.BadInputException) URI(java.net.URI) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)3838 URL (java.net.URL)2885 IOException (java.io.IOException)1194 File (java.io.File)910 ArrayList (java.util.ArrayList)372 InputStream (java.io.InputStream)367 HttpURLConnection (java.net.HttpURLConnection)295 URISyntaxException (java.net.URISyntaxException)270 URI (java.net.URI)239 InputStreamReader (java.io.InputStreamReader)226 BufferedReader (java.io.BufferedReader)208 HashMap (java.util.HashMap)200 URLClassLoader (java.net.URLClassLoader)168 Map (java.util.Map)166 URLConnection (java.net.URLConnection)148 FileNotFoundException (java.io.FileNotFoundException)137 Matcher (java.util.regex.Matcher)132 Test (org.junit.Test)129 UnsupportedEncodingException (java.io.UnsupportedEncodingException)119 Pattern (java.util.regex.Pattern)113