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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse. 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: HandlerRedirectUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getRedirectResponse(String redirectAddress, String path, HttpResponseStatus code) {
	checkNotNull(redirectAddress, "Redirect address");
	checkNotNull(path, "Path");

	String newLocation = String.format("%s%s", redirectAddress, path);

	HttpResponse redirectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, code);
	redirectResponse.headers().set(HttpHeaders.Names.LOCATION, newLocation);
	redirectResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);

	return redirectResponse;
}
 
Example #2
Source File: RedirectingSslHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
	String path = request == null ? "" : request.uri();
	String redirectAddress = getRedirectAddress(ctx);
	log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
	HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
		redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
	if (request != null) {
		KeepAliveWrite.flush(ctx, request, response);
	} else {
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
}
 
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: 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 #5
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example #6
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
	if (!HttpHeaders.isKeepAlive(request)) {
		return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	} else {
		response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ctx.writeAndFlush(response);
	}
}
 
Example #7
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 #8
Source File: HandlerRedirectUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getRedirectResponse(String redirectAddress, String path, HttpResponseStatus code) {
	checkNotNull(redirectAddress, "Redirect address");
	checkNotNull(path, "Path");

	String newLocation = String.format("%s%s", redirectAddress, path);

	HttpResponse redirectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, code);
	redirectResponse.headers().set(HttpHeaders.Names.LOCATION, newLocation);
	redirectResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);

	return redirectResponse;
}
 
Example #9
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the "date" and "cache" headers for the HTTP Response.
 *
 * @param response    The HTTP response object.
 * @param fileToCache File to extract the modification timestamp from.
 */
public static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
	SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
	dateFormatter.setTimeZone(GMT_TIMEZONE);

	// date header
	Calendar time = new GregorianCalendar();
	response.headers().set(DATE, dateFormatter.format(time.getTime()));

	// cache headers
	time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
	response.headers().set(EXPIRES, dateFormatter.format(time.getTime()));
	response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
	response.headers().set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
 
Example #10
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
	LOG.debug("Received {}", msg);

	if (msg instanceof HttpResponse) {
		HttpResponse response = (HttpResponse) msg;

		currentStatus = response.getStatus();
		currentType = response.headers().get(HttpHeaders.Names.CONTENT_TYPE);
		currentLocation = response.headers().get(HttpHeaders.Names.LOCATION);

		if (HttpHeaders.isTransferEncodingChunked(response)) {
			LOG.debug("Content is chunked");
		}
	}

	if (msg instanceof HttpContent) {
		HttpContent content = (HttpContent) msg;

		// Add the content
		currentContent += content.content().toString(CharsetUtil.UTF_8);

		// Finished with this
		if (content instanceof LastHttpContent) {
			responses.add(new SimpleHttpResponse(currentStatus, currentType,
					currentContent, currentLocation));

			currentStatus = null;
			currentType = null;
			currentLocation = null;
			currentContent = "";

			ctx.close();
		}
	}
}
 
Example #11
Source File: RedirectingSslHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
	String path = request == null ? "" : request.uri();
	String redirectAddress = getRedirectAddress(ctx);
	log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
	HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
		redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
	if (request != null) {
		KeepAliveWrite.flush(ctx, request, response);
	} else {
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #12
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 #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: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example #15
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
	if (!HttpHeaders.isKeepAlive(request)) {
		return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	} else {
		response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ctx.writeAndFlush(response);
	}
}
 
Example #16
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 #17
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 #18
Source File: HttpTestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
	LOG.debug("Received {}", msg);

	if (msg instanceof HttpResponse) {
		HttpResponse response = (HttpResponse) msg;

		currentStatus = response.getStatus();
		currentType = response.headers().get(HttpHeaders.Names.CONTENT_TYPE);
		currentLocation = response.headers().get(HttpHeaders.Names.LOCATION);

		if (HttpHeaders.isTransferEncodingChunked(response)) {
			LOG.debug("Content is chunked");
		}
	}

	if (msg instanceof HttpContent) {
		HttpContent content = (HttpContent) msg;

		// Add the content
		currentContent += content.content().toString(CharsetUtil.UTF_8);

		// Finished with this
		if (content instanceof LastHttpContent) {
			responses.add(new SimpleHttpResponse(currentStatus, currentType,
					currentContent, currentLocation));

			currentStatus = null;
			currentType = null;
			currentLocation = null;
			currentContent = "";

			ctx.close();
		}
	}
}
 
Example #19
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 #20
Source File: StaticFileServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the "date" and "cache" headers for the HTTP Response.
 *
 * @param response    The HTTP response object.
 * @param fileToCache File to extract the modification timestamp from.
 */
public static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
	SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
	dateFormatter.setTimeZone(GMT_TIMEZONE);

	// date header
	Calendar time = new GregorianCalendar();
	response.headers().set(DATE, dateFormatter.format(time.getTime()));

	// cache headers
	time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
	response.headers().set(EXPIRES, dateFormatter.format(time.getTime()));
	response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
	response.headers().set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
 
Example #21
Source File: HandlerRedirectUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static HttpResponse getRedirectResponse(String redirectAddress, String path, HttpResponseStatus code) {
	checkNotNull(redirectAddress, "Redirect address");
	checkNotNull(path, "Path");

	String newLocation = String.format("%s%s", redirectAddress, path);

	HttpResponse redirectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, code);
	redirectResponse.headers().set(HttpHeaders.Names.LOCATION, newLocation);
	redirectResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);

	return redirectResponse;
}
 
Example #22
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 #23
Source File: KeepAliveWrite.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
	if (!HttpHeaders.isKeepAlive(request)) {
		return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	} else {
		response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ctx.writeAndFlush(response);
	}
}
 
Example #24
Source File: KeepAliveWrite.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example #25
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 #26
Source File: RedirectingSslHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
	String path = request == null ? "" : request.uri();
	String redirectAddress = getRedirectAddress(ctx);
	log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
	HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
		redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
	if (request != null) {
		KeepAliveWrite.flush(ctx, request, response);
	} else {
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #27
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
	LOG.debug("Received {}", msg);

	if (msg instanceof HttpResponse) {
		HttpResponse response = (HttpResponse) msg;

		currentStatus = response.getStatus();
		currentType = response.headers().get(HttpHeaders.Names.CONTENT_TYPE);
		currentLocation = response.headers().get(HttpHeaders.Names.LOCATION);

		if (HttpHeaders.isTransferEncodingChunked(response)) {
			LOG.debug("Content is chunked");
		}
	}

	if (msg instanceof HttpContent) {
		HttpContent content = (HttpContent) msg;

		// Add the content
		currentContent += content.content().toString(CharsetUtil.UTF_8);

		// Finished with this
		if (content instanceof LastHttpContent) {
			responses.add(new SimpleHttpResponse(currentStatus, currentType,
					currentContent, currentLocation));

			currentStatus = null;
			currentType = null;
			currentLocation = null;
			currentContent = "";

			ctx.close();
		}
	}
}
 
Example #28
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 #29
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the "date" and "cache" headers for the HTTP Response.
 *
 * @param response    The HTTP response object.
 * @param fileToCache File to extract the modification timestamp from.
 */
public static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
	SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
	dateFormatter.setTimeZone(GMT_TIMEZONE);

	// date header
	Calendar time = new GregorianCalendar();
	response.headers().set(DATE, dateFormatter.format(time.getTime()));

	// cache headers
	time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
	response.headers().set(EXPIRES, dateFormatter.format(time.getTime()));
	response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
	response.headers().set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
 
Example #30
Source File: LeaderRetrievalHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest routedRequest, RestfulGateway gateway) throws Exception {
	Assert.assertTrue(channelHandlerContext.channel().eventLoop().inEventLoop());
	HttpResponse response = HandlerRedirectUtils.getResponse(HttpResponseStatus.OK, RESPONSE_MESSAGE);
	KeepAliveWrite.flush(channelHandlerContext.channel(), routedRequest.getRequest(), response);
}