Example usage for io.netty.handler.codec.http.multipart MixedFileUpload addContent

List of usage examples for io.netty.handler.codec.http.multipart MixedFileUpload addContent

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.multipart MixedFileUpload addContent.

Prototype

@Override
    public void addContent(ByteBuf buffer, boolean last) throws IOException 

Source Link

Usage

From source file:de.ocarthon.core.network.HttpClient.java

License:Apache License

public synchronized String postRequest(String query, List<Map.Entry<String, String>> postParameters,
        String filePostName, String fileName, ByteBuf fileData, String mime) {
    if (bootstrap == null) {
        setupBootstrap();// w  w  w . ja  v  a2 s.c o m
    }

    if (channel == null || forceReconnect) {
        ChannelFuture cf = bootstrap.connect(host, port);
        forceReconnect = false;
        cf.awaitUninterruptibly();
        channel = cf.channel();

        channel.pipeline().addLast("handler", new SimpleChannelInboundHandler<HttpObject>() {
            @Override
            protected void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
                if (msg instanceof HttpResponse) {
                    HttpResponse response = ((HttpResponse) msg);
                    String connection = (String) response.headers().get(HttpHeaderNames.CONNECTION);
                    if (connection != null && connection.equalsIgnoreCase(HttpHeaderValues.CLOSE.toString()))
                        forceReconnect = true;
                }

                if (msg instanceof HttpContent) {
                    HttpContent chunk = (HttpContent) msg;
                    String message = chunk.content().toString(CharsetUtil.UTF_8);

                    if (!message.isEmpty()) {
                        result[0] = message;

                        synchronized (result) {
                            result.notify();
                        }
                    }
                }
            }
        });
    }
    boolean isFileAttached = fileData != null && fileData.isReadable();
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            scheme + "://" + host + ":" + port + "/" + query);
    HttpPostRequestEncoder bodyReqEncoder;
    try {
        bodyReqEncoder = new HttpPostRequestEncoder(httpDataFactory, request, isFileAttached);

        for (Map.Entry<String, String> entry : postParameters) {
            bodyReqEncoder.addBodyAttribute(entry.getKey(), entry.getValue());
        }

        if (isFileAttached) {
            if (mime == null)
                mime = "application/octet-stream";

            MixedFileUpload mfu = new MixedFileUpload(filePostName, fileName, mime, "binary", null,
                    fileData.capacity(), DefaultHttpDataFactory.MINSIZE);
            mfu.addContent(fileData, true);
            bodyReqEncoder.addBodyHttpData(mfu);
        }

        HttpHeaders headers = request.headers();
        headers.set(HttpHeaderNames.HOST, host);
        headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
        headers.set(HttpHeaderNames.USER_AGENT, "OcarthonCore HttpClient");
        request = bodyReqEncoder.finalizeRequest();
    } catch (Exception e) {
        throw new NullPointerException("key or value is empty or null");
    }

    channel.write(request);

    if (bodyReqEncoder.isChunked()) {
        channel.write(bodyReqEncoder);
    }
    channel.flush();

    synchronized (result) {
        try {
            result.wait();
        } catch (InterruptedException e) {
            return null;
        }
    }

    return result[0];
}