Refine search
/** * Create a new {@code UrlResource} based on a URI specification. * <p>The given parts will automatically get encoded if necessary. * @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon); * also known as "scheme" * @param location the location (e.g. the file path within that protocol); * also known as "scheme-specific part" * @param fragment the fragment within that location (e.g. anchor on an HTML page, * as following after a "#" separator) * @throws MalformedURLException if the given URL specification is not valid * @see java.net.URI#URI(String, String, String) */ public UrlResource(String protocol, String location, @Nullable String fragment) throws MalformedURLException { try { this.uri = new URI(protocol, location, fragment); this.url = this.uri.toURL(); this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString()); } catch (URISyntaxException ex) { MalformedURLException exToThrow = new MalformedURLException(ex.getMessage()); exToThrow.initCause(ex); throw exToThrow; } }
/** * Method to get data from api * @param url String url to fetch data from * @return arr: Return result in form of JSONArray object */ private JSONArray getDataFromApi(String url) { URI uri = null; try { uri = new URI(url); } catch (URISyntaxException e1) { e1.printStackTrace(); } JSONTokener tokener = null; try { tokener = new JSONTokener(uri.toURL().openStream()); } catch (Exception e1) { e1.printStackTrace(); } JSONArray arr = new JSONArray(tokener); return arr; }
private static URI validateIssuer(String issuer) throws URISyntaxException { try { new URL(issuer); } catch (MalformedURLException x) { throw new URISyntaxException(issuer, x.getMessage()); } return new URI(issuer); }
/** * Give a URI break off any URI options and store them in a Key / Value Mapping. * * @param uri The URI whose query should be extracted and processed. * @return A Mapping of the URI options. * @throws java.net.URISyntaxException */ public static Map<String, String> parseQuery(String uri) throws URISyntaxException { try { uri = uri.substring(uri.lastIndexOf("?") + 1); // get only the relevant part of the query Map<String, String> rc = new HashMap<>(); if (uri != null && !uri.isEmpty()) { parseParameters(rc, uri.split("[&;]")); } return rc; } catch (UnsupportedEncodingException e) { throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e); } }
static public URL makeURL( URL parent, String urlString) throws MalformedURLException { if (installed) return new URL( parent, urlString); // install failed, use alternate form of URL constructor try { URI uri = new URI( urlString); URLStreamHandler h = (URLStreamHandler) hash.get( uri.getScheme().toLowerCase());; return new URL( parent, urlString, h); } catch (URISyntaxException e) { throw new MalformedURLException(e.getMessage()); } // return new URL( url.getScheme(), url.getHost(), url.getPort(), url.getFile(), h); }
public static URL getEncodedURL(String urlStr) throws MalformedURLException { URL url = new URL(urlStr); String scheme = String.valueOf(url.getProtocol()).toLowerCase(); if (scheme.equals("http") || scheme.equals("https")) { try { return new URL(url.toURI().toASCIIString()); } catch (URISyntaxException e) { MalformedURLException malformedURLException = new MalformedURLException(e.getMessage()); malformedURLException.initCause(e); throw malformedURLException; } } return url; }
try url = new URI(taintedURL).normalize().toURL(); throw new MalformedURLException(e.getMessage()); final String path = url.getPath().replace("/$", ""); final SortedMap<String, String> params = createParameterMap(url.getQuery()); final int port = url.getPort(); final String queryString;
/** * It's a lot easier to configure and manage the location of the graphite server if we combine * the ip and port into a single string. Using a "fake" transport and the ipString means we get * standard host/port parsing (including domain names, ipv4 and ipv6) for free. */ private static URI parseStringAsUri(String ipString) { try { URI uri = new URI("socket://" + ipString); if (uri.getHost() == null || uri.getPort() == -1) { throw new URISyntaxException(ipString, "URI must have host and port parts"); } return uri; } catch (URISyntaxException e) { throw (IllegalArgumentException) new IllegalArgumentException( "Graphite server address needs to be defined as {host}:{port}.").initCause(e); } } }
private URI createUri(UriComponents uriComponents) { try { // Avoid further encoding (in the case of strictEncoding=true) return new URI(uriComponents.toUriString()); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not create URI object: " + ex.getMessage(), ex); } }
@Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) try { Desktop.getDesktop().browse(new URI(e.getURL().toString())); } catch (IOException e1) { e1.printStackTrace(); } catch (URISyntaxException e1) { e1.printStackTrace(); } } });
@Override protected List<PathData> expandArgument(String arg) throws IOException { List<PathData> items = new LinkedList<PathData>(); if (arg.equals("-")) { readStdin = true; } else { try { items.add(new PathData(new URI(arg), getConf())); } catch (URISyntaxException e) { if (Path.WINDOWS) { // Unlike URI, PathData knows how to parse Windows drive-letter paths. items.add(new PathData(arg, getConf())); } else { throw new IOException("Unexpected URISyntaxException: " + e.toString()); } } } return items; }
public KVMStoragePool getStoragePoolByURI(String uri) { URI storageUri = null; try { storageUri = new URI(uri); } catch (URISyntaxException e) { throw new CloudRuntimeException(e.toString()); } String sourcePath = null; String uuid = null; String sourceHost = ""; StoragePoolType protocol = null; if (storageUri.getScheme().equalsIgnoreCase("nfs") || storageUri.getScheme().equalsIgnoreCase("NetworkFilesystem")) { sourcePath = storageUri.getPath(); sourcePath = sourcePath.replace("//", "/"); sourceHost = storageUri.getHost(); uuid = UUID.nameUUIDFromBytes(new String(sourceHost + sourcePath).getBytes()).toString(); protocol = StoragePoolType.NetworkFilesystem; } // secondary storage registers itself through here return createStoragePool(uuid, sourceHost, 0, sourcePath, "", protocol, false); }
public void mouseClicked (MouseEvent e) { if (e.getClickCount() > 0) { if (Desktop.isDesktopSupported()) { Desktop desktop = Desktop.getDesktop(); try { URI uri = new URI( "https://github.com/libgdx/libgdx/wiki/Improving-workflow-with-Gradle#how-to-remove-gradle-ide-integration-from-your-project"); desktop.browse(uri); } catch (IOException ex) { ex.printStackTrace(); } catch (URISyntaxException ex) { ex.printStackTrace(); } } } } });
public void actionPerformed(ActionEvent event) { try { Compatibility.browse((new URL(url).toURI())); } catch (MalformedURLException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } }
public static URI getRootURI(ServletContext servletContext) throws MalformedURLException { URI rootURI = (URI)servletContext.getAttribute(URI_ATTRIBUTE); if (rootURI != null) { return rootURI; } try { URL rootURL = servletContext.getResource(PATH_WEB_INF); String path = rootURL.getPath(); int index = path.indexOf(PATH_WEB_INF); if (index < 0) { throw new MalformedURLException("Invalid URL " + rootURL); } if (index == 0) { path = StringPool.SLASH; } else { path = path.substring(0, index); } rootURI = new URI(rootURL.getProtocol(), path, null); servletContext.setAttribute(URI_ATTRIBUTE, rootURI); } catch (URISyntaxException urise) { throw new MalformedURLException(urise.getMessage()); } return rootURI; }
private void mergeConfigs(String configFileName, boolean replaceDuplicates) { try { File parent = new File(baseURL.toURI().getPath()).getParentFile(); URL fileURL = new File(parent.getPath() + File.separatorChar + configFileName).toURI().toURL(); Logger logger = Logger.getLogger(ConfigHandler.class.getSimpleName()); logger.fine((replaceDuplicates ? "extending" : "including") + " config:" + fileURL.toURI()); SaxLoader saxLoader = new SaxLoader(fileURL, globalProperties, rpdMap, replaceDuplicates); saxLoader.load(); } catch (IOException e) { throw new RuntimeException("Error while processing <include file=\"" + configFileName + "\">: " + e, e); } catch (URISyntaxException e) { e.printStackTrace(); } } }
private String normalize(String theValue) { if (theValue == null) { return null; } URI retVal; try { retVal = new URI(theValue).normalize(); String urlString = retVal.toString(); if (urlString.endsWith("/") && urlString.length() > 1) { retVal = new URI(urlString.substring(0, urlString.length() - 1)); } } catch (URISyntaxException e) { ourLog.debug("Failed to normalize URL '{}', message was: {}", theValue, e.toString()); return theValue; } return retVal.toASCIIString(); }
/** * Removes relative parts from the given path. * * @param path element * @return fixed path */ private String getFixedPath(String path) { path = path.replaceAll("\\/", "/").replaceAll("\\\\\\.", "."); try { path = new URI(path).normalize().getPath(); } catch (URISyntaxException e) { e.printStackTrace(); } return path.replaceAll("/\\.{1,2}/", "/").replaceAll("^\\.{0,2}/", ""); }
private String getHost(String url) { URI uri; try { uri = new URI(url); } catch (URISyntaxException e) { throw new IllegalStateException(e.getMessage()); } return uri.getHost(); }
private static void configLog4j() { URL configUrl = System.class.getResource("/conf/log4j-cloud.xml"); if (configUrl == null) configUrl = ClassLoader.getSystemResource("log4j-cloud.xml"); if (configUrl == null) configUrl = ClassLoader.getSystemResource("conf/log4j-cloud.xml"); if (configUrl != null) { try { System.out.println("Configure log4j using " + configUrl.toURI().toString()); } catch (URISyntaxException e1) { e1.printStackTrace(); } try { File file = new File(configUrl.toURI()); System.out.println("Log4j configuration from : " + file.getAbsolutePath()); DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); } catch (URISyntaxException e) { System.out.println("Unable to convert log4j configuration Url to URI"); } } else { System.out.println("Configure log4j with default properties"); } }