From: Richard Millet Date: Fri, 28 Feb 2020 17:40:37 +0000 (-0800) Subject: DRYD-832: Replaced class that downloaded media files with a simplier one. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=741f0eae565ead0f80c34328d411de6e51cd16b3;p=tmp%2Fjakarta-migration.git DRYD-832: Replaced class that downloaded media files with a simplier one. --- diff --git a/services/common/src/main/java/org/collectionspace/services/common/Download.java b/services/common/src/main/java/org/collectionspace/services/common/Download.java index c3750ce5c..1ca669d2d 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/Download.java +++ b/services/common/src/main/java/org/collectionspace/services/common/Download.java @@ -4,6 +4,7 @@ import java.net.*; import java.util.*; // This class downloads a file from a URL. +@Deprecated // - see JIRA DRYD-832 public class Download extends Observable implements Runnable { public static URL verifyUrl(String url) { diff --git a/services/common/src/main/java/org/collectionspace/services/common/HttpDownloadUtility.java b/services/common/src/main/java/org/collectionspace/services/common/HttpDownloadUtility.java new file mode 100644 index 000000000..9e9c865ce --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/HttpDownloadUtility.java @@ -0,0 +1,136 @@ +package org.collectionspace.services.common; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HttpDownloadUtility { + private static final Logger logger = LoggerFactory.getLogger(HttpDownloadUtility.class); + + private static final int BUFFER_SIZE = 4096; + + private static String getDestDir(String destDir) { + if (destDir.endsWith(File.separator) != true) { + destDir = destDir + File.separator; + } + // + // Check if the destDir exists, if not try to create it. + // + File destDirFile = new File(destDir); + boolean destDirExists = destDirFile.exists(); + boolean triedToCreateDir = false; + if (destDirExists == false) { + triedToCreateDir = true; + destDirExists = destDirFile.mkdir(); + } else { + destDirExists = destDirFile.isDirectory(); + } + // + // If we couldn't create the directory or if it is already a file then fail. + // + if (destDirExists == false) { + if (triedToCreateDir) { + logger.error(String.format("Tried and failed to create a temp directory '%s' for storing a downloaded file.", + destDir)); + } + return null; + } + + return destDir; + } + + public static File downloadFile(String fileURL) throws IOException { + File result = null; + + try { + String tmpdir = System.getProperty("java.io.tmpdir"); + if (tmpdir.endsWith(File.separator) == false) { + tmpdir = tmpdir + File.separator; + } + + String destDir = getDestDir(tmpdir + UUID.randomUUID() + File.separator); + String filePath = downloadFile(fileURL, destDir); + result = new File(filePath); + } catch (Exception e) { + String msg = String.format("Could not download file use this URL: %s", fileURL); + logger.error(msg, e); + } + + return result; + } + + /** + * Downloads a file from a URL + * + * @param fileURL HTTP URL of the file to be downloaded + * @param saveDir path of the directory to save the file + * @throws IOException + */ + private static String downloadFile(String fileURL, String saveDir) throws IOException { + URL url = new URL(fileURL); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + int httpResponseCode = 0; + String result = null; + + try { + httpResponseCode = httpConn.getResponseCode(); + + // always check HTTP response code first + if (httpResponseCode == HttpURLConnection.HTTP_OK) { + String fileName = ""; + String disposition = httpConn.getHeaderField("Content-Disposition"); + + if (disposition != null) { + // extracts file name from header field + int index = disposition.indexOf("filename="); + if (index > 0) { + fileName = disposition.substring(index + 9, disposition.length()); + } + } else { + // extracts file name from URL + fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length()); + } + + if (logger.isDebugEnabled()) { + String contentType = httpConn.getContentType(); + int contentLength = httpConn.getContentLength(); + logger.debug("File name is:" + fileName); + logger.debug("Disposition is:" + disposition != null ? disposition : ""); + logger.debug("Content type is:" + contentType != null ? contentType : ""); + logger.debug("Content length is:" + contentLength); + } + + // opens input stream from the HTTP connection + InputStream inputStream = httpConn.getInputStream(); + String saveFilePath = saveDir + File.separator + fileName; //FIXME: File.separator NOT needed + + // opens an output stream to save into file + FileOutputStream outputStream = new FileOutputStream(saveFilePath); + + try { + int bytesRead = -1; + byte[] buffer = new byte[BUFFER_SIZE]; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } finally { + outputStream.close(); + inputStream.close(); + } + + result = saveFilePath; + } + } finally { + httpConn.disconnect(); + } + + return result; + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/blob/BlobInput.java b/services/common/src/main/java/org/collectionspace/services/common/blob/BlobInput.java index 6a7d3e8be..99c293e19 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/blob/BlobInput.java +++ b/services/common/src/main/java/org/collectionspace/services/common/blob/BlobInput.java @@ -2,6 +2,7 @@ package org.collectionspace.services.common.blob; import java.io.File; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Path; @@ -14,6 +15,7 @@ import javax.servlet.http.HttpServletRequest; import org.collectionspace.services.nuxeo.client.java.CommonList; import org.collectionspace.services.common.Download; +import org.collectionspace.services.common.HttpDownloadUtility; import org.collectionspace.services.common.ServiceMain; import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.imaging.nuxeo.NuxeoBlobUtils; @@ -195,25 +197,14 @@ public class BlobInput { return result; } - - public void createBlobFile(String theBlobUri) throws MalformedURLException, Exception { + + public void createBlobFile(String theBlobUri) throws MalformedURLException, Exception { URL blobUrl = new URL(theBlobUri); File theBlobFile = null; - if (isProtocolHttp(blobUrl) == true) { - Download fetchedFile = new Download(blobUrl); - if (logger.isDebugEnabled() == true) { - logger.debug("Starting blob download into temp file:" + fetchedFile.getFilePath()); - } - while (fetchedFile.getStatus() == Download.DOWNLOADING) { - // Do nothing while we wait for the file to download - } - if (logger.isDebugEnabled() == true) { - logger.debug("Finished blob download into temp file: " + fetchedFile.getFilePath()); - } - - int status = fetchedFile.getStatus(); - if (status == Download.COMPLETE) { - theBlobFile = fetchedFile.getFile(); + + if (isProtocolHttp(blobUrl) == true) { + theBlobFile = HttpDownloadUtility.downloadFile(theBlobUri); + if (theBlobFile != null) { setIsTemporaryFile(true); // setting to true ensures the file will get cleanup (removed) when we're done with it } else { String msg = URL_DOWNLOAD_FAILED + theBlobUri;