/****************************************************************************
 * Copyright (C) 2012-2013 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.transport.httpcore;

import java.io.PrintWriter;
import java.io.StringWriter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.slf4j.Logger;


/**
 * Utility functions for Apache HTTP core.
 *
 * @author Tobias Wich
 */
public class HttpUtils {

    /**
     * Dump the given HTTP request and log it with the given logger instance.
     *
     * @see #dumpHttpRequest(org.slf4j.Logger, java.lang.String, org.openecard.apache.http.HttpRequest)
     * @param logger Logger to dump HTTP request to.
     * @param req Request to dump.
     */
    public static void dumpHttpRequest(@Nonnull Logger logger, @Nonnull HttpRequest req) {
	dumpHttpRequest(logger, null, req);
    }

    /**
     * Dump the given HTTP request and log it with the given logger instance.
     * An optional message can be given wich will be printed in the head of the log entry to define the context of the
     * message. The request message is not modified by this method.
     *
     * @param logger Logger to dump HTTP request to.
     * @param msg Message qualifying the context of the request.
     * @param req Request to dump.
     */
    public static void dumpHttpRequest(@Nonnull Logger logger, @Nullable String msg, @Nonnull HttpRequest req) {
	if (logger.isDebugEnabled()) {
	    StringWriter w = new StringWriter();
	    PrintWriter pw = new PrintWriter(w);

	    pw.print("HTTP Request");
	    if (msg != null) {
		pw.format(" (%s)", msg);
	    }
	    pw.println(":");
	    RequestLine rl = req.getRequestLine();
	    pw.format("  %s %s %s%n", rl.getMethod(), rl.getUri(), rl.getProtocolVersion().toString());
	    for (Header h : req.getAllHeaders()) {
		pw.format("  %s: %s%n", h.getName(), h.getValue());
	    }
	    pw.flush();

	    logger.debug(w.toString());
	}
    }

    /**
     * Dump the given HTTP response and log it with the given logger instance.
     * The response message is not modifyed by the method. If the data contained in the message should be printed, it
     * must be extracted seperately and provided in the respective parameter..
     *
     * @param logger Logger to dump HTTP request to.
     * @param res Response to dump.
     * @param entityData Response data to dump if not null.
     */
    public static void dumpHttpResponse(@Nonnull Logger logger, @Nonnull HttpResponse res,
	    @Nullable byte[] entityData) {
	if (logger.isDebugEnabled()) {
	    StringWriter w = new StringWriter();
	    PrintWriter pw = new PrintWriter(w);

	    pw.println("HTTP Response:");
	    StatusLine sl = res.getStatusLine();
	    pw.format("  %s %d %s%n", sl.getProtocolVersion().toString(), sl.getStatusCode(), sl.getReasonPhrase());
	    for (Header h : res.getAllHeaders()) {
		pw.format("  %s: %s%n", h.getName(), h.getValue());
	    }
	    if (entityData != null) {
		pw.print(new String(entityData));
	    }
	    pw.println();
	    pw.flush();

	    logger.debug(w.toString());
	}
    }

}

@@ -3170,18 +3170,17 @@
 rameter.
-.
 %0A
+
      *%0A 
@@ -3445,14 +3445,14 @@
 ble 
-byte%5B%5D
+String
  ent
@@ -3917,19 +3917,8 @@
 int(
-new String(
 enti
@@ -3924,17 +3924,16 @@
 ityData)
-)
 ;%0A%09    %7D
@@ -3995,28 +3995,28 @@
 (w.toString());%0A
-
 %09%7D%0A    %7D%0A%0A%7D%0A
@@ -4013,10 +4013,675 @@
 %0A    %7D%0A%0A
+    /**%0A     * Dump the given HTTP response and log it with the given logger instance.%0A     * The response message is not modifyed by the method. If the data contained in the message should be printed, it%0A     * must be extracted seperately and provided in the respective parameter.%0A     *%0A     * @param logger Logger to dump HTTP request to.%0A     * @param res Response to dump.%0A     * @param entityData Response data to dump if not null.%0A     */%0A    public static void dumpHttpResponse(@Nonnull Logger logger, @Nonnull HttpResponse res,%0A%09    @Nullable byte%5B%5D entityData) %7B%0A%09dumpHttpResponse(logger, res, entityData != null ? new String(entityData) : null);%0A    %7D%0A%0A
 %7D%0A

Add printing of HTTP response string entities