Example usage for io.netty.handler.codec.http ClientCookieEncoder encode

List of usage examples for io.netty.handler.codec.http ClientCookieEncoder encode

Introduction

In this page you can find the example usage for io.netty.handler.codec.http ClientCookieEncoder encode.

Prototype

@Deprecated
public static String encode(Iterable<Cookie> cookies) 

Source Link

Document

Encodes the specified cookies into a single Cookie header value.

Usage

From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java

License:Apache License

private void getEvents(Channel ch, FullHttpResponse resp) {
    StringWriter estr = new StringWriter();
    getEvents.write(estr);//from  w w w .  java  2 s  .  co m
    String contentStr = estr.toString();

    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/events/get",
            ch.alloc().buffer().writeBytes(contentStr.getBytes()));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());
    // Set some example cookies.
    request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(sessCookies));

    l.info("getevents bytes {}", request.content().readableBytes());

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java

License:Apache License

public void login(Channel ch, JSONObject credential) {
    StringWriter estr = new StringWriter();
    credential.write(estr);/*from   w ww  . j a  va2 s . co m*/
    String contentStr = estr.toString();
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/login",
            ch.alloc().buffer().writeBytes(contentStr.getBytes()));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
    if (sessCookies != null)
        request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(sessCookies));

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());

    l.info("login request {}", request);

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java

License:Apache License

public void pubEvent(Channel ch, JSONObject e, FullHttpResponse resp) {
    // Prepare the HTTP request.
    JSONArray es = new JSONArray();
    es.put(e);/*from w w w .j  av  a2 s  .co m*/

    StringWriter estr = new StringWriter();
    es.write(estr);
    String contentStr = estr.toString();
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/events",
            ch.alloc().buffer().writeBytes(contentStr.getBytes()));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());
    // Set some example cookies.
    request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(sessCookies));

    l.info("PUB Event request {}", request);

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:com.mastfrog.acteur.headers.CookieHeader.java

License:Open Source License

@Override
public String toString(Cookie[] value) {
    return ClientCookieEncoder.encode(value);
}

From source file:divconq.api.internal.ClientHandler.java

License:Open Source License

public void send(Message msg) {
    Logger.debug("Sending message: " + msg);

    try {//from ww w  . jav a2  s . c  om
        if (this.chan != null) {
            if (this.info.getKind() == ConnectorKind.WebSocket)
                this.chan.writeAndFlush(new TextWebSocketFrame(msg.toString()));
            else {
                DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
                        this.info.getPath());

                req.headers().set(Names.HOST, this.info.getHost());
                req.headers().set(Names.USER_AGENT, "DivConq HyperAPI Client 1.0");
                req.headers().set(Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
                req.headers().set(Names.CONTENT_ENCODING, "UTF-8");
                req.headers().set(Names.CONTENT_TYPE, "application/json; charset=utf-8");
                req.headers().set(Names.COOKIE, ClientCookieEncoder.encode(this.cookies.values()));

                // TODO make more efficient - UTF8 encode directly to buffer
                ByteBuf buf = Unpooled.copiedBuffer(msg.toString(), CharsetUtil.UTF_8);
                int clen = buf.readableBytes();
                req.content().writeBytes(buf);
                buf.release();

                // Add 'Content-Length' header only for a keep-alive connection.
                req.headers().set(Names.CONTENT_LENGTH, clen);

                this.chan.writeAndFlush(req);
            }
        }
    } catch (Exception x) {
        Logger.error("Send HTTP Message error: " + x);
    }
}

From source file:divconq.api.internal.DownloadHandler.java

License:Open Source License

public void start(final HyperSession parent, WritableByteChannel dest, String chanid,
        Map<String, Cookie> cookies, long size, long offset, final OperationCallback callback) {
    this.dest = dest;
    this.cookies = cookies;
    this.callback = callback;
    this.size = size;
    this.sent = offset;

    this.src = this.allocateChannel(parent, callback);

    if (this.callback.hasErrors()) {
        callback.complete();/*from  w  w  w.  j av  a 2  s . com*/
        return;
    }

    // send a request to get things going

    HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/download/" + chanid);

    req.headers().set(Names.HOST, parent.getInfo().getHost());
    req.headers().set(Names.USER_AGENT, "DivConq HyperAPI Client 1.0");
    req.headers().set(Names.CONNECTION, HttpHeaders.Values.CLOSE);
    req.headers().set(Names.COOKIE, ClientCookieEncoder.encode(this.cookies.values()));

    // send request
    this.src.writeAndFlush(req);
}

From source file:divconq.api.internal.UploadPostHandler.java

License:Open Source License

public void start(final HyperSession parent, ReadableByteChannel src, String chanid,
        Map<String, Cookie> cookies, long size, final OperationCallback callback) {
    this.src = src;
    this.cookies = cookies;
    this.callback = callback;

    this.dest = this.allocateChannel(parent, callback);

    if (this.callback.hasErrors()) {
        callback.complete();/*w ww  .  j av  a  2s .  c  om*/
        return;
    }

    // send a request to get things going      

    HttpDataFactory factory = new DefaultHttpDataFactory(false); // no disk 

    HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/upload/" + chanid + "/Final");

    req.headers().set(Names.HOST, parent.getInfo().getHost());
    req.headers().set(Names.USER_AGENT, "DivConq HyperAPI Client 1.0");
    req.headers().set(Names.CONNECTION, HttpHeaders.Values.CLOSE);
    req.headers().set(Names.COOKIE, ClientCookieEncoder.encode(this.cookies.values()));
    req.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED);

    HttpPostRequestEncoder bodyRequestEncoder = null;

    try {
        bodyRequestEncoder = new HttpPostRequestEncoder(factory, req, true); // true => multipart

        bodyRequestEncoder.addBodyHttpData(new UploadStream(src, "file", "fname", "application/octet-stream",
                "binary", null, size, callback));

        req = bodyRequestEncoder.finalizeRequest();
    } catch (ErrorDataEncoderException x) {
        callback.error(1, "Problem with send encoder: " + x);
        callback.complete();
        return;
    }

    // send request headers
    this.dest.write(req);

    try {
        this.dest.writeAndFlush(bodyRequestEncoder).sync();

        // wait for a response - then close, see messageReceived
    } catch (InterruptedException x) {
        callback.error(1, "Unable to write to socket: " + x);
        callback.complete();
    }
}

From source file:divconq.api.internal.UploadPutHandler.java

License:Open Source License

public void start(final HyperSession parent, ScatteringByteChannel src, String chanid,
        Map<String, Cookie> cookies, long size, long offset, final OperationCallback callback) {
    this.src = src;
    this.cookies = cookies;
    this.callback = callback;

    this.dest = this.allocateChannel(parent, callback);

    if (this.callback.hasErrors()) {
        callback.complete();/*from  ww w  .j  a  v  a2  s.  c  om*/
        return;
    }

    // send a request to get things going      
    HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT,
            "/upload/" + chanid + "/Final");

    req.headers().set(Names.HOST, parent.getInfo().getHost());
    req.headers().set(Names.USER_AGENT, "DivConq HyperAPI Client 1.0");
    req.headers().set(Names.CONNECTION, HttpHeaders.Values.CLOSE);
    req.headers().set(Names.COOKIE, ClientCookieEncoder.encode(this.cookies.values()));
    req.headers().set(HttpHeaders.Names.CONTENT_LENGTH, size - offset);

    // send request headers - must flush here in case CL = 0
    this.dest.writeAndFlush(req);

    // now start sending the file
    long sent = offset;
    callback.getContext().setAmountCompleted((int) (sent * 100 / size));

    ByteBuf bb = null;

    try {
        bb = Hub.instance.getBufferAllocator().directBuffer(64 * 1024); // TODO review if direct is best here

        long toskip = offset;

        if (src instanceof SeekableByteChannel) {
            ((SeekableByteChannel) src).position(toskip);
        } else {
            while (toskip > 0) {
                int skip = (int) Math.min(bb.capacity(), toskip);
                toskip -= bb.writeBytes(src, skip);
                bb.clear();
            }
        }

        // now start writing the upload
        int amt = bb.writeBytes(src, bb.capacity());

        while (amt != -1) {
            bb.retain(); // this ups ref cnt to 2 - we plan to reuse the buffer

            this.dest.writeAndFlush(bb).sync();

            sent += amt;

            if (size > 0)
                callback.getContext().setAmountCompleted((int) (sent * 100 / size));

            // by the time we get here, that buffer has been used up and we can use it for the next buffer
            if (bb.refCnt() != 1)
                throw new IOException("Buffer reference count is not correct");

            // stop writing if canceled
            if (!this.dest.isOpen()) {
                this.finish(); // might already be finished but to be sure (this is helpful when api.abortStream is called)
                break;
            }

            bb.clear();

            amt = bb.writeBytes(src, bb.capacity());
        }

        // we are now done with it
        bb.release();
    } catch (Exception x) {
        try {
            if (bb != null)
                bb.release();
        } catch (Exception x2) {
        }

        callback.error(1, "Local read error: " + x);
        this.finish();
    }
}

From source file:eu.smartenit.sbox.interfaces.sboxsdn.SboxSdnClient.java

License:Apache License

/**
 * The method that prepares the HTTP request header.
 * /*  w  w  w. java 2 s  .  c  o m*/
 * @param uri
 *            The URI of the SDN controller REST API
 * @param content
 *            The content to be sent, as plain text
 */
public DefaultFullHttpRequest prepareHttpRequest(URI uri, String content) {
    logger.debug("Preparing the http request.");
    // Create the HTTP content bytes.
    /*
    ByteBuf buffer = ByteBufUtil.encodeString(ByteBufAllocator.DEFAULT,
    CharBuffer.wrap(content), CharsetUtil.UTF_8);
    */

    // Prepare the HTTP request.
    // Set the HTTP protocol version, method, uri and content.
    /*
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(
    HttpVersion.HTTP_1_1, HttpMethod.POST, uri.getRawPath(), buffer);
    */
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            uri.getRawPath(), Unpooled.wrappedBuffer(content.getBytes()));

    // Set certain header parameters.
    request.headers().set(HttpHeaders.Names.HOST, uri.getHost());
    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT, "application/json; q=0.9,*/*;q=0.8");
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=UTF-8");
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.length());
    request.headers().set(HttpHeaders.Names.PRAGMA, HttpHeaders.Values.NO_CACHE);
    request.headers().set(HttpHeaders.Names.CACHE_CONTROL, HttpHeaders.Values.NO_CACHE);

    // Set some example cookies.
    request.headers().set(HttpHeaders.Names.COOKIE,
            ClientCookieEncoder.encode(new DefaultCookie("smartenit-cookie", "smartenit")));
    logger.debug("Prepared the following HTTP request to be sent: \n" + request.toString() + "\n"
            + request.content().toString(CharsetUtil.UTF_8));
    return request;
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientRequest.java

License:Apache License

public HttpClientRequest<T> withCookie(Cookie cookie) {
    String cookieHeader = ClientCookieEncoder.encode(cookie);
    return withHeader(HttpHeaders.Names.COOKIE, cookieHeader);
}