/*
 ************************************************************************************
 * Copyright (C) 2016 Openbravo S.L.U.
 * Licensed under the Openbravo Commercial License version 1.0
 * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
 * or in the legal folder of this module distribution.
 ************************************************************************************
 */
package com.openbravo.but.integrationplatform.webservices;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import org.apache.commons.io.IOUtils;
import org.openbravo.base.exception.OBException;
import org.openbravo.erpCommon.utility.OBMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class to use with a WebService which data is a String and the response is also another String
 * 
 */
public class StringWsClient extends WsClient {
  private static final Logger log = LoggerFactory.getLogger(StringWsClient.class);
  private String requestData;
  private String responseData;

  @Override
  protected void writeRequestData(OutputStream ouput) {
    try {
      OutputStreamWriter writer = new OutputStreamWriter(ouput);
      writer.write(requestData);
      writer.close();
    } catch (IOException e) {
      throw new OBException(OBMessageUtils.messageBD("BUTINTP_error_writing_content"), e);
    }

  }

  @Override
  protected void readRequestResponse(InputStream inputStream) {
    if (inputStream == null) {
      responseData = "";
      return;
    }
    StringWriter writer = new StringWriter();
    try {
      IOUtils.copy(inputStream, writer);
      responseData = writer.toString();
      writer.close();
    } catch (IOException e) {
      throw new OBException(OBMessageUtils.messageBD("BUTINTP_error_reading_response"), e);
    }

  }

  /**
   * Method that connects to a WebService and returns a string
   * 
   */
  public final String connectAndReturnString(String urlStr, String method, String user,
      String password, String requestDataStr) {

    return connectAndReturnString(urlStr, method, user, password, requestDataStr, true);
  }

  /**
   * Method that connects to a WebService and returns a string
   * 
   */
  public final String connectAndReturnString(String urlStr, String method, String user,
      String password, String requestDataStr, boolean showStacktrace) {
    long t = System.currentTimeMillis();
    try {
      log.debug("Sending request data: \n {}", requestDataStr);
      log.debug("Sending request url: {} and method: {}", urlStr, method);
      requestData = requestDataStr;
      // Connection to the Web Service
      connect(urlStr, method, user, password, showStacktrace);
    } finally {
      log.debug("Time elapsed receiving response: {} miliseconds", System.currentTimeMillis() - t);
      log.debug("Receiving response data: \n {}", responseData);
    }
    return responseData;
  }
}
