Example usage for java.net URL toURI

List of usage examples for java.net URL toURI

Introduction

In this page you can find the example usage for java.net URL toURI.

Prototype

public URI toURI() throws URISyntaxException 

Source Link

Document

Returns a java.net.URI equivalent to this URL.

Usage

From source file:eu.eubrazilcc.lvl.core.util.UrlUtils.java

/**
 * Checks whether or not a URL is valid.
 * @param str - input URL/* ww w  .j a  v a  2  s.  c om*/
 * @param strict when is set to {@code true}, an additional check is performed 
 *        to verify that the URL is formatted strictly according to RFC2396.
 * @return {@code true} if the specified URL is valid. Otherwise, {@code false}.
 */
public static boolean isValid(final String str, final boolean strict) {
    URL url = null;
    if (isNotBlank(str)) {
        try {
            url = new URL(str);
            if (strict) {
                url.toURI();
            }
        } catch (MalformedURLException | URISyntaxException e) {
            url = null;
        }
    }
    return url != null;
}

From source file:com.ms.commons.test.common.FileUtil.java

public static File convertURLToFile(URL url) {
    if (url == null) {
        return null;
    }//from ww w .j  ava2s  .  c o m

    try {
        return new File(url.toURI());
    } catch (URISyntaxException e) {
        try {
            return new File(URLDecoder.decode(url.getPath(), "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            return new File(url.getPath());
        }
    }
}

From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java

public static void delete(URL url, String user, String passwd) throws Exception {
    //System.out.println("DELETE "+url);
    HttpClient client = HttpClients.createDefault();
    HttpDelete request = new HttpDelete(url.toURI());

    HttpClientContext context = HttpClientContext.create();
    if (user != null && passwd != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd));
        context.setCredentialsProvider(credsProvider);
    }/*from  w  w w. ja  v a 2 s .  c  o  m*/

    HttpResponse response = client.execute(request, context);

    StatusLine s = response.getStatusLine();
    int code = s.getStatusCode();
    //System.out.println(code);
    if (code != 204 && code != 404)
        throw new Exception(
                "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase());
}

From source file:gov.nih.nci.cacis.common.test.TestUtils.java

/**
 * Reads an XML file as a DOM//ww  w.  j a  v  a  2 s .  c  o m
 *
 * @param fileName XML file name
 * @return Document XML
 * @throws java.net.URISyntaxException           exception
 * @throws javax.xml.parsers.ParserConfigurationException exception
 * @throws java.io.IOException                  exception
 * @throws org.xml.sax.SAXException                 exception
 */
public static Document getXMLDoc(URL fileName)
        throws URISyntaxException, ParserConfigurationException, IOException, SAXException {
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    final DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(new File(fileName.toURI()));
}

From source file:com.centeractive.ws.builder.ServiceComplianceTest.java

public static String getExpectedMessage(int testServiceId, String bindingName, String operationName,
        MessageType msg) {/*from w ww  . ja  va2s  . com*/
    String serviceFolderPath = getTestServiceFolderPath(testServiceId);
    String messageFolderPath = String.format("%s/operations/%s", serviceFolderPath, bindingName);
    String fileName = operationName + "." + (MessageType.REQUEST.equals(msg) ? "request" : "response") + ".xml";
    URL fileUrl = ResourceUtils.getResourceWithAbsolutePackagePath(messageFolderPath, fileName);
    File file = null;
    try {
        file = new File(fileUrl.toURI());
    } catch (URISyntaxException e) {
        file = new File(fileUrl.getPath());
    }
    try {
        return FileUtils.readFileToString(file);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:it.geosolutions.unredd.StatsTests.java

private static File loadFileFromResources(String resourceName) {

    URL propUrl = null;
    try {/* www  . ja va 2  s.  com*/
        propUrl = StatsTests.class.getResource(resourceName);
        return new File(propUrl.toURI());
    } catch (URISyntaxException e) {
        fail("One resource layer fails the loading... check the file specified in testSimpleStats.properties files before start to debugging...");
    }
    return null;
}

From source file:org.yamj.api.common.http.HttpClientWrapper.java

protected static URI toURI(URL url) {
    try {/*  w ww . jav  a 2  s  .  c  o  m*/
        return url.toURI();
    } catch (URISyntaxException ex) {
        throw new IllegalArgumentException("Invalid URL: " + url, ex);
    }
}

From source file:com.bfd.harpc.common.configure.PathUtils.java

/**
 * Normalize the path by suppressing sequences like "path/.." and inner
 * simple dots./*from   www  .  ja va 2  s .co  m*/
 * <p>
 * The result is convenient for path comparison. For other uses, notice that
 * Windows separators ("\") are replaced by simple slashes.
 * 
 * @param originalUrl
 *            the url with original path
 * @return the url with normalized path
 * @throws MalformedURLException
 * @throws URISyntaxException
 */
public static URL cleanPath(URL originalUrl) throws MalformedURLException, URISyntaxException {
    String path = originalUrl.toString();
    if (StringUtils.isEmpty(path)) {
        return null;
    }
    URL curl = new URL(cleanPath(path));
    // curl.toURI().getPath()?
    return new URL(curl.getProtocol(), curl.getHost(), curl.toURI().getPath());
}

From source file:org.lightcouch.CouchDbUtil.java

/**
 * List directory contents for a resource folder. Not recursive. This is
 * basically a brute-force implementation. Works for regular files and also
 * JARs.//from w ww  .j a v a  2s . c om
 * 
 * @author Greg Briggs
 * @param clazz
 *            Any java class that lives in the same place as the resources
 *            you want.
 * @param path
 *            Should end with "/", but not start with one.
 * @return Just the name of each member item, not the full paths.
 */
public static List<String> listResources(String path) {
    String fileURL = null;
    try {
        URL entry = Platform.getBundle("org.lightcouch").getEntry(path);
        File file = null;
        if (entry != null) {
            fileURL = FileLocator.toFileURL(entry).getPath();
            // remove leading slash from absolute path when on windows
            if (OSValidator.isWindows())
                fileURL = fileURL.substring(1, fileURL.length());
            file = new File(fileURL);
        }
        URL dirURL = file.toPath().toUri().toURL();

        if (dirURL != null && dirURL.getProtocol().equals("file")) {
            return Arrays.asList(new File(dirURL.toURI()).list());
        }
        if (dirURL != null && dirURL.getProtocol().equals("jar")) {
            String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
            JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
            Enumeration<JarEntry> entries = jar.entries();
            Set<String> result = new HashSet<String>();
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (name.startsWith(path)) {
                    String entry1 = name.substring(path.length());
                    int checkSubdir = entry1.indexOf("/");
                    if (checkSubdir >= 0) {
                        entry1 = entry1.substring(0, checkSubdir);
                    }
                    if (entry1.length() > 0) {
                        result.add(entry1);
                    }
                }
            }
            return new ArrayList<String>(result);
        }
        return null;
    } catch (Exception e) {
        throw new CouchDbException("fileURL: " + fileURL, e);
    }
}

From source file:fr.free.movierenamer.utils.URIRequest.java

public static int getResponseCode(URL url, RequestProperty... properties) {
    try {/*  w  w  w. ja va 2s .  c  o m*/
        HttpURLConnection huc = (HttpURLConnection) openConnection(url.toURI(), properties);
        huc.setRequestMethod("GET");
        huc.connect();
        return huc.getResponseCode();
    } catch (Exception ex) {
        return 0;
    }
}