Search in sources :

Example 51 with JarURLConnection

use of java.net.JarURLConnection in project ceylon by eclipse.

the class CeylonAssemblyRunner method getAssemblyManifestAttributes.

private static Attributes getAssemblyManifestAttributes() throws IOException {
    ProtectionDomain protectionDomain = CeylonAssemblyRunner.class.getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    if (codeSource != null) {
        URL srcUrl = codeSource.getLocation();
        URL assemblyUrl = new URL("jar", "", srcUrl + "!/");
        JarURLConnection uc = (JarURLConnection) assemblyUrl.openConnection();
        return uc.getMainAttributes();
    }
    return null;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) JarURLConnection(java.net.JarURLConnection) CodeSource(java.security.CodeSource) URL(java.net.URL)

Example 52 with JarURLConnection

use of java.net.JarURLConnection in project LogHub by fbacchella.

the class ResourceFiles method processRequest.

@Override
protected boolean processRequest(FullHttpRequest request, ChannelHandlerContext ctx) throws HttpRequestFailure {
    String name = ROOT.relativize(Paths.get(request.uri()).normalize()).toString();
    if (!name.startsWith("static/")) {
        throw new HttpRequestFailure(HttpResponseStatus.FORBIDDEN, "Access to " + name + " forbiden");
    }
    URL resourceUrl = getClass().getClassLoader().getResource(name);
    if (resourceUrl == null) {
        throw new HttpRequestFailure(HttpResponseStatus.NOT_FOUND, request.uri() + " not found");
    } else if ("jar".equals(resourceUrl.getProtocol())) {
        try {
            JarURLConnection jarConnection = (JarURLConnection) resourceUrl.openConnection();
            JarEntry entry = jarConnection.getJarEntry();
            if (entry.isDirectory()) {
                throw new HttpRequestFailure(HttpResponseStatus.FORBIDDEN, "Directory listing refused");
            }
            int length = jarConnection.getContentLength();
            internalPath = entry.getName();
            internalDate = new Date(entry.getLastModifiedTime().toMillis());
            ChunkedInput<ByteBuf> content = new ChunkedStream(jarConnection.getInputStream());
            return writeResponse(ctx, request, content, length);
        } catch (IOException e) {
            throw new HttpRequestFailure(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    } else {
        throw new HttpRequestFailure(HttpResponseStatus.INTERNAL_SERVER_ERROR, request.uri() + " not managed");
    }
}
Also used : ChunkedInput(io.netty.handler.stream.ChunkedInput) JarURLConnection(java.net.JarURLConnection) ChunkedStream(io.netty.handler.stream.ChunkedStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Date(java.util.Date)

Example 53 with JarURLConnection

use of java.net.JarURLConnection in project LogHub by fbacchella.

the class Helpers method readRessources.

public static void readRessources(ClassLoader loader, String lookingfor, Consumer<InputStream> reader) throws IOException, URISyntaxException {
    List<URL> patternsUrls = Collections.list(loader.getResources(lookingfor));
    for (URL url : patternsUrls) {
        URLConnection cnx = url.openConnection();
        if (cnx instanceof JarURLConnection) {
            JarURLConnection jarcnx = (JarURLConnection) cnx;
            final JarFile jarfile = jarcnx.getJarFile();
            Helpers.ThrowingFunction<JarEntry, InputStream> openner = i -> jarfile.getInputStream(i);
            StreamSupport.stream(Helpers.enumIterable(jarfile.entries()).spliterator(), false).filter(i -> i.getName().startsWith(lookingfor)).map(openner).forEach(reader);
        } else if ("file".equals(url.getProtocol())) {
            Path p = Paths.get(url.toURI());
            if (Files.isDirectory(p)) {
                try (DirectoryStream<Path> ds = Files.newDirectoryStream(p)) {
                    for (Path entry : ds) {
                        InputStream is = new FileInputStream(entry.toFile());
                        reader.accept(is);
                    }
                }
            }
        } else {
            throw new RuntimeException("cant load ressource at " + url);
        }
    }
}
Also used : Enumeration(java.util.Enumeration) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) FutureTask(java.util.concurrent.FutureTask) Callable(java.util.concurrent.Callable) JarFile(java.util.jar.JarFile) Function(java.util.function.Function) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) InetAddress(java.net.InetAddress) DirectoryStream(java.nio.file.DirectoryStream) JarEntry(java.util.jar.JarEntry) URLConnection(java.net.URLConnection) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) JarURLConnection(java.net.JarURLConnection) Path(java.nio.file.Path) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) NetUtil(io.netty.util.NetUtil) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) UnknownHostException(java.net.UnknownHostException) Executors(java.util.concurrent.Executors) Consumer(java.util.function.Consumer) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) InputStream(java.io.InputStream) Path(java.nio.file.Path) JarURLConnection(java.net.JarURLConnection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DirectoryStream(java.nio.file.DirectoryStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) FileInputStream(java.io.FileInputStream)

Example 54 with JarURLConnection

use of java.net.JarURLConnection in project fabric8 by fabric8io.

the class URLUtils method prepareForSSL.

/**
 * Prepares an url connection for authentication if necessary.
 *
 * @param connection the connection to be prepared
 * @return the prepared conection
 */
public static URLConnection prepareForSSL(final URLConnection connection) {
    NullArgumentException.validateNotNull(connection, "url connection cannot be null");
    URLConnection conn = connection;
    if (conn instanceof JarURLConnection) {
        try {
            conn = ((JarURLConnection) connection).getJarFileURL().openConnection();
            conn.connect();
        } catch (IOException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        }
    }
    if (conn instanceof HttpsURLConnection) {
        try {
            SSLContext ctx = SSLContext.getInstance("SSLv3");
            ctx.init(null, new TrustManager[] { new AllCertificatesTrustManager() }, null);
            ((HttpsURLConnection) conn).setSSLSocketFactory(ctx.getSocketFactory());
        } catch (KeyManagementException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        }
    }
    return connection;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) AllCertificatesTrustManager(io.fabric8.utils.ssl.AllCertificatesTrustManager)

Example 55 with JarURLConnection

use of java.net.JarURLConnection in project artisynth_core by artisynth.

the class ClassFinder method findClasses.

/**
 * Searches through all "subdirectories" of a URL, gathering classes of type T that
 * match regex
 */
public static ArrayList<Class<?>> findClasses(URL url, String pkg, Pattern regex, Class<?> T) {
    ArrayList<Class<?>> classList = new ArrayList<Class<?>>();
    // remove initial period
    if (pkg.startsWith(".")) {
        pkg = pkg.substring(1);
    }
    if ("file".equals(url.getProtocol())) {
        File file = new File(url.getPath());
        return findClasses(file, pkg, regex, T);
    } else if ("jar".equals(url.getProtocol())) {
        JarFile jar = null;
        JarEntry jarEntry = null;
        try {
            JarURLConnection connection = (JarURLConnection) (url.openConnection());
            jar = connection.getJarFile();
            jarEntry = connection.getJarEntry();
        } catch (IOException ioe) {
            Logger logger = getLogger();
            logger.debug("Unable to process jar: " + url.toString());
            logger.trace(ioe);
            return classList;
        }
        if (jarEntry.getName().endsWith(".class")) {
            String className = jarEntry.getName();
            // remove extension
            className = className.substring(0, className.length() - 6);
            maybeAddClass(className, regex, T, classList);
        } else {
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (entry.getName().startsWith(jarEntry.getName())) {
                    // we have the Jar entry
                    if (entry.getName().endsWith(".class")) {
                        String className = entry.getName();
                        // remove extension
                        className = className.substring(0, className.length() - 6);
                        className = className.replace('/', '.');
                        maybeAddClass(className, regex, T, classList);
                    }
                }
            }
        }
    }
    return classList;
}
Also used : Enumeration(java.util.Enumeration) JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JarURLConnection (java.net.JarURLConnection)220 URL (java.net.URL)159 JarFile (java.util.jar.JarFile)128 IOException (java.io.IOException)119 JarEntry (java.util.jar.JarEntry)104 File (java.io.File)90 URLConnection (java.net.URLConnection)88 ArrayList (java.util.ArrayList)30 InputStream (java.io.InputStream)26 MalformedURLException (java.net.MalformedURLException)25 URISyntaxException (java.net.URISyntaxException)21 Enumeration (java.util.Enumeration)17 Manifest (java.util.jar.Manifest)16 CodeSource (java.security.CodeSource)12 FileInputStream (java.io.FileInputStream)11 LinkedHashSet (java.util.LinkedHashSet)11 URI (java.net.URI)10 Attributes (java.util.jar.Attributes)10 ZipEntry (java.util.zip.ZipEntry)9 FileNotFoundException (java.io.FileNotFoundException)8