List of usage examples for io.netty.buffer Unpooled wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuffer... buffers)
From source file:com.linecorp.armeria.client.endpoint.dns.DnsTextEndpointGroupTest.java
License:Apache License
private static DnsRecord newTooLongTxtRecord(String hostname) { return new DefaultDnsRawRecord(hostname, TXT, 60, Unpooled.wrappedBuffer(new byte[] { 1, 0, 0 // Contains one more byte than expected }));/* ww w . j a v a2 s. c o m*/ }
From source file:com.linecorp.armeria.internal.grpc.ArmeriaMessageDeframerTest.java
License:Apache License
@Test public void deframe_tooLargeUncompressed() throws Exception { SimpleRequest request = SimpleRequest.newBuilder() .setPayload(Payload.newBuilder().setBody(ByteString.copyFromUtf8(Strings.repeat("a", 1024)))) .build();// w w w . ja va 2 s. c om byte[] frame = GrpcTestUtil.uncompressedFrame(Unpooled.wrappedBuffer(request.toByteArray())); assertThat(frame.length).isGreaterThan(1024); deframer.request(1); assertThatThrownBy(() -> deframer.deframe(HttpData.of(frame), false)) .isInstanceOf(StatusRuntimeException.class); }
From source file:com.linecorp.armeria.internal.grpc.ArmeriaMessageDeframerTest.java
License:Apache License
@Test public void deframe_tooLargeCompressed() throws Exception { // Simple repeated character compresses below the frame threshold but uncompresses above it. SimpleRequest request = SimpleRequest.newBuilder() .setPayload(Payload.newBuilder().setBody(ByteString.copyFromUtf8(Strings.repeat("a", 1024)))) .build();//from w ww .j a v a2 s .co m byte[] frame = GrpcTestUtil.compressedFrame(Unpooled.wrappedBuffer(request.toByteArray())); assertThat(frame.length).isLessThan(1024); deframer.request(1); deframer.deframe(HttpData.of(frame), false); ArgumentCaptor<ByteBufOrStream> messageCaptor = ArgumentCaptor.forClass(ByteBufOrStream.class); verify(listener).messageRead(messageCaptor.capture()); verifyNoMoreInteractions(listener); try (InputStream stream = messageCaptor.getValue().stream()) { assertThatThrownBy(() -> ByteStreams.toByteArray(stream)).isInstanceOf(StatusRuntimeException.class); } }
From source file:com.linecorp.armeria.internal.PooledObjects.java
License:Apache License
private static <T> T copyAndRelease(ByteBufHolder o) { try {//from ww w. ja v a2 s . co m final ByteBuf content = Unpooled.wrappedBuffer(ByteBufUtil.getBytes(o.content())); @SuppressWarnings("unchecked") final T copy = (T) o.replace(content); return copy; } finally { ReferenceCountUtil.safeRelease(o); } }
From source file:com.linecorp.armeria.server.http.file.HttpFileServiceInvocationHandler.java
License:Apache License
@Override public void invoke(ServiceInvocationContext ctx, Executor blockingTaskExecutor, Promise<Object> promise) throws Exception { final HttpRequest req = ctx.originalRequest(); if (req.method() != HttpMethod.GET) { respond(ctx, promise, HttpResponseStatus.METHOD_NOT_ALLOWED, 0, ERROR_MIME_TYPE, Unpooled.wrappedBuffer(CONTENT_METHOD_NOT_ALLOWED)); return;/* w w w . j a v a 2 s . c om*/ } final String path = normalizePath(ctx.mappedPath()); if (path == null) { respond(ctx, promise, HttpResponseStatus.NOT_FOUND, 0, ERROR_MIME_TYPE, Unpooled.wrappedBuffer(CONTENT_NOT_FOUND)); return; } Entry entry = getEntry(path); long lastModifiedMillis; if ((lastModifiedMillis = entry.lastModifiedMillis()) == 0) { boolean found = false; if (path.charAt(path.length() - 1) == '/') { // Try index.html if it was a directory access. entry = getEntry(path + "index.html"); if ((lastModifiedMillis = entry.lastModifiedMillis()) != 0) { found = true; } } if (!found) { respond(ctx, promise, HttpResponseStatus.NOT_FOUND, 0, ERROR_MIME_TYPE, Unpooled.wrappedBuffer(CONTENT_NOT_FOUND)); return; } } long ifModifiedSinceMillis = Long.MIN_VALUE; try { ifModifiedSinceMillis = req.headers().getTimeMillis(HttpHeaderNames.IF_MODIFIED_SINCE, Long.MIN_VALUE); } catch (Exception e) { // Ignore the ParseException, which is raised on malformed date. //noinspection ConstantConditions if (!(e instanceof ParseException)) { throw e; } } // HTTP-date does not have subsecond-precision; add 999ms to it. if (ifModifiedSinceMillis > Long.MAX_VALUE - 999) { ifModifiedSinceMillis = Long.MAX_VALUE; } else { ifModifiedSinceMillis += 999; } if (lastModifiedMillis < ifModifiedSinceMillis) { respond(ctx, promise, HttpResponseStatus.NOT_MODIFIED, lastModifiedMillis, entry.mimeType(), Unpooled.EMPTY_BUFFER); return; } respond(ctx, promise, HttpResponseStatus.OK, lastModifiedMillis, entry.mimeType(), entry.readContent(ctx.alloc())); }
From source file:com.linecorp.armeria.server.http.healthcheck.HttpHealthCheckService.java
License:Apache License
/** * Creates a new response which is sent when the {@link Server} is healthy. *///from ww w . j a v a2s.c o m protected FullHttpResponse newHealthyResponse( @SuppressWarnings("UnusedParameters") ServiceInvocationContext ctx) { return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(RES_OK)); }
From source file:com.linecorp.armeria.server.http.healthcheck.HttpHealthCheckService.java
License:Apache License
/** * Creates a new response which is sent when the {@link Server} is unhealthy. *//*from ww w . j a va 2s .co m*/ protected FullHttpResponse newUnhealthyResponse( @SuppressWarnings("UnusedParameters") ServiceInvocationContext ctx) { return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(RES_NOT_OK)); }
From source file:com.linecorp.armeria.server.http.Http2RequestDecoder.java
License:Apache License
private void writeErrorResponse(ChannelHandlerContext ctx, int streamId, HttpResponseStatus status) { final byte[] content = status.toString().getBytes(StandardCharsets.UTF_8); writer.writeHeaders(ctx, streamId,// ww w . j a v a 2s .c o m new DefaultHttp2Headers(false).status(status.codeAsText()) .set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) .setInt(HttpHeaderNames.CONTENT_LENGTH, content.length), 0, false, ctx.voidPromise()); writer.writeData(ctx, streamId, Unpooled.wrappedBuffer(content), 0, true, ctx.voidPromise()); }
From source file:com.linecorp.armeria.server.http.HttpServiceCodec.java
License:Apache License
@Override public ByteBuf encodeFailureResponse(ServiceInvocationContext ctx, Throwable cause) throws Exception { final StringWriter sw = new StringWriter(512); final PrintWriter pw = new PrintWriter(sw); cause.printStackTrace(pw);/* ww w . ja v a 2 s. c o m*/ pw.flush(); return Unpooled.wrappedBuffer(sw.toString().getBytes(CharsetUtil.UTF_8)); }
From source file:com.linkedin.flashback.smartproxy.utils.NoMatchResponseGenerator.java
License:Open Source License
public static FullHttpResponse generateNoMatchResponse(RecordedHttpRequest recordedHttpRequest) { StringBuilder bodyTextBuilder = new StringBuilder(); bodyTextBuilder.append("No Matching Request\n").append("Incoming Request Method: ") .append(recordedHttpRequest.getMethod()).append("\n").append("Incoming Request URI: ") .append(recordedHttpRequest.getUri()).append("\n").append("Incoming Request Headers: ") .append(recordedHttpRequest.getHeaders()).append("\n"); RecordedHttpBody incomingBody = recordedHttpRequest.getHttpBody(); if (incomingBody != null) { if (incomingBody instanceof RecordedEncodedHttpBody) { incomingBody = ((RecordedEncodedHttpBody) incomingBody).getDecodedBody(); }/*w w w . j ava2s .c o m*/ if (incomingBody instanceof RecordedStringHttpBody) { bodyTextBuilder.append("Incoming Request Body: ") .append(((RecordedStringHttpBody) incomingBody).getContent()); } else { bodyTextBuilder.append("Incoming Request Body: (binary content)"); } } ByteBuf badRequestBody = Unpooled .wrappedBuffer(bodyTextBuilder.toString().getBytes(Charset.forName("UTF-8"))); return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST, badRequestBody); }