Search in sources :

Example 1 with ProcessException

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;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ProcessException(org.geotools.process.ProcessException) File(java.io.File) DescribeResult(org.geotools.process.factory.DescribeResult)

Example 2 with ProcessException

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;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) ZipInputStream(java.util.zip.ZipInputStream) ProcessException(org.geotools.process.ProcessException) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 3 with ProcessException

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;
}
Also used : ProcessException(org.geotools.process.ProcessException) HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test)

Example 4 with ProcessException

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;
}
Also used : MalformedChunkCodingException(org.apache.http.MalformedChunkCodingException) ProcessException(org.geotools.process.ProcessException) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 5 with ProcessException

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;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ProcessException(org.geotools.process.ProcessException) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test)

Aggregations

ProcessException (org.geotools.process.ProcessException)5 ZipInputStream (java.util.zip.ZipInputStream)3 File (java.io.File)2 IOException (java.io.IOException)2 HttpClient (org.apache.http.client.HttpClient)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 HttpResponse (org.apache.http.HttpResponse)1 MalformedChunkCodingException (org.apache.http.MalformedChunkCodingException)1 StatusLine (org.apache.http.StatusLine)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 DescribeResult (org.geotools.process.factory.DescribeResult)1