You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/12/13 15:04:46 UTC

[httpcomponents-core] 02/02: Re-use server support code in classic HttpService

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch development
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit c2be4f07458492b654eb6db5529ea866053b50a8
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Fri Dec 13 15:19:34 2019 +0100

    Re-use server support code in classic HttpService
---
 .../http2/impl/nio/ServerH2StreamHandler.java      |  7 +++++--
 .../core5/http/impl/{nio => }/ServerSupport.java   | 13 ++++--------
 .../apache/hc/core5/http/impl/io/HttpService.java  | 24 +++++-----------------
 .../http/impl/nio/ServerHttp1StreamHandler.java    |  6 +++++-
 4 files changed, 19 insertions(+), 31 deletions(-)

diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java
index 9a5db5f..fb61bf3 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java
@@ -42,8 +42,8 @@ import org.apache.hc.core5.http.Method;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
 import org.apache.hc.core5.http.impl.IncomingEntityDetails;
+import org.apache.hc.core5.http.impl.ServerSupport;
 import org.apache.hc.core5.http.impl.nio.MessageState;
-import org.apache.hc.core5.http.impl.nio.ServerSupport;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
 import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.AsyncResponseProducer;
@@ -51,6 +51,7 @@ import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
 import org.apache.hc.core5.http.nio.DataStreamChannel;
 import org.apache.hc.core5.http.nio.HandlerFactory;
 import org.apache.hc.core5.http.nio.ResponseChannel;
+import org.apache.hc.core5.http.nio.support.BasicResponseProducer;
 import org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
@@ -241,7 +242,9 @@ public class ServerH2StreamHandler implements H2StreamHandler {
                     exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
                 } catch (final HttpException ex) {
                     if (!responseCommitted.get()) {
-                        final AsyncResponseProducer responseProducer = ServerSupport.handleException(ex);
+                        final AsyncResponseProducer responseProducer = new BasicResponseProducer(
+                                ServerSupport.toStatusCode(ex),
+                                ServerSupport.toErrorMessage(ex));
                         exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
                         exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
                     } else {
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerSupport.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java
similarity index 86%
rename from httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerSupport.java
rename to httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java
index db20285..29cca6b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerSupport.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/ServerSupport.java
@@ -24,7 +24,7 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.hc.core5.http.impl.nio;
+package org.apache.hc.core5.http.impl;
 
 import org.apache.hc.core5.annotation.Internal;
 import org.apache.hc.core5.http.EntityDetails;
@@ -35,8 +35,6 @@ import org.apache.hc.core5.http.MethodNotSupportedException;
 import org.apache.hc.core5.http.NotImplementedException;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.UnsupportedHttpVersionException;
-import org.apache.hc.core5.http.nio.AsyncResponseProducer;
-import org.apache.hc.core5.http.nio.support.BasicResponseProducer;
 
 /**
  * HTTP Server support methods.
@@ -59,12 +57,9 @@ public class ServerSupport {
         }
     }
 
-    public static AsyncResponseProducer handleException(final Exception ex) {
-        String message = ex.getMessage();
-        if (message == null) {
-            message = ex.toString();
-        }
-        return new BasicResponseProducer(toStatusCode(ex), message);
+    public static String toErrorMessage(final Exception ex) {
+        final String message = ex.getMessage();
+        return message != null ? message : ex.toString();
     }
 
     public static int toStatusCode(final Exception ex) {
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
index 91a3955..31dd3a1 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
@@ -42,13 +42,10 @@ import org.apache.hc.core5.http.HttpHeaders;
 import org.apache.hc.core5.http.HttpRequestMapper;
 import org.apache.hc.core5.http.HttpResponseFactory;
 import org.apache.hc.core5.http.HttpStatus;
-import org.apache.hc.core5.http.MethodNotSupportedException;
-import org.apache.hc.core5.http.NotImplementedException;
-import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.UnsupportedHttpVersionException;
 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.hc.core5.http.impl.Http1StreamListener;
+import org.apache.hc.core5.http.impl.ServerSupport;
 import org.apache.hc.core5.http.io.HttpRequestHandler;
 import org.apache.hc.core5.http.io.HttpServerConnection;
 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
@@ -210,6 +207,7 @@ public class HttpService {
                 @Override
                 public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
                     try {
+                        ServerSupport.validateResponse(response, response.getEntity());
                         context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
                         processor.process(response, response.getEntity(), context);
 
@@ -267,7 +265,7 @@ public class HttpService {
      * @param response the HTTP response.
      */
     protected void handleException(final HttpException ex, final ClassicHttpResponse response) {
-        response.setCode(toStatusCode(ex, response));
+        response.setCode(toStatusCode(ex));
         String message = ex.getMessage();
         if (message == null) {
             message = ex.toString();
@@ -275,20 +273,8 @@ public class HttpService {
         response.setEntity(new StringEntity(message, ContentType.TEXT_PLAIN));
     }
 
-    protected int toStatusCode(final Exception ex, final ClassicHttpResponse response) {
-        final int code;
-        if (ex instanceof MethodNotSupportedException) {
-            code = HttpStatus.SC_NOT_IMPLEMENTED;
-        } else if (ex instanceof UnsupportedHttpVersionException) {
-            code = HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED;
-        } else if (ex instanceof NotImplementedException) {
-            code = HttpStatus.SC_NOT_IMPLEMENTED;
-        } else if (ex instanceof ProtocolException) {
-            code = HttpStatus.SC_BAD_REQUEST;
-        } else {
-            code = HttpStatus.SC_INTERNAL_SERVER_ERROR;
-        }
-        return code;
+    protected int toStatusCode(final Exception ex) {
+        return ServerSupport.toStatusCode(ex);
     }
 
 }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java
index 4083ca9..6c7cb1b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1StreamHandler.java
@@ -44,6 +44,7 @@ import org.apache.hc.core5.http.MisdirectedRequestException;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.ProtocolVersion;
 import org.apache.hc.core5.http.UnsupportedHttpVersionException;
+import org.apache.hc.core5.http.impl.ServerSupport;
 import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.AsyncResponseProducer;
 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
@@ -52,6 +53,7 @@ import org.apache.hc.core5.http.nio.DataStreamChannel;
 import org.apache.hc.core5.http.nio.HandlerFactory;
 import org.apache.hc.core5.http.nio.ResourceHolder;
 import org.apache.hc.core5.http.nio.ResponseChannel;
+import org.apache.hc.core5.http.nio.support.BasicResponseProducer;
 import org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
@@ -252,7 +254,9 @@ class ServerHttp1StreamHandler implements ResourceHolder {
             exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
         } catch (final HttpException ex) {
             if (!responseCommitted.get()) {
-                final AsyncResponseProducer responseProducer = ServerSupport.handleException(ex);
+                final AsyncResponseProducer responseProducer = new BasicResponseProducer(
+                        ServerSupport.toStatusCode(ex),
+                        ServerSupport.toErrorMessage(ex));
                 exchangeHandler = new ImmediateResponseExchangeHandler(responseProducer);
                 exchangeHandler.handleRequest(request, requestEntityDetails, responseChannel, context);
             } else {