From a3519c6df10d51bb5e4eb91dc26d4a9737cf16ab Mon Sep 17 00:00:00 2001
From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
Date: Mon, 20 Jan 2014 14:47:59 +0100
Subject: [PATCH] Reconfigured REST API to a single class per instance

---
 .../textgrid/services/aggregator/REST.java    | 157 ++++++++++++++++++
 .../services/aggregator/epub/EPUB.java        |   1 +
 .../services/aggregator/html/HTML.java        |   3 +-
 .../aggregator/teicorpus/TEICorpus.java       |   1 +
 .../textgrid/services/aggregator/zip/ZIP.java |   1 +
 src/main/webapp/WEB-INF/beans.xml             |  84 +++-------
 6 files changed, 187 insertions(+), 60 deletions(-)
 create mode 100644 src/main/java/info/textgrid/services/aggregator/REST.java

diff --git a/src/main/java/info/textgrid/services/aggregator/REST.java b/src/main/java/info/textgrid/services/aggregator/REST.java
new file mode 100644
index 0000000..534c160
--- /dev/null
+++ b/src/main/java/info/textgrid/services/aggregator/REST.java
@@ -0,0 +1,157 @@
+package info.textgrid.services.aggregator;
+
+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.ProtocolNotImplementedFault;
+import info.textgrid.services.aggregator.epub.EPUBSerializer;
+import info.textgrid.services.aggregator.html.HTMLWriter;
+import info.textgrid.services.aggregator.teicorpus.TEICorpusExporter;
+import info.textgrid.services.aggregator.zip.ZipResult;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutionException;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.StreamingOutput;
+
+import net.sf.saxon.s9api.SaxonApiException;
+
+import org.apache.cxf.jaxrs.model.wadl.Description;
+import org.apache.cxf.jaxrs.model.wadl.Descriptions;
+import org.apache.cxf.jaxrs.model.wadl.DocTarget;
+
+import com.google.common.base.Optional;
+
+public class REST {
+	
+	private final ITextGridRep repository;
+	
+	
+	private StylesheetManager stylesheetManager;
+	private StylesheetManager getStylesheetManager() {
+		if (stylesheetManager == null)
+			stylesheetManager = new StylesheetManager(servlet, repository);
+		return stylesheetManager;
+	}
+
+	@Context
+	private ServletContext servlet;
+
+
+	private final String version;
+
+	public REST(final ITextGridRep repository, final String version) {
+		this.repository = repository;
+		this.version = version;
+	}
+	
+	@GET
+	@Path("/teicorpus/{uris}")
+	@Produces("application/tei+xml")
+	@Descriptions({
+			@Description(target=DocTarget.METHOD, value="Creates a TEI corpus of all the TEI documents (recursively) aggregated by the given aggregation"),
+			@Description(target=DocTarget.RETURN, value="TEI corpus document")
+	})
+	public Response getCorpus(@Description("TextGrid URIs of the root objects, separated by commas") @PathParam("uris") final String uriList,
+			@Description("Whether to generate a Content-Disposition: attachment header") @QueryParam("attach") @DefaultValue("true") final boolean attach,
+			@Description("If true, no intermediate TEI corpus documents will be generated for intermediate aggregations, hierarchical structure will be lost") @QueryParam("flat") @DefaultValue("false") final boolean flat,
+			@Description("Title for the container if multiple root objects are given") @QueryParam("title") final String titleArgument, 
+			@Description("Session id for accessing restricted resources") @QueryParam("sid") final String sid,
+			@Context Request request)
+					throws URISyntaxException, ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault, ProtocolNotImplementedFault, IOException, SaxonApiException {
+		
+		TEICorpusExporter exporter = new TEICorpusExporter(repository, request, uriList);
+		exporter.setFlat(flat);
+		exporter.setTitle(titleArgument);
+		
+		return exporter.createResponse().build();
+	}
+	
+	
+	@GET
+	@Path(value = "/epub/{object}")
+	@Produces(value = "application/epub+zip")
+	@Description("Converts the given TEI object or the aggregation of TEI objects to an E-Book in EPUB format")
+	public Response getEPUB(
+			@Description("The TextGrid URI(s) of the object(s) to convert, separated by commas. Should be either TEI objects or aggregations of TEI (and maybe other) objects")
+			@PathParam("object") final String uriList,
+			@Description("URL of an alternative stylesheet to use. Must be compatible.") @QueryParam("stylesheet") final URI xsluri,
+			@Description("Title if multiple root objects given") @QueryParam("title") final String titleParam,
+			@Description("Session ID for accessing protected objects") @QueryParam("sid") final String sid,
+			@Context final Request request)
+					throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault,
+					ProtocolNotImplementedFault, IOException, SaxonApiException {
+
+		final EPUBSerializer serializer = new EPUBSerializer(repository, getStylesheetManager(), uriList, Optional.fromNullable(sid), request);
+		serializer.setStylesheet(xsluri);
+		serializer.setTitle(titleParam);
+
+		return serializer.createResponse().build();
+	}
+
+	@GET
+	@Path(value = "/html/{object}")
+	@Produces(value = "text/html")
+	public Response getHTML(
+			@Description("The TextGrid URIs of the TEI document(s) or aggregation(s) to transform, separated by commas") @PathParam("object") final String uriList,
+			@Description("If given, an alternative XSLT stylesheet to use") @QueryParam("stylesheet") final URI xsluri,
+			@Description("If true, check for an <?xsl-stylesheet?> processing instruction in the document to render") @QueryParam("pi") final boolean pi,
+			@Description("If true and a stylesheet has been given, force reloading the stylesheet, do not cache") @QueryParam("refreshStylesheet") final boolean refreshStylesheet,
+			@Description("Session ID to access protected resources") @QueryParam("sid") final String sid,
+			@Description("If true, pass the information the stylesheet that its result will be embedded into some website") @QueryParam("embedded") final boolean embedded,
+			@Description("URL of the CSS that should be referenced in the HTML that is created") @QueryParam("css") final URI css,
+			@Description("The requested content type. E.g., text/html or text/xml") @QueryParam("mediatype") final String mediaType,
+			@Context final Request request) throws ObjectNotFoundFault,
+			MetadataParseFault, IoFault, AuthFault,
+			ProtocolNotImplementedFault, WebApplicationException, IOException,
+			SaxonApiException, ExecutionException {
+
+
+		final HTMLWriter writer = new HTMLWriter(repository, getStylesheetManager(), uriList, xsluri,
+				refreshStylesheet, pi, embedded, css, sid, mediaType, request);
+		return writer.createResponse().build();
+	}
+	
+	@GET
+	@Path(value = "/zip/{objects}")
+	@Produces("application/zip")
+	public Response getZIP(
+			@Description("The TextGridURIs of the TEI documents or aggregations to zip, separated by commas (,)")
+			@PathParam("objects") final String uriList,
+			@Description("Session ID to access protected resources")
+			@QueryParam("sid") final String sid,
+			@Description("(optional) title for the exported data, currently only used for generating the filename. If none is given, the first title of the first object will be used.")
+			@QueryParam("title") String title,
+			@Context final Request request) throws MetadataParseFault, ObjectNotFoundFault, IoFault, AuthFault, ProtocolNotImplementedFault, IOException, SaxonApiException {
+		
+		ZipResult zipResult = new ZipResult(repository, request, uriList);
+		if (title != null)
+			zipResult.setTitle(title);
+		if (sid != null)
+			zipResult.sid(sid);
+		return zipResult.createResponse().build();
+	}
+	
+	@GET
+	@Path(value = "/version")
+	@Produces("text/html")
+	public StreamingOutput getVersion() {
+		Version version = new Version(repository, this.version);
+		return version.get();
+	}
+
+}
diff --git a/src/main/java/info/textgrid/services/aggregator/epub/EPUB.java b/src/main/java/info/textgrid/services/aggregator/epub/EPUB.java
index 3d30de7..e309dd9 100644
--- a/src/main/java/info/textgrid/services/aggregator/epub/EPUB.java
+++ b/src/main/java/info/textgrid/services/aggregator/epub/EPUB.java
@@ -28,6 +28,7 @@
 
 import com.google.common.base.Optional;
 
+@Deprecated
 @Path("/epub")
 @Description("Converts the given TEI object or the aggregation of TEI objects to an E-Book in EPUB format")
 public class EPUB {
diff --git a/src/main/java/info/textgrid/services/aggregator/html/HTML.java b/src/main/java/info/textgrid/services/aggregator/html/HTML.java
index 8798df5..1cf932f 100644
--- a/src/main/java/info/textgrid/services/aggregator/html/HTML.java
+++ b/src/main/java/info/textgrid/services/aggregator/html/HTML.java
@@ -28,6 +28,7 @@
 
 import org.apache.cxf.jaxrs.model.wadl.Description;
 
+@Deprecated
 @Path("/html")
 @Description("Creates an HTML representation of the given TEI document, or aggregation of TEI documents.")
 public class HTML {
@@ -52,7 +53,7 @@ public HTML(final ITextGridRep repository) throws IOException {
 
 	@GET
 	@Path(value = "/{object}")
-	@Produces(value = "text/xml")
+	@Produces(value = "text/html")
 	public Response get(
 			@Description("The TextGrid URIs of the TEI document(s) or aggregation(s) to transform, separated by commas") @PathParam("object") final String uriList,
 			@Description("If given, an alternative XSLT stylesheet to use") @QueryParam("stylesheet") final URI xsluri,
diff --git a/src/main/java/info/textgrid/services/aggregator/teicorpus/TEICorpus.java b/src/main/java/info/textgrid/services/aggregator/teicorpus/TEICorpus.java
index cf6b8b5..271ab34 100644
--- a/src/main/java/info/textgrid/services/aggregator/teicorpus/TEICorpus.java
+++ b/src/main/java/info/textgrid/services/aggregator/teicorpus/TEICorpus.java
@@ -53,6 +53,7 @@
  */
 @Description("Creates a TEI corpus of all the TEI documents (recursively) aggregated by the given aggregation")
 @Path("/teicorpus")
+@Deprecated
 public class TEICorpus {
 
 	private final ITextGridRep repository;
diff --git a/src/main/java/info/textgrid/services/aggregator/zip/ZIP.java b/src/main/java/info/textgrid/services/aggregator/zip/ZIP.java
index 9007b62..035241b 100644
--- a/src/main/java/info/textgrid/services/aggregator/zip/ZIP.java
+++ b/src/main/java/info/textgrid/services/aggregator/zip/ZIP.java
@@ -22,6 +22,7 @@
 
 import org.apache.cxf.jaxrs.model.wadl.Description;
 
+@Deprecated
 @Path("/zip")
 public class ZIP {
 	private final ITextGridRep repository;
diff --git a/src/main/webapp/WEB-INF/beans.xml b/src/main/webapp/WEB-INF/beans.xml
index 482504a..42c646e 100644
--- a/src/main/webapp/WEB-INF/beans.xml
+++ b/src/main/webapp/WEB-INF/beans.xml
@@ -26,66 +26,32 @@ http://cxf.apache.org/schemas/jaxrs.xsd">
   </bean>
 
 
-   <jaxrs:server id="services" address="/" publishedEndpointUrl="${aggregator.endpoint.published}">
-    <jaxrs:serviceBeans>
-      <!-- bean class="info.textgrid.services.aggregator.HelloWorld" /-->
-      <bean class="info.textgrid.services.aggregator.teicorpus.TEICorpus" scope="singleton">
-      	<constructor-arg ref="stable-repo"/>
-      </bean>
-      <bean class="info.textgrid.services.aggregator.epub.EPUB" scope="singleton">
-      	<constructor-arg ref="stable-repo"/>
-      </bean>
-      <bean class="info.textgrid.services.aggregator.pdf.PDF" scope="singleton">
-      	<constructor-arg ref="stable-repo"/>
-      </bean>
-      <bean class="info.textgrid.services.aggregator.html.HTML" scope="singleton">
-      	<constructor-arg ref="stable-repo"/>
-      </bean>
-      <bean class="info.textgrid.services.aggregator.zip.ZIP" scope="singleton">
-      	<constructor-arg ref="stable-repo"/>
-      </bean>
-      <bean class="info.textgrid.services.aggregator.Version" scope="singleton">
-      	<constructor-arg ref="stable-repo"/>
-      	<constructor-arg name="version" value="${project.version}" />
-      </bean>
-    </jaxrs:serviceBeans>
-    <jaxrs:providers>
-        <!-- bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/-->
-        <bean class="info.textgrid.services.aggregator.GenericExceptionMapper"/>
-        <!--bean class="org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper"/-->        
-    </jaxrs:providers>
-    </jaxrs:server>
-    
-    <jaxrs:server id="services-dev" address="/dev" publishedEndpointUrl="${aggregator.endpoint.published}/dev">
-	<jaxrs:serviceBeans>
-		<!-- bean class="info.textgrid.services.aggregator.HelloWorld" / -->
-		<bean class="info.textgrid.services.aggregator.teicorpus.TEICorpus" scope="singleton">
-			<constructor-arg ref="dev-repo" />
-		</bean>
-		<bean class="info.textgrid.services.aggregator.epub.EPUB" scope="singleton">
-			<constructor-arg ref="dev-repo" />
-		</bean>
-		<bean class="info.textgrid.services.aggregator.pdf.PDF" scope="singleton">
-			<constructor-arg ref="dev-repo" />
-		</bean>
-		<bean class="info.textgrid.services.aggregator.html.HTML" scope="singleton">
-			<constructor-arg ref="dev-repo" />
-		</bean>
-		<bean class="info.textgrid.services.aggregator.zip.ZIP" scope="singleton">
-			<constructor-arg ref="dev-repo" />
-		</bean>
-	      <bean class="info.textgrid.services.aggregator.Version" scope="singleton">
-	      	<constructor-arg ref="dev-repo"/>
-	      	<constructor-arg name="version" value="${project.version}" />
-	      </bean>
-	</jaxrs:serviceBeans>
-	<jaxrs:providers>
-		<!-- bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/ -->
-		<!-- bean class="org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper" /-->
-        <bean class="info.textgrid.services.aggregator.GenericExceptionMapper"/>
-	</jaxrs:providers>
+	<jaxrs:server id="services" address="/"
+		publishedEndpointUrl="${aggregator.endpoint.published}">
+		<jaxrs:serviceBeans>
+			<bean class="info.textgrid.services.aggregator.REST" scope="singleton">
+				<constructor-arg ref="stable-repo" />
+				<constructor-arg name="version" value="${project.version}" />
+			</bean>
+		</jaxrs:serviceBeans>
+		<jaxrs:providers>
+			<bean class="info.textgrid.services.aggregator.GenericExceptionMapper" />
+		</jaxrs:providers>
+	</jaxrs:server>
+
+	<jaxrs:server id="services-dev" address="/dev"
+		publishedEndpointUrl="${aggregator.endpoint.published}/dev">
+		<jaxrs:serviceBeans>
+			<bean class="info.textgrid.services.aggregator.REST" scope="singleton">
+				<constructor-arg ref="stable-repo" />
+				<constructor-arg name="version" value="${project.version}" />
+			</bean>
+		</jaxrs:serviceBeans>
+		<jaxrs:providers>
+			<bean class="info.textgrid.services.aggregator.GenericExceptionMapper" />
+		</jaxrs:providers>
 
-</jaxrs:server>
+	</jaxrs:server>
     
 
 </beans>
-- 
GitLab