enterprises.orbital.impl.evexmlapi.FileCopyConnector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eve-xml-api Show documentation
Show all versions of eve-xml-api Show documentation
Library to interact with EVE XML API servers
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();
}
}