/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.afbb.bibo.servletclient.connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

/**
 * The HttpResponse is a wrapper class for the unnecessary complicated
 * HttpUrlConnection object. It reads the HttpURLConnection and extracts the
 * response data and the response status.
 *
 * @author fi13.pendrulat
 */
public class HttpResponse {

	private String data;
	private final int status;

	/**
	 * Create a new HttpResponse object. The HttpResponse is a wrapper class for
	 * the unnecessary complicated HttpUrlConnection object. It reads the
	 * HttpURLConnection and extracts the response data and the response status.
	 *
	 * @param connection
	 * @throws IOException
	 */
	public HttpResponse(final HttpURLConnection connection) throws IOException {
		data = "";
		try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				data += inputLine + "\\n";
			}
			/*
			 * FIXME will throw an StringIndexOutOfBoundsException for empty
			 * string (and I don't see why this might be useful otherwise
			 */
			// data = data.substring(0, data.length() - 1);
		}
		status = connection.getResponseCode();
	}

	/**
	 * Get the response data.
	 *
	 * @return the response data.
	 */
	public String getData() {
		return data;
	}

	/**
	 * Get the http status code of the response
	 *
	 * @return the http response status code.
	 */
	public int getStatus() {
		return status;
	}
}

@@ -1073,16 +1073,26 @@
  = %22%22;%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09try (B
@@ -1188,16 +1188,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 String i
@@ -1210,16 +1210,17 @@
 ine;%0D%0A%0D%0A
+%09
 %09%09%09while
@@ -1265,16 +1265,17 @@
  %7B%0D%0A%09%09%09%09
+%09
 data += 
@@ -1292,17 +1292,16 @@
 + %22%5C
-%5C
 n%22;%0D%0A%09%09%09
 %7D%0D%0A%09
@@ -1300,211 +1300,184 @@
 %0A%09%09%09
+%09
 %7D%0D%0A
-%09%09%09/*%0D%0A%09%09%09 * FIXME will throw an StringIndexOutOfBoundsException for empty%0D%0A%09%09%09 * string (and I don't see why this might be useful otherwise%0D%0A%09%09%09 */%0D%0A%09%09%09// data = data.substring(0, data.length() - 1);
+%0D%0A%09%09%09%09if (!data.isEmpty()) %7B%0D%0A%09%09%09%09%09data = data.substring(0, data.length() - 1);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D catch (final IOException e) %7B%0D%0A%09%09%09// done this way to preserve status code
 %0D%0A%09%09

fix HttpResponse to preserve status code on failed data read