diff options
| -rw-r--r-- | eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java | 28 | 
1 files changed, 28 insertions, 0 deletions
| diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java index 1b6df3de..4b358adf 100644 --- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java @@ -41,6 +41,7 @@ import org.apache.hc.core5.http.ClassicHttpResponse;  import org.apache.hc.core5.http.ContentType;  import org.apache.hc.core5.http.HttpException;  import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.ParseException;  import org.apache.hc.core5.http.io.HttpClientResponseHandler;  import org.apache.hc.core5.http.io.entity.EntityUtils;  import org.apache.hc.core5.http.message.StatusLine; @@ -102,6 +103,33 @@ public class HttpUtils {    }    /** +   * Http response-handler that gives a pair of http status-code, a copy of the +   * full http-body as {@link InputStream} and the response {@link ContentType}. +   *  +   * @return {@link Triple} of http response {@link StatusLine}, http body as +   *         {@link InputStream}, and {@link ContentType} +   */ +  public static HttpClientResponseHandler<Triple<StatusLine, String, ContentType>> stringBodyStatusCodeResponseHandler() { +    return new HttpClientResponseHandler<Triple<StatusLine, String, ContentType>>() { +      @Override +      public Triple<StatusLine, String, ContentType> handleResponse( +          ClassicHttpResponse response) +          throws ClientProtocolException, IOException { +        try { +          String body = EntityUtils.toString(response.getEntity()); +          return Triple.newInstance(new StatusLine(response), body, +              ContentType.parse(response.getEntity().getContentType())); + +        } catch (ParseException | IOException e) { +          log.warn("Can not process HTTP response. Return empty body content!", e); +          return Triple.newInstance(new StatusLine(response), StringUtils.EMPTY, +              ContentType.parse(response.getEntity().getContentType())); +        } +      } +    }; +  } + +  /**     * Helper method to retrieve server URL including context path.     *     * @param request HttpServletRequest | 
