/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.common.http;

import com.ntels.onecable.common.exception.ParseResponseException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

/**
 * <pre>
 * 1. ClassName :   ResponseHelper
 * 2. FileName  :   ResponseHelper.java
 * 3. Package   :   com.ntels.onecable.common.http
 * 4. Date      :   2016-12-06
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class ResponseHelper {
  public static final int HTTP_SERVICE_ERROR = -1;
  public static final String CHARSET = "UTF-8";
  public static final int BUFFER_SIZE = 10240;

  /**
   * InputStream reader.
   *
   * @param content InputStream content
   * @return content string
   */
  static String read(InputStream content) {
    Reader reader = null;
    StringWriter writer = null;
    try {
      reader = new InputStreamReader(content, CHARSET);
      writer = new StringWriter();

      char[] buffer = new char[BUFFER_SIZE];
      for (int length = 0; (length = reader.read(buffer)) > 0; ) {
        writer.write(buffer, 0, length);
      }
    } catch (UnsupportedEncodingException e) {
      throw new ParseResponseException("Unable to parse response stream to string.", e);
    } catch (IOException e) {
      throw new ParseResponseException("Unable to read from response stream reader during parse.", e);
    } finally {
      try {
        writer.close();
      } catch (IOException e) {
        throw new ParseResponseException("Unable to close writer after parsing response stream to string.", e);
      }

      try {
        reader.close();
      } catch (IOException e) {
        throw new ParseResponseException("Unable to close reader after parsing response stream to string.", e);
      }
    }

    return writer.toString();
  }

  /**
   * Make exception message.
   *
   * @param msg Exception message
   * @param e {@link Exception}
   * @return Exception {@link Response}
   */
  static Response writeException(String msg, Exception e) {
    Throwable cause = e.getCause() != null ? e.getCause() : e;
    return new Response(HTTP_SERVICE_ERROR, msg, String.format("%s, Cause: %s[%s]", e.getClass().getName(), cause.getClass().getName(), cause.getLocalizedMessage()));
  }

}
