diff --git a/src/main/java/info/textgrid/services/aggregator/EPUB.java b/src/main/java/info/textgrid/services/aggregator/EPUB.java new file mode 100644 index 0000000000000000000000000000000000000000..dd1f80caa567583180727f7cb814d991b6d1c600 --- /dev/null +++ b/src/main/java/info/textgrid/services/aggregator/EPUB.java @@ -0,0 +1,99 @@ +package info.textgrid.services.aggregator; + +import info.textgrid.namespaces.metadata.core._2010.MetadataContainerType; +import info.textgrid.namespaces.metadata.core._2010.ObjectType; +import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.AuthFault; +import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.IoFault; +import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.MetadataParseFault; +import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.ObjectNotFoundFault; +import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URI; +import java.text.MessageFormat; +import java.util.logging.Logger; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import com.google.common.io.Files; + +@Path("/epub") +public class EPUB { + private final ITextGridRep repository = TextGridRepProvider.getInstance(); + + final Logger logger = Logger.getLogger("info.textgrid.services.aggregator"); + + @GET + @Path(value="/{object}") + @Produces(value="application/epub+zip") + public Response get(@PathParam("object") final URI uri) { + logger.fine("EPUB called for root object: " + uri); + final TGCrudService crud = repository.getCRUDService(); + try { + final MetadataContainerType container = crud.readMetadata(null, null, uri.toString()); + final ObjectType rootObject = container.getObject(); + final String mimeType = rootObject.getGeneric().getProvided().getFormat(); + final boolean aggregation = mimeType.contains("aggregation"); + if (!aggregation && !mimeType.matches("^text/.*xml.*")) { + final String errorMsg = "The EPUB export can only convert aggregations or XML documents to EPUB, however, the document {0} you referred to has the MIME type {1}."; + logger.warning("Failing with: " + errorMsg); + return Response + .status(Status.UNSUPPORTED_MEDIA_TYPE) + .entity(MessageFormat.format(errorMsg, uri, mimeType)) + .type("text/plain") + .build(); + } + + final File workingDir = Files.createTempDir(); + logger.fine("Using " + workingDir + " to build the E-Book"); + + // First, use the aggregator to create a TEI corpus file to build on + final TEICorpusSerializer corpusSerializer = new TEICorpusSerializer(rootObject, true); + final File corpus = new File(workingDir, "corpus.xml"); + final FileOutputStream corpusOutput = new FileOutputStream(corpus); + corpusSerializer.write(corpusOutput); + corpusOutput.close(); + + + + return Response.ok().type("text/plain").entity("Done, see " + workingDir).build(); + + + } catch (final ObjectNotFoundFault e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (final MetadataParseFault e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (final IoFault e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (final AuthFault e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (final FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (final WebApplicationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (final IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Response.noContent().build(); + + + + } + +} diff --git a/src/main/webapp/WEB-INF/beans.xml b/src/main/webapp/WEB-INF/beans.xml index aea781229857d8155528ab8776ffa024e1960585..32ba3b57e264ecc00d0b7e2cb079efe0204d6a33 100644 --- a/src/main/webapp/WEB-INF/beans.xml +++ b/src/main/webapp/WEB-INF/beans.xml @@ -23,6 +23,7 @@ http://cxf.apache.org/schemas/jaxrs.xsd"> <jaxrs:serviceBeans> <!-- bean class="info.textgrid.services.aggregator.HelloWorld" /--> <bean class="info.textgrid.services.aggregator.TEICorpus" scope="singleton" /> + <bean class="info.textgrid.services.aggregator.EPUB" scope="singleton" /> </jaxrs:serviceBeans> <jaxrs:providers> <!-- bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/-->