Skip to content
Snippets Groups Projects
Commit 58108927 authored by Thorsten Vitt's avatar Thorsten Vitt
Browse files

Removed invocations of the default TextGridRep instance from non-test

code.
parent ce23ca2a
No related branches found
No related tags found
No related merge requests found
......@@ -131,7 +131,7 @@ protected Source loadSource(final boolean bufferRequired)
if (sourceType == SourceType.AGGREGATION
|| sourceType == SourceType.BASKET) {
final TEICorpusSerializer corpusSerializer =
new TEICorpusSerializer(getRootObjects(), isFlatCorpus(), getSid().orNull());
new TEICorpusSerializer(repository, getRootObjects(), isFlatCorpus(), getSid().orNull());
final OutputStream corpusBuffer = getBufferFactory().getBufferSink();
corpusSerializer.write(corpusBuffer);
corpusBuffer.close();
......
package info.textgrid.services.aggregator.teicorpus;
import java.io.IOException;
import java.io.OutputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response.Status;
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;
......@@ -14,32 +7,39 @@
import info.textgrid.services.aggregator.AbstractExporter;
import info.textgrid.services.aggregator.ITextGridRep;
import java.io.IOException;
import java.io.OutputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response.Status;
public class TEICorpusExporter extends AbstractExporter {
private boolean flat;
public TEICorpusExporter(ITextGridRep repository, Request request,
String uriList) {
public TEICorpusExporter(final ITextGridRep repository, final Request request,
final String uriList) {
super(repository, request, uriList);
setFileExtension("xml");
setMediaType("application/tei+xml");
}
@Override
public void write(OutputStream output) throws IOException,
public void write(final OutputStream output) throws IOException,
WebApplicationException {
TEICorpusSerializer serializer;
try {
serializer = new TEICorpusSerializer(getRootObjects(), isFlat(), getSid().orNull());
serializer = new TEICorpusSerializer(repository, getRootObjects(), isFlat(), getSid().orNull());
serializer.setTitle(getTitle());
serializer.write(output);
} catch (ObjectNotFoundFault e) {
} catch (final ObjectNotFoundFault e) {
throw new WebApplicationException(e, Status.NOT_FOUND);
} catch (MetadataParseFault e) {
} catch (final MetadataParseFault e) {
throw new WebApplicationException(e, Status.BAD_REQUEST);
} catch (IoFault e) {
} catch (final IoFault e) {
throw new WebApplicationException(e);
} catch (AuthFault e) {
} catch (final AuthFault e) {
throw new WebApplicationException(e, Status.FORBIDDEN);
}
}
......@@ -48,7 +48,7 @@ public boolean isFlat() {
return flat;
}
public void setFlat(boolean flat) {
public void setFlat(final boolean flat) {
this.flat = flat;
}
......
......@@ -8,8 +8,8 @@
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.ITextGridRep;
import info.textgrid.services.aggregator.tree.AggregationTreeWalker;
import info.textgrid.services.aggregator.util.TextGridRepProvider;
import info.textgrid.utils.linkrewriter.ConfigurableXMLRewriter;
import java.io.IOException;
......@@ -45,7 +45,7 @@ public class TEICorpusSerializer extends AggregationTreeWalker implements
private static final String TEI_CORPUS = "teiCorpus";
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 XMLOutputFactory outputFactory;
private XMLEventFactory eventFactory;
......@@ -70,27 +70,27 @@ private static String toString(final ObjectType object) {
object.getGeneric().getProvided().getFormat() + ")";
}
private static String toString(final ObjectType[] objects) {
if (objects.length == 0)
if (objects.length == 0)
return "nothing";
else if (objects.length == 1)
return toString(objects[0]);
else return toString(objects[0]) + " etc.";
}
public TEICorpusSerializer(final ObjectType[] rootObjects)
public TEICorpusSerializer(final ITextGridRep repository, final ObjectType[] rootObjects)
throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault {
this.rootObjects = rootObjects;
setRepository(TextGridRepProvider.getInstance());
setRepository(repository);
logger.info("Starting TEIcorpus serialization for " + toString(rootObjects));
}
public TEICorpusSerializer(final ObjectType rootObject, final boolean flat, final String sid) throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault {
this(new ObjectType[] { rootObject}, flat, sid);
public TEICorpusSerializer(final ITextGridRep repository, final ObjectType rootObject, final boolean flat, final String sid) throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault {
this(repository, new ObjectType[] { rootObject}, flat, sid);
}
public TEICorpusSerializer(final ObjectType[] rootObjects, final boolean flat, final String sid)
public TEICorpusSerializer(final ITextGridRep repository, final ObjectType[] rootObjects, final boolean flat, final String sid)
throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault {
this(rootObjects);
this(repository, rootObjects);
this.flat = flat;
this.setSid(sid);
}
......@@ -106,7 +106,7 @@ public void write(final OutputStream output) throws IOException, WebApplicationE
writer = outputFactory.createXMLEventWriter(output);
writer.setDefaultNamespace(TEI_NS);
writer.add(eventFactory.createStartDocument());
if (rootObjects.length > 1) {
writer.add(eventFactory.createStartElement("", TEI_NS, TEI_CORPUS));
writeContainerHeader(getTitle());
......@@ -150,8 +150,8 @@ protected void walkAggregation(final ObjectType object, final boolean again) {
level++;
if (stack == null)
try {
stack = new TEIHeaderStack(object, getSearchClient(isPublic(object)));
} catch (Exception e) {
stack = new TEIHeaderStack(getRepository(), object, getSearchClient(isPublic(object)));
} catch (final Exception e) {
throw Throwables.propagate(e);
}
else
......@@ -162,10 +162,10 @@ protected void walkAggregation(final ObjectType object, final boolean again) {
writer.add(eventFactory.createStartElement("", TEI_NS, TEI_CORPUS));
writer.add(eventFactory.createAttribute("corresp", object
.getGeneric().getGenerated().getTextgridUri().getValue()));
} catch (XMLStreamException e) {
} catch (final XMLStreamException e) {
throw new IllegalStateException(e);
}
// writeHeader(object);
stack.writeHeader(new StAXResult(new XMLEventFilter(writer) {
......@@ -184,7 +184,7 @@ public void add(final XMLEvent event) throws XMLStreamException {
if (!flat || level <= 1)
try {
writer.add(eventFactory.createEndElement("", TEI_NS, TEI_CORPUS));
} catch (XMLStreamException e) {
} catch (final XMLStreamException e) {
throw new IllegalStateException(e);
}
......@@ -201,7 +201,7 @@ private void writeHeader(final ObjectType object) throws XMLStreamException {
for (final String title : object.getGeneric().getProvided().getTitle())
writeSimpleTEIElement("title", title);
if (object.getEdition() != null)
for (AgentType agent : object.getEdition().getAgent()) {
for (final AgentType agent : object.getEdition().getAgent()) {
writer.add(eventFactory.createStartElement("", TEI_NS, "respStmt"));
writeSimpleTEIElement("resp", agent.getRole().toString());
writer.add(eventFactory.createStartElement("", TEI_NS, "author"));
......@@ -259,8 +259,8 @@ protected void walkXML(final ObjectType object) {
// skip to document node:
while (reader.hasNext() && !reader.peek().isStartElement())
reader.next();
ConfigurableXMLRewriter rewriter = new ConfigurableXMLRewriter(null, true);
final ConfigurableXMLRewriter rewriter = new ConfigurableXMLRewriter(null, true);
rewriter.setMergeLinkAdjuster(new ConfigurableXMLRewriter.DefaultMergeLinkAdjuster(URI.create(object.getGeneric().getGenerated().getTextgridUri().getValue())));
rewriter.configure(URI.create("internal:tei#tei"));
rewriter.rewrite(reader, writer);
......@@ -289,14 +289,14 @@ private void writeSkip(final ObjectType object) {
try {
writer.add(eventFactory.createComment(MessageFormat.format(
"Skipped {0}.", toString(object))));
} catch (XMLStreamException e) {
} catch (final XMLStreamException e) {
throw new IllegalStateException(e);
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
public void setTitle(final String title) {
this.title = title;
}
......
......@@ -16,7 +16,6 @@
import info.textgrid.namespaces.middleware.tgsearch.PathResponse;
import info.textgrid.namespaces.middleware.tgsearch.PathType;
import info.textgrid.services.aggregator.ITextGridRep;
import info.textgrid.services.aggregator.util.TextGridRepProvider;
import java.io.InputStream;
import java.util.List;
......@@ -49,22 +48,21 @@
/**
* Generates headers for every possible TEIcorpus level.
*
*
* Clients initially create a new TEIHeaderStack for the root instance. This
* class will then prepare a path request and initialize the stack. Clients can
* afterwards push() new objects onto the stack while they descend into the hierarchy,
* generate a header for the current stack, and pop() objects afterwards.
*
*
*/
public class TEIHeaderStack {
private final List<ObjectType> parents = Lists.newLinkedList();
private final ITextGridRep repository = TextGridRepProvider.getInstance();
private Templates stylesheet;
private static final Logger logger = Logger.getLogger(TEIHeaderStack.class.getCanonicalName());
public TEIHeaderStack(final ObjectType object, SearchClient searchClient) throws ObjectNotFoundFault,
public TEIHeaderStack(final ITextGridRep repository, final ObjectType object, final SearchClient searchClient) throws ObjectNotFoundFault,
MetadataParseFault, IoFault {
final String uri = object.getGeneric().getGenerated().getTextgridUri()
.getValue();
......@@ -75,34 +73,34 @@ public TEIHeaderStack(final ObjectType object, SearchClient searchClient) throws
for (final EntryType entry : path.getEntry())
if (!entry.getTextgridUri().equals(uri)) {
try {
MetadataContainerType container = repository.getCRUDService().readMetadata(searchClient.getSid(), null, entry.getTextgridUri());
final MetadataContainerType container = repository.getCRUDService().readMetadata(searchClient.getSid(), null, entry.getTextgridUri());
parents.add(0, container.getObject());
} catch (AuthFault e) {
} catch (final AuthFault e) {
logger.log(Level.WARNING, MessageFormat.format("Could not access ancestor {0} while building header for {1}: {2}. Skipping.", entry.getTextgridUri(), uri, e.getFaultInfo().getCause()));
}
}
}
}
}
public TEIHeaderStack(final String title) {
ObjectType object = new ObjectType();
GenericType generic = new GenericType();
ProvidedType provided = new ProvidedType();
GeneratedType generated = new GeneratedType();
TextgridUri textgridUri = new GeneratedType.TextgridUri();
final ObjectType object = new ObjectType();
final GenericType generic = new GenericType();
final ProvidedType provided = new ProvidedType();
final GeneratedType generated = new GeneratedType();
final TextgridUri textgridUri = new GeneratedType.TextgridUri();
textgridUri.setValue("textgrid:pseudo-object");
generated.setTextgridUri(textgridUri);
provided.setFormat("application/tg.pseudo-object");
provided.getTitle().add(title);
provided.setNotes("Generated using TextGridRep's basket");
generic.setProvided(provided);
generic.setGenerated(generated);
object.setGeneric(generic);
parents.add(object);
}
......
......@@ -13,7 +13,6 @@
import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService;
import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.tgcrudclient.TGCrudClientUtilities;
import info.textgrid.services.aggregator.ITextGridRep;
import info.textgrid.services.aggregator.ITextGridRep.TGOSupplier;
import java.io.IOException;
import java.io.InputStream;
......@@ -40,7 +39,7 @@ public class TextGridRepProvider implements ITextGridRep {
private ConfservClient confservClient;
private TGCrudService crud;
private SearchClient publicSearchClient;
private String CONF_ENDPOINT;
private String CONF_ENDPOINT;
private static final String DEFAULT_CONF_ENDPOINT = "https://www.textgridlab.org/1.0/confserv";
private static final Logger logger = Logger.getLogger(TextGridRepProvider.class.getCanonicalName());
......@@ -53,19 +52,15 @@ public TextGridRepProvider(final String endpoint) {
this.CONF_ENDPOINT = endpoint;
logger.info("Initialized a TextGridRepProvider with the endpoint " + endpoint);
}
private TextGridRepProvider() {
this(DEFAULT_CONF_ENDPOINT);
logger.warning("-- using default endpoint");
}
private static final ITextGridRep instance = new TextGridRepProvider();
private static ITextGridRep instance;
@Override
public String getConfValue(final String key) throws WebApplicationException {
return getConfig().get(key);
}
@Override
public Map<String, String> getConfig() {
if (config == null) {
try {
......@@ -91,7 +86,7 @@ public TGCrudService getCRUDService() {
if (crud == null) {
try {
crud = TGCrudClientUtilities.getTgcrud(getConfValue(ConfservClientConstants.TG_CRUD), true);
} catch (MalformedURLException e) {
} catch (final MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
......@@ -153,7 +148,7 @@ public InputStream getContent(final URI uri) throws ObjectNotFoundFault,
@Override
public SearchClient getPublicSearchClient() {
if (publicSearchClient == null) {
String endpoint = getConfValue(ConfservClientConstants.TG_SEARCH_PUBLIC);
final String endpoint = getConfValue(ConfservClientConstants.TG_SEARCH_PUBLIC);
publicSearchClient = new SearchClient(endpoint);
logger.info("Instantiated public search client at: " + endpoint);
}
......@@ -164,13 +159,13 @@ public SearchClient getPublicSearchClient() {
* Creates a search client for the non-public repository, readily
* initialized with the given session id. Note that this must be cached by
* the client.
*
*
* @param sid
* @return
*/
@Override
public SearchClient createPrivateSearchClient(final String sid) {
String confValue = getConfValue(ConfservClientConstants.TG_SEARCH);
final String confValue = getConfValue(ConfservClientConstants.TG_SEARCH);
final SearchClient endpoint = new SearchClient(confValue);
endpoint.setSid(sid);
logger.info("Created and configured a private search client at: " + endpoint);
......@@ -178,6 +173,9 @@ public SearchClient createPrivateSearchClient(final String sid) {
}
public static ITextGridRep getInstance() {
logger.warning("Someone wants the default instance -- I hope we're in a test.");
if (instance == null)
instance = new TextGridRepProvider(DEFAULT_CONF_ENDPOINT);
return instance;
}
......
......@@ -33,10 +33,10 @@ http://cxf.apache.org/schemas/jaxrs.xsd">
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"/>
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>
<bean id="stable-repo" class="info.textgrid.services.aggregator.util.TextGridRepProvider">
<bean id="stable-repo" class="info.textgrid.services.aggregator.util.TextGridRepProvider" scope="singleton">
<constructor-arg value="${aggregator.textgridrep.default}"/>
</bean>
<bean id="dev-repo" class="info.textgrid.services.aggregator.util.TextGridRepProvider">
<bean id="dev-repo" class="info.textgrid.services.aggregator.util.TextGridRepProvider" scope="singleton">
<constructor-arg value="${aggregator.textgridrep.dev}"/>
</bean>
......
......@@ -36,7 +36,7 @@ public void extracted() throws ObjectNotFoundFault, MetadataParseFault,
IoFault, AuthFault {
final ObjectType rootObject = repository.getCRUDService()
.readMetadata("", "", "textgrid:jfsn.0").getObject();
stack = new TEIHeaderStack(rootObject, repository.getPublicSearchClient());
stack = new TEIHeaderStack(repository, rootObject, repository.getPublicSearchClient());
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment