default <T> void writeBody(RoutingContext context, T object) { context .response() .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) .setChunked(true) .setStatusCode(HttpResponseStatus.OK.code()) .end(Json.encode(object), "UTF-8"); }
/** * Checks if a response is okay. All 2XX responses are supposed to be okay. * * @param status * @return whether it is a 2XX code or not (error!) */ private boolean httpResponseOkay(HttpResponseStatus status) { if (HttpResponseStatus.OK.equals(status) || HttpResponseStatus.NO_CONTENT.equals(status) || HttpResponseStatus.ACCEPTED.equals(status) || HttpResponseStatus.CREATED.equals(status)) { return true; } else { return false; } }
private void checkMemory(RoutingContext routingContext) { OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactoryHelper.getOperatingSystemMXBean(); long max = bean.getTotalPhysicalMemorySize(); long free = bean.getFreePhysicalMemorySize(); routingContext .response() .putHeader("Content-Type", "text/plain") .setStatusCode(HttpResponseStatus.OK.code()) .end(String.valueOf(max - free)); }
public static final class ResponseStatusCodes { public static final int CONTINUE_100 = HttpResponseStatus.CONTINUE.code(); public static final int SWITCHING_PROTOCOLS_101 = HttpResponseStatus.SWITCHING_PROTOCOLS.code(); public static final int OK_200 = HttpResponseStatus.OK.code(); public static final int MOVED_PERMANENTLY_301 = HttpResponseStatus.MOVED_PERMANENTLY.code(); public static final int FOUND_302 = HttpResponseStatus.FOUND.code(); public static final int SEE_OTHER_303 = HttpResponseStatus.SEE_OTHER.code(); public static final int NOT_MODIFIED_304 = HttpResponseStatus.NOT_MODIFIED.code(); public static final int TEMPORARY_REDIRECT_307 = HttpResponseStatus.TEMPORARY_REDIRECT.code(); public static final int UNAUTHORIZED_401 = HttpResponseStatus.UNAUTHORIZED.code(); public static final int PROXY_AUTHENTICATION_REQUIRED_407 = HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED.code(); private ResponseStatusCodes() {} }
@Test public void iframeHtml() throws Exception { final SockJsConfig config = config(); final String path = config.prefix() + "/iframe.html"; final FullHttpResponse response = Iframe.response(config, createHttpRequest(path)); assertThat(response.getStatus().code(), is(HttpResponseStatus.OK.code())); assertThat( response.headers().get(HttpHeaders.Names.CONTENT_TYPE), equalTo("text/html; charset=UTF-8")); assertThat( response.headers().get(HttpHeaders.Names.CACHE_CONTROL), equalTo("max-age=31536000, public")); assertThat(response.headers().get(HttpHeaders.Names.EXPIRES), is(notNullValue())); assertThat(response.headers().get(HttpHeaders.Names.SET_COOKIE), is(nullValue())); assertThat(response.headers().get(HttpHeaders.Names.ETAG), is(notNullValue())); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { messageReceiveCount++; if (msg instanceof HttpResponse) { try { HttpResponse response = (HttpResponse) msg; StringBuilder sb = new StringBuilder(); if (LOG.isDebugEnabled()) { sb.append("STATUS: ") .append(response.getStatus()) .append(", VERSION: ") .append(response.getProtocolVersion()) .append(", HEADER: "); } if (!response.headers().names().isEmpty()) { for (String name : response.headers().names()) { for (String value : response.headers().getAll(name)) { if (LOG.isDebugEnabled()) { sb.append(name).append(" = ").append(value); } if (this.length == -1 && name.equals("Content-Length")) { this.length = Long.parseLong(value); } } } } if (LOG.isDebugEnabled()) { LOG.debug(sb.toString()); } if (response.getStatus().code() == HttpResponseStatus.NO_CONTENT.code()) { LOG.warn("There are no data corresponding to the request"); length = 0; return; } else if (response.getStatus().code() != HttpResponseStatus.OK.code()) { LOG.error(response.getStatus().reasonPhrase()); state = TajoProtos.FetcherState.FETCH_FAILED; return; } } catch (Exception e) { LOG.error(e.getMessage(), e); } finally { ReferenceCountUtil.release(msg); } } if (msg instanceof HttpContent) { try { HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { content.readBytes(fc, content.readableBytes()); } if (msg instanceof LastHttpContent) { if (raf != null) { fileLen = file.length(); } finishTime = System.currentTimeMillis(); if (state != TajoProtos.FetcherState.FETCH_FAILED) { state = TajoProtos.FetcherState.FETCH_FINISHED; } IOUtils.cleanup(LOG, fc, raf); } } catch (Exception e) { LOG.error(e.getMessage(), e); } finally { ReferenceCountUtil.release(msg); } } }
public static boolean isRejection(HttpResponse response) { return !HttpResponseStatus.OK.equals(response.getStatus()); }