List of usage examples for io.netty.handler.codec.http FullHttpResponse replace
@Override FullHttpResponse replace(ByteBuf content);
From source file:org.restlet.engine.netty.HttpServerHelper.java
License:Open Source License
@Override public void onNext(HttpRequest nettyRequest) { System.out.println("onNext: " + nettyRequest); HttpServerRequest request = null;//from w w w. j a v a2 s . c o m Response response = null; try { request = new HttpServerRequest(getContext(), getServerChannel(), nettyRequest); response = new Response(request); // Effectively handle the request getHelped().handle(request, response); if (!response.isCommitted() && response.isAutoCommitting()) { response.setCommitted(true); } if (response.isCommitted()) { FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); Series<Header> headers = new Series<>(Header.class); try { // Add all the necessary headers HeaderUtils.addGeneralHeaders(response, headers); HeaderUtils.addResponseHeaders(response, headers); // Set the status code in the response if (response.getStatus() != null) { nettyResponse.setStatus(new HttpResponseStatus(response.getStatus().getCode(), response.getStatus().getReasonPhrase())); } if ((response.getRequest().getMethod() != null) && response.getRequest().getMethod().equals(Method.HEAD)) { HeaderUtils.addEntityHeaders(response, headers); response.setEntity(null); } else if (Method.GET.equals(response.getRequest().getMethod()) && Status.SUCCESS_OK.equals(response.getStatus()) && (!response.isEntityAvailable())) { HeaderUtils.addEntityHeaders(response, headers); getLogger().warn( "A response with a 200 (Ok) status should have an entity. Make sure that resource \"" + response.getRequest().getResourceRef() + "\" returns one or sets the status to 204 (No content)."); } else if (response.getStatus().equals(Status.SUCCESS_NO_CONTENT)) { HeaderUtils.addEntityHeaders(response, headers); if (response.isEntityAvailable()) { getLogger().debug( "Responses with a 204 (No content) status generally don't have an entity. Only adding entity headers for resource \"" + response.getRequest().getResourceRef() + "\"."); response.setEntity(null); } } else if (response.getStatus().equals(Status.SUCCESS_RESET_CONTENT)) { if (response.isEntityAvailable()) { getLogger().warn( "Responses with a 205 (Reset content) status can't have an entity. Ignoring the entity for resource \"" + response.getRequest().getResourceRef() + "\"."); response.setEntity(null); } } else if (response.getStatus().equals(Status.REDIRECTION_NOT_MODIFIED)) { if (response.getEntity() != null) { HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(), headers); response.setEntity(null); } } else if (response.getStatus().isInformational()) { if (response.isEntityAvailable()) { getLogger().warn( "Responses with an informational (1xx) status can't have an entity. Ignoring the entity for resource \"" + response.getRequest().getResourceRef() + "\"."); response.setEntity(null); } } else { HeaderUtils.addEntityHeaders(response, headers); if (!response.isEntityAvailable()) { if ((response.getEntity() != null) && (response.getEntity().getSize() != 0)) { getLogger().warn( "A response with an unavailable and potentially non empty entity was returned. Ignoring the entity for resource \"" + response.getRequest().getResourceRef() + "\"."); } response.setEntity(null); } } // Add the response headers HeaderUtils.addResponseHeaders(response, headers); // Copy Restlet headers to Netty headers for (Header header : headers) { nettyResponse.headers().add(header.getName(), header.getValue()); } // Copy the content (NON OPTIMAL) if (response.getEntity() != null) { nettyResponse = nettyResponse .replace(Unpooled.wrappedBuffer(response.getEntity().getText().getBytes())); } // Send the response to the client subscriber.onNext(nettyResponse); } catch (Exception e) { Context.getCurrentLogger().warn("Exception intercepted while adding the response headers", e); response.setStatus(Status.SERVER_ERROR_INTERNAL); } finally { if (response.getOnSent() != null) { response.getOnSent().handle(response.getRequest(), response); } } } } catch (Throwable t) { getLogger().warn("Error while handling an HTTP server call", t); response.setStatus(Status.SERVER_ERROR_INTERNAL, t); onError(t); } finally { Engine.clearThreadLocalVariables(); } }