List of usage examples for io.netty.buffer Unpooled copiedBuffer
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset)
From source file:com.couchbase.client.core.ResponseHandlerTest.java
License:Apache License
@Test public void shouldSendProposedConfigToProvider() throws Exception { ClusterFacade clusterMock = mock(ClusterFacade.class); ConfigurationProvider providerMock = mock(ConfigurationProvider.class); ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock); ByteBuf config = Unpooled.copiedBuffer("{\"json\": true}", CharsetUtil.UTF_8); ResponseEvent retryEvent = new ResponseEvent(); retryEvent.setMessage(new InsertResponse(ResponseStatus.RETRY, KeyValueStatus.ERR_TEMP_FAIL.code(), 0, "bucket", config, null, mock(InsertRequest.class))); retryEvent.setObservable(mock(Subject.class)); handler.onEvent(retryEvent, 1, true); verify(providerMock, times(1)).proposeBucketConfig("bucket", "{\"json\": true}"); assertEquals(0, config.refCnt());/*from w w w. jav a 2 s. c o m*/ assertNull(retryEvent.getMessage()); assertNull(retryEvent.getObservable()); }
From source file:com.couchbase.client.core.ResponseHandlerTest.java
License:Apache License
@Test public void shouldIgnoreInvalidConfig() throws Exception { ClusterFacade clusterMock = mock(ClusterFacade.class); ConfigurationProvider providerMock = mock(ConfigurationProvider.class); ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock); ByteBuf config = Unpooled.copiedBuffer("Not my Vbucket", CharsetUtil.UTF_8); ResponseEvent retryEvent = new ResponseEvent(); retryEvent.setMessage(new InsertResponse(ResponseStatus.RETRY, KeyValueStatus.ERR_TEMP_FAIL.code(), 0, "bucket", config, null, mock(InsertRequest.class))); retryEvent.setObservable(mock(Subject.class)); handler.onEvent(retryEvent, 1, true); verify(providerMock, never()).proposeBucketConfig("bucket", "Not my Vbucket"); assertEquals(0, config.refCnt());//from w w w. j a v a2 s .c o m assertNull(retryEvent.getMessage()); assertNull(retryEvent.getObservable()); }
From source file:com.couchbase.client.core.util.HttpUtils.java
License:Open Source License
/** * Add the HTTP basic auth headers to a netty request. * * @param request the request to modify. * @param user the user of the request.// w w w.java2s . c o m * @param password the password of the request. */ public static void addAuth(final HttpRequest request, final String user, final String password) { ByteBuf rawBuf = Unpooled.copiedBuffer(user + ":" + password, CharsetUtil.UTF_8); try { ByteBuf encoded = Base64.encode(rawBuf); request.headers().add(HttpHeaders.Names.AUTHORIZATION, encoded.toString(CharsetUtil.UTF_8)); } finally { rawBuf.release(); } }
From source file:com.couchbase.client.io.QueryEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, QueryEvent ev, List<Object> out) throws Exception { ByteBuf queryBuf = Unpooled.copiedBuffer(ev.getQuery(), CharsetUtil.UTF_8); HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/query", queryBuf); request.headers().set("Content-Length", queryBuf.readableBytes()); request.headers().set("Content-Type", "text/plain"); out.add(request);// w ww . j a va 2 s. c o m queue.add(ev); }
From source file:com.crm.provisioning.thread.osa.CallbackServerHandler.java
License:Apache License
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx, String actionType) { StringBuilder content = new StringBuilder(); content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); content.append("<SOAP-ENV:Envelope"); content.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""); content.append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\""); content.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); content.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""); content.append(" xmlns:osaxsd=\"http://www.csapi.org/osa/schema\""); content.append(" xmlns:osa=\"http://www.csapi.org/osa/wsdl\""); content.append(" xmlns:csxsd=\"http://www.csapi.org/cs/schema\""); content.append(" xmlns:cs=\"http://www.csapi.org/cs/wsdl\">"); content.append("<SOAP-ENV:Body"); content.append(" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"); content.append("<cs:"); content.append(actionType);//from w w w.j a va 2 s.c o m content.append("Response></cs:"); content.append(actionType); content.append("Response>"); content.append("</SOAP-ENV:Body>"); content.append("</SOAP-ENV:Envelope>"); content.append("\r\n"); // Decide whether to close the connection or not. boolean keepAlive = isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_0, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(content.toString(), CharsetUtil.UTF_8)); dfGMT.setTimeZone(tz); response.headers().set(DATE, dfGMT.format(new Date())); response.headers().set(CONTENT_TYPE, "text/htlm"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(CONNECTION, keepAlive ? HttpHeaders.Values.KEEP_ALIVE : HttpHeaders.Values.CLOSE); // Write the response. ctx.write(response); return keepAlive; }
From source file:com.dempe.ketty.srv.http.HttpStaticFileServerHandler.java
License:Apache License
public void sendError(ChannelHandlerContext ctx, HttpResponseStatus status, FullHttpRequest request) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.dlc.server.DLCHttpServerHandler.java
private void writeResponse(ChannelHandlerContext ctx, String json) { buf.setLength(0);/*from w ww .j a v a2 s . com*/ buf.append(json); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); ctx.write(response).addListener(ChannelFutureListener.CLOSE); }
From source file:com.dwarf.netty.guide.http.snoop.HttpSnoopServerHandler.java
License:Apache License
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaderUtil.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, currentObj.decoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); // Add keep alive header as per: // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); }//from www . j av a 2 s. co m // Encode the cookie. String cookieString = request.headers().getAndConvert(COOKIE); if (cookieString != null) { Set<Cookie> cookies = ServerCookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
From source file:com.fanavard.challenge.server.websocket.server.WebSocketServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//w w w . ja v a 2 s . c o m buf.release(); HttpHeaderUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.fjn.helper.frameworkex.netty.v4.echotest.EchoClientHandler.java
License:Apache License
public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.write(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8)); }