From 33633fd059dfcc01245dc12b1a9f9f4736257d74 Mon Sep 17 00:00:00 2001
From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
Date: Thu, 2 Aug 2012 16:30:32 +0000
Subject: [PATCH] Merge branch 'master' into teiheader

git-svn-id: https://develop.sub.uni-goettingen.de/repos/textgrid/trunk/services/aggregator@13448 7c539038-3410-0410-b1ec-0f2a7bf1c452
---
 .gitignore                                    |  1 +
 .../services/aggregator/ITextGridRep.java     | 17 +++++
 .../services/aggregator/TEICorpus.java        | 59 ++-------------
 .../aggregator/TEICorpusSerializer.java       | 19 +++--
 .../aggregator/TextGridRepProvider.java       | 72 +++++++++++++++++++
 src/main/webapp/WEB-INF/beans.xml             |  1 +
 ...EICorpusTest.java => TextGridRepTest.java} | 19 +++--
 7 files changed, 119 insertions(+), 69 deletions(-)
 create mode 100644 src/main/java/info/textgrid/services/aggregator/ITextGridRep.java
 create mode 100644 src/main/java/info/textgrid/services/aggregator/TextGridRepProvider.java
 rename src/test/java/info/textgrid/services/aggregator/{TEICorpusTest.java => TextGridRepTest.java} (72%)

diff --git a/.gitignore b/.gitignore
index 05a7301..0e2b0a7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 /.settings
 /.classpath
+/target
diff --git a/src/main/java/info/textgrid/services/aggregator/ITextGridRep.java b/src/main/java/info/textgrid/services/aggregator/ITextGridRep.java
new file mode 100644
index 0000000..caa1984
--- /dev/null
+++ b/src/main/java/info/textgrid/services/aggregator/ITextGridRep.java
@@ -0,0 +1,17 @@
+package info.textgrid.services.aggregator;
+
+import info.textgrid.middleware.tgsearch.client.SearchClient;
+import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService;
+
+import javax.ws.rs.WebApplicationException;
+
+public interface ITextGridRep {
+
+	public SearchClient getPublicSearchClient();
+
+	public TGCrudService getCRUDService();
+
+	public String getConfValue(final String key) throws WebApplicationException;
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/info/textgrid/services/aggregator/TEICorpus.java b/src/main/java/info/textgrid/services/aggregator/TEICorpus.java
index 0786051..2ecff1a 100644
--- a/src/main/java/info/textgrid/services/aggregator/TEICorpus.java
+++ b/src/main/java/info/textgrid/services/aggregator/TEICorpus.java
@@ -1,23 +1,15 @@
 package info.textgrid.services.aggregator;
 
-import info.textgrid.middleware.confclient.ConfservClient;
-import info.textgrid.middleware.confclient.ConfservClientConstants;
-import info.textgrid.middleware.tgsearch.client.SearchClient;
 import info.textgrid.namespaces.metadata.core._2010.MetadataContainerType;
 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 info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService_Service;
 
-import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URL;
 import java.text.MessageFormat;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -32,11 +24,6 @@
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriBuilder;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.ws.BindingProvider;
-import javax.xml.ws.soap.MTOMFeature;
-
-import org.codehaus.jettison.json.JSONException;
 
 /*
  * Generating a TEIcorpus document from a (single) aggregation
@@ -65,11 +52,8 @@
 @Path("/teicorpus")
 public class TEICorpus {
 
-	private static final String CONF_ENDPOINT = "https://www.textgridlab.org/1.0/confserv";
-	private ConfservClient confservClient;
-	private SearchClient searchClient;
-	private HashMap<String, String> config;
-	private TGCrudService crud;
+	private final ITextGridRep repository = TextGridRepProvider.getInstance();
+
 	final Logger logger = Logger.getLogger("info.textgrid.services.aggregator");
 
 	@GET
@@ -77,12 +61,12 @@ public class TEICorpus {
 	@Produces("text/xml")
 	public Response get(@PathParam("aggregation") final URI uri, @QueryParam("attach") @DefaultValue("true") final boolean attach) throws URISyntaxException {
 		logger.fine("TEIcorpus called for root aggregation: " + uri);
-		final TGCrudService crud = getCRUDService();
+		final TGCrudService crud = repository.getCRUDService();
 		logger.finest("Yo, clients are there.");
 		try {
 			final MetadataContainerType rootAggregationMetadata = crud.readMetadata(null, null, uri.toString());
 			logger.finer("CRUD request for root aggregation successful");
-			final TEICorpusSerializer serializer = new TEICorpusSerializer(rootAggregationMetadata.getObject(), this);
+			final TEICorpusSerializer serializer = new TEICorpusSerializer(rootAggregationMetadata.getObject());
 			final String format = rootAggregationMetadata.getObject().getGeneric().getProvided().getFormat();
 			if (!format.contains("aggregation")) {
 				logger.log(Level.SEVERE, "The requested object {0} is a {1}, not an aggregation, and thus cannot be aggregated.", new Object[] {uri,format});
@@ -128,39 +112,4 @@ public Response get(@PathParam("aggregation") final URI uri, @QueryParam("attach
 		// return null;
 	}
 
-	TGCrudService getCRUDService() {
-		if (crud == null) {
-			final URL wsdl = TGCrudService_Service.class.getResource("/wsdl/TGCrudService.wsdl");
-			final TGCrudService_Service tgCrudService_Service = new TGCrudService_Service(wsdl);
-			final TGCrudService _crud = tgCrudService_Service.getTGCrudPort(new MTOMFeature());
-			final BindingProvider bp = (BindingProvider) _crud;
-			final Map<String, Object> requestContext = bp.getRequestContext();
-			requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, getConfValue(ConfservClientConstants.TG_CRUD));
-			crud = _crud;
-		}
-		return crud;
-	}
-
-	SearchClient getSearchClient() {
-		if (searchClient == null)
-			searchClient = new SearchClient(getConfValue(ConfservClientConstants.TG_SEARCH_PUBLIC));
-		return searchClient;
-	}
-
-	String getConfValue(final String key) throws WebApplicationException {
-		if (config == null) {
-			try {
-				confservClient = new ConfservClient(CONF_ENDPOINT);
-				config = confservClient.getAll();
-			} catch (final IOException e) {
-				throw new WebApplicationException(e);
-			} catch (final JSONException e) {
-				throw new WebApplicationException(e);
-			} catch (final XMLStreamException e) {
-				throw new WebApplicationException(e);
-			}
-		}
-		return config.get(key);
-	}
-
 }
diff --git a/src/main/java/info/textgrid/services/aggregator/TEICorpusSerializer.java b/src/main/java/info/textgrid/services/aggregator/TEICorpusSerializer.java
index 74bb8a4..007911c 100644
--- a/src/main/java/info/textgrid/services/aggregator/TEICorpusSerializer.java
+++ b/src/main/java/info/textgrid/services/aggregator/TEICorpusSerializer.java
@@ -41,11 +41,12 @@ public class TEICorpusSerializer implements StreamingOutput {
 	private static final String TEI_NS = "http://www.tei-c.org/ns/1.0";
 	private final Logger logger = Logger.getLogger("info.textgrid.services.aggregator.teicorpus.serializer");
 	
+	private final ITextGridRep repository = TextGridRepProvider.getInstance();
+
 	private XMLOutputFactory outputFactory;
 	private XMLEventFactory eventFactory;
 	private XMLEventWriter writer;
 	private final ObjectType rootObject;
-	private final TEICorpus teiCorpus;
 	private XMLInputFactory inputFactory;
 	private final Set<String> seen = Sets.newHashSet();
 	private static final EventFilter NO_DOCUMENT_NODE = new EventFilter() {
@@ -61,9 +62,8 @@ private static String toString(final ObjectType object) {
 			object.getGeneric().getProvided().getFormat() + ")";
 	}
 
-	public TEICorpusSerializer(final ObjectType object, final TEICorpus teiCorpus) {
+	public TEICorpusSerializer(final ObjectType object) {
 		this.rootObject = object;
-		this.teiCorpus = teiCorpus;
 		logger.info("Starting TEIcorpus serialization for " + toString(object));
 	}
 
@@ -104,7 +104,8 @@ private void write(final OutputStream output, final ObjectType object) throws XM
 
 	private void writeAggregation(final OutputStream output, final ObjectType object) throws XMLStreamException {
 		logger.fine("[Processing aggregation " + toString(object));
-		final info.textgrid.namespaces.middleware.tgsearch.Response response = teiCorpus.getSearchClient().listAggregation(
+		final info.textgrid.namespaces.middleware.tgsearch.Response response = repository
+				.getPublicSearchClient().listAggregation(
 				object.getGeneric().getGenerated().getTextgridUri().getValue());
 		final List<ResultType> result = response.getResult();
 
@@ -123,7 +124,7 @@ private void writeAggregation(final OutputStream output, final ObjectType object
 		logger.fine("Finished aggregation " + toString(object) + "]");
 	}
 
-	private void writeHeader(ObjectType object) throws XMLStreamException {
+	private void writeHeader(final ObjectType object) throws XMLStreamException {
 		writer.add(eventFactory.createStartElement("",  TEI_NS, "teiHeader"));
 		writer.add(eventFactory.createComment("This is a (temporary) header generated from aggregation metadata."));
 		writer.add(eventFactory.createStartElement("",  TEI_NS, "fileDesc"));
@@ -154,7 +155,7 @@ private void writeHeader(ObjectType object) throws XMLStreamException {
 		writer.add(eventFactory.createEndElement("",  TEI_NS, "teiHeader"));
 	}
 
-	private void writeSimpleTEIElement(String element, final String content) throws XMLStreamException {
+	private void writeSimpleTEIElement(final String element, final String content) throws XMLStreamException {
 		writer.add(eventFactory.createStartElement("",  TEI_NS, element));
 		writer.add(eventFactory.createCharacters(content));
 		writer.add(eventFactory.createEndElement("",  TEI_NS, element));
@@ -165,7 +166,11 @@ private void writeXML(final OutputStream output, final ObjectType object) {
 		final Holder<MetadataContainerType> mdHolder = new Holder<MetadataContainerType>();
 		final Holder<DataHandler> dhHolder = new Holder<DataHandler>();
 		try {
-			teiCorpus.getCRUDService().read("", "", object.getGeneric().getGenerated().getTextgridUri().getValue(), mdHolder,
+			repository.getCRUDService().read(
+					"",
+					"",
+					object.getGeneric().getGenerated().getTextgridUri()
+							.getValue(), mdHolder,
 					dhHolder);
 			final XMLEventReader reader = inputFactory.createFilteredReader(
 					inputFactory.createXMLEventReader(dhHolder.value.getInputStream()),
diff --git a/src/main/java/info/textgrid/services/aggregator/TextGridRepProvider.java b/src/main/java/info/textgrid/services/aggregator/TextGridRepProvider.java
new file mode 100644
index 0000000..ce2e06d
--- /dev/null
+++ b/src/main/java/info/textgrid/services/aggregator/TextGridRepProvider.java
@@ -0,0 +1,72 @@
+package info.textgrid.services.aggregator;
+
+import info.textgrid.middleware.confclient.ConfservClient;
+import info.textgrid.middleware.confclient.ConfservClientConstants;
+import info.textgrid.middleware.tgsearch.client.SearchClient;
+import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService;
+import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService_Service;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.WebApplicationException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.ws.BindingProvider;
+import javax.xml.ws.soap.MTOMFeature;
+
+import org.codehaus.jettison.json.JSONException;
+
+public class TextGridRepProvider implements ITextGridRep {
+
+	private HashMap<String, String> config;
+	private ConfservClient confservClient;
+	private TGCrudService crud;
+	private SearchClient publicSearchClient;
+	private static final String CONF_ENDPOINT = "https://www.textgridlab.org/1.0/confserv";
+	private static final ITextGridRep instance = new TextGridRepProvider();
+
+	@Override
+	public String getConfValue(final String key) throws WebApplicationException {
+		if (config == null) {
+			try {
+				confservClient = new ConfservClient(CONF_ENDPOINT);
+				config = confservClient.getAll();
+			} catch (final IOException e) {
+				throw new WebApplicationException(e);
+			} catch (final JSONException e) {
+				throw new WebApplicationException(e);
+			} catch (final XMLStreamException e) {
+				throw new WebApplicationException(e);
+			}
+		}
+		return config.get(key);
+	}
+
+	@Override
+	public TGCrudService getCRUDService() {
+		if (crud == null) {
+			final URL wsdl = TGCrudService_Service.class.getResource("/wsdl/TGCrudService.wsdl");
+			final TGCrudService_Service tgCrudService_Service = new TGCrudService_Service(wsdl);
+			final TGCrudService _crud = tgCrudService_Service.getTGCrudPort(new MTOMFeature());
+			final BindingProvider bp = (BindingProvider) _crud;
+			final Map<String, Object> requestContext = bp.getRequestContext();
+			requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, getConfValue(ConfservClientConstants.TG_CRUD));
+			crud = _crud;
+		}
+		return crud;
+	}
+
+	@Override
+	public SearchClient getPublicSearchClient() {
+		if (publicSearchClient == null)
+			publicSearchClient = new SearchClient(getConfValue(ConfservClientConstants.TG_SEARCH_PUBLIC));
+		return publicSearchClient;
+	}
+
+	public static ITextGridRep getInstance() {
+		return instance;
+	}
+
+}
diff --git a/src/main/webapp/WEB-INF/beans.xml b/src/main/webapp/WEB-INF/beans.xml
index 5499619..aea7812 100644
--- a/src/main/webapp/WEB-INF/beans.xml
+++ b/src/main/webapp/WEB-INF/beans.xml
@@ -10,6 +10,7 @@ http://www.springframework.org/schema/context http://www.springframework.org/sch
 http://cxf.apache.org/jaxrs
 http://cxf.apache.org/schemas/jaxrs.xsd">
 
+ 
   <import resource="classpath:META-INF/cxf/cxf.xml" />
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
   <context:property-placeholder/>
diff --git a/src/test/java/info/textgrid/services/aggregator/TEICorpusTest.java b/src/test/java/info/textgrid/services/aggregator/TextGridRepTest.java
similarity index 72%
rename from src/test/java/info/textgrid/services/aggregator/TEICorpusTest.java
rename to src/test/java/info/textgrid/services/aggregator/TextGridRepTest.java
index f635f06..95efdda 100644
--- a/src/test/java/info/textgrid/services/aggregator/TEICorpusTest.java
+++ b/src/test/java/info/textgrid/services/aggregator/TextGridRepTest.java
@@ -7,15 +7,20 @@
 import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService;
 
 import org.junit.Test;
+import org.springframework.stereotype.Service;
 
-public class TEICorpusTest {
+@Service
+public class TextGridRepTest {
 
-	private TEICorpus teicorpus;
+	private final ITextGridRep repository = TextGridRepProvider.getInstance();
 
-	TEICorpus getTeiCorpus() {
-		if (teicorpus == null)
-			teicorpus = new TEICorpus();
-		return teicorpus;
+	ITextGridRep getTeiCorpus() {
+		return repository;
+	}
+
+	@Test
+	public void testAutoWire() {
+		assertNotNull(repository);
 	}
 
 	@Test
@@ -33,7 +38,7 @@ public void testGetCRUDService() {
 
 	@Test
 	public void testGetSearchClient() {
-		final SearchClient searchClient = getTeiCorpus().getSearchClient();
+		final SearchClient searchClient = getTeiCorpus().getPublicSearchClient();
 		assertNotNull("TG-search service is null!?", searchClient);
 	}
 
-- 
GitLab