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

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

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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Date;
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 saves a copy of each response to a file.
 */
public class FileCopyConnector extends ApiConnector {
  private final Logger       logger = Logger.getLogger(FileCopyConnector.class.getName());
  private final ApiConnector baseConnector;
  private final File         destinationDirectory;

  public FileCopyConnector(File destinationDirectory) {
    this(null, destinationDirectory);
  }

  public FileCopyConnector(ApiConnector baseConnector, File destinationDirectory) {
    this.baseConnector = baseConnector;
    this.destinationDirectory = destinationDirectory;
    if (!this.destinationDirectory.exists()) this.destinationDirectory.mkdirs();
  }

  @Override
  public  E execute(ApiRequest request, Digester digester, Class clazz) throws IOException {
    ApiConnector connector = getConnector();
    URI uri = connector.getURI(request);
    Map params = connector.getParams(request);
    InputStream inputStream = connector.getInputStream(uri, params);
    String outputFileName = request.getEndpoint().getFileName() + "-" + new Date().getTime() + ".xml";
    File outputFile = new File(destinationDirectory, outputFileName);
    FileOutputStream outputStream = null;
    try {
      outputStream = new FileOutputStream(outputFile);
      inputStream = new InputStreamSplitter(inputStream, outputStream);
    } catch (FileNotFoundException e) {
      logger.log(Level.SEVERE, "Could not write response xml to file: ", e);
    }
    E response = getApiResponse(digester, inputStream, clazz);
    if (outputStream != null) {
      try {
        outputStream.flush();
        outputStream.close();
      } catch (IOException e) {
        logger.log(Level.SEVERE, "Could not flush/close response xml file: ", e);
      }
    }
    return response;
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy