Package org.apache.mina.core.buffer

Examples of org.apache.mina.core.buffer.IoBuffer


        }
    }

    @Override
    public void messageReceived(IoSession session, Object message) {
        IoBuffer buf = (IoBuffer) message;
        // Print out read buffer content.
        while (buf.hasRemaining()) {
            System.out.print((char) buf.get());
        }
        System.out.flush();
    }
View Full Code Here


    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {

        if (message instanceof IoBuffer) {
            IoBuffer buffer = (IoBuffer) message;
            SocketAddress remoteAddress = session.getRemoteAddress();
            server.recvUpdate(remoteAddress, buffer.getLong());
        }
    }
View Full Code Here

                }
            }

            if (!hasChunkedData) {
                if (contentLength > 0) {
                    IoBuffer tmp = decoder.decodeFully(buf);
                    if (tmp == null) {
                        return;
                    }
                    responseData.setAutoExpand(true);
                    responseData.put(tmp);
                    contentLength = 0;
                }

                if ("chunked".equalsIgnoreCase(StringUtilities
                        .getSingleValuedHeader(parsedResponse.getHeaders(),
                                "Transfer-Encoding"))) {
                    // Handle Transfer-Encoding: Chunked
                    logger.debug("Retrieving additional http response chunks");
                    hasChunkedData = true;
                    waitingChunkedData = true;
                }
            }

            if (hasChunkedData) {
                // Read chunks
                while (waitingChunkedData) {
                    if (contentLength == 0) {
                        decoder.setDelimiter(CRLF_DELIMITER, false);
                        IoBuffer tmp = decoder.decodeFully(buf);
                        if (tmp == null) {
                            return;
                        }

                        String chunkSize = tmp.getString(getProxyIoSession()
                                .getCharset().newDecoder());
                        int pos = chunkSize.indexOf(';');
                        if (pos >= 0) {
                            chunkSize = chunkSize.substring(0, pos);
                        } else {
                            chunkSize = chunkSize.substring(0, chunkSize
                                    .length() - 2);
                        }
                        contentLength = Integer.decode("0x" + chunkSize);
                        if (contentLength > 0) {
                            contentLength += 2; // also read chunk's trailing CRLF
                            decoder.setContentLength(contentLength, true);
                        }
                    }

                    if (contentLength == 0) {
                        waitingChunkedData = false;
                        waitingFooters = true;
                        entityBodyLimitPosition = responseData.position();
                        break;
                    }

                    IoBuffer tmp = decoder.decodeFully(buf);
                    if (tmp == null) {
                        return;
                    }
                    contentLength = 0;
                    responseData.put(tmp);
                    buf.position(buf.position());
                }

                // Read footers
                while (waitingFooters) {
                    decoder.setDelimiter(CRLF_DELIMITER, false);
                    IoBuffer tmp = decoder.decodeFully(buf);
                    if (tmp == null) {
                        return;
                    }

                    if (tmp.remaining() == 2) {
                        waitingFooters = false;
                        break;
                    }

                    // add footer to headers         
                    String footer = tmp.getString(getProxyIoSession()
                            .getCharset().newDecoder());
                    String[] f = footer.split(":\\s?", 2);
                    StringUtilities.addValueToHeader(parsedResponse
                            .getHeaders(), f[0], f[1], false);
                    responseData.put(tmp);
View Full Code Here

     */
    private void writeRequest0(final NextFilter nextFilter,
            final HttpProxyRequest request) {
        try {
            String data = request.toHttpString();
            IoBuffer buf = IoBuffer.wrap(data.getBytes(getProxyIoSession()
                    .getCharsetName()));

            logger.debug("   write:\n{}", data.replace("\r", "\\r").replace(
                    "\n", "\\n\n"));

View Full Code Here

            final IoSession session, final Object message)
            throws ProxyAuthException {
        ProxyLogicHandler handler = getProxyHandler(session);

        synchronized (handler) {
            IoBuffer buf = (IoBuffer) message;

            if (handler.isHandshakeComplete()) {
                // Handshake done - pass data on as-is
                nextFilter.messageReceived(session, buf);

            } else {
                logger.debug(" Data Read: {} ({})", handler, buf);

                // Keep sending handshake data to the handler until we run out
                // of data or the handshake is finished
                while (buf.hasRemaining() && !handler.isHandshakeComplete()) {
                    logger.debug(" Pre-handshake - passing to handler");

                    int pos = buf.position();
                    handler.messageReceived(nextFilter, buf);

                    // Data not consumed or session closing
                    if (buf.position() == pos || session.isClosing()) {
                        return;
                    }
                }

                // Pass on any remaining data to the next filter
                if (buf.hasRemaining()) {
                    logger.debug(" Passing remaining data to next filter");

                    nextFilter.messageReceived(session, buf);
                }
            }
View Full Code Here

     * @param request the socks proxy request data
     * @return the encoded buffer
     */
    private IoBuffer encodeInitialGreetingPacket(final SocksProxyRequest request) {
        byte nbMethods = (byte) SocksProxyConstants.SUPPORTED_AUTH_METHODS.length;
        IoBuffer buf = IoBuffer.allocate(2 + nbMethods);

        buf.put(request.getProtocolVersion());
        buf.put(nbMethods);
        buf.put(SocksProxyConstants.SUPPORTED_AUTH_METHODS);

        return buf;
    }
View Full Code Here

        } else {
            len += 1 + host.length;
            addressType = SocksProxyConstants.DOMAIN_NAME_ADDRESS_TYPE;
        }

        IoBuffer buf = IoBuffer.allocate(len);

        buf.put(request.getProtocolVersion());
        buf.put(request.getCommandCode());
        buf.put((byte) 0x00); // Reserved
        buf.put(addressType);

        if (addressType == SocksProxyConstants.DOMAIN_NAME_ADDRESS_TYPE) {
            buf.put((byte) host.length);
            buf.put(host);
        } else {
            buf.put(request.getIpAddress());
        }

        buf.put(request.getPort());

        return buf;
    }
View Full Code Here

        }

        private Object getMessageCopy(Object message) {
            Object messageCopy = message;
            if (message instanceof IoBuffer) {
                IoBuffer rb = (IoBuffer) message;
                rb.mark();
                IoBuffer wb = IoBuffer.allocate(rb.remaining());
                wb.put(rb);
                wb.flip();
                rb.reset();
                messageCopy = wb;
            }
            return messageCopy;
        }
View Full Code Here

        case SocksProxyConstants.BASIC_AUTH:
            // The basic auth scheme packet is sent
            byte[] user = request.getUserName().getBytes("ASCII");
            byte[] pwd = request.getPassword().getBytes("ASCII");
            IoBuffer buf = IoBuffer.allocate(3 + user.length + pwd.length);

            buf.put(SocksProxyConstants.BASIC_AUTH_SUBNEGOTIATION_VERSION);
            buf.put((byte) user.length);
            buf.put(user);
            buf.put((byte) pwd.length);
            buf.put(pwd);

            return buf;
        }

        return null;
View Full Code Here

        byte[] token = (byte[]) getSession().getAttribute(GSS_TOKEN);
        if (token != null) {
            logger.debug("  Received Token[{}] = {}", token.length,
                    ByteUtilities.asHex(token));
        }
        IoBuffer buf = null;

        if (!ctx.isEstablished()) {
            // token is ignored on the first call
            if (token == null) {
                token = new byte[32];
            }

            token = ctx.initSecContext(token, 0, token.length);

            // Send a token to the server if one was generated by
            // initSecContext
            if (token != null) {
                logger.debug("  Sending Token[{}] = {}", token.length,
                        ByteUtilities.asHex(token));

                getSession().setAttribute(GSS_TOKEN, token);
                buf = IoBuffer.allocate(4 + token.length);
                buf.put(new byte[] {
                        SocksProxyConstants.GSSAPI_AUTH_SUBNEGOTIATION_VERSION,
                        SocksProxyConstants.GSSAPI_MSG_TYPE });

                buf.put(ByteUtilities.intToNetworkByteOrder(token.length,
                        new byte[2], 0, 2));
                buf.put(token);
            }
        }

        return buf;
    }
View Full Code Here

TOP

Related Classes of org.apache.mina.core.buffer.IoBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.