List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(Charset charset);
From source file:org.apache.camel.component.hl7.HL7MLLPNettyDecoder.java
License:Apache License
private String asString(ByteBuf msg) { String s = msg.toString(config.getCharset()); if (config.isConvertLFtoCR()) { return s.replace('\n', '\r'); }/* w w w . j a va 2 s .c om*/ return s; }
From source file:org.apache.camel.component.netty4.codec.DatagramPacketStringDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception { if (msg.content() instanceof ByteBuf) { ByteBuf payload = (ByteBuf) msg.content(); AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>( payload.toString(charset), msg.recipient(), msg.sender()); out.add(addressedEnvelop);/*from w w w .j a v a 2 s .c o m*/ } }
From source file:org.apache.camel.component.netty4.http.handlers.HttpServerChannelHandler.java
License:Apache License
/** * Extracts the username and password details from the HTTP basic header Authorization. * <p/>/*from w ww. j av a 2 s.c om*/ * This requires that the <tt>Authorization</tt> HTTP header is provided, and its using Basic. * Currently Digest is <b>not</b> supported. * * @return {@link HttpPrincipal} with username and password details, or <tt>null</tt> if not possible to extract */ protected static HttpPrincipal extractBasicAuthSubject(HttpRequest request) { String auth = request.headers().get("Authorization"); if (auth != null) { String constraint = ObjectHelper.before(auth, " "); if (constraint != null) { if ("Basic".equalsIgnoreCase(constraint.trim())) { String decoded = ObjectHelper.after(auth, " "); // the decoded part is base64 encoded, so we need to decode that ByteBuf buf = NettyConverter.toByteBuffer(decoded.getBytes()); ByteBuf out = Base64.decode(buf); String userAndPw = out.toString(Charset.defaultCharset()); String username = ObjectHelper.before(userAndPw, ":"); String password = ObjectHelper.after(userAndPw, ":"); HttpPrincipal principal = new HttpPrincipal(username, password); LOG.debug("Extracted Basic Auth principal from HTTP header: {}", principal); return principal; } } } return null; }
From source file:org.apache.hadoop.hbase.security.SaslClientHandler.java
License:Apache License
/** * Get the read status//from www . j a v a 2s .c om * * @param inStream to read * @throws org.apache.hadoop.ipc.RemoteException if status was not success */ private static void readStatus(ByteBuf inStream) throws RemoteException { int status = inStream.readInt(); // read status if (status != SaslStatus.SUCCESS.state) { throw new RemoteException(inStream.toString(Charset.forName("UTF-8")), inStream.toString(Charset.forName("UTF-8"))); } }
From source file:org.apache.hadoop.hdfs.server.datanode.web.dtp.TestDtpHttp2.java
License:Apache License
@Test public void test() throws InterruptedException, ExecutionException { int streamId = 3; FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); request.headers().add(HttpUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId); Promise<FullHttpResponse> promise = CHANNEL.eventLoop().newPromise(); synchronized (RESPONSE_HANDLER) { CHANNEL.writeAndFlush(request);//from w w w. j av a2s . c o m RESPONSE_HANDLER.put(streamId, promise); } assertEquals(HttpResponseStatus.OK, promise.get().status()); ByteBuf content = promise.get().content(); assertEquals("HTTP/2 DTP", content.toString(StandardCharsets.UTF_8)); }
From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.RecordIdDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { throw new IOException( "Received unexpected empty frame. Maybe you have enabled secure transmission on only one endpoint of the connection."); }/* ww w. j av a2 s . com*/ byte type = frame.readByte(); frame.discardReadBytes(); String id = frame.toString(CharsetUtil.UTF_8); try { log.debug("received type {} with id {}", type, id); return RecordId.fromString(store.getTracker(), id); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); } return null; }
From source file:org.apache.pulsar.functions.worker.FunctionStatsGeneratorTest.java
License:Apache License
@Test public void testFunctionsStatsGenerate() { FunctionRuntimeManager functionRuntimeManager = mock(FunctionRuntimeManager.class); Map<String, FunctionRuntimeInfo> functionRuntimeInfoMap = new HashMap<>(); WorkerService workerService = mock(WorkerService.class); doReturn(functionRuntimeManager).when(workerService).getFunctionRuntimeManager(); CompletableFuture<InstanceCommunication.MetricsData> metricsDataCompletableFuture = new CompletableFuture<>(); InstanceCommunication.MetricsData metricsData = InstanceCommunication.MetricsData.newBuilder() .putMetrics("__total_processed__", InstanceCommunication.MetricsData.DataDigest.newBuilder().setCount(100.0).setMax(200.0) .setSum(300.0).setMin(0.0).build()) .putMetrics("__avg_latency_ms__", InstanceCommunication.MetricsData.DataDigest.newBuilder() .setCount(10.0).setMax(20.0).setSum(30.0).setMin(0.0).build()) .build();// w w w . j av a 2 s . c o m metricsDataCompletableFuture.complete(metricsData); Runtime runtime = mock(Runtime.class); doReturn(metricsDataCompletableFuture).when(runtime).getAndResetMetrics(); RuntimeSpawner runtimeSpawner = mock(RuntimeSpawner.class); doReturn(runtime).when(runtimeSpawner).getRuntime(); Function.FunctionMetaData function1 = Function.FunctionMetaData.newBuilder() .setFunctionDetails(Function.FunctionDetails.newBuilder().setTenant("test-tenant") .setNamespace("test-namespace").setName("func-1")) .build(); Function.Instance instance = Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0) .build(); FunctionRuntimeInfo functionRuntimeInfo = mock(FunctionRuntimeInfo.class); doReturn(runtimeSpawner).when(functionRuntimeInfo).getRuntimeSpawner(); doReturn(instance).when(functionRuntimeInfo).getFunctionInstance(); functionRuntimeInfoMap.put(Utils.getFullyQualifiedInstanceId(instance), functionRuntimeInfo); doReturn(functionRuntimeInfoMap).when(functionRuntimeManager).getFunctionRuntimeInfos(); ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer(); SimpleTextOutputStream statsOut = new SimpleTextOutputStream(buf); FunctionsStatsGenerator.generate(workerService, "default", statsOut); String str = buf.toString(Charset.defaultCharset()); buf.release(); Map<String, Metric> metrics = parseMetrics(str); Assert.assertEquals(metrics.size(), 8); Metric m = metrics.get("pulsar_function__total_processed__count"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 100.0); m = metrics.get("pulsar_function__total_processed__max"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 200.0); m = metrics.get("pulsar_function__total_processed__sum"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 300.0); m = metrics.get("pulsar_function__total_processed__min"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 0.0); m = metrics.get("pulsar_function__avg_latency_ms__count"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 10.0); m = metrics.get("pulsar_function__avg_latency_ms__max"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 20.0); m = metrics.get("pulsar_function__avg_latency_ms__sum"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 30.0); m = metrics.get("pulsar_function__avg_latency_ms__min"); assertEquals(m.tags.get("cluster"), "default"); assertEquals(m.tags.get("instanceId"), "0"); assertEquals(m.tags.get("name"), "func-1"); assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace"); assertEquals(m.value, 0.0); }
From source file:org.apache.spark.network.sasl.SparkSaslServer.java
License:Apache License
/** Return a Base64-encoded string. */ private static String getBase64EncodedString(String str) { ByteBuf byteBuf = null;// www.jav a2s . c om ByteBuf encodedByteBuf = null; try { byteBuf = Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8)); encodedByteBuf = Base64.encode(byteBuf); return encodedByteBuf.toString(StandardCharsets.UTF_8); } finally { // The release is called to suppress the memory leak error messages raised by netty. if (byteBuf != null) { byteBuf.release(); if (encodedByteBuf != null) { encodedByteBuf.release(); } } } }
From source file:org.apache.tajo.plan.function.stream.ByteBufLineReader.java
License:Apache License
public String readLine() throws IOException { ByteBuf buf = readLineBuf(lineReadBytes); if (buf != null) { return buf.toString(CharsetUtil.UTF_8); }/*from w w w.j a va2 s. c o m*/ return null; }
From source file:org.apache.tajo.storage.TestLineReader.java
License:Apache License
@Test public void testCRLFLine() throws IOException { TajoConf conf = new TajoConf(); Path testFile = new Path(CommonTestingUtil.getTestDir(TEST_PATH), "testCRLFLineText.txt"); FileSystem fs = testFile.getFileSystem(conf); FSDataOutputStream outputStream = fs.create(testFile, true); outputStream.write("0\r\n1\r\n".getBytes()); outputStream.flush();/*from w w w. j a va 2 s. c om*/ IOUtils.closeStream(outputStream); ByteBufInputChannel channel = new ByteBufInputChannel(fs.open(testFile)); ByteBufLineReader reader = new ByteBufLineReader(channel, BufferPool.directBuffer(2)); FileStatus status = fs.getFileStatus(testFile); long totalRead = 0; int i = 0; AtomicInteger bytes = new AtomicInteger(); for (;;) { ByteBuf buf = reader.readLineBuf(bytes); totalRead += bytes.get(); if (buf == null) break; String row = buf.toString(Charset.defaultCharset()); assertEquals(i, Integer.parseInt(row)); i++; } IOUtils.cleanup(null, reader); assertEquals(status.getLen(), totalRead); assertEquals(status.getLen(), reader.readBytes()); }