From d9d6b017767da2ece2a18c8189b406fbfd6323fd Mon Sep 17 00:00:00 2001 From: Julia Damerow Date: Tue, 20 Mar 2018 11:11:29 -0700 Subject: [PATCH 1/2] Story/geco 95 (#15) * [GECO-95] let admins stop/start kafka listeners * [GECO-95] extract kafka listener logic; use general propmanager logic to stop/start kafka listeners has been extracted into its own plugin; using PropertiesManager from util plugin; --- cepheus/pom.xml | 14 ++++ .../cepheus/config/CepheusConfig.java | 22 +++++ .../cepheus/config/CepheusKafkaConfig.java | 5 +- .../kafka/ExtractionRequestReceiver.java | 5 -- ...PropertiesManager.java => Properties.java} | 16 +--- .../service/impl/PropertiesManager.java | 83 ------------------- .../service/pdf/impl/AExtractionManager.java | 5 +- .../pdf/impl/ImageExtractionManager.java | 15 ++-- .../cepheus/web/EditPropertiesController.java | 11 +-- cepheus/src/main/resources/config.properties | 9 +- .../webapp/WEB-INF/spring/root-context.xml | 2 +- .../src/main/webapp/WEB-INF/tiles-defs.xml | 3 + .../main/webapp/WEB-INF/tiles/skeleton.jsp | 3 + .../WEB-INF/views/admin/manage_listeners.jsp | 45 ++++++++++ .../kafka/ExtractionRequestReceiverTest.java | 5 +- 15 files changed, 119 insertions(+), 124 deletions(-) create mode 100644 cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusConfig.java rename cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/{IPropertiesManager.java => Properties.java} (55%) delete mode 100755 cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/impl/PropertiesManager.java create mode 100644 cepheus/src/main/webapp/WEB-INF/views/admin/manage_listeners.jsp diff --git a/cepheus/pom.xml b/cepheus/pom.xml index 5835b51..2b8cfa1 100644 --- a/cepheus/pom.xml +++ b/cepheus/pom.xml @@ -14,6 +14,8 @@ 0.6 0.4.2 0.2.2-SNAPSHOT + 0.1 + 0.2 admin @@ -53,6 +55,18 @@ util ${geco.util.version} + + + edu.asu.diging.giles-eco + kafka-util + ${geco.kafka-util.version} + + + + edu.asu.diging.giles-eco + september-util + ${geco.september.util.version} + diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusConfig.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusConfig.java new file mode 100644 index 0000000..2437c85 --- /dev/null +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusConfig.java @@ -0,0 +1,22 @@ +package edu.asu.diging.gilesecosystem.cepheus.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import edu.asu.diging.gilesecosystem.cepheus.service.Properties; +import edu.asu.diging.gilesecosystem.septemberutil.service.ISystemMessageHandler; +import edu.asu.diging.gilesecosystem.septemberutil.service.impl.SystemMessageHandler; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; + +@Configuration +public class CepheusConfig { + + @Autowired + private IPropertiesManager propertyManager; + + @Bean + public ISystemMessageHandler getMessageHandler() { + return new SystemMessageHandler(propertyManager.getProperty(Properties.APPLICATION_ID)); + } +} diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusKafkaConfig.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusKafkaConfig.java index 8e06a36..500fdc6 100644 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusKafkaConfig.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/config/CepheusKafkaConfig.java @@ -16,8 +16,9 @@ import org.springframework.kafka.core.DefaultKafkaConsumerFactory; import edu.asu.diging.gilesecosystem.cepheus.kafka.ExtractionRequestReceiver; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; +import edu.asu.diging.gilesecosystem.cepheus.service.Properties; import edu.asu.diging.gilesecosystem.requests.kafka.KafkaConfig; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; @Configuration @EnableKafka @@ -65,7 +66,7 @@ public ExtractionRequestReceiver receiver() { @Override public String getHosts() { - return propertiesManager.getProperty(IPropertiesManager.KAFKA_HOSTS); + return propertiesManager.getProperty(Properties.KAFKA_HOSTS); } @Override diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiver.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiver.java index 86c59df..d5da1c5 100644 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiver.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiver.java @@ -13,7 +13,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusExtractionException; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; import edu.asu.diging.gilesecosystem.cepheus.service.pdf.IImageExtractionManager; import edu.asu.diging.gilesecosystem.requests.IImageExtractionRequest; import edu.asu.diging.gilesecosystem.requests.impl.ImageExtractionRequest; @@ -26,10 +25,6 @@ public class ExtractionRequestReceiver { @Autowired private IImageExtractionManager imageExtractionManager; - @Autowired - protected IPropertiesManager propertiesManager; - - @KafkaListener(id="cepheus.extraction", topics = {"${topic_extract_images_request}"}) public void receiveMessage(String message, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) { extractImage(message); diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/IPropertiesManager.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/Properties.java similarity index 55% rename from cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/IPropertiesManager.java rename to cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/Properties.java index 92e14ff..ef49f14 100644 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/IPropertiesManager.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/Properties.java @@ -1,12 +1,8 @@ package edu.asu.diging.gilesecosystem.cepheus.service; -import java.util.Map; - -import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusPropertiesStorageException; - -public interface IPropertiesManager { +public interface Properties { - public final static String CEPHEUS_URL = "cepheus_url"; + public final static String CEPHEUS_URL = "app_url"; public final static String PDF_TO_IMAGE_DPI = "pdf_to_image_dpi"; public final static String PDF_TO_IMAGE_TYPE = "pdf_to_image_type"; public final static String PDF_TO_IMAGE_FORMAT = "pdf_to_image_format"; @@ -15,13 +11,7 @@ public interface IPropertiesManager { public final static String KAFKA_IMAGE_EXTRACTION_TOPIC = "topic_extract_images_request"; public final static String KAFKA_IMAGE_EXTRACTION_COMPLETE_TOPIC = "topic_extract_images_request_complete"; public final static String GILES_ACCESS_TOKEN = "giles_access_token"; - - - public abstract void setProperty(String key, String value) throws CepheusPropertiesStorageException; - - public abstract String getProperty(String key); + public final static String APPLICATION_ID = "application_id"; - public abstract void updateProperties(Map props) - throws CepheusPropertiesStorageException; } diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/impl/PropertiesManager.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/impl/PropertiesManager.java deleted file mode 100755 index a3bcc46..0000000 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/impl/PropertiesManager.java +++ /dev/null @@ -1,83 +0,0 @@ -package edu.asu.diging.gilesecosystem.cepheus.service.impl; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.Map; -import java.util.Properties; - -import javax.annotation.PostConstruct; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.core.io.PathResource; -import org.springframework.stereotype.Service; -import org.springframework.util.DefaultPropertiesPersister; -import org.springframework.util.PropertiesPersister; - -import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusPropertiesStorageException; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; - -@PropertySource("classpath:/config.properties") -@Service -public class PropertiesManager implements IPropertiesManager { - - private final Logger logger = LoggerFactory.getLogger(getClass()); - - @Autowired - private Environment env; - - private PropertiesPersister persister; - private Properties properties; - private PathResource customPropsResource; - - @PostConstruct - public void init() throws IOException, URISyntaxException { - persister = new DefaultPropertiesPersister(); - properties = new Properties(); - - URL resURL = getClass().getResource("/custom.properties"); - customPropsResource = new PathResource(resURL.toURI()); - - persister.load(properties, customPropsResource.getInputStream()); - } - - @Override - public String getProperty(String key) { - String value = properties.getProperty(key); - if (value != null) { - return value.trim(); - } - - value = env.getProperty(key); - if (value != null) { - value = value.trim(); - } - return value; - } - - @Override - public void setProperty(String key, String value) throws CepheusPropertiesStorageException { - properties.setProperty(key, value); - saveProperties(); - } - - @Override - public void updateProperties(Map props) throws CepheusPropertiesStorageException { - for (String key : props.keySet()) { - properties.setProperty(key, props.get(key)); - } - saveProperties(); - } - - protected void saveProperties() throws CepheusPropertiesStorageException { - try { - persister.store(properties, customPropsResource.getOutputStream(), "Giles custom properties."); - } catch (IOException e) { - throw new CepheusPropertiesStorageException("Could not store properties.", e); - } - } -} diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/AExtractionManager.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/AExtractionManager.java index fca6def..8b1a8ce 100644 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/AExtractionManager.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/AExtractionManager.java @@ -18,8 +18,9 @@ import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.web.client.RestTemplate; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; +import edu.asu.diging.gilesecosystem.cepheus.service.Properties; import edu.asu.diging.gilesecosystem.util.files.IFileStorageManager; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; public class AExtractionManager { @@ -89,7 +90,7 @@ protected Page saveTextToFile(int pageNr, String requestId, } protected String getRestEndpoint() { - String restEndpoint = propertiesManager.getProperty(IPropertiesManager.CEPHEUS_URL); + String restEndpoint = propertiesManager.getProperty(Properties.CEPHEUS_URL); if (restEndpoint.endsWith("/")) { restEndpoint = restEndpoint.substring(0, restEndpoint.length()-1); } diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java index 4c542ac..6105f47 100644 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java @@ -26,7 +26,7 @@ import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusExtractionException; import edu.asu.diging.gilesecosystem.cepheus.rest.DownloadFileController; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; +import edu.asu.diging.gilesecosystem.cepheus.service.Properties; import edu.asu.diging.gilesecosystem.cepheus.service.pdf.IImageExtractionManager; import edu.asu.diging.gilesecosystem.requests.ICompletedImageExtractionRequest; import edu.asu.diging.gilesecosystem.requests.IImageExtractionRequest; @@ -37,6 +37,7 @@ import edu.asu.diging.gilesecosystem.requests.impl.CompletedImageExtractionRequest; import edu.asu.diging.gilesecosystem.requests.kafka.IRequestProducer; import edu.asu.diging.gilesecosystem.util.files.IFileStorageManager; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; @Service public class ImageExtractionManager extends AExtractionManager implements @@ -83,12 +84,12 @@ public void extractImages(IImageExtractionRequest request) throws CepheusExtractionException { logger.info("Extracting images for: " + request.getDownloadUrl()); - String dpi = propertiesManager.getProperty(IPropertiesManager.PDF_TO_IMAGE_DPI) + String dpi = propertiesManager.getProperty(Properties.PDF_TO_IMAGE_DPI) .trim(); - String type = propertiesManager.getProperty(IPropertiesManager.PDF_TO_IMAGE_TYPE) + String type = propertiesManager.getProperty(Properties.PDF_TO_IMAGE_TYPE) .trim(); String format = propertiesManager.getProperty( - IPropertiesManager.PDF_TO_IMAGE_FORMAT).trim(); + Properties.PDF_TO_IMAGE_FORMAT).trim(); PDDocument pdfDocument; try { @@ -163,7 +164,7 @@ public void extractImages(IImageExtractionRequest request) .sendRequest( completedRequest, propertiesManager - .getProperty(IPropertiesManager.KAFKA_IMAGE_EXTRACTION_COMPLETE_TOPIC)); + .getProperty(Properties.KAFKA_IMAGE_EXTRACTION_COMPLETE_TOPIC)); } catch (MessageCreationException e) { logger.error("Could not send message.", e); } @@ -172,10 +173,10 @@ public void extractImages(IImageExtractionRequest request) private Page saveImage(String requestId, String documentId, BufferedImage image, String fileName) throws IOException, FileNotFoundException { - String dpi = propertiesManager.getProperty(IPropertiesManager.PDF_TO_IMAGE_DPI) + String dpi = propertiesManager.getProperty(Properties.PDF_TO_IMAGE_DPI) .trim(); String format = propertiesManager.getProperty( - IPropertiesManager.PDF_TO_IMAGE_FORMAT).trim(); + Properties.PDF_TO_IMAGE_FORMAT).trim(); String dirFolder = fileStorageManager.getAndCreateStoragePath(requestId, documentId, null); diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/web/EditPropertiesController.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/web/EditPropertiesController.java index 23d696e..2f62b8e 100755 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/web/EditPropertiesController.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/web/EditPropertiesController.java @@ -18,10 +18,11 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; -import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusPropertiesStorageException; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; +import edu.asu.diging.gilesecosystem.cepheus.service.Properties; import edu.asu.diging.gilesecosystem.cepheus.web.pages.SystemConfigPage; import edu.asu.diging.gilesecosystem.cepheus.web.validators.SystemConfigValidator; +import edu.asu.diging.gilesecosystem.util.exceptions.PropertiesStorageException; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; @Controller public class EditPropertiesController { @@ -38,7 +39,7 @@ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder b public String getConfigPage(Model model) { SystemConfigPage page = new SystemConfigPage(); - page.setBaseUrl(propertyManager.getProperty(IPropertiesManager.CEPHEUS_URL)); + page.setBaseUrl(propertyManager.getProperty(Properties.CEPHEUS_URL)); model.addAttribute("systemConfigPage", page); return "admin/system/config"; @@ -56,11 +57,11 @@ public String storeSystemConfig(@Validated @ModelAttribute SystemConfigPage syst } Map propertiesMap = new HashMap(); - propertiesMap.put(IPropertiesManager.CEPHEUS_URL, systemConfigPage.getBaseUrl()); + propertiesMap.put(Properties.CEPHEUS_URL, systemConfigPage.getBaseUrl()); try { propertyManager.updateProperties(propertiesMap); - } catch (CepheusPropertiesStorageException e) { + } catch (PropertiesStorageException e) { model.addAttribute("show_alert", true); model.addAttribute("alert_type", "danger"); model.addAttribute("alert_msg", "An unexpected error occurred. System Configuration could not be saved."); diff --git a/cepheus/src/main/resources/config.properties b/cepheus/src/main/resources/config.properties index da56f23..36573eb 100644 --- a/cepheus/src/main/resources/config.properties +++ b/cepheus/src/main/resources/config.properties @@ -7,8 +7,6 @@ pdf_to_image_dpi=${pdf.conversion.dpi} pdf_to_image_type=${pdf.conversion.type} pdf_to_image_format=${pdf.conversion.format} -cepheus_url=${cepheus.base.url} - # Kafka server kafka_hosts=${cepheus.kafka.hosts} @@ -17,6 +15,9 @@ topic_extract_text_request=geco.requests.pdf.extract topic_extract_text_request_complete=geco.requests.pdf.extract.complete topic_extract_images_request=geco.requests.pdf.toimages topic_extract_images_request_complete=geco.requests.pdf.toimages.complete +topic_system_messages=geco.requests.system.messages -# connection to Giles -giles_access_token=${cepheus.giles.accessToken} \ No newline at end of file +# app info +app_name=Cepheus +app_url=${cepheus.base.url} +application_id=geco.cepheus \ No newline at end of file diff --git a/cepheus/src/main/webapp/WEB-INF/spring/root-context.xml b/cepheus/src/main/webapp/WEB-INF/spring/root-context.xml index 6b83deb..460e600 100644 --- a/cepheus/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/cepheus/src/main/webapp/WEB-INF/spring/root-context.xml @@ -14,7 +14,7 @@ http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> - + diff --git a/cepheus/src/main/webapp/WEB-INF/tiles-defs.xml b/cepheus/src/main/webapp/WEB-INF/tiles-defs.xml index c6edafe..acc2ff3 100644 --- a/cepheus/src/main/webapp/WEB-INF/tiles-defs.xml +++ b/cepheus/src/main/webapp/WEB-INF/tiles-defs.xml @@ -22,5 +22,8 @@ + + + \ No newline at end of file diff --git a/cepheus/src/main/webapp/WEB-INF/tiles/skeleton.jsp b/cepheus/src/main/webapp/WEB-INF/tiles/skeleton.jsp index e034bad..12ca132 100644 --- a/cepheus/src/main/webapp/WEB-INF/tiles/skeleton.jsp +++ b/cepheus/src/main/webapp/WEB-INF/tiles/skeleton.jsp @@ -66,6 +66,9 @@
  • ">System Config
  • +
  • ">Kafka +
  • diff --git a/cepheus/src/main/webapp/WEB-INF/views/admin/manage_listeners.jsp b/cepheus/src/main/webapp/WEB-INF/views/admin/manage_listeners.jsp new file mode 100644 index 0000000..564befa --- /dev/null +++ b/cepheus/src/main/webapp/WEB-INF/views/admin/manage_listeners.jsp @@ -0,0 +1,45 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + + +
    + +

    + Kafka listeners are currently: + + Active + + + Stopped + +

    + +
    + + + + +


    + Once Kafka listeners have been stopped, Cepheus will no longer process image extraction requests. +

    +
    + + + + + +


    + Once Kafka listeners have been started, Cepheus will proceed processing image extraction requests. +

    +
    +
    +
    + + diff --git a/cepheus/src/test/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiverTest.java b/cepheus/src/test/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiverTest.java index b22d1f1..5f5f3c1 100644 --- a/cepheus/src/test/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiverTest.java +++ b/cepheus/src/test/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiverTest.java @@ -10,9 +10,10 @@ import org.mockito.MockitoAnnotations; import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusExtractionException; -import edu.asu.diging.gilesecosystem.cepheus.service.IPropertiesManager; +import edu.asu.diging.gilesecosystem.cepheus.service.Properties; import edu.asu.diging.gilesecosystem.cepheus.service.pdf.IImageExtractionManager; import edu.asu.diging.gilesecosystem.requests.impl.ImageExtractionRequest; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; public class ExtractionRequestReceiverTest { @@ -40,7 +41,7 @@ public void setUp() { Mockito.when( propertiesManager - .getProperty(IPropertiesManager.KAFKA_IMAGE_EXTRACTION_TOPIC)) + .getProperty(Properties.KAFKA_IMAGE_EXTRACTION_TOPIC)) .thenReturn("geco.requests.pdf.toimages"); } From e42797fea6bee06c5d46caf8eaa4d80328764080 Mon Sep 17 00:00:00 2001 From: Julia Damerow Date: Wed, 21 Mar 2018 15:27:42 -0700 Subject: [PATCH 2/2] [bugfix] report back to giles if extraction failed --- .../pdf/impl/ImageExtractionManager.java | 91 ++++++++++--------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java index 6105f47..7e1cae8 100644 --- a/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java +++ b/cepheus/src/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/ImageExtractionManager.java @@ -91,58 +91,63 @@ public void extractImages(IImageExtractionRequest request) String format = propertiesManager.getProperty( Properties.PDF_TO_IMAGE_FORMAT).trim(); - PDDocument pdfDocument; + PDDocument pdfDocument = null; + RequestStatus status = RequestStatus.COMPLETE; try { pdfDocument = PDDocument.load(new ByteArrayInputStream(downloadFile(request.getDownloadUrl())), MemoryUsageSetting.setupTempFileOnly()); } catch (IOException e) { - throw new CepheusExtractionException(e); + logger.error("Could not extract text.", e); + status = RequestStatus.FAILED; } - int numPages = pdfDocument.getNumberOfPages(); - PDFRenderer renderer = new PDFRenderer(pdfDocument); List pages = new ArrayList<>(); - - String restEndpoint = getRestEndpoint(); - - for (int i = 0; i < numPages; i++) { - edu.asu.diging.gilesecosystem.requests.impl.Page requestPage = new edu.asu.diging.gilesecosystem.requests.impl.Page(); - requestPage.setPageNr(i); + if (pdfDocument != null) { + int numPages = pdfDocument.getNumberOfPages(); + PDFRenderer renderer = new PDFRenderer(pdfDocument); + + String restEndpoint = getRestEndpoint(); + + for (int i = 0; i < numPages; i++) { + edu.asu.diging.gilesecosystem.requests.impl.Page requestPage = new edu.asu.diging.gilesecosystem.requests.impl.Page(); + requestPage.setPageNr(i); + + try { + BufferedImage image = renderer.renderImageWithDPI(i, + Float.parseFloat(dpi), ImageType.valueOf(type)); + String fileName = request.getFilename() + "." + i + "." + format; + Page pageImage = saveImage(request.getRequestId(), + request.getDocumentId(), image, fileName); + + requestPage.setDownloadUrl(restEndpoint + + DownloadFileController.GET_FILE_URL + .replace(DownloadFileController.REQUEST_ID_PLACEHOLDER, + request.getRequestId()) + .replace(DownloadFileController.DOCUMENT_ID_PLACEHOLDER, + request.getDocumentId()) + .replace(DownloadFileController.FILENAME_PLACEHOLDER, + pageImage.filename)); + requestPage.setPathToFile(pageImage.path); + requestPage.setFilename(pageImage.filename); + requestPage.setContentType(pageImage.contentType); + requestPage.setSize(pageImage.size); + requestPage.setStatus(PageStatus.COMPLETE); + + } catch (IllegalArgumentException | IOException e) { + logger.error("Could not render image.", e); + requestPage.setStatus(PageStatus.FAILED); + requestPage.setErrorMsg(e.getMessage()); + } + + pages.add(requestPage); + } try { - BufferedImage image = renderer.renderImageWithDPI(i, - Float.parseFloat(dpi), ImageType.valueOf(type)); - String fileName = request.getFilename() + "." + i + "." + format; - Page pageImage = saveImage(request.getRequestId(), - request.getDocumentId(), image, fileName); - - requestPage.setDownloadUrl(restEndpoint - + DownloadFileController.GET_FILE_URL - .replace(DownloadFileController.REQUEST_ID_PLACEHOLDER, - request.getRequestId()) - .replace(DownloadFileController.DOCUMENT_ID_PLACEHOLDER, - request.getDocumentId()) - .replace(DownloadFileController.FILENAME_PLACEHOLDER, - pageImage.filename)); - requestPage.setPathToFile(pageImage.path); - requestPage.setFilename(pageImage.filename); - requestPage.setContentType(pageImage.contentType); - requestPage.setSize(pageImage.size); - requestPage.setStatus(PageStatus.COMPLETE); - - } catch (IllegalArgumentException | IOException e) { - logger.error("Could not render image.", e); - requestPage.setStatus(PageStatus.FAILED); - requestPage.setErrorMsg(e.getMessage()); - } - - pages.add(requestPage); + pdfDocument.close(); + } catch (IOException e) { + logger.error("Error closing document.", e); + } } - try { - pdfDocument.close(); - } catch (IOException e) { - logger.error("Error closing document.", e); - } ICompletedImageExtractionRequest completedRequest = null; try { @@ -154,7 +159,7 @@ public void extractImages(IImageExtractionRequest request) } completedRequest.setDocumentId(request.getDocumentId()); - completedRequest.setStatus(RequestStatus.COMPLETE); + completedRequest.setStatus(status); completedRequest.setExtractionDate(OffsetDateTime.now(ZoneId.of("UTC")) .toString()); completedRequest.setPages(pages);