List of usage examples for java.net URI toURL
public URL toURL() throws MalformedURLException
From source file:org.mrgeo.utils.FileUtils.java
public static String resolveURL(final String path) { try {//from w w w. jav a 2 s . c o m URI uri = new URI(path); if (uri.getScheme() == null) { String fragment = uri.getFragment(); URI part = new File(uri.getPath()).toURI(); uri = new URI(part.getScheme(), part.getPath(), fragment); } return uri.toURL().toString(); } catch (URISyntaxException e) { } catch (MalformedURLException e) { } return path; }
From source file:org.wso2.carbon.identity.jwt.client.extension.util.JWTClientUtil.java
private static KeyStore loadKeyStore(final File keystoreFile, final String password, final String keyStoreType) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { if (null == keystoreFile) { throw new IllegalArgumentException("Keystore url may not be null"); }//from ww w . j a v a2s.com URI keystoreUri = keystoreFile.toURI(); URL keystoreUrl = keystoreUri.toURL(); KeyStore keystore = KeyStore.getInstance(keyStoreType); InputStream is = null; try { is = keystoreUrl.openStream(); keystore.load(is, null == password ? null : password.toCharArray()); } finally { if (null != is) { is.close(); } } return keystore; }
From source file:com.jayway.restassured.module.jsv.JsonSchemaValidator.java
private static URL toURL(URI uri) { validateSchemaIsNotNull(uri);//from ww w .j ava 2 s . c o m try { return uri.toURL(); } catch (MalformedURLException e) { throw new IllegalArgumentException("Can't convert the supplied URI to a URL", e); } }
From source file:AIR.Common.xml.XmlReader.java
public static XmlReader create(URI uri) throws XmlReaderException { try {/* ww w . java 2 s .c o m*/ return new XmlReader(uri.toURL().openStream()); } catch (Exception exp) { throw new XmlReaderException(exp); } }
From source file:org.gvnix.web.theme.roo.addon.Theme.java
/** * Parses and build Document from the given theme descriptor URI to Theme * object.//from w ww .j a va2 s . c om * * @param uri URI to theme descriptor to parse * @return Theme object. Null if there is any problem parsing the stream or * the stream doesn't contain a valid XML. */ public static Theme parseTheme(URI uri) { try { Theme theme = Theme.parseTheme(uri.toURL().openStream()); theme.setDescriptor(uri); return theme; } catch (IOException e) { throw new IllegalStateException("I/O exception.", e); } }
From source file:org.elasticsearch.hadoop.mr.HadoopIOUtils.java
public static InputStream open(String resource, Configuration conf) { ClassLoader loader = conf.getClassLoader(); if (loader == null) { loader = Thread.currentThread().getContextClassLoader(); }//from www.j av a 2s . co m if (loader == null) { loader = HadoopIOUtils.class.getClassLoader(); } boolean trace = log.isTraceEnabled(); try { // no prefix means classpath if (!resource.contains(":")) { InputStream result = loader.getResourceAsStream(resource); if (result != null) { if (trace) { log.trace(String.format("Loaded resource %s from classpath", resource)); } return result; } // fall back to the distributed cache URI[] uris = DistributedCache.getCacheFiles(conf); if (uris != null) { for (URI uri : uris) { if (uri.toString().contains(resource)) { if (trace) { log.trace(String.format("Loaded resource %s from distributed cache", resource)); } return uri.toURL().openStream(); } } } } // fall back to file system Path p = new Path(resource); FileSystem fs = p.getFileSystem(conf); return fs.open(p); } catch (IOException ex) { throw new EsHadoopIllegalArgumentException( String.format("Cannot open stream for resource %s", resource)); } }
From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java
public static void readIntents(RuntimeBean runtime, URI source) throws IOException { InputStream is = source.toURL().openStream(); InputStreamReader rdr = new InputStreamReader(is); ApplicationLogic.readIntents(runtime.getApp(), rdr); rdr.close();/*from w w w . j a v a 2s. co m*/ runtime.firePropertyChange("app", null, runtime.getApp()); setProp("app.intents", source.toString()); }
From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java
public static void readUtterances(RuntimeBean runtime, URI source) throws IOException { InputStream is = source.toURL().openStream(); InputStreamReader rdr = new InputStreamReader(is); UtteranceLogic.read(runtime.getApp(), rdr); rdr.close();// ww w .j a v a 2 s . c o m runtime.firePropertyChange("app", null, runtime.getApp()); setProp("app.utterances", source.toString()); }
From source file:org.apache.brooklyn.util.net.Urls.java
/** creates a URL, preserving null and propagating exceptions *unchecked* */ public static final URL toUrl(@Nullable URI uri) { if (uri == null) return null; try {//w ww .j a va2s . c o m return uri.toURL(); } catch (MalformedURLException e) { // FOAD throw Throwables.propagate(e); } }
From source file:com.mashape.unirest.android.http.HttpClientHelper.java
private static HttpRequestBase prepareRequest(HttpRequest request, boolean async) { Object defaultHeaders = Options.getOption(Option.DEFAULT_HEADERS); if (defaultHeaders != null) { @SuppressWarnings("unchecked") Set<Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet(); for (Entry<String, String> entry : entrySet) { request.header(entry.getKey(), entry.getValue()); }//from w w w.ja v a 2s.co m } if (!request.getHeaders().containsKey(USER_AGENT_HEADER)) { request.header(USER_AGENT_HEADER, USER_AGENT); } if (!request.getHeaders().containsKey(ACCEPT_ENCODING_HEADER)) { request.header(ACCEPT_ENCODING_HEADER, "gzip"); } HttpRequestBase reqObj = null; String urlToRequest = null; try { URL url = new URL(request.getUrl()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef()); urlToRequest = uri.toURL().toString(); if (url.getQuery() != null && !url.getQuery().trim().equals("")) { if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest += "?"; } urlToRequest += url.getQuery(); } else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1); } } catch (Exception e) { throw new RuntimeException(e); } switch (request.getHttpMethod()) { case GET: reqObj = new HttpGet(urlToRequest); break; case POST: reqObj = new HttpPost(urlToRequest); break; case PUT: reqObj = new HttpPut(urlToRequest); break; case DELETE: //reqObj = new HttpDeleteWithBody(urlToRequest); break; case PATCH: //reqObj = new HttpPatchWithBody(urlToRequest); break; case OPTIONS: reqObj = new HttpOptions(urlToRequest); break; case HEAD: reqObj = new HttpHead(urlToRequest); break; } Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet(); for (Entry<String, List<String>> entry : entrySet) { List<String> values = entry.getValue(); if (values != null) { for (String value : values) { reqObj.addHeader(entry.getKey(), value); } } } // Set body if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new RuntimeException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } return reqObj; }