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;
}
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");
}
}
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);
}
}
}
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;
}
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;
}
Aggregations