diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/SourceHandler.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/SourceHandler.java index 335ccb17de..bcd7242f8a 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/SourceHandler.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/SourceHandler.java @@ -28,6 +28,7 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; +import org.apache.http.MethodNotSupportedException; import org.apache.http.message.BasicHttpResponse; import org.apache.http.nio.ContentDecoder; import org.apache.http.nio.ContentEncoder; @@ -895,42 +896,12 @@ public void exception(NHttpServerConnection conn, Exception ex) { isFault = true; SourceContext.updateState(conn, ProtocolState.CLOSED); sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); + } else if (ex instanceof MethodNotSupportedException) { + isFault = generateHTTPErrorResponse(conn, ex, HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_IMPLEMENTED, + "Not Implemented"); } else if (ex instanceof HttpException) { - log.error("HttpException occurred ", ex); - if (PassThroughCorrelationConfigDataHolder.isEnable()) { - logHttpRequestErrorInCorrelationLog(conn, "HTTP Exception"); - } - try { - if (conn.isResponseSubmitted()) { - sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); - return; - } - HttpContext httpContext = conn.getContext(); - - HttpResponse response = new BasicHttpResponse( - HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad request"); - response.setParams( - new DefaultedHttpParams(sourceConfiguration.getHttpParams(), - response.getParams())); - response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); - - // Pre-process HTTP request - httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); - httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null); - httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response); - - sourceConfiguration.getHttpProcessor().process(response, httpContext); - - conn.submitResponse(response); - SourceContext.updateState(conn, ProtocolState.CLOSED); - informWriterError(conn); - conn.close(); - } catch (Exception ex1) { - log.error(ex1.getMessage(), ex1); - SourceContext.updateState(conn, ProtocolState.CLOSED); - sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); - isFault = true; - } + isFault = generateHTTPErrorResponse(conn, ex, HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, + "Bad request"); } else { log.error("Unexpected error: " + ex.getMessage(), ex); SourceContext.updateState(conn, ProtocolState.CLOSED); @@ -943,6 +914,45 @@ public void exception(NHttpServerConnection conn, Exception ex) { } } + private boolean generateHTTPErrorResponse (NHttpServerConnection conn, Exception ex, HttpVersion version, + int statusCode, String reason) { + log.error("HttpException occurred ", ex); + boolean isFault = false; + if (PassThroughCorrelationConfigDataHolder.isEnable()) { + logHttpRequestErrorInCorrelationLog(conn, "HTTP Exception"); + } + try { + if (conn.isResponseSubmitted()) { + sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); + return false; + } + HttpContext httpContext = conn.getContext(); + + HttpResponse response = new BasicHttpResponse(version, statusCode, reason); + response.setParams( + new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams())); + response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); + + // Pre-process HTTP request + httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); + httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null); + httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response); + + sourceConfiguration.getHttpProcessor().process(response, httpContext); + + conn.submitResponse(response); + SourceContext.updateState(conn, ProtocolState.CLOSED); + informWriterError(conn); + conn.close(); + } catch (Exception ex1) { + log.error(ex1.getMessage(), ex1); + SourceContext.updateState(conn, ProtocolState.CLOSED); + sourceConfiguration.getSourceConnections().shutDownConnection(conn, true); + isFault = true; + } + return isFault; + } + private Map getLoggingInfo(NHttpServerConnection conn, ProtocolState state) { HashMap logDetails = new HashMap<>(); SourceContext sourceContext = SourceContext.get(conn);