List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(Charset charset);
From source file:com.corundumstudio.socketio.parser.PayloadTest.java
License:Apache License
@Test public void testPayloadEncode() throws IOException { Queue<Packet> packets = new ConcurrentLinkedQueue<Packet>(); Packet packet1 = new Packet(PacketType.MESSAGE); packet1.setData("5"); packets.add(packet1);// www . j a v a 2s .c o m Packet packet2 = new Packet(PacketType.MESSAGE); packet2.setData("53d"); packets.add(packet2); ByteBuf result = Unpooled.buffer(); // encoder.encodePackets(packets, result, UnpooledByteBufAllocator.DEFAULT); Assert.assertEquals("\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
public ByteBuf preprocessJson(Integer jsonIndex, ByteBuf content) throws IOException { String packet = URLDecoder.decode(content.toString(CharsetUtil.UTF_8), CharsetUtil.UTF_8.name()); int startPos = 0; if (jsonIndex != null) { // skip "d=" startPos = 2;//w ww. java2s . c om /** * double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side * (c) socket.io.js * * @see https://github.com/Automattic/socket.io-client/blob/1.3.3/socket.io.js#L2682 */ packet = packet.replace("\\\\n", "\\n"); } int splitIndex = packet.indexOf(":"); String len = packet.substring(startPos, splitIndex); Integer length = Integer.valueOf(len); packet = packet.substring(splitIndex + 1, splitIndex + length + 1); packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8); return Unpooled.wrappedBuffer(packet.getBytes(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
public Packet decodePackets(ByteBuf buffer, ClientHead client) throws IOException { if (isStringPacket(buffer)) { // TODO refactor int headEndIndex = buffer.bytesBefore((byte) -1); int len = (int) readLong(buffer, headEndIndex); ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len); // skip this frame buffer.readerIndex(buffer.readerIndex() + 1 + len); return decode(client, frame); } else if (hasLengthHeader(buffer)) { // TODO refactor int lengthEndIndex = buffer.bytesBefore((byte) ':'); int lenHeader = (int) readLong(buffer, lengthEndIndex); int len = utf8scanner.getActualLength(buffer, lenHeader); ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len); if (lenHeader != len) { frame = Unpooled.wrappedBuffer(frame.toString(CharsetUtil.UTF_8).getBytes(CharsetUtil.ISO_8859_1)); }/* www . java 2s . c om*/ // skip this frame buffer.readerIndex(buffer.readerIndex() + 1 + len); return decode(client, frame); } return decode(client, buffer); }
From source file:com.corundumstudio.socketio.SocketIOEncoder.java
License:Apache License
private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out) { HttpResponse res = createHttpResponse(msg.getOrigin(), out); channel.write(res);/* w ww .j a v a 2 s . com*/ if (log.isTraceEnabled()) { log.trace("Out message: {} - sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId()); } if (out.isReadable()) { channel.write(out); } else { out.release(); } ChannelFuture f = channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); f.addListener(ChannelFutureListener.CLOSE); }
From source file:com.corundumstudio.socketio.SocketIOEncoder.java
License:Apache License
private void handle(WebSocketPacketMessage webSocketPacketMessage, Channel channel, ByteBuf out) throws IOException { encoder.encodePacket(webSocketPacketMessage.getPacket(), out); WebSocketFrame res = new TextWebSocketFrame(out); log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), webSocketPacketMessage.getSessionId()); channel.writeAndFlush(res);/*from www . j av a 2s . c o m*/ if (!out.isReadable()) { out.release(); } }
From source file:com.couchbase.client.core.endpoint.AbstractGenericHandler.java
License:Apache License
/** * Add basic authentication headers to a {@link HttpRequest}. * * The given information is Base64 encoded and the authorization header is set appropriately. Since this needs * to be done for every request, it is refactored out. * * @param ctx the handler context./*from w w w . j a v a2 s . c o m*/ * @param request the request where the header should be added. * @param user the username for auth. * @param password the password for auth. */ public static void addHttpBasicAuth(final ChannelHandlerContext ctx, final HttpRequest request, final String user, final String password) { final String pw = password == null ? "" : password; ByteBuf raw = ctx.alloc().buffer(user.length() + pw.length() + 1); raw.writeBytes((user + ":" + pw).getBytes(CHARSET)); ByteBuf encoded = Base64.encode(raw, false); request.headers().add(HttpHeaders.Names.AUTHORIZATION, "Basic " + encoded.toString(CHARSET)); encoded.release(); raw.release(); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Helper method to decode all other response messages. * * @param request the current request./*from w w w . ja v a2 s . c om*/ * @param msg the current response message. * @param status the response status code. * @return the decoded response or null if none did match. */ private static CouchbaseResponse handleOtherResponseMessages(BinaryRequest request, FullBinaryMemcacheResponse msg, ResponseStatus status, boolean seqOnMutation, String remoteHostname) { CouchbaseResponse response = null; ByteBuf content = msg.content(); long cas = msg.getCAS(); short statusCode = msg.getStatus(); String bucket = request.bucket(); if (request instanceof UnlockRequest) { response = new UnlockResponse(status, statusCode, bucket, content, request); } else if (request instanceof TouchRequest) { response = new TouchResponse(status, statusCode, bucket, content, request); } else if (request instanceof AppendRequest) { MutationToken descr = extractToken(bucket, seqOnMutation, status.isSuccess(), msg.getExtras(), request.partition()); response = new AppendResponse(status, statusCode, cas, bucket, content, descr, request); } else if (request instanceof PrependRequest) { MutationToken descr = extractToken(bucket, seqOnMutation, status.isSuccess(), msg.getExtras(), request.partition()); response = new PrependResponse(status, statusCode, cas, bucket, content, descr, request); } else if (request instanceof KeepAliveRequest) { releaseContent(content); response = new KeepAliveResponse(status, statusCode, request); } else if (request instanceof CounterRequest) { long value = status.isSuccess() ? content.readLong() : 0; releaseContent(content); MutationToken descr = extractToken(bucket, seqOnMutation, status.isSuccess(), msg.getExtras(), request.partition()); response = new CounterResponse(status, statusCode, bucket, value, cas, descr, request); } else if (request instanceof StatRequest) { String key = new String(msg.getKey(), CHARSET); String value = content.toString(CHARSET); releaseContent(content); response = new StatResponse(status, statusCode, remoteHostname, key, value, bucket, request); } else if (request instanceof GetAllMutationTokensRequest) { // 2 bytes for partition ID, and 8 bytes for sequence number MutationToken[] mutationTokens = new MutationToken[content.readableBytes() / 10]; for (int i = 0; i < mutationTokens.length; i++) { mutationTokens[i] = new MutationToken((long) content.readShort(), 0, content.readLong(), request.bucket()); } releaseContent(content); response = new GetAllMutationTokensResponse(mutationTokens, status, statusCode, bucket, request); } else if (request instanceof ObserveRequest) { byte observed = ObserveResponse.ObserveStatus.UNKNOWN.value(); long observedCas = 0; if (status.isSuccess()) { short keyLength = content.getShort(2); observed = content.getByte(keyLength + 4); observedCas = content.getLong(keyLength + 5); } releaseContent(content); response = new ObserveResponse(status, statusCode, observed, ((ObserveRequest) request).master(), observedCas, bucket, request); } else if (request instanceof ObserveSeqnoRequest) { if (status.isSuccess()) { byte format = content.readByte(); switch (format) { case 0: response = new NoFailoverObserveSeqnoResponse(((ObserveSeqnoRequest) request).master(), content.readShort(), content.readLong(), content.readLong(), content.readLong(), status, statusCode, bucket, request); break; case 1: response = new FailoverObserveSeqnoResponse(((ObserveSeqnoRequest) request).master(), content.readShort(), content.readLong(), content.readLong(), content.readLong(), content.readLong(), content.readLong(), status, statusCode, bucket, request); break; default: throw new IllegalStateException("Unknown format for observe-seq: " + format); } } else { response = new NoFailoverObserveSeqnoResponse(((ObserveSeqnoRequest) request).master(), (short) 0, 0, 0, 0, status, statusCode, bucket, request); } releaseContent(content); } return response; }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Base method to handle the response for the generic query request. * * It waits for the first few bytes on the actual response to determine if an error is raised or if a successful * response can be expected. The actual error and/or chunk parsing is deferred to other parts of this handler. * * @return a {@link CouchbaseResponse} if eligible. *//* w ww. j a v a 2s.c om*/ private CouchbaseResponse handleGenericQueryResponse(boolean lastChunk) { String requestId; String clientId = ""; if (responseContent.readableBytes() < MINIMUM_WINDOW_FOR_REQUESTID + MINIMUM_WINDOW_FOR_CLIENTID_TOKEN && !lastChunk) { return null; //wait for more data } int startIndex = responseContent.readerIndex(); if (responseContent.readableBytes() >= MINIMUM_WINDOW_FOR_REQUESTID) { responseContent.skipBytes(findNextChar(responseContent, ':')); responseContent.skipBytes(findNextChar(responseContent, '"') + 1); int endOfId = findNextChar(responseContent, '"'); ByteBuf slice = responseContent.readSlice(endOfId); requestId = slice.toString(CHARSET); } else { return null; } //IMPORTANT: from there on, before returning null to get more data you need to reset //the cursor, since following code will consume data from the buffer. if (responseContent.readableBytes() >= MINIMUM_WINDOW_FOR_CLIENTID_TOKEN && findNextChar(responseContent, ':') < MINIMUM_WINDOW_FOR_CLIENTID_TOKEN) { responseContent.markReaderIndex(); ByteBuf slice = responseContent.readSlice(findNextChar(responseContent, ':')); if (slice.toString(CHARSET).contains("clientContextID")) { //find the size of the client id responseContent.skipBytes(findNextChar(responseContent, '"') + 1); //opening of clientId int clientIdSize = findNextCharNotPrefixedBy(responseContent, '"', '\\'); if (clientIdSize < 0) { //reset the cursor way back before requestID, there was not enough data to get the whole id responseContent.readerIndex(startIndex); //wait for more data return null; } //read it clientId = responseContent.readSlice(clientIdSize).toString(CHARSET); //advance to next token if possible //closing quote boolean hasClosingQuote = responseContent.readableBytes() > 0; if (hasClosingQuote) { responseContent.skipBytes(1); } //next token's quote int openingNextToken = findNextChar(responseContent, '"'); if (openingNextToken > -1) { responseContent.skipBytes(openingNextToken); } } else { //reset the cursor, there was no client id responseContent.resetReaderIndex(); } } boolean success = true; if (responseContent.readableBytes() >= 20) { ByteBuf peekForErrors = responseContent.slice(responseContent.readerIndex(), 20); if (peekForErrors.toString(CHARSET).contains("errors")) { success = false; } } else { //it is important to reset the readerIndex if returning null, in order to allow for complete retry responseContent.readerIndex(startIndex); return null; } ResponseStatus status = ResponseStatusConverter.fromHttp(responseHeader.getStatus().code()); if (!success) { status = ResponseStatus.FAILURE; } Scheduler scheduler = env().scheduler(); long ttl = env().autoreleaseAfter(); queryRowObservable = UnicastAutoReleaseSubject.create(ttl, TimeUnit.MILLISECONDS, scheduler); queryErrorObservable = UnicastAutoReleaseSubject.create(ttl, TimeUnit.MILLISECONDS, scheduler); queryStatusObservable = AsyncSubject.create(); queryInfoObservable = UnicastAutoReleaseSubject.create(ttl, TimeUnit.MILLISECONDS, scheduler); querySignatureObservable = UnicastAutoReleaseSubject.create(ttl, TimeUnit.MILLISECONDS, scheduler); return new GenericQueryResponse(queryErrorObservable.onBackpressureBuffer().observeOn(scheduler), queryRowObservable.onBackpressureBuffer().observeOn(scheduler), querySignatureObservable.onBackpressureBuffer().observeOn(scheduler), queryStatusObservable.onBackpressureBuffer().observeOn(scheduler), queryInfoObservable.onBackpressureBuffer().observeOn(scheduler), currentRequest(), status, requestId, clientId); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Peek the next token, returning the QUERY_STATE corresponding to it and placing the readerIndex just after * the token's ':'. Must be at the end of the previous token. * * @param lastChunk true if this is the last chunk * @return the next QUERY_STATE/* ww w. j a v a 2 s . c o m*/ */ private byte transitionToNextToken(boolean lastChunk) { int endNextToken = findNextChar(responseContent, ':'); if (endNextToken < 0 && !lastChunk) { return queryParsingState; } if (endNextToken < 0 && lastChunk && queryParsingState >= QUERY_STATE_STATUS) { return QUERY_STATE_NO_INFO; } byte newState; ByteBuf peekSlice = responseContent.readSlice(endNextToken + 1); String peek = peekSlice.toString(CHARSET); if (peek.contains("\"signature\":")) { newState = QUERY_STATE_SIGNATURE; } else if (peek.endsWith("\"results\":")) { newState = QUERY_STATE_ROWS_DECIDE; } else if (peek.endsWith("\"status\":")) { newState = QUERY_STATE_STATUS; } else if (peek.endsWith("\"errors\":")) { newState = QUERY_STATE_ERROR; } else if (peek.endsWith("\"warnings\":")) { newState = QUERY_STATE_WARNING; } else if (peek.endsWith("\"metrics\":")) { newState = QUERY_STATE_INFO; } else { if (lastChunk) { IllegalStateException e = new IllegalStateException( "Error parsing query response (in TRANSITION) at \"" + peek + "\", enable trace to see response content"); if (LOGGER.isTraceEnabled()) { LOGGER.trace(responseContent.toString(CHARSET), e); } throw e; } else { //we need more data return queryParsingState; } } sectionDone = false; return newState; }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Last before the end of the stream, we can now parse the final result status * (including full execution of the query). */// w w w . java 2 s. com private void parseQueryStatus(boolean lastChunk) { //some sections don't always come up, unlike status. Take this chance to close said sections' observables here. querySignatureObservable.onCompleted(); queryRowObservable.onCompleted(); queryErrorObservable.onCompleted(); responseContent.markReaderIndex(); responseContent.skipBytes(findNextChar(responseContent, '"') + 1); int endStatus = findNextChar(responseContent, '"'); if (endStatus > -1) { ByteBuf resultSlice = responseContent.readSlice(endStatus); queryStatusObservable.onNext(resultSlice.toString(CHARSET)); queryStatusObservable.onCompleted(); sectionDone(); queryParsingState = transitionToNextToken(lastChunk); } else { responseContent.resetReaderIndex(); return; //need more data } }