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 = "";
}
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;
}
}
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();
}
}
}
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();
}
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());
}
Aggregations