use of org.geotools.process.ProcessException in project coastal-hazards by USGS-CIDA.
the class FetchAndUnzipProcess method execute.
@DescribeResult(name = "filePath", description = "path to the unzipped file")
public String execute(@DescribeParameter(name = "zipUrl", min = 1, max = 1, description = "URL to the zipped file to retrieve") String zipUrl, @DescribeParameter(name = "token", min = 1, max = 1, description = "Token for authorizing the upload") String token) {
String unzippedPath = null;
File unzippedFile = null;
if (zipUrl.isEmpty() || token.isEmpty()) {
LOGGER.info("Missing zipUrl or token in the FetchAndUnzipProcess.");
} else {
LOGGER.info("In FetchAndUnzipProcess on Geoserver with zip url and token:" + zipUrl);
}
if (isAuthorized(token)) {
ZipInputStream zipStream = getZipFromUrl(zipUrl, getHttpClient());
unzippedFile = unzipToDir(zipStream, getNewZipDestination());
unzippedPath = unzippedFile.getAbsolutePath();
} else {
throw new ProcessException(new SecurityException("Not Authorized."));
}
return unzippedPath;
}
use of org.geotools.process.ProcessException in project coastal-hazards by USGS-CIDA.
the class FetchAndUnzipProcess method getZipFromUrl.
ZipInputStream getZipFromUrl(String zipUrl, HttpClient client) {
HttpUriRequest req = new HttpGet(zipUrl);
HttpResponse response;
ZipInputStream zipStream = null;
try {
LOGGER.fine("retrieving zip from: " + zipUrl);
// this will call the portal viat the URI which will trigger the jersey TempFileResource search
response = client.execute(req);
StatusLine status = response.getStatusLine();
int statusCode = status.getStatusCode();
if (400 <= statusCode) {
throw new ProcessException("Could not retrieve file '" + zipUrl + "'. Got HTTP " + statusCode);
} else {
zipStream = new ZipInputStream(response.getEntity().getContent());
}
} catch (IOException ex) {
throw new ProcessException(ex);
}
return zipStream;
}
use of org.geotools.process.ProcessException in project coastal-hazards by USGS-CIDA.
the class FetchAndUnzipProcessTest method testGetZipFromBadUrl.
/**
* Test of getZipFromUrl method, of class FetchAndUnzipProcess.
*/
@Test
public void testGetZipFromBadUrl() {
int[] badCodes = new int[] { 400, 401, 404, 500, 501 };
for (int badCode : badCodes) {
HttpClient client = mockHttpClient(badCode, null);
try {
instance.getZipFromUrl("http://owi.usgs.gov", client);
fail("Should throw exception on HTTP error");
} catch (ProcessException ex) {
// do nothing, this is expected
}
}
// Exceptions were thrown on all HTTP errors
return;
}
use of org.geotools.process.ProcessException in project coastal-hazards by USGS-CIDA.
the class FetchAndUnzipProcess method unzipToDir.
File unzipToDir(ZipInputStream zipStream, File zipDir) {
// return only one path back
File unzippedFile = null;
try {
ZipEntry entry;
if (null != (entry = zipStream.getNextEntry())) {
if (!entry.isDirectory()) {
String entryFileName = entry.getName();
String safeFileName = makeSafeFileName();
unzippedFile = new File(zipDir, safeFileName);
String entryFileAbsolutePath = unzippedFile.getAbsolutePath();
LOGGER.fine("unzipping '" + entryFileName + "' to " + entryFileAbsolutePath);
new File(unzippedFile.getParent()).mkdirs();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(unzippedFile);
long start = System.nanoTime();
LOGGER.info("Starting to unzip the zipped stream to local disk");
IOUtils.copyLarge(zipStream, fos);
long end = System.nanoTime();
// duration in ms
long duration = (end - start) / 1000000;
LOGGER.info("Finished unzipping the zipped stream to local disk. Duration: " + duration + " ms");
} catch (FileNotFoundException ex) {
throw new ProcessException("Error finding file '" + entryFileAbsolutePath + "'.", ex);
} catch (MalformedChunkCodingException ex) {
throw new ProcessException("Error writing file '" + entryFileName + "' to '" + entryFileAbsolutePath + "'. This can happen if the zip file contains multiple files. Only one file is allowed.", ex);
} catch (IOException ex) {
throw new ProcessException("Error writing file '" + entryFileName + "' to '" + entryFileAbsolutePath + "'.", ex);
} finally {
IOUtils.closeQuietly(fos);
}
}
}
} catch (IOException ex) {
throw new ProcessException("error getting next entry in zip file", ex);
} finally {
IOUtils.closeQuietly(zipStream);
}
return unzippedFile;
}
use of org.geotools.process.ProcessException in project coastal-hazards by USGS-CIDA.
the class FetchAndUnzipProcessTest method testGetZipFromGoodUrls.
/**
* Test of getZipFromUrl method, of class FetchAndUnzipProcess.
*/
@Test
public void testGetZipFromGoodUrls() {
int[] goodCodes = new int[] { 200, 301, 302, 304, 307 };
byte[] empty = new byte[] {};
for (int goodCode : goodCodes) {
HttpClient client = mockHttpClient(goodCode, new ByteArrayInputStream(empty));
try {
ZipInputStream zipStream = instance.getZipFromUrl("http://owi.usgs.gov", client);
assertNotNull(zipStream);
} catch (ProcessException ex) {
fail("Should not throw exception on good HTTP status codes");
}
}
// No Exceptions were thrown on any HTTP status codes
return;
}
Aggregations