/** * Converts an exception raised while processing an HTTP request into a suitable HTTP response. * * <p>The response is marshalled and queued for writing on the socket associated with the original * request. * * @param msg The HTTP request being processed when the exception occurred. * @param e The exception that was raised. * @throws IOException if an error occurs marshalling or writing the response. */ protected void handleMessageException(HttpMessageBuffer msg, Exception e) throws IOException { HttpResponse httpRsp; if (e instanceof HttpResponseException) { if (log.logWarn()) { log.warn("HttpResponseException", e); } httpRsp = this.newHttpResponse(msg, (HttpResponseException) e); this.queueWrite(msg.getSocket(), httpRsp.marshal(), true); } else if (e instanceof RemoteSocketClosedException) { if (log.logTrace()) { log.trace("Remote entity closed connection", e); } } else { if (log.logError()) { log.error("Internal Server Error", e); } httpRsp = this.newHttpResponse( msg, new HttpResponseException( HttpConstants.StatusCodes._500_INTERNAL_SERVER_ERROR, "Internal Server Error", e)); this.queueWrite(msg.getSocket(), httpRsp.marshal(), true); } }
protected HttpResponse newHttpResponse(HttpMessageBuffer msg, HttpResponseException e) { HttpResponse httpRsp = e.toHttpResponse(msg.getHttpVersionString()); if (msg.getHttpVersion() > 1.0) { httpRsp.addHeader(HttpConstants.Headers.HOST, this.headerHostValue); } httpRsp.addHeader(HttpConstants.Headers.SERVER, this.getServerName()); httpRsp.addHeader(HttpConstants.Headers.CONNECTION, "close"); return httpRsp; }
/** * Constructs a new {@link HttpResponse} containing the given XML-RPC method response. * * <p>This implementation encodes the response using <code>UTF-8</code>, sets the status code to * <code>200</code>, and sets <code>Content-Type</code> header to <code>text/xml</code> as * required. No other headers are set. * * @param methodRsp The XML-RPC method response to be returned in the HTTP response. * @param encoding An {@link Encoding} describing the encoding to use when constructing this * message. This also informs the <code>Content-Encoding</code> header value. May be <code> * null</code>. * @return A new {@link HttpResponse} with the marshalled XML-RPC response as its content. * @throws IOException if an error occurs while marshalling the XML-RPC response. * @throws MarshallingException */ protected HttpResponse toHttpResponse( HttpMessageBuffer origMsg, RpcResponse methodRsp, Encoding encoding) throws IOException, MarshallingException { ByteArrayOutputStream byteOs = new ByteArrayOutputStream(); methodRsp.marshal(byteOs, Charset.forName("UTF-8")); byte[] rspXml = byteOs.toByteArray(); HttpResponse httpRsp = this.newHttpResponse(origMsg, 200, "OK", encoding); httpRsp.addHeader(HttpConstants.Headers.CONTENT_TYPE, methodRsp.getContentType()); httpRsp.setContent(rspXml); return httpRsp; }
protected HttpResponse newHttpResponse( HttpMessageBuffer origMsg, int statusCode, String reasonPhrase, Encoding encoding) { Encoding responseEncoding = null; if (this.encodeResponses) { responseEncoding = encoding; } HttpResponse httpRsp; if (origMsg == null) { httpRsp = new HttpResponse(statusCode, reasonPhrase, responseEncoding); } else { httpRsp = new HttpResponse( origMsg.getHttpVersionString(), statusCode, reasonPhrase, responseEncoding); } if (origMsg.getHttpVersion() > 1.0) { httpRsp.addHeader(HttpConstants.Headers.HOST, this.headerHostValue); } httpRsp.addHeader(HttpConstants.Headers.SERVER, this.getServerName()); return httpRsp; }