FileArchive.java [src/cokeyconverter] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cokeyconverter;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
*/
public class FileArchive implements ServiceArchive {
HashMap<String, ModelArchive> archives = new HashMap<>();
String directory;
public FileArchive(String directory) throws Exception {
this.directory = directory;
if (!Files.isDirectory(Paths.get(directory))) {
throw new Exception("Directory specified does not exist: (" + directory + ")");
}
FilenameFilter jsonFilter = (File dir, String name) -> {
String lowercaseName = name.toLowerCase();
return lowercaseName.endsWith(".json");
};
File folder = new File(directory);
File[] fileList = folder.listFiles(jsonFilter);
for (File file : fileList) {
if (file.isFile() && file.canRead()) {
ModelArchive model = new ModelArchive(readFile(file));
archives.put(model.getSUID(), model);
}
}
}
@Override
public ArrayList<ModelArchive> getArchiveByReqIP(String ip, String service) throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public ModelArchive getArchiveBySUID(String suid) throws Exception {
return archives.get(suid);
}
@Override
public long getCount() {
return archives.size();
}
@Override
public JSONObject getServiceRequest(String suid) throws Exception {
JSONObject requestData = null;
InputStream zipFile = new FileInputStream(directory + "/" + suid + ".zip");
try (ZipInputStream zin = new ZipInputStream(zipFile)) {
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().contains(".request")) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(zin));
StringBuilder fileContent = new StringBuilder();
String inputStr;
while ((inputStr = bReader.readLine()) != null) {
fileContent.append(inputStr);
}
requestData = new JSONObject(fileContent.toString());
break;
}
}
}
return requestData;
}
@Override
public JSONObject getServiceResponse(String suid) throws Exception {
JSONObject requestData = null;
InputStream zipFile = new FileInputStream(directory + "/" + suid + ".zip");
try (ZipInputStream zin = new ZipInputStream(zipFile)) {
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().contains(".response")) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(zin));
StringBuilder fileContent = new StringBuilder();
String inputStr;
while ((inputStr = bReader.readLine()) != null) {
fileContent.append(inputStr);
}
requestData = new JSONObject(fileContent.toString());
break;
}
}
}
return requestData;
}
@Override
public boolean hasArchive(String suid) throws Exception {
return archives.containsKey(suid);
}
@Override
public Set<String> keys(int skip, int limit, String sortby, boolean sortAsc) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void shutdown() throws Exception {
}
private JSONObject readFile(File file) throws FileNotFoundException, IOException, JSONException {
JSONObject ret_val;
try (BufferedReader br = new BufferedReader(new FileReader(file));) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String jsonData = sb.toString();
ret_val = new JSONObject(jsonData);
}
return ret_val;
}
}