-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from diging/develop
Prepare release v0.1
- Loading branch information
Showing
20 changed files
with
712 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...tions/CepheusTextExtractionException.java → ...xceptions/CepheusExtractionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
.../src/main/java/edu/asu/diging/gilesecosystem/cepheus/kafka/ExtractionRequestReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package edu.asu.diging.gilesecosystem.cepheus.kafka; | ||
|
||
import java.io.IOException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.kafka.support.KafkaHeaders; | ||
import org.springframework.messaging.handler.annotation.Header; | ||
|
||
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.cepheus.service.pdf.ITextExtractionManager; | ||
import edu.asu.diging.gilesecosystem.requests.IImageExtractionRequest; | ||
import edu.asu.diging.gilesecosystem.requests.ITextExtractionRequest; | ||
import edu.asu.diging.gilesecosystem.requests.impl.ImageExtractionRequest; | ||
import edu.asu.diging.gilesecosystem.requests.impl.TextExtractionRequest; | ||
|
||
@PropertySource("classpath:/config.properties") | ||
public class ExtractionRequestReceiver { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private ITextExtractionManager textExtractionManager; | ||
|
||
@Autowired | ||
private IImageExtractionManager imageExtractionManager; | ||
|
||
@Autowired | ||
protected IPropertiesManager propertiesManager; | ||
|
||
|
||
@KafkaListener(id="cepheus.extraction", topics = {"${topic_extract_text_request}", "${topic_extract_images_request}"}) | ||
public void receiveMessage(String message, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) { | ||
if (topic.equals(propertiesManager.getProperty(IPropertiesManager.KAFKA_EXTRACTION_TOPIC))) { | ||
extractText(message); | ||
} else if (topic.equals(propertiesManager.getProperty(IPropertiesManager.KAFKA_IMAGE_EXTRACTION_TOPIC))) { | ||
extractImage(message); | ||
} | ||
} | ||
|
||
private void extractText(String message) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
ITextExtractionRequest request = null; | ||
try { | ||
request = mapper.readValue(message, TextExtractionRequest.class); | ||
} catch (IOException e) { | ||
logger.error("Could not unmarshall request.", e); | ||
// FIXME: handle this case | ||
return; | ||
} | ||
|
||
try { | ||
textExtractionManager.extractText(request); | ||
} catch (CepheusExtractionException e) { | ||
logger.error("Could not extract text."); | ||
// FIXME: send to monitoring app | ||
} | ||
} | ||
|
||
private void extractImage(String message) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
IImageExtractionRequest request = null; | ||
try { | ||
request = mapper.readValue(message, ImageExtractionRequest.class); | ||
} catch (IOException e) { | ||
logger.error("Could not unmarshall request.", e); | ||
// FIXME: handle this case | ||
return; | ||
} | ||
|
||
try { | ||
imageExtractionManager.extractImages(request); | ||
} catch (CepheusExtractionException e) { | ||
logger.error("Could not extract text."); | ||
// FIXME: send to monitoring app | ||
} | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
.../main/java/edu/asu/diging/gilesecosystem/cepheus/kafka/TextExtractionRequestReceiver.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/IImageExtractionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package edu.asu.diging.gilesecosystem.cepheus.service.pdf; | ||
|
||
import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusExtractionException; | ||
import edu.asu.diging.gilesecosystem.requests.IImageExtractionRequest; | ||
|
||
public interface IImageExtractionManager { | ||
|
||
public abstract void extractImages(IImageExtractionRequest request) | ||
throws CepheusExtractionException; | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
...c/main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/ITextExtractionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package edu.asu.diging.gilesecosystem.cepheus.service.pdf; | ||
|
||
import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusTextExtractionException; | ||
import edu.asu.diging.gilesecosystem.cepheus.exceptions.CepheusExtractionException; | ||
import edu.asu.diging.gilesecosystem.requests.ITextExtractionRequest; | ||
|
||
public interface ITextExtractionManager { | ||
|
||
public abstract void extractText(ITextExtractionRequest request) throws CepheusTextExtractionException; | ||
public abstract void extractText(ITextExtractionRequest request) throws CepheusExtractionException; | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
.../main/java/edu/asu/diging/gilesecosystem/cepheus/service/pdf/impl/AExtractionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package edu.asu.diging.gilesecosystem.cepheus.service.pdf.impl; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
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.util.files.IFileStorageManager; | ||
|
||
public class AExtractionManager { | ||
|
||
protected final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
protected IFileStorageManager fileStorageManager; | ||
|
||
|
||
@Autowired | ||
protected IPropertiesManager propertiesManager; | ||
|
||
public byte[] downloadFile(String url) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter()); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM)); | ||
headers.set( | ||
"Authorization", | ||
"token " | ||
+ propertiesManager | ||
.getProperty(IPropertiesManager.GILES_ACCESS_TOKEN)); | ||
HttpEntity<String> entity = new HttpEntity<String>(headers); | ||
|
||
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, | ||
entity, byte[].class); | ||
if (response.getStatusCode().equals(HttpStatus.OK)) { | ||
return response.getBody(); | ||
} | ||
return null; | ||
} | ||
|
||
protected Page saveTextToFile(int pageNr, String requestId, | ||
String documentId, String pageText, String filename, String fileExtentions) { | ||
String docFolder = fileStorageManager.getAndCreateStoragePath(requestId, | ||
documentId, null); | ||
|
||
if (pageNr > -1) { | ||
filename = filename + "." + pageNr; | ||
} | ||
|
||
if (!fileExtentions.startsWith(".")) { | ||
fileExtentions = "." + fileExtentions; | ||
} | ||
filename = filename + fileExtentions; | ||
|
||
String filePath = docFolder + File.separator + filename; | ||
File fileObject = new File(filePath); | ||
try { | ||
fileObject.createNewFile(); | ||
} catch (IOException e) { | ||
logger.error("Could not create file.", e); | ||
return null; | ||
} | ||
|
||
try { | ||
FileWriter writer = new FileWriter(fileObject); | ||
BufferedWriter bfWriter = new BufferedWriter(writer); | ||
bfWriter.write(pageText); | ||
bfWriter.close(); | ||
writer.close(); | ||
} catch (IOException e) { | ||
logger.error("Could not write text to file.", e); | ||
return null; | ||
} | ||
|
||
String relativePath = fileStorageManager.getFileFolderPathInBaseFolder(requestId, documentId, null); | ||
Page page = new Page(relativePath + File.separator + filename, filename); | ||
page.size = fileObject.length(); | ||
return page; | ||
} | ||
|
||
protected String getRestEndpoint() { | ||
String restEndpoint = propertiesManager.getProperty(IPropertiesManager.CEPHEUS_URL); | ||
if (restEndpoint.endsWith("/")) { | ||
restEndpoint = restEndpoint.substring(0, restEndpoint.length()-1); | ||
} | ||
return restEndpoint; | ||
} | ||
|
||
class Page { | ||
public String path; | ||
public String filename; | ||
public String contentType; | ||
public long size; | ||
|
||
public Page(String path, String filename) { | ||
super(); | ||
this.path = path; | ||
this.filename = filename; | ||
} | ||
} | ||
} |
Oops, something went wrong.