Search in sources :

Example 6 with VertxException

use of io.vertx.core.VertxException in project vert.x by eclipse.

the class FileResolver method unpackFromFileURL.

private synchronized File unpackFromFileURL(URL url, String fileName, ClassLoader cl) {
    File resource;
    try {
        resource = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new VertxException(e);
    }
    boolean isDirectory = resource.isDirectory();
    File cacheFile = new File(cacheDir, fileName);
    if (!isDirectory) {
        cacheFile.getParentFile().mkdirs();
        try {
            if (ENABLE_CACHING) {
                Files.copy(resource.toPath(), cacheFile.toPath());
            } else {
                Files.copy(resource.toPath(), cacheFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (FileAlreadyExistsException ignore) {
        } catch (IOException e) {
            throw new VertxException(e);
        }
    } else {
        cacheFile.mkdirs();
        String[] listing = resource.list();
        for (String file : listing) {
            String subResource = fileName + "/" + file;
            URL url2 = cl.getResource(subResource);
            unpackFromFileURL(url2, subResource, cl);
        }
    }
    return cacheFile;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) VertxException(io.vertx.core.VertxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) URL(java.net.URL)

Example 7 with VertxException

use of io.vertx.core.VertxException in project vert.x by eclipse.

the class FileResolver method unpackFromJarURL.

private synchronized File unpackFromJarURL(URL url, String fileName, ClassLoader cl) {
    ZipFile zip = null;
    try {
        String path = url.getPath();
        int idx1 = path.lastIndexOf(".jar!");
        if (idx1 == -1) {
            idx1 = path.lastIndexOf(".zip!");
        }
        int idx2 = path.lastIndexOf(".jar!", idx1 - 1);
        if (idx2 == -1) {
            idx2 = path.lastIndexOf(".zip!", idx1 - 1);
        }
        if (idx2 == -1) {
            File file = new File(URLDecoder.decode(path.substring(5, idx1 + 4), "UTF-8"));
            zip = new ZipFile(file);
        } else {
            String s = path.substring(idx2 + 6, idx1 + 4);
            File file = resolveFile(s);
            zip = new ZipFile(file);
        }
        String inJarPath = path.substring(idx1 + 6);
        String[] parts = JAR_URL_SEP_PATTERN.split(inJarPath);
        StringBuilder prefixBuilder = new StringBuilder();
        for (int i = 0; i < parts.length - 1; i++) {
            prefixBuilder.append(parts[i]).append("/");
        }
        String prefix = prefixBuilder.toString();
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.startsWith(prefix.isEmpty() ? fileName : prefix + fileName)) {
                File file = new File(cacheDir, prefix.isEmpty() ? name : name.substring(prefix.length()));
                if (name.endsWith("/")) {
                    // Directory
                    file.mkdirs();
                } else {
                    file.getParentFile().mkdirs();
                    try (InputStream is = zip.getInputStream(entry)) {
                        if (ENABLE_CACHING) {
                            Files.copy(is, file.toPath());
                        } else {
                            Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        }
                    } catch (FileAlreadyExistsException ignore) {
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new VertxException(e);
    } finally {
        closeQuietly(zip);
    }
    return new File(cacheDir, fileName);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) VertxException(io.vertx.core.VertxException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 8 with VertxException

use of io.vertx.core.VertxException in project vert.x by eclipse.

the class HttpServerImpl method createHandshaker.

WebSocketServerHandshaker createHandshaker(Channel ch, HttpRequest request) {
    // As a fun part, Firefox 6.0.2 supports Websockets protocol '7'. But,
    // it doesn't send a normal 'Connection: Upgrade' header. Instead it
    // sends: 'Connection: keep-alive, Upgrade'. Brilliant.
    String connectionHeader = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
    if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
        sendError("\"Connection\" must be \"Upgrade\".", BAD_REQUEST, ch);
        return null;
    }
    if (request.getMethod() != HttpMethod.GET) {
        sendError(null, METHOD_NOT_ALLOWED, ch);
        return null;
    }
    try {
        WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ch.pipeline(), request), subProtocols, false, options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames());
        WebSocketServerHandshaker shake = factory.newHandshaker(request);
        if (shake == null) {
            log.error("Unrecognised websockets handshake");
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ch);
        }
        return shake;
    } catch (Exception e) {
        throw new VertxException(e);
    }
}
Also used : WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) VertxException(io.vertx.core.VertxException) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) VertxException(io.vertx.core.VertxException) URISyntaxException(java.net.URISyntaxException) Http2Exception(io.netty.handler.codec.http2.Http2Exception) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)

Example 9 with VertxException

use of io.vertx.core.VertxException in project vert.x by eclipse.

the class Http2ConnectionBase method setWindowSize.

@Override
public HttpConnection setWindowSize(int windowSize) {
    try {
        Http2Stream stream = handler.encoder().connection().connectionStream();
        int delta = windowSize - this.windowSize;
        handler.decoder().flowController().incrementWindowSize(stream, delta);
        this.windowSize = windowSize;
        return this;
    } catch (Http2Exception e) {
        throw new VertxException(e);
    }
}
Also used : Http2Exception(io.netty.handler.codec.http2.Http2Exception) VertxException(io.vertx.core.VertxException) Http2Stream(io.netty.handler.codec.http2.Http2Stream)

Example 10 with VertxException

use of io.vertx.core.VertxException in project vert.x by eclipse.

the class HostnameResolutionTest method testInvalidHostsConfig.

@Test
public void testInvalidHostsConfig() {
    try {
        AddressResolverOptions options = new AddressResolverOptions().setHostsPath("whatever.txt");
        vertx(new VertxOptions().setAddressResolverOptions(options));
        fail();
    } catch (VertxException ignore) {
    }
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) VertxException(io.vertx.core.VertxException) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Aggregations

VertxException (io.vertx.core.VertxException)10 File (java.io.File)3 IOException (java.io.IOException)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 ZipFile (java.util.zip.ZipFile)3 Http2Exception (io.netty.handler.codec.http2.Http2Exception)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 WebSocketServerHandshaker (io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker)1 WebSocketServerHandshakerFactory (io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)1 Http2Stream (io.netty.handler.codec.http2.Http2Stream)1 JdkSslContext (io.netty.handler.ssl.JdkSslContext)1 OpenSslContext (io.netty.handler.ssl.OpenSslContext)1 SslContext (io.netty.handler.ssl.SslContext)1 SimpleTrustManagerFactory (io.netty.handler.ssl.util.SimpleTrustManagerFactory)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 VertxOptions (io.vertx.core.VertxOptions)1 AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1