Search in sources :

Example 36 with MalformedURLException

use of java.net.MalformedURLException in project groovy-core by groovy.

the class AbstractHttpServlet method generateNamePrefixOnce.

protected void generateNamePrefixOnce() {
    URI uri = null;
    String realPath = servletContext.getRealPath("/");
    //prevent NPE if in .war
    if (realPath != null) {
        uri = new File(realPath).toURI();
    }
    try {
        URL res = servletContext.getResource("/");
        if (res != null) {
            uri = res.toURI();
        }
    } catch (MalformedURLException ignore) {
    } catch (URISyntaxException ignore) {
    }
    if (uri != null) {
        try {
            namePrefix = uri.toURL().toExternalForm();
            return;
        } catch (MalformedURLException e) {
            log("generateNamePrefixOnce [ERROR] Malformed URL for base path / == '" + uri + '\'', e);
        }
    }
    namePrefix = "";
}
Also used : MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) URL(java.net.URL)

Example 37 with MalformedURLException

use of java.net.MalformedURLException in project hibernate-orm by hibernate.

the class StandardArchiveDescriptorFactory method adjustJarFileEntryUrl.

@Override
public URL adjustJarFileEntryUrl(URL url, URL rootUrl) {
    final String protocol = url.getProtocol();
    final boolean check = StringHelper.isEmpty(protocol) || "file".equals(protocol) || "vfszip".equals(protocol) || "vfsfile".equals(protocol);
    if (!check) {
        return url;
    }
    final String filePart = extractLocalFilePath(url);
    if (filePart.startsWith("/") || new File(url.getFile()).isAbsolute()) {
        // the URL is already an absolute form
        return url;
    } else {
        // prefer to resolve the relative URL relative to the root PU URL per
        // JPA 2.0 clarification.
        final File rootUrlFile = new File(extractLocalFilePath(rootUrl));
        try {
            if (rootUrlFile.isDirectory()) {
                // The PU root is a directory (exploded).  Here we can just build
                // the relative File reference and use the Filesystem API to convert
                // to URI and then a URL
                final File combined = new File(rootUrlFile, filePart);
                // make sure it exists..
                if (combined.exists()) {
                    return combined.toURI().toURL();
                }
            } else {
                // handle the nested entry reference (the !/ part).
                return new URL("jar:" + protocol + "://" + rootUrlFile.getAbsolutePath() + "!/" + filePart);
            }
        } catch (MalformedURLException e) {
            // allow to pass through to return the original URL
            log.debugf(e, "Unable to adjust relative <jar-file/> URL [%s] relative to root URL [%s]", filePart, rootUrlFile.getAbsolutePath());
        }
        return url;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) File(java.io.File) URL(java.net.URL)

Example 38 with MalformedURLException

use of java.net.MalformedURLException in project iosched by google.

the class ServerUtilities method post.

/**
     * Issue a POST request to the server.
     *
     * @param endpoint POST address.
     * @param params   request parameters.
     * @throws java.io.IOException propagated from POST.
     */
private static void post(String endpoint, Map<String, String> params, String key) throws IOException {
    URL url;
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    params.put("key", key);
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    LOGW(TAG, "Posting to " + url);
    LOGV(TAG, "Posting '" + body + "'");
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setChunkedStreamingMode(0);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        conn.setRequestProperty("Content-Length", Integer.toString(body.length()));
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(body.getBytes());
        out.close();
        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
            throw new IOException("Post failed with error code " + status);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Entry(java.util.Map.Entry) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 39 with MalformedURLException

use of java.net.MalformedURLException in project j2objc by google.

the class Support_Resources method getURL.

public static String getURL(String name) {
    String folder = null;
    String fileName = name;
    File resources = createTempFolder();
    int index = name.lastIndexOf("/");
    if (index != -1) {
        folder = name.substring(0, index);
        name = name.substring(index + 1);
    }
    copyFile(resources, folder, name);
    URL url = null;
    String resPath = resources.toString();
    if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') {
        resPath = resPath.substring(1);
    }
    try {
        url = new URL("file:/" + resPath + "/" + fileName);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return url.toString();
}
Also used : MalformedURLException(java.net.MalformedURLException) File(java.io.File) URL(java.net.URL)

Example 40 with MalformedURLException

use of java.net.MalformedURLException in project j2objc by google.

the class URLTest method testSquareBracketsWithHostname.

public void testSquareBracketsWithHostname() throws Exception {
    try {
        new URL("http://[www.android.com]/");
        fail();
    } catch (MalformedURLException expected) {
    }
    URL url = new URL("http", "[www.android.com]", "/");
    assertEquals("[www.android.com]", url.getHost());
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)1597 URL (java.net.URL)1204 IOException (java.io.IOException)482 File (java.io.File)405 ArrayList (java.util.ArrayList)168 InputStream (java.io.InputStream)135 HttpURLConnection (java.net.HttpURLConnection)131 HashMap (java.util.HashMap)100 URISyntaxException (java.net.URISyntaxException)98 URI (java.net.URI)95 Map (java.util.Map)80 URLClassLoader (java.net.URLClassLoader)77 InputStreamReader (java.io.InputStreamReader)75 BufferedReader (java.io.BufferedReader)72 URLConnection (java.net.URLConnection)62 FileNotFoundException (java.io.FileNotFoundException)59 Test (org.junit.Test)54 HashSet (java.util.HashSet)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)44 JSONException (org.json.JSONException)40