diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2af7cef --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..cfedc1e --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + nl.elstarit.document-conversion + document-conversion-api + 0.0.1-SNAPSHOT + jar + + document-conversion-api + Project to convert documents to plain text + + + org.springframework.boot + spring-boot-starter-parent + 1.5.8.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.apache.tika + tika-core + 1.16 + + + + commons-io + commons-io + 2.5 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/nl/elstarit/documentconversion/api/DocumentConversionApiApplication.java b/src/main/java/nl/elstarit/documentconversion/api/DocumentConversionApiApplication.java new file mode 100644 index 0000000..67b1635 --- /dev/null +++ b/src/main/java/nl/elstarit/documentconversion/api/DocumentConversionApiApplication.java @@ -0,0 +1,12 @@ +package nl.elstarit.documentconversion.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DocumentConversionApiApplication { + + public static void main(String[] args) { + SpringApplication.run(DocumentConversionApiApplication.class, args); + } +} diff --git a/src/main/java/nl/elstarit/documentconversion/api/controller/DocumentConversionController.java b/src/main/java/nl/elstarit/documentconversion/api/controller/DocumentConversionController.java new file mode 100644 index 0000000..9cdfd78 --- /dev/null +++ b/src/main/java/nl/elstarit/documentconversion/api/controller/DocumentConversionController.java @@ -0,0 +1,47 @@ +package nl.elstarit.documentconversion.api.controller; + +import nl.elstarit.documentconversion.api.model.DocumentConversionRequest; +import nl.elstarit.documentconversion.api.model.DocumentConversionResponse; +import org.apache.commons.io.FileUtils; +import org.apache.tika.metadata.Metadata; +import org.apache.tika.parser.AutoDetectParser; +import org.apache.tika.sax.BodyContentHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpServletRequest; +import java.io.InputStream; + +@Controller +public class DocumentConversionController { + + + private static final Logger LOG = LoggerFactory.getLogger(DocumentConversionController.class); + + @RequestMapping("/convert-to-plain-text") + public ResponseEntity covertToPlainText(@RequestBody DocumentConversionRequest documentConversionRequest){ + BodyContentHandler handler = new BodyContentHandler(); + + AutoDetectParser parser = new AutoDetectParser(); + Metadata metadata = new Metadata(); + DocumentConversionResponse documentConversionResponse = new DocumentConversionResponse(); + try{ + if(documentConversionRequest.getFile() != null ) { + InputStream stream = FileUtils.openInputStream(documentConversionRequest.getFile()); + parser.parse(stream, handler, metadata); + documentConversionResponse.setConvertedText(handler.toString()); + }else{ + documentConversionResponse.setMessage("No File found in request"); + } + }catch(Exception e){ + LOG.error("Error on Document Conversion API: {}", e.getMessage()); + } + return new ResponseEntity(documentConversionResponse, HttpStatus.OK); + } + +} diff --git a/src/main/java/nl/elstarit/documentconversion/api/model/DocumentConversionRequest.java b/src/main/java/nl/elstarit/documentconversion/api/model/DocumentConversionRequest.java new file mode 100644 index 0000000..97f2453 --- /dev/null +++ b/src/main/java/nl/elstarit/documentconversion/api/model/DocumentConversionRequest.java @@ -0,0 +1,53 @@ +package nl.elstarit.documentconversion.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.io.File; + +public class DocumentConversionRequest { + + @JsonIgnore + private File file = null; + + @JsonIgnore + private String media_type = ""; + + public void setFile(File file) { + this.file = file; + } + public File getFile() { + return file; + } + public void setMedia_type(String media_type) { + this.media_type = media_type; + } + public String getMedia_type() { + return media_type; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DocumentConversionRequest that = (DocumentConversionRequest) o; + + if (file != null ? !file.equals(that.file) : that.file != null) return false; + return media_type != null ? media_type.equals(that.media_type) : that.media_type == null; + } + + @Override + public int hashCode() { + int result = file != null ? file.hashCode() : 0; + result = 31 * result + (media_type != null ? media_type.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "DocumentConversionRequest{" + + "file=" + file + + ", media_type='" + media_type + '\'' + + '}'; + } +} diff --git a/src/main/java/nl/elstarit/documentconversion/api/model/DocumentConversionResponse.java b/src/main/java/nl/elstarit/documentconversion/api/model/DocumentConversionResponse.java new file mode 100644 index 0000000..701aec4 --- /dev/null +++ b/src/main/java/nl/elstarit/documentconversion/api/model/DocumentConversionResponse.java @@ -0,0 +1,46 @@ +package nl.elstarit.documentconversion.api.model; + +public class DocumentConversionResponse { + + private String convertedText; + private String message; + + public String getConvertedText() { + return convertedText; + } + + public void setConvertedText(String convertedText) { + this.convertedText = convertedText; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DocumentConversionResponse that = (DocumentConversionResponse) o; + + return convertedText != null ? convertedText.equals(that.convertedText) : that.convertedText == null; + } + + @Override + public int hashCode() { + return convertedText != null ? convertedText.hashCode() : 0; + } + + @Override + public String toString() { + return "DocumentConversionResponse{" + + "convertedText='" + convertedText + '\'' + + "message='" + message + '\'' + + '}'; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/nl/elstarit/documentconversion/api/DocumentConversionApiApplicationTests.java b/src/test/java/nl/elstarit/documentconversion/api/DocumentConversionApiApplicationTests.java new file mode 100644 index 0000000..bd10b83 --- /dev/null +++ b/src/test/java/nl/elstarit/documentconversion/api/DocumentConversionApiApplicationTests.java @@ -0,0 +1,16 @@ +package nl.elstarit.documentconversion.api; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class DocumentConversionApiApplicationTests { + + @Test + public void contextLoads() { + } + +}