Search in sources :

Example 1 with HttpDataFactory

use of io.netty.handler.codec.http.multipart.HttpDataFactory in project netty by netty.

the class HttpUploadClient method main.

public static void main(String[] args) throws Exception {
    String postSimple, postFile, get;
    if (BASE_URL.endsWith("/")) {
        postSimple = BASE_URL + "formpost";
        postFile = BASE_URL + "formpostmultipart";
        get = BASE_URL + "formget";
    } else {
        postSimple = BASE_URL + "/formpost";
        postFile = BASE_URL + "/formpostmultipart";
        get = BASE_URL + "/formget";
    }
    URI uriSimple = new URI(postSimple);
    String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme();
    String host = uriSimple.getHost() == null ? "127.0.0.1" : uriSimple.getHost();
    int port = uriSimple.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }
    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    URI uriFile = new URI(postFile);
    File file = new File(FILE);
    if (!file.canRead()) {
        throw new FileNotFoundException(FILE);
    }
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    // setup the factory: here using a mixed memory/disk based on size threshold
    // Disk if MINSIZE exceed
    HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
    // should delete file on exit (in normal exit)
    DiskFileUpload.deleteOnExitTemporaryFile = true;
    // system temp directory
    DiskFileUpload.baseDirectory = null;
    // should delete file on exit (in normal exit)
    DiskAttribute.deleteOnExitTemporaryFile = true;
    // system temp directory
    DiskAttribute.baseDirectory = null;
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientIntializer(sslCtx));
        // Simple Get form: no factory used (not usable)
        List<Entry<String, String>> headers = formget(b, host, port, get, uriSimple);
        if (headers == null) {
            factory.cleanAllHttpData();
            return;
        }
        // Simple Post form: factory used for big attributes
        List<InterfaceHttpData> bodylist = formpost(b, host, port, uriSimple, file, factory, headers);
        if (bodylist == null) {
            factory.cleanAllHttpData();
            return;
        }
        // Multipart Post form: factory used
        formpostmultipart(b, host, port, uriFile, factory, headers, bodylist);
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
        // Really clean all temporary files if they still exist
        factory.cleanAllHttpData();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Entry(java.util.Map.Entry) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) Bootstrap(io.netty.bootstrap.Bootstrap) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)1 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 SslContext (io.netty.handler.ssl.SslContext)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 URI (java.net.URI)1 Entry (java.util.Map.Entry)1