org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse Java Examples

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse. 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: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the "date" header for the HTTP response.
 *
 * @param response HTTP response
 */
public static void setDateHeader(FullHttpResponse response) {
	SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
	dateFormatter.setTimeZone(GMT_TIMEZONE);

	Calendar time = new GregorianCalendar();
	response.headers().set(DATE, dateFormatter.format(time.getTime()));
}
 
Example #2
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #3
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
	if (msg instanceof HttpResponse && ((HttpResponse) msg).status().equals(REQUEST_ENTITY_TOO_LARGE)) {
		jsonFuture.completeExceptionally(
			new RestClientException(
				String.format(
					REQUEST_ENTITY_TOO_LARGE + ". Try to raise [%s]",
					RestOptions.CLIENT_MAX_CONTENT_LENGTH.key()),
				((HttpResponse) msg).status()));
	} else if (msg instanceof FullHttpResponse) {
		readRawResponse((FullHttpResponse) msg);
	} else {
		LOG.error("Implementation error: Received a response that wasn't a FullHttpResponse.");
		if (msg instanceof HttpResponse) {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					((HttpResponse) msg).getStatus()));
		} else {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					HttpResponseStatus.INTERNAL_SERVER_ERROR));
		}

	}
	ctx.close();
}
 
Example #4
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 #5
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the "date" header for the HTTP response.
 *
 * @param response HTTP response
 */
public static void setDateHeader(FullHttpResponse response) {
	SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
	dateFormatter.setTimeZone(GMT_TIMEZONE);

	Calendar time = new GregorianCalendar();
	response.headers().set(DATE, dateFormatter.format(time.getTime()));
}
 
Example #6
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "304 Not Modified" response. This response can be used when the
 * file timestamp is the same as what the browser is sending up.
 *
 * @param ctx The channel context to write the response to.
 */
public static void sendNotModified(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
	setDateHeader(response);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #7
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 #8
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "405 Method Not Allowed" response.
 *
 * @param ctx The channel context to write the response to.
 */
private static void sendMethodNotAllowed(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #9
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #10
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
	if (msg instanceof HttpResponse && ((HttpResponse) msg).status().equals(REQUEST_ENTITY_TOO_LARGE)) {
		jsonFuture.completeExceptionally(
			new RestClientException(
				String.format(
					REQUEST_ENTITY_TOO_LARGE + ". Try to raise [%s]",
					RestOptions.CLIENT_MAX_CONTENT_LENGTH.key()),
				((HttpResponse) msg).status()));
	} else if (msg instanceof FullHttpResponse) {
		readRawResponse((FullHttpResponse) msg);
	} else {
		LOG.error("Implementation error: Received a response that wasn't a FullHttpResponse.");
		if (msg instanceof HttpResponse) {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					((HttpResponse) msg).getStatus()));
		} else {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					HttpResponseStatus.INTERNAL_SERVER_ERROR));
		}

	}
	ctx.close();
}
 
Example #11
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 #12
Source File: MesosArtifactServer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "405 Method Not Allowed" response.
 *
 * @param ctx The channel context to write the response to.
 */
private static void sendMethodNotAllowed(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #13
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "304 Not Modified" response. This response can be used when the
 * file timestamp is the same as what the browser is sending up.
 *
 * @param ctx The channel context to write the response to.
 */
public static void sendNotModified(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
	setDateHeader(response);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #14
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 #15
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "405 Method Not Allowed" response.
 *
 * @param ctx The channel context to write the response to.
 */
private static void sendMethodNotAllowed(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #16
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #17
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
	if (msg instanceof HttpResponse && ((HttpResponse) msg).status().equals(REQUEST_ENTITY_TOO_LARGE)) {
		jsonFuture.completeExceptionally(
			new RestClientException(
				String.format(
					REQUEST_ENTITY_TOO_LARGE + ". Try to raise [%s]",
					RestOptions.CLIENT_MAX_CONTENT_LENGTH.key()),
				((HttpResponse) msg).status()));
	} else if (msg instanceof FullHttpResponse) {
		readRawResponse((FullHttpResponse) msg);
	} else {
		LOG.error("Implementation error: Received a response that wasn't a FullHttpResponse.");
		if (msg instanceof HttpResponse) {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					((HttpResponse) msg).getStatus()));
		} else {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					HttpResponseStatus.INTERNAL_SERVER_ERROR));
		}

	}
	ctx.close();
}
 
Example #18
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 #19
Source File: StaticFileServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the "date" header for the HTTP response.
 *
 * @param response HTTP response
 */
public static void setDateHeader(FullHttpResponse response) {
	SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
	dateFormatter.setTimeZone(GMT_TIMEZONE);

	Calendar time = new GregorianCalendar();
	response.headers().set(DATE, dateFormatter.format(time.getTime()));
}
 
Example #20
Source File: StaticFileServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "304 Not Modified" response. This response can be used when the
 * file timestamp is the same as what the browser is sending up.
 *
 * @param ctx The channel context to write the response to.
 */
public static void sendNotModified(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
	setDateHeader(response);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #21
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 #22
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void sendNotFound(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #23
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void sendNotFound(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #24
Source File: MesosArtifactServer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static void sendNotFound(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}