org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled Java Examples

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ByteBufUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create a source buffer whose length is <tt>size</tt>. The content between <tt>readerIndex</tt> and
 * <tt>readerIndex + accumulationSize</tt> is <tt>ACCUMULATION_BYTE</tt> and the remaining is
 * <tt>NON_ACCUMULATION_BYTE</tt>.
 *
 * @param size The size of the source buffer.
 * @param readerIndex The reader index of the source buffer.
 * @param accumulationSize The size of bytes that will be read for accumulating.
 *
 * @return The required source buffer.
 */
private ByteBuf createSourceBuffer(int size, int readerIndex, int accumulationSize) {
	ByteBuf buf = Unpooled.buffer(size);

	for (int i = 0; i < readerIndex; i++) {
		buf.writeByte(NON_ACCUMULATION_BYTE);
	}

	for (int i = readerIndex; i < readerIndex + accumulationSize; i++) {
		buf.writeByte(ACCUMULATION_BYTE);
	}

	for (int i = readerIndex + accumulationSize; i < size; i++) {
		buf.writeByte(NON_ACCUMULATION_BYTE);
	}

	buf.readerIndex(readerIndex);
	return buf;
}
 
Example #2
Source File: ByteBufUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccumulateWithoutCopy() {
	int sourceLength = 128;
	int sourceReaderIndex = 32;
	int expectedAccumulationSize = 16;

	ByteBuf src = createSourceBuffer(sourceLength, sourceReaderIndex, expectedAccumulationSize);
	ByteBuf target = Unpooled.buffer(expectedAccumulationSize);

	// If src has enough data and no data has been copied yet, src will be returned without modification.
	ByteBuf accumulated = ByteBufUtils.accumulate(target, src, expectedAccumulationSize, target.readableBytes());

	assertSame(src, accumulated);
	assertEquals(sourceReaderIndex, src.readerIndex());
	verifyBufferContent(src, sourceReaderIndex, expectedAccumulationSize);
}
 
Example #3
Source File: PipelineErrorHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void sendError(ChannelHandlerContext ctx, String error) {
	if (ctx.channel().isActive()) {
		DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			Unpooled.wrappedBuffer(error.getBytes(ConfigConstants.DEFAULT_CHARSET)));

		response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
		response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

		ctx.writeAndFlush(response);
	}
}
 
Example #4
Source File: ByteBufUtilsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccumulateWithCopy() {
	int sourceLength = 128;
	int firstSourceReaderIndex = 32;
	int secondSourceReaderIndex = 0;
	int expectedAccumulationSize = 128;

	int firstAccumulationSize = sourceLength - firstSourceReaderIndex;
	int secondAccumulationSize = expectedAccumulationSize - firstAccumulationSize;

	ByteBuf firstSource = createSourceBuffer(sourceLength, firstSourceReaderIndex, firstAccumulationSize);
	ByteBuf secondSource = createSourceBuffer(sourceLength, secondSourceReaderIndex, secondAccumulationSize);

	ByteBuf target = Unpooled.buffer(expectedAccumulationSize);

	// If src does not have enough data, src will be copied into target and null will be returned.
	ByteBuf accumulated = ByteBufUtils.accumulate(
		target,
		firstSource,
		expectedAccumulationSize,
		target.readableBytes());
	assertNull(accumulated);
	assertEquals(sourceLength, firstSource.readerIndex());
	assertEquals(firstAccumulationSize, target.readableBytes());

	// The remaining data will be copied from the second buffer, and the target buffer will be returned
	// after all data is accumulated.
	accumulated = ByteBufUtils.accumulate(
		target,
		secondSource,
		expectedAccumulationSize,
		target.readableBytes());
	assertSame(target, accumulated);
	assertEquals(secondSourceReaderIndex + secondAccumulationSize, secondSource.readerIndex());
	assertEquals(expectedAccumulationSize, target.readableBytes());

	verifyBufferContent(accumulated, 0, expectedAccumulationSize);
}
 
Example #5
Source File: AbstractHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #6
Source File: NetworkBuffer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf readBytes(int length) {
	// copied from the one in netty 4.0.50 fixing the wrong allocator being used
	checkReadableBytes(length);
	if (length == 0) {
		return Unpooled.EMPTY_BUFFER;
	}

	ByteBuf buf = alloc().buffer(length, maxCapacity());
	int readerIndex = readerIndex();
	buf.writeBytes(this, readerIndex, length);
	readerIndex(readerIndex + length);
	return buf;
}
 
Example #7
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example #8
Source File: HandlerRedirectUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
	ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0)
		: Unpooled.wrappedBuffer(message.getBytes(ENCODING));
	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

	return response;
}
 
Example #9
Source File: PipelineErrorHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void sendError(ChannelHandlerContext ctx, String error) {
	if (ctx.channel().isActive()) {
		DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			Unpooled.wrappedBuffer(error.getBytes(ConfigConstants.DEFAULT_CHARSET)));

		response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
		response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

		ctx.writeAndFlush(response);
	}
}
 
Example #10
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #11
Source File: AbstractHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = TestingRestfulGateway.newBuilder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #12
Source File: NetworkBuffer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf readBytes(int length) {
	// copied from the one in netty 4.0.50 fixing the wrong allocator being used
	checkReadableBytes(length);
	if (length == 0) {
		return Unpooled.EMPTY_BUFFER;
	}

	ByteBuf buf = alloc().buffer(length, maxCapacity());
	int readerIndex = readerIndex();
	buf.writeBytes(this, readerIndex, length);
	readerIndex(readerIndex + length);
	return buf;
}
 
Example #13
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example #14
Source File: AbstractHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = TestingRestfulGateway.newBuilder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #15
Source File: MesosArtifactServer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #16
Source File: ConstantTextHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, RoutedRequest routed) throws Exception {
	HttpResponse response = new DefaultFullHttpResponse(
		HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText));

	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");

	KeepAliveWrite.flush(ctx, routed.getRequest(), response);
}
 
Example #17
Source File: HandlerRedirectUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
	ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0)
		: Unpooled.wrappedBuffer(message.getBytes(ENCODING));
	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

	return response;
}
 
Example #18
Source File: HandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example #19
Source File: NetworkBuffer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf readBytes(int length) {
	// copied from the one in netty 4.0.50 fixing the wrong allocator being used
	checkReadableBytes(length);
	if (length == 0) {
		return Unpooled.EMPTY_BUFFER;
	}

	ByteBuf buf = alloc().buffer(length, maxCapacity());
	int readerIndex = readerIndex();
	buf.writeBytes(this, readerIndex, length);
	readerIndex(readerIndex + length);
	return buf;
}
 
Example #20
Source File: HandlerRedirectUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
	ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0)
		: Unpooled.wrappedBuffer(message.getBytes(ENCODING));
	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

	return response;
}
 
Example #21
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #22
Source File: PipelineErrorHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void sendError(ChannelHandlerContext ctx, String error) {
	if (ctx.channel().isActive()) {
		DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			Unpooled.wrappedBuffer(error.getBytes(ConfigConstants.DEFAULT_CHARSET)));

		response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
		response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

		ctx.writeAndFlush(response);
	}
}
 
Example #23
Source File: ConstantTextHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, RoutedRequest routed) throws Exception {
	HttpResponse response = new DefaultFullHttpResponse(
		HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText));

	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");

	KeepAliveWrite.flush(ctx, routed.getRequest(), response);
}
 
Example #24
Source File: NettyServerLowAndHighWatermarkTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new buffer of the given size.
 */
private static ByteBuf buffer(int size) {
	// Set the writer index to the size to ensure that all bytes are written to the wire
	// although the buffer is "empty".
	return Unpooled.buffer(size).writerIndex(size);
}
 
Example #25
Source File: NettyServerLowAndHighWatermarkTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new buffer of the given size.
 */
private static ByteBuf buffer(int size) {
	// Set the writer index to the size to ensure that all bytes are written to the wire
	// although the buffer is "empty".
	return Unpooled.buffer(size).writerIndex(size);
}
 
Example #26
Source File: RestClient.java    From flink with Apache License 2.0 4 votes vote down vote up
public <M extends MessageHeaders<R, P, U>, U extends MessageParameters, R extends RequestBody, P extends ResponseBody> CompletableFuture<P> sendRequest(
		String targetAddress,
		int targetPort,
		M messageHeaders,
		U messageParameters,
		R request,
		Collection<FileUpload> fileUploads,
		RestAPIVersion apiVersion) throws IOException {
	Preconditions.checkNotNull(targetAddress);
	Preconditions.checkArgument(NetUtils.isValidHostPort(targetPort), "The target port " + targetPort + " is not in the range [0, 65535].");
	Preconditions.checkNotNull(messageHeaders);
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(messageParameters);
	Preconditions.checkNotNull(fileUploads);
	Preconditions.checkState(messageParameters.isResolved(), "Message parameters were not resolved.");

	if (!messageHeaders.getSupportedAPIVersions().contains(apiVersion)) {
		throw new IllegalArgumentException(String.format(
			"The requested version %s is not supported by the request (method=%s URL=%s). Supported versions are: %s.",
			apiVersion,
			messageHeaders.getHttpMethod(),
			messageHeaders.getTargetRestEndpointURL(),
			messageHeaders.getSupportedAPIVersions().stream().map(RestAPIVersion::getURLVersionPrefix).collect(Collectors.joining(","))));
	}

	String versionedHandlerURL = "/" + apiVersion.getURLVersionPrefix() + messageHeaders.getTargetRestEndpointURL();
	String targetUrl = MessageParameters.resolveUrl(versionedHandlerURL, messageParameters);

	LOG.debug("Sending request of class {} to {}:{}{}", request.getClass(), targetAddress, targetPort, targetUrl);
	// serialize payload
	StringWriter sw = new StringWriter();
	objectMapper.writeValue(sw, request);
	ByteBuf payload = Unpooled.wrappedBuffer(sw.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));

	Request httpRequest = createRequest(targetAddress + ':' + targetPort, targetUrl, messageHeaders.getHttpMethod().getNettyHttpMethod(), payload, fileUploads);

	final JavaType responseType;

	final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();

	if (typeParameters.isEmpty()) {
		responseType = objectMapper.constructType(messageHeaders.getResponseClass());
	} else {
		responseType = objectMapper.getTypeFactory().constructParametricType(
			messageHeaders.getResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	return submitRequest(targetAddress, targetPort, httpRequest, responseType);
}
 
Example #27
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public <M extends MessageHeaders<R, P, U>, U extends MessageParameters, R extends RequestBody, P extends ResponseBody> CompletableFuture<P> sendRequest(
		String targetAddress,
		int targetPort,
		M messageHeaders,
		U messageParameters,
		R request,
		Collection<FileUpload> fileUploads,
		RestAPIVersion apiVersion) throws IOException {
	Preconditions.checkNotNull(targetAddress);
	Preconditions.checkArgument(0 <= targetPort && targetPort < 65536, "The target port " + targetPort + " is not in the range (0, 65536].");
	Preconditions.checkNotNull(messageHeaders);
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(messageParameters);
	Preconditions.checkNotNull(fileUploads);
	Preconditions.checkState(messageParameters.isResolved(), "Message parameters were not resolved.");

	if (!messageHeaders.getSupportedAPIVersions().contains(apiVersion)) {
		throw new IllegalArgumentException(String.format(
			"The requested version %s is not supported by the request (method=%s URL=%s). Supported versions are: %s.",
			apiVersion,
			messageHeaders.getHttpMethod(),
			messageHeaders.getTargetRestEndpointURL(),
			messageHeaders.getSupportedAPIVersions().stream().map(RestAPIVersion::getURLVersionPrefix).collect(Collectors.joining(","))));
	}

	String versionedHandlerURL = "/" + apiVersion.getURLVersionPrefix() + messageHeaders.getTargetRestEndpointURL();
	String targetUrl = MessageParameters.resolveUrl(versionedHandlerURL, messageParameters);

	LOG.debug("Sending request of class {} to {}:{}{}", request.getClass(), targetAddress, targetPort, targetUrl);
	// serialize payload
	StringWriter sw = new StringWriter();
	objectMapper.writeValue(sw, request);
	ByteBuf payload = Unpooled.wrappedBuffer(sw.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));

	Request httpRequest = createRequest(targetAddress + ':' + targetPort, targetUrl, messageHeaders.getHttpMethod().getNettyHttpMethod(), payload, fileUploads);

	final JavaType responseType;

	final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();

	if (typeParameters.isEmpty()) {
		responseType = objectMapper.constructType(messageHeaders.getResponseClass());
	} else {
		responseType = objectMapper.getTypeFactory().constructParametricType(
			messageHeaders.getResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	return submitRequest(targetAddress, targetPort, httpRequest, responseType);
}
 
Example #28
Source File: RestClient.java    From flink with Apache License 2.0 4 votes vote down vote up
public <M extends MessageHeaders<R, P, U>, U extends MessageParameters, R extends RequestBody, P extends ResponseBody> CompletableFuture<P> sendRequest(
		String targetAddress,
		int targetPort,
		M messageHeaders,
		U messageParameters,
		R request,
		Collection<FileUpload> fileUploads,
		RestAPIVersion apiVersion) throws IOException {
	Preconditions.checkNotNull(targetAddress);
	Preconditions.checkArgument(0 <= targetPort && targetPort < 65536, "The target port " + targetPort + " is not in the range (0, 65536].");
	Preconditions.checkNotNull(messageHeaders);
	Preconditions.checkNotNull(request);
	Preconditions.checkNotNull(messageParameters);
	Preconditions.checkNotNull(fileUploads);
	Preconditions.checkState(messageParameters.isResolved(), "Message parameters were not resolved.");

	if (!messageHeaders.getSupportedAPIVersions().contains(apiVersion)) {
		throw new IllegalArgumentException(String.format(
			"The requested version %s is not supported by the request (method=%s URL=%s). Supported versions are: %s.",
			apiVersion,
			messageHeaders.getHttpMethod(),
			messageHeaders.getTargetRestEndpointURL(),
			messageHeaders.getSupportedAPIVersions().stream().map(RestAPIVersion::getURLVersionPrefix).collect(Collectors.joining(","))));
	}

	String versionedHandlerURL = "/" + apiVersion.getURLVersionPrefix() + messageHeaders.getTargetRestEndpointURL();
	String targetUrl = MessageParameters.resolveUrl(versionedHandlerURL, messageParameters);

	LOG.debug("Sending request of class {} to {}:{}{}", request.getClass(), targetAddress, targetPort, targetUrl);
	// serialize payload
	StringWriter sw = new StringWriter();
	objectMapper.writeValue(sw, request);
	ByteBuf payload = Unpooled.wrappedBuffer(sw.toString().getBytes(ConfigConstants.DEFAULT_CHARSET));

	Request httpRequest = createRequest(targetAddress + ':' + targetPort, targetUrl, messageHeaders.getHttpMethod().getNettyHttpMethod(), payload, fileUploads);

	final JavaType responseType;

	final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();

	if (typeParameters.isEmpty()) {
		responseType = objectMapper.constructType(messageHeaders.getResponseClass());
	} else {
		responseType = objectMapper.getTypeFactory().constructParametricType(
			messageHeaders.getResponseClass(),
			typeParameters.toArray(new Class<?>[typeParameters.size()]));
	}

	return submitRequest(targetAddress, targetPort, httpRequest, responseType);
}
 
Example #29
Source File: PartitionRequestQueueTest.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Blocks the given channel by adding a buffer that is bigger than the high watermark.
 *
 * <p>The channel may be unblocked with:
 * <pre>
 * channel.flush();
 * assertSame(channelBlockingBuffer, channel.readOutbound());
 * </pre>
 *
 * @param channel the channel to block
 * @return the created blocking buffer
 */
static ByteBuf blockChannel(EmbeddedChannel channel) {
	final int highWaterMark = channel.config().getWriteBufferHighWaterMark();
	// Set the writer index to the high water mark to ensure that all bytes are written
	// to the wire although the buffer is "empty".
	ByteBuf channelBlockingBuffer = Unpooled.buffer(highWaterMark).writerIndex(highWaterMark);
	channel.write(channelBlockingBuffer);
	assertFalse(channel.isWritable());

	return channelBlockingBuffer;
}
 
Example #30
Source File: PartitionRequestQueueTest.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Blocks the given channel by adding a buffer that is bigger than the high watermark.
 *
 * <p>The channel may be unblocked with:
 * <pre>
 * channel.flush();
 * assertSame(channelBlockingBuffer, channel.readOutbound());
 * </pre>
 *
 * @param channel the channel to block
 * @return the created blocking buffer
 */
static ByteBuf blockChannel(EmbeddedChannel channel) {
	final int highWaterMark = channel.config().getWriteBufferHighWaterMark();
	// Set the writer index to the high water mark to ensure that all bytes are written
	// to the wire although the buffer is "empty".
	ByteBuf channelBlockingBuffer = Unpooled.buffer(highWaterMark).writerIndex(highWaterMark);
	channel.write(channelBlockingBuffer);
	assertFalse(channel.isWritable());

	return channelBlockingBuffer;
}