All Downloads are FREE. Search and download functionalities are using the official Maven repository.

enterprises.orbital.impl.evexmlapi.LoggingConnector Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package enterprises.orbital.impl.evexmlapi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.digester.Digester;

import enterprises.orbital.impl.evexmlapi.utils.InputStreamSplitter;

/**
 * Connector which logs the text of each response.
 */
public class LoggingConnector extends ApiConnector {
  private static final Logger logger = Logger.getLogger(LoggingConnector.class.getName());

  private final ApiConnector  baseConnector;

  public LoggingConnector() {
    this.baseConnector = null;
  }

  public LoggingConnector(ApiConnector baseConnector) {
    this.baseConnector = baseConnector;
  }

  @Override
  public  E execute(ApiRequest request, Digester digester, Class clazz) throws IOException {
    if (logger.isLoggable(Level.INFO)) logger.info("\nRequest:\n" + request.toString());
    ApiConnector connector = getConnector();
    URI url = connector.getURI(request);
    Map params = connector.getParams(request);
    InputStream is = connector.getInputStream(url, params);
    return getApiResponse(digester, is, clazz);
  }

  @Override
  @SuppressWarnings("unchecked")
  protected  E getApiResponse(Digester digester, InputStream inputStream, Class clazz) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    if (logger.isLoggable(Level.INFO)) {
      inputStream = new InputStreamSplitter(inputStream, outputStream);
    }
    try {
      return (E) digester.parse(inputStream);
    } catch (IOException e) {
      throw e;
    } catch (Exception e) {
      throw new IOException(e);
    } finally {
      logger.info("\nResponse:\n" + outputStream.toString());
    }
  }

  @Override
  protected ApiConnector getConnector() {
    if (baseConnector != null) return baseConnector;
    return super.getConnector();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy