List of usage examples for io.netty.channel.embedded EmbeddedChannel finish
public boolean finish()
From source file:com.ebay.jetstream.http.netty.client.HttpClient.java
License:MIT License
public void post(URI uri, Object content, Map<String, String> headers, ResponseFuture responsefuture) throws Exception { if (m_shutDown.get()) { throw new IOException("IO has been Shutdown = "); }//ww w . java 2s . co m if (uri == null) throw new NullPointerException("uri is null or incorrect"); if (!m_started.get()) { synchronized (this) { if (!m_started.get()) { start(); m_started.set(true); } } } ByteBuf byteBuf = Unpooled.buffer(m_config.getInitialRequestBufferSize()); ByteBufOutputStream outputStream = new ByteBufOutputStream(byteBuf); // transform to json try { m_mapper.writeValue(outputStream, content); } catch (JsonGenerationException e) { throw e; } catch (JsonMappingException e) { throw e; } catch (IOException e) { throw e; } finally { outputStream.close(); } EmbeddedChannel encoder; String contenteEncoding; if ("gzip".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP, 1)); contenteEncoding = "gzip"; } else if ("deflate".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB, 1)); contenteEncoding = "deflate"; } else { encoder = null; contenteEncoding = null; } if (encoder != null) { encoder.config().setAllocator(UnpooledByteBufAllocator.DEFAULT); encoder.writeOutbound(byteBuf); encoder.finish(); byteBuf = (ByteBuf) encoder.readOutbound(); encoder.close(); } DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toString(), byteBuf); if (contenteEncoding != null) { HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_ENCODING, contenteEncoding); } HttpHeaders.setHeader(request, HttpHeaders.Names.ACCEPT_ENCODING, "gzip, deflate"); HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_TYPE, "application/json"); HttpHeaders.setContentLength(request, byteBuf.readableBytes()); if (isKeepAlive()) HttpHeaders.setHeader(request, HttpHeaders.Names.CONNECTION, "keep-alive"); if (headers != null) { @SuppressWarnings("rawtypes") Iterator it = headers.entrySet().iterator(); while (it.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry pairs = (Map.Entry) it.next(); HttpHeaders.setHeader(request, (String) pairs.getKey(), pairs.getValue()); } } if (responsefuture != null) { RequestId reqid = RequestId.newRequestId(); m_responseDispatcher.add(reqid, responsefuture); HttpHeaders.setHeader(request, "X_EBAY_REQ_ID", reqid.toString()); // we expect this to be echoed in the response used for correlation. } if (m_dataQueue.size() < m_workQueueCapacity) { ProcessHttpWorkRequest workRequest = new ProcessHttpWorkRequest(this, uri, request); if (!m_dataQueue.offer(workRequest)) { if (responsefuture != null) { responsefuture.setFailure(); m_responseDispatcher.remove(request.headers().get("X_EBAY_REQ_ID")); } } } else { throw new IOException("downstream Queue is full "); } }
From source file:com.github.nettybook.ch7.junit.Base64EncoderTest.java
License:Apache License
@Test public void testEncoder() { String writeData = ""; ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes()); Base64Encoder encoder = new Base64Encoder(); EmbeddedChannel embeddedChannel = new EmbeddedChannel(encoder); embeddedChannel.writeOutbound(request); ByteBuf response = (ByteBuf) embeddedChannel.readOutbound(); String expect = "7JWI64WV7ZWY7IS47JqU"; assertEquals(expect, response.toString(Charset.defaultCharset())); embeddedChannel.finish(); }
From source file:com.github.nettybook.ch7.junit.DelimiterBasedFrameDecoderTest.java
License:Apache License
@Test public void testDecoder() { String writeData = "\r\n\r\n"; String firstResponse = "\r\n"; String secondResponse = "\r\n"; DelimiterBasedFrameDecoder decoder = new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter()); EmbeddedChannel embeddedChannel = new EmbeddedChannel(decoder); ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes()); boolean result = embeddedChannel.writeInbound(request); assertTrue(result);/*from w w w. j a va2s . co m*/ ByteBuf response = null; response = (ByteBuf) embeddedChannel.readInbound(); assertEquals(firstResponse, response.toString(Charset.defaultCharset())); response = (ByteBuf) embeddedChannel.readInbound(); assertEquals(secondResponse, response.toString(Charset.defaultCharset())); embeddedChannel.finish(); }
From source file:com.linkedin.r2.transport.http.client.TestRAPClientCodec.java
License:Apache License
@Test(dataProvider = "restRequest") public void testRequestEncoder(String uri, RestRequest request) { final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec()); ch.writeOutbound(request);// ww w . j a v a2 s . co m FullHttpRequest nettyRequest = (FullHttpRequest) ch.readOutbound(); Assert.assertEquals(nettyRequest.uri(), uri); Assert.assertEquals(nettyRequest.method(), HttpMethod.valueOf(request.getMethod())); Assert.assertEquals(nettyRequest.content().toString(CHARSET), request.getEntity().asString(CHARSET)); Assert.assertEquals(nettyRequest.headers().get(HttpHeaderNames.HOST), HOST); assertList(nettyRequest.headers().getAll(HttpConstants.REQUEST_COOKIE_HEADER_NAME), request.getCookies()); for (String name : request.getHeaders().keySet()) { Assert.assertEquals(nettyRequest.headers().get(name), request.getHeader(name)); } ch.finish(); }
From source file:com.linkedin.r2.transport.http.client.TestRAPClientCodec.java
License:Apache License
@Test(dataProvider = "responseData") public void testResponseDecoder(int status, String entity, HttpHeaders headers, String[] cookies) { final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec()); ByteBuf content = Unpooled.copiedBuffer(entity, CHARSET); FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(status), content); nettyResponse.headers().set(headers); for (String cookie : cookies) { nettyResponse.headers().add(HttpHeaderNames.SET_COOKIE, cookie); }/*from w w w .j ava 2 s. c om*/ ch.writeInbound(nettyResponse); RestResponse response = (RestResponse) ch.readInbound(); Assert.assertEquals(response.getStatus(), status); Assert.assertEquals(response.getEntity().asString(CHARSET), entity); assertList(response.getCookies(), nettyResponse.headers().getAll(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)); for (Map.Entry<String, String> header : nettyResponse.headers()) { if (!header.getKey().equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) { List<String> values = response.getHeaderValues(header.getKey()); Assert.assertNotNull(values); Assert.assertTrue(values.contains(header.getValue())); } } // make sure the incoming ByteBuf is released Assert.assertEquals(content.refCnt(), 0); ch.finish(); }
From source file:com.linkedin.r2.transport.http.client.TestRAPClientCodec.java
License:Apache License
@Test public void testDecodeException() { final EmbeddedChannel ch = new EmbeddedChannel(new HttpClientCodec(), new HttpObjectAggregator(65536), new RAPClientCodec()); // When we received an invalid message, a decode exception should be thrown out of the // end of netty pipeline. String junk = "Not a HTTP message\r\n"; try {/*from w ww. ja v a2 s . c o m*/ ch.writeInbound(Unpooled.copiedBuffer(junk, CHARSET)); Assert.fail("Should have thrown decode exception"); } catch (Exception ex) { // expected. } ch.finish(); }
From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java
License:Apache License
@Test public void testSinglePacket() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(makeNetflowDecoder()); byte[] bytes = get10V5MessagesBytes(); ch.writeInbound(Unpooled.wrappedBuffer(bytes)); List<Record> records = collect10NetflowV5MessagesFromChannel(ch, bytes.length); ch.finish(); NetflowTestUtil.assertRecordsForTenPackets(records); }
From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java
License:Apache License
@Test public void testMultiplePackets() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(makeNetflowDecoder()); byte[] bytes = get10V5MessagesBytes(); long bytesWritten = 0; List<List<Byte>> slices = NetTestUtils.getRandomByteSlices(bytes); for (int s = 0; s < slices.size(); s++) { List<Byte> slice = slices.get(s); byte[] sliceBytes = Bytes.toArray(slice); ch.writeInbound(Unpooled.wrappedBuffer(sliceBytes)); bytesWritten += sliceBytes.length; }/*from w w w .j a v a 2 s . c o m*/ assertThat(bytesWritten, equalTo((long) bytes.length)); List<Record> records = collect10NetflowV5MessagesFromChannel(ch, bytes.length); ch.finish(); NetflowTestUtil.assertRecordsForTenPackets(records); }
From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java
License:Apache License
@Test public void netflowV9SevenFlows() throws Exception { // TODO: add tests that include options templates/data EmbeddedChannel ch = new EmbeddedChannel(makeNetflowDecoder()); byte[] bytes = getV9MessagesBytes7Flows(); final boolean randomlySlice = RandomTestUtils.getRandom().nextBoolean(); writeBytesToChannel(ch, bytes, randomlySlice); final int expectedNumMsgs = 7; List<NetflowV9Message> messages = new ArrayList<>(); List<Record> records = collectNetflowV9MessagesFromChannel(ch, expectedNumMsgs, messages); ch.finish(); assertThat(messages, hasSize(expectedNumMsgs)); assertThat(records, hasSize(expectedNumMsgs)); for (int i = 0; i < expectedNumMsgs; i++) { NetflowV9Message message = messages.get(i); Record record = records.get(i);/*w w w. j av a 2 s .c om*/ // same value for all flows int exportSourceId = 1; int size = 56; FlowKind kind = FlowKind.FLOWSET; // flow-specific values Integer tcpFlags = null; Long firstSwitched = null; Long lastSwitched = null; String srcIpV4Addr = null; String destIpV4Addr = null; Integer srcPort = null; Integer destPort = null; Integer protocol = null; BigDecimal inPackets = null; BigDecimal inBytes = null; switch (i) { case 0: tcpFlags = 0; firstSwitched = 86400042L; lastSwitched = 86940154L; srcIpV4Addr = "127.0.0.1"; destIpV4Addr = "127.0.0.1"; srcPort = 52767; destPort = 9995; protocol = PROTOCOL_UDP; inPackets = new BigDecimal(29); inBytes = new BigDecimal(34964); break; case 1: tcpFlags = 0; firstSwitched = 87028319L; lastSwitched = 87028320L; srcIpV4Addr = "172.17.0.4"; destIpV4Addr = "192.168.65.1"; srcPort = 34460; destPort = 53; protocol = PROTOCOL_UDP; inPackets = new BigDecimal(1); inBytes = new BigDecimal(75); break; case 2: tcpFlags = 0; firstSwitched = 87028319L; lastSwitched = 87028320L; srcIpV4Addr = "192.168.65.1"; destIpV4Addr = "172.17.0.4"; srcPort = 53; destPort = 34460; protocol = PROTOCOL_UDP; inPackets = new BigDecimal(1); inBytes = new BigDecimal(75); break; case 3: tcpFlags = 0; firstSwitched = 87028320L; lastSwitched = 87028333L; srcIpV4Addr = "172.17.0.4"; destIpV4Addr = "192.168.65.1"; srcPort = 48251; destPort = 53; protocol = PROTOCOL_UDP; inPackets = new BigDecimal(1); inBytes = new BigDecimal(64); break; case 4: tcpFlags = 0; firstSwitched = 87028320L; lastSwitched = 87028333L; srcIpV4Addr = "192.168.65.1"; destIpV4Addr = "172.17.0.4"; srcPort = 53; destPort = 48251; protocol = PROTOCOL_UDP; inPackets = new BigDecimal(1); inBytes = new BigDecimal(128); break; case 5: tcpFlags = 0x1b; firstSwitched = 87028333L; lastSwitched = 87059145L; srcIpV4Addr = "91.189.88.161"; destIpV4Addr = "172.17.0.4"; srcPort = 80; destPort = 51156; protocol = PROTOCOL_TCP; inPackets = new BigDecimal(25233); inBytes = new BigDecimal(36890729); break; case 6: tcpFlags = 0x1b; firstSwitched = 87028333L; lastSwitched = 87059145L; srcIpV4Addr = "172.17.0.4"; destIpV4Addr = "91.189.88.161"; srcPort = 51156; destPort = 80; protocol = PROTOCOL_TCP; inPackets = new BigDecimal(11214); inBytes = new BigDecimal(452409); break; } assertNetflowV9MessageAndRecord(kind, message, record, tcpFlags, exportSourceId, size, firstSwitched, lastSwitched, srcIpV4Addr, destIpV4Addr, srcPort, destPort, protocol, inPackets, inBytes); } }
From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java
License:Apache License
@Test public void ciscoAsa() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(makeNetflowDecoder()); boolean randomlySlice = RandomTestUtils.getRandom().nextBoolean(); byte[] tplBytes = getMessagesBytes("ciscoasa/netflow9_test_cisco_asa_1_tpl.dat"); writeBytesToChannel(ch, tplBytes, randomlySlice); byte[] msgBytes = getMessagesBytes("ciscoasa/netflow9_test_cisco_asa_1_data.dat"); randomlySlice = RandomTestUtils.getRandom().nextBoolean(); writeBytesToChannel(ch, msgBytes, randomlySlice); final int expectedNumMsgs = 9; List<NetflowV9Message> messages = new ArrayList<>(); List<Record> records = collectNetflowV9MessagesFromChannel(ch, expectedNumMsgs, messages); ch.finish(); assertThat(messages, hasSize(expectedNumMsgs)); assertThat(records, hasSize(expectedNumMsgs)); }