sun.net.www.protocol.file.FileURLConnection Java Examples

The following examples show how to use sun.net.www.protocol.file.FileURLConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExampleChooserHelper.java    From r2m-plugin-android with Apache License 2.0 6 votes vote down vote up
/**
 * Found on http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability
 * Pings a HTTP URL. This effectively sends a HEAD request and returns <code>true</code> if the response code is in
 * the 200-399 range.
 * @param url The HTTP URL to be pinged.
 * @param timeout The timeout in millis for both the connection timeout and the response read timeout. Note that
 * the total timeout is effectively two times the given timeout.
 * @return <code>true</code> if the given HTTP URL has returned response code 200-399 on a HEAD request within the
 * given timeout, otherwise <code>false</code>.
 */
public static boolean ping(String url, int timeout) {
    try {
        URLConnection conn  = new URL(url).openConnection();
        if (conn instanceof FileURLConnection) {
            return true;
        }
        HttpURLConnection connection = (HttpURLConnection) conn;
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);
        connection.setRequestMethod("HEAD");
        int responseCode = connection.getResponseCode();
        return (200 <= responseCode && responseCode <= 399);
    } catch (IOException exception) {
        return false;
    }
}
 
Example #2
Source File: $.java    From pysonar2 with Apache License 2.0 5 votes vote down vote up
public static void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
    URLConnection urlConnection = originUrl.openConnection();
    if (urlConnection instanceof JarURLConnection) {
        copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
    } else if (urlConnection instanceof FileURLConnection) {
        FileUtils.copyDirectory(new File(originUrl.getPath()), destination);
    } else {
        die("Unsupported URL type: " + urlConnection);
    }
}
 
Example #3
Source File: Environment.java    From desktop with GNU General Public License v3.0 5 votes vote down vote up
public void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
    URLConnection urlConnection = originUrl.openConnection();
    if (urlConnection instanceof JarURLConnection) {
        copyResourcesFromJar((JarURLConnection) urlConnection, destination);
    } else if (urlConnection instanceof FileURLConnection) {
        // I know that this is not so beatiful... I'll try to change in a future...
        if (operatingSystem == OperatingSystem.Mac || operatingSystem == OperatingSystem.Linux) {
            destination = defaultUserConfDir;
        }
        FileUtils.copyDirectoryToDirectory(new File(originUrl.getPath()), destination);
    } else {
        throw new Exception("URLConnection[" + urlConnection.getClass().getSimpleName() +
                "] is not a recognized/implemented connection type.");
    }
}
 
Example #4
Source File: $.java    From pysonar2 with Apache License 2.0 5 votes vote down vote up
public static void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
    URLConnection urlConnection = originUrl.openConnection();
    if (urlConnection instanceof JarURLConnection) {
        copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
    } else if (urlConnection instanceof FileURLConnection) {
        FileUtils.copyDirectory(new File(originUrl.getPath()), destination);
    } else {
        die("Unsupported URL type: " + urlConnection);
    }
}
 
Example #5
Source File: ClassLoaderHelper.java    From ForgeHax with MIT License 4 votes vote down vote up
/**
 * Will attempt to find every class inside the package recursively.
 *
 * @param classLoader class loader to get the package resource from
 * @param packageDir name of package to search
 * @param recursive if the scan should look into sub directories
 * @return list of all the classes found
 */
public static List<Path> getClassPathsInPackage(
    final ClassLoader classLoader, String packageDir, final boolean recursive)
    throws IOException {
  Objects.requireNonNull(packageDir);
  Objects.requireNonNull(classLoader);
  
  List<Path> results = Lists.newArrayList();
  
  final String pkgdir = asFilePath(packageDir);
  Enumeration<URL> inside = classLoader.getResources(pkgdir);
  Streamables.enumerationStream(inside)
      .forEach(
          url -> {
            URLConnection connection;
            try {
              connection = url.openConnection();
              
              // get the path to the jar/folder containing the classes
              String path =
                  URLDecoder.decode(url.getPath(), "UTF-8")
                      .replace('\\', '/'); // get path and covert backslashes to forward slashes
              path =
                  path.substring(
                      path.indexOf('/') + 1
                  ); // remove the initial '/' or 'file:/' appended to the path
              
              if (!System.getProperty("os.name").startsWith("Windows")) {
                path = "/" + path;
              }
              
              // the root directory to the jar/folder containing the classes
              String rootDir = path.substring(0, path.indexOf(pkgdir));
              // package directory
              String packDir = path.substring(path.lastIndexOf(pkgdir));
              
              if (connection instanceof FileURLConnection) {
                final Path root = Paths.get(rootDir).normalize();
                getClassPathsInDirectory(path, recursive)
                    .stream()
                    .map(root::relativize)
                    .forEach(results::add);
              } else if (connection instanceof JarURLConnection) {
                results.addAll(
                    getClassPathsInJar(
                        ((JarURLConnection) connection).getJarFile(), packDir, recursive));
              } else {
                throw new UnknownConnectionType();
              }
            } catch (Exception e) {
              throw new RuntimeException(e);
            }
          });
  
  return results;
}
 
Example #6
Source File: WarpScriptMacroLibrary.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
public static Macro find(String name) throws WarpScriptException { 
  
  // Reject names with relative path components in them or starting with '/'
  if (name.contains("/../") || name.contains("/./") || name.startsWith("../") || name.startsWith("./") || name.startsWith("/")) {
    return null;
  }

  Macro macro = (Macro) macros.get(name);
  
  //
  // The macro is not (yet) known, we will attempt to load it from the
  // classpath
  //
  
  if (null == macro || macro.isExpired()) {
    String rsc = name + WarpScriptMacroRepository.WARPSCRIPT_FILE_EXTENSION;
    URL url = WarpScriptMacroLibrary.class.getClassLoader().getResource(rsc);
    
    if (null != url) {
      try {
        URLConnection conn = url.openConnection();
        
        if (conn instanceof JarURLConnection) {
          //
          // This case is when the requested macro is in a jar
          //
          final JarURLConnection connection = (JarURLConnection) url.openConnection();
          final URL fileurl = connection.getJarFileURL();
          File f = new File(fileurl.toURI());
          addJar(f.getAbsolutePath(), rsc);
          macro = (Macro) macros.get(name);
        } else if (conn instanceof FileURLConnection) {
          //
          // This case is when the requested macro is in the classpath but not in a jar.
          // In this case we do not cache the parsed macro, allowing for dynamic modification.
          //
          String urlstr = url.toString();
          File root = new File(urlstr.substring(0, urlstr.length() - name.length()  - WarpScriptMacroRepository.WARPSCRIPT_FILE_EXTENSION.length()));
          macro = loadMacro(root, conn.getInputStream(), name);
        }          
      } catch (URISyntaxException use) {
        throw new WarpScriptException("Error while loading '" + name + "'", use);
      } catch (IOException ioe) {
        throw new WarpScriptException("Error while loading '" + name + "'", ioe);
      }
    }
  }
  
  return macro;
}