String toExternalForm(URL url, boolean escapeIllegalCharacters) { StringBuilder result = new StringBuilder(); result.append(url.getProtocol()); result.append(':'); String ref = url.getRef(); if (ref != null) { result.append('#');
/** * Parse a URL query and fragment parameters into a key-value bundle. * * @param url the URL to parse * @return a dictionary bundle of keys and values */ public static Map<String, String> parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("fbconnect", "http"); try { URL u = new URL(url); Map<String, String> params = decodeUrl(u.getQuery()); params.putAll(decodeUrl(u.getRef())); return params; } catch (MalformedURLException e) { return new HashMap<>(); } }
urlFile = new File(new URI(resourceURL.toString().split("#")[0])); } catch (IllegalArgumentException | URISyntaxException e) { urlFile = new File(resourceURL.getPath()); timestamp = urlConnection.getLastModified(); if (!noSubdir) { String path = resourceURL.getHost() + resourceURL.getPath(); cacheSubdir = new File(cacheSubdir, path.substring(0, path.lastIndexOf('/') + 1)); if (resourceURL.getRef() != null) { String newName = resourceURL.getRef();
assertTrue(graphic instanceof ExternalGraphic); assertEquals( ((ExternalGraphic) graphic).getLocation().getPath(), iconFile.toURI().toURL().getPath()); assertEquals("param1=1", ((ExternalGraphic) graphic).getLocation().getQuery()); assertEquals("textAfterHash", ((ExternalGraphic) graphic).getLocation().getRef());
/** * Parse a URL query and fragment parameters into a key-value bundle. * * @param url the URL to parse * @return a dictionary bundle of keys and values */ public static Map<String, String> parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("fbconnect", "http"); try { URL u = new URL(url); Map<String, String> params = decodeUrl(u.getQuery()); params.putAll(decodeUrl(u.getRef())); return params; } catch (MalformedURLException e) { return new HashMap<>(); } }
public URL convertToURLEscapingIllegalCharacters(String string){ try { String decodedURL = URLDecoder.decode(string, "UTF-8"); URL url = new URL(decodedURL); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toURL(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
/** * Encode URL * * @param urlStr string URL * @return the encoded URL * @throws URISyntaxException URI syntax * @throws MalformedURLException URL malformed */ public static String encodeURL(String urlStr) throws URISyntaxException, MalformedURLException { URL url = new URL(urlStr); URI constructed = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return constructed.toString(); }
public URL parseUrl(String s) throws Exception { URL u = new URL(s); return new URI( u.getProtocol(), u.getAuthority(), u.getPath(), u.getQuery(), u.getRef()). toURL(); }
import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); //http System.out.println("authority = " + aURL.getAuthority()); //example.com:80 System.out.println("host = " + aURL.getHost()); //example.com System.out.println("port = " + aURL.getPort()); //80 System.out.println("path = " + aURL.getPath()); // /docs/books/tutorial/index.html System.out.println("query = " + aURL.getQuery()); //name=networking System.out.println("filename = " + aURL.getFile()); ///docs/books/tutorial/index.html?name=networking System.out.println("ref = " + aURL.getRef()); //DOWNLOADING } }
/** * URL may contain spaces and other nasties that will cause a failure. * * @param existing the existing URL to transform * @return the new URL, or old one if there was a failure */ private static URL reformat(URL existing) { try { URL url = new URL(existing.toString()); URI uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); return url; } catch (MalformedURLException e) { return existing; } catch (URISyntaxException e) { return existing; } }
private String evaluate(URL url, int index) { if (url == null || index < 0 || index >= partnames.length) { return null; } switch (partnames[index]) { case HOST : return url.getHost(); case PATH : return url.getPath(); case QUERY : return url.getQuery(); case REF : return url.getRef(); case PROTOCOL : return url.getProtocol(); case FILE : return url.getFile(); case AUTHORITY : return url.getAuthority(); case USERINFO : return url.getUserInfo(); case QUERY_WITH_KEY: return evaluateQuery(url.getQuery(), paths[index]); case NULLNAME: default : return null; } }
/** * This provider can handle uris like https://git-codecommit.$AWS_REGION.amazonaws.com/v1/repos/$REPO * @see org.springframework.cloud.config.server.credentials.GitCredentialsProvider#canHandleUri(java.lang.String) */ public static boolean canHandle(String uri) { if (!hasText(uri)) { return false; } try { URL url = new URL(uri); URI u = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); if (u.getScheme().equals("https")) { String host = u.getHost(); if (host.endsWith(".amazonaws.com") && host.startsWith("git-codecommit.")) { return true; } } } catch (Throwable t) { // ignore all, we can't handle it } return false; }
private String evaluate(URL url, int index) { if (url == null || index < 0 || index >= partnames.length) { return null; } switch (partnames[index]) { case HOST : return url.getHost(); case PATH : return url.getPath(); case QUERY : return url.getQuery(); case REF : return url.getRef(); case PROTOCOL : return url.getProtocol(); case FILE : return url.getFile(); case AUTHORITY : return url.getAuthority(); case USERINFO : return url.getUserInfo(); case QUERY_WITH_KEY: return evaluateQuery(url.getQuery(), paths[index]); case NULLNAME: default : return null; } }
/** * This method splits a URL into parts and return a map containing them. * * @param url * @return */ public static Map<URLParts, String> splitURL(URL url) { Map<URLParts, String> urlParts = new HashMap<>(); urlParts.put(URLParts.PROTOCOL, url.getProtocol()); urlParts.put(URLParts.USERINFO, url.getUserInfo()); urlParts.put(URLParts.AUTHORITY, url.getAuthority()); urlParts.put(URLParts.HOST, url.getHost()); urlParts.put(URLParts.PATH, url.getPath()); urlParts.put(URLParts.QUERY, url.getQuery()); urlParts.put(URLParts.FILENAME, url.getFile()); urlParts.put(URLParts.REF, url.getRef()); int port = url.getPort(); if(port!=-1) { urlParts.put(URLParts.PORT, String.valueOf(port)); } else { urlParts.put(URLParts.PORT, null); } return urlParts; }