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

Added session ID handling to TEIcorpus and EPUB export

parent 0439b6aa
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,10 @@ public abstract class AggregationTreeWalker { ...@@ -15,6 +15,10 @@ public abstract class AggregationTreeWalker {
protected final Set<String> seen = Sets.newHashSet(); protected final Set<String> seen = Sets.newHashSet();
private String sid;
private SearchClient privateSearchClient;
/** /**
* Process the given object. * Process the given object.
* <p> * <p>
...@@ -85,7 +89,7 @@ protected void walkAggregation(final ObjectType aggregation, ...@@ -85,7 +89,7 @@ protected void walkAggregation(final ObjectType aggregation,
throw new IllegalStateException( throw new IllegalStateException(
"AggregationTreeWalkers must be initialized with a repository before use."); "AggregationTreeWalkers must be initialized with a repository before use.");
final Response response = getRepository().getPublicSearchClient() final Response response = getSearchClient(isPublic(aggregation))
.listAggregation( .listAggregation(
aggregation.getGeneric().getGenerated() aggregation.getGeneric().getGenerated()
.getTextgridUri().getValue()); .getTextgridUri().getValue());
...@@ -100,6 +104,26 @@ protected void walkAggregation(final ObjectType aggregation, ...@@ -100,6 +104,26 @@ protected void walkAggregation(final ObjectType aggregation,
} }
/**
* Returns whether the object identified by the argument is public.
*/
protected static boolean isPublic(final ObjectType object) {
final String availability = object.getGeneric().getGenerated()
.getAvailability();
return (availability != null && availability.contains("public"));
}
protected SearchClient getSearchClient(final boolean isPublic) {
if (isPublic)
return getRepository().getPublicSearchClient();
else {
if (privateSearchClient == null)
privateSearchClient = getRepository()
.createPrivateSearchClient(getSid());
return privateSearchClient;
}
}
/** /**
* Called by {@link #walkAggregation(ObjectType, boolean)} when the * Called by {@link #walkAggregation(ObjectType, boolean)} when the
* aggregation contains entries for which there is no metadata included. * aggregation contains entries for which there is no metadata included.
...@@ -119,4 +143,13 @@ public ITextGridRep getRepository() { ...@@ -119,4 +143,13 @@ public ITextGridRep getRepository() {
public void setRepository(final ITextGridRep repository) { public void setRepository(final ITextGridRep repository) {
this.repository = repository; this.repository = repository;
} }
public String getSid() {
return sid;
}
public void setSid(final String sid) {
this.sid = sid;
this.privateSearchClient = null;
}
} }
...@@ -34,6 +34,8 @@ public abstract InputStream getContent(final URI uri, String sid) ...@@ -34,6 +34,8 @@ public abstract InputStream getContent(final URI uri, String sid)
public abstract TGOSupplier<InputStream> read(final URI uri, final String sid) public abstract TGOSupplier<InputStream> read(final URI uri, final String sid)
throws WebApplicationException; throws WebApplicationException;
public abstract SearchClient createPrivateSearchClient(final String sid);
public interface TGOSupplier<T> extends InputSupplier<T> { public interface TGOSupplier<T> extends InputSupplier<T> {
public ObjectType getMetadata(); public ObjectType getMetadata();
} }
......
...@@ -37,6 +37,7 @@ public class TextGridRepProvider implements ITextGridRep { ...@@ -37,6 +37,7 @@ public class TextGridRepProvider implements ITextGridRep {
private TGCrudService crud; private TGCrudService crud;
private SearchClient publicSearchClient; private SearchClient publicSearchClient;
private String CONF_ENDPOINT = "https://www.textgridlab.org/1.0/confserv"; private String CONF_ENDPOINT = "https://www.textgridlab.org/1.0/confserv";
public String getCONF_ENDPOINT() { public String getCONF_ENDPOINT() {
return CONF_ENDPOINT; return CONF_ENDPOINT;
} }
...@@ -66,19 +67,25 @@ public String getConfValue(final String key) throws WebApplicationException { ...@@ -66,19 +67,25 @@ public String getConfValue(final String key) throws WebApplicationException {
@Override @Override
public TGCrudService getCRUDService() { public TGCrudService getCRUDService() {
if (crud == null) { if (crud == null) {
final URL wsdl = TGCrudService_Service.class.getResource("/wsdl/TGCrudService.wsdl"); final URL wsdl = TGCrudService_Service.class
final TGCrudService_Service tgCrudService_Service = new TGCrudService_Service(wsdl); .getResource("/wsdl/TGCrudService.wsdl");
final TGCrudService _crud = tgCrudService_Service.getTGCrudPort(new MTOMFeature()); final TGCrudService_Service tgCrudService_Service = new TGCrudService_Service(
wsdl);
final TGCrudService _crud = tgCrudService_Service
.getTGCrudPort(new MTOMFeature());
final BindingProvider bp = (BindingProvider) _crud; final BindingProvider bp = (BindingProvider) _crud;
final Map<String, Object> requestContext = bp.getRequestContext(); final Map<String, Object> requestContext = bp.getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, getConfValue(ConfservClientConstants.TG_CRUD)); requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
getConfValue(ConfservClientConstants.TG_CRUD));
crud = _crud; crud = _crud;
} }
return crud; return crud;
} }
@Override @Override
public InputStream getContent(final URI uri, final String sid) throws ObjectNotFoundFault, MetadataParseFault, IoFault, ProtocolNotImplementedFault, AuthFault, IOException { public InputStream getContent(final URI uri, final String sid)
throws ObjectNotFoundFault, MetadataParseFault, IoFault,
ProtocolNotImplementedFault, AuthFault, IOException {
final TGCrudService crudService = getCRUDService(); final TGCrudService crudService = getCRUDService();
final Holder<MetadataContainerType> mdHolder = new Holder<MetadataContainerType>(); final Holder<MetadataContainerType> mdHolder = new Holder<MetadataContainerType>();
final Holder<DataHandler> dHolder = new Holder<DataHandler>(); final Holder<DataHandler> dHolder = new Holder<DataHandler>();
...@@ -87,14 +94,14 @@ public InputStream getContent(final URI uri, final String sid) throws ObjectNotF ...@@ -87,14 +94,14 @@ public InputStream getContent(final URI uri, final String sid) throws ObjectNotF
} }
@Override @Override
public TGOSupplier<InputStream> read(final URI uri, final String sid) throws WebApplicationException { public TGOSupplier<InputStream> read(final URI uri, final String sid)
throws WebApplicationException {
final TGCrudService crudService = getCRUDService(); final TGCrudService crudService = getCRUDService();
final Holder<MetadataContainerType> mdHolder = new Holder<MetadataContainerType>(); final Holder<MetadataContainerType> mdHolder = new Holder<MetadataContainerType>();
final Holder<DataHandler> dHolder = new Holder<DataHandler>(); final Holder<DataHandler> dHolder = new Holder<DataHandler>();
try { try {
crudService.read(sid, null, uri.toString(), mdHolder, dHolder); crudService.read(sid, null, uri.toString(), mdHolder, dHolder);
return new TGOSupplier<InputStream>() { return new TGOSupplier<InputStream>() {
@Override @Override
...@@ -121,17 +128,36 @@ public ObjectType getMetadata() { ...@@ -121,17 +128,36 @@ public ObjectType getMetadata() {
} }
@Override @Override
public InputStream getContent(final URI uri) throws ObjectNotFoundFault, MetadataParseFault, IoFault, ProtocolNotImplementedFault, AuthFault, IOException { public InputStream getContent(final URI uri) throws ObjectNotFoundFault,
MetadataParseFault, IoFault, ProtocolNotImplementedFault,
AuthFault, IOException {
return getContent(uri, null); return getContent(uri, null);
} }
@Override @Override
public SearchClient getPublicSearchClient() { public SearchClient getPublicSearchClient() {
if (publicSearchClient == null) if (publicSearchClient == null)
publicSearchClient = new SearchClient(getConfValue(ConfservClientConstants.TG_SEARCH_PUBLIC)); publicSearchClient = new SearchClient(
getConfValue(ConfservClientConstants.TG_SEARCH_PUBLIC));
return publicSearchClient; return publicSearchClient;
} }
/**
* 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) {
final SearchClient client = new SearchClient(
getConfValue(ConfservClientConstants.TG_SEARCH));
client.setSid(sid);
return client;
}
public static ITextGridRep getInstance() { public static ITextGridRep getInstance() {
return instance; return instance;
} }
......
...@@ -68,13 +68,14 @@ public EPUB(final ITextGridRep repository) { ...@@ -68,13 +68,14 @@ public EPUB(final ITextGridRep repository) {
@Path(value = "/{object}") @Path(value = "/{object}")
@Produces(value = "application/epub+zip") @Produces(value = "application/epub+zip")
public Response get(@PathParam("object") final URI uri, public Response get(@PathParam("object") final URI uri,
@QueryParam("stylesheet") final URI xsluri) @QueryParam("stylesheet") final URI xsluri,
@QueryParam("sid") final String sid)
throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault, throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault,
ProtocolNotImplementedFault { ProtocolNotImplementedFault {
logger.fine("EPUB called for root object: " + uri); logger.fine("EPUB called for root object: " + uri);
final TGCrudService crud = repository.getCRUDService(); final TGCrudService crud = repository.getCRUDService();
try { try {
final MetadataContainerType container = crud.readMetadata(null, final MetadataContainerType container = crud.readMetadata(sid,
null, uri.toString()); null, uri.toString());
final ObjectType rootObject = container.getObject(); final ObjectType rootObject = container.getObject();
final String mimeType = rootObject.getGeneric().getProvided() final String mimeType = rootObject.getGeneric().getProvided()
...@@ -95,7 +96,7 @@ public Response get(@PathParam("object") final URI uri, ...@@ -95,7 +96,7 @@ public Response get(@PathParam("object") final URI uri,
if (aggregation) { if (aggregation) {
// First, use the aggregator to create a TEI corpus file to build on // First, use the aggregator to create a TEI corpus file to build on
final TEICorpusSerializer corpusSerializer = new TEICorpusSerializer( final TEICorpusSerializer corpusSerializer = new TEICorpusSerializer(
rootObject, true); rootObject, true, sid);
final FileOutputStream corpusOutput = new FileOutputStream(corpus); final FileOutputStream corpusOutput = new FileOutputStream(corpus);
corpusSerializer.write(corpusOutput); corpusSerializer.write(corpusOutput);
corpusOutput.close(); corpusOutput.close();
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.MetadataParseFault; 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.ObjectNotFoundFault;
import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService; import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.TGCrudService;
import info.textgrid.services.aggregator.AggregationTreeWalker;
import info.textgrid.services.aggregator.ITextGridRep; import info.textgrid.services.aggregator.ITextGridRep;
import info.textgrid.services.aggregator.RESTUtils; import info.textgrid.services.aggregator.RESTUtils;
...@@ -67,16 +68,18 @@ public TEICorpus(final ITextGridRep repository) { ...@@ -67,16 +68,18 @@ public TEICorpus(final ITextGridRep repository) {
@Produces("text/xml") @Produces("text/xml")
public Response get(@PathParam("aggregation") final URI uri, public Response get(@PathParam("aggregation") final URI uri,
@QueryParam("attach") @DefaultValue("true") final boolean attach, @QueryParam("attach") @DefaultValue("true") final boolean attach,
@QueryParam("flat") @DefaultValue("false") final boolean flat) @QueryParam("flat") @DefaultValue("false") final boolean flat,
@QueryParam("sid") final String sid)
throws URISyntaxException { throws URISyntaxException {
logger.fine("TEIcorpus called for root aggregation: " + uri); logger.fine("TEIcorpus called for root aggregation: " + uri);
final TGCrudService crud = repository.getCRUDService(); final TGCrudService crud = repository.getCRUDService();
logger.finest("Yo, clients are there."); logger.finest("Yo, clients are there.");
try { try {
final MetadataContainerType rootAggregationMetadata = crud.readMetadata(null, null, uri.toString()); final MetadataContainerType rootAggregationMetadata = crud
.readMetadata(sid, null, uri.toString());
logger.finer("CRUD request for root aggregation successful"); logger.finer("CRUD request for root aggregation successful");
final TEICorpusSerializer serializer = new TEICorpusSerializer( final AggregationTreeWalker serializer = new TEICorpusSerializer(
rootAggregationMetadata.getObject(), flat); rootAggregationMetadata.getObject(), flat, sid);
final String format = rootAggregationMetadata.getObject().getGeneric().getProvided().getFormat(); final String format = rootAggregationMetadata.getObject().getGeneric().getProvided().getFormat();
if (!format.contains("aggregation")) { 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}); logger.log(Level.SEVERE, "The requested object {0} is a {1}, not an aggregation, and thus cannot be aggregated.", new Object[] {uri,format});
......
...@@ -61,7 +61,6 @@ public boolean accept(final XMLEvent event) { ...@@ -61,7 +61,6 @@ public boolean accept(final XMLEvent event) {
private boolean handled; private boolean handled;
private boolean flat; private boolean flat;
private int level = 0; private int level = 0;
private static String toString(final ObjectType object) { private static String toString(final ObjectType object) {
return Joiner.on(" / ").join(object.getGeneric().getProvided().getTitle()) + " (" + object.getGeneric().getGenerated().getTextgridUri().getValue() + ", " + return Joiner.on(" / ").join(object.getGeneric().getProvided().getTitle()) + " (" + object.getGeneric().getGenerated().getTextgridUri().getValue() + ", " +
object.getGeneric().getProvided().getFormat() + ")"; object.getGeneric().getProvided().getFormat() + ")";
...@@ -74,10 +73,12 @@ public TEICorpusSerializer(final ObjectType object) ...@@ -74,10 +73,12 @@ public TEICorpusSerializer(final ObjectType object)
logger.info("Starting TEIcorpus serialization for " + toString(object)); logger.info("Starting TEIcorpus serialization for " + toString(object));
} }
public TEICorpusSerializer(final ObjectType object, final boolean flat) public TEICorpusSerializer(final ObjectType object, final boolean flat,
final String sid)
throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault { throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault {
this(object); this(object);
this.flat = flat; this.flat = flat;
this.setSid(sid);
} }
@Override @Override
...@@ -216,7 +217,7 @@ protected void walkXML(final ObjectType object) { ...@@ -216,7 +217,7 @@ protected void walkXML(final ObjectType object) {
final Holder<DataHandler> dhHolder = new Holder<DataHandler>(); final Holder<DataHandler> dhHolder = new Holder<DataHandler>();
try { try {
getRepository().getCRUDService().read( getRepository().getCRUDService().read(
"", getSid(),
"", "",
object.getGeneric().getGenerated().getTextgridUri() object.getGeneric().getGenerated().getTextgridUri()
.getValue(), mdHolder, .getValue(), mdHolder,
......
...@@ -19,7 +19,8 @@ public class EPUBTest { ...@@ -19,7 +19,8 @@ public class EPUBTest {
public void testGet() throws ObjectNotFoundFault, MetadataParseFault, public void testGet() throws ObjectNotFoundFault, MetadataParseFault,
IoFault, AuthFault, ProtocolNotImplementedFault { IoFault, AuthFault, ProtocolNotImplementedFault {
final EPUB epub = new EPUB(TextGridRepProvider.getInstance()); final EPUB epub = new EPUB(TextGridRepProvider.getInstance());
final Response response = epub.get(URI.create("textgrid:jfst.0"), null); final Response response = epub.get(URI.create("textgrid:jfst.0"), null,
null);
} }
} }
package info.textgrid.services.aggregator.teicorpus; package info.textgrid.services.aggregator.teicorpus;
import info.textgrid.services.aggregator.TextGridRepProvider; import info.textgrid.services.aggregator.TextGridRepProvider;
import info.textgrid.services.aggregator.teicorpus.TEICorpus;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
...@@ -31,7 +30,7 @@ public void setUp() throws Exception { ...@@ -31,7 +30,7 @@ public void setUp() throws Exception {
@Test(expected = WebApplicationException.class) @Test(expected = WebApplicationException.class)
public void testGet404() throws URISyntaxException { public void testGet404() throws URISyntaxException {
final Response response = teiCorpus.get(URI.create("textgrid:doesnotexist"), final Response response = teiCorpus.get(URI.create("textgrid:doesnotexist"),
true, false); true, false, null);
Assert.assertEquals(404, response.getStatus()); Assert.assertEquals(404, response.getStatus());
} }
...@@ -39,7 +38,7 @@ public void testGet404() throws URISyntaxException { ...@@ -39,7 +38,7 @@ public void testGet404() throws URISyntaxException {
public void testGet() throws URISyntaxException, WebApplicationException, public void testGet() throws URISyntaxException, WebApplicationException,
IOException { IOException {
final Response response = teiCorpus.get(URI.create("textgrid:jmzg.0"), true, final Response response = teiCorpus.get(URI.create("textgrid:jmzg.0"), true,
false); false, null);
final Object entity = response.getEntity(); final Object entity = response.getEntity();
final ByteArrayOutputStream output = new ByteArrayOutputStream(); final ByteArrayOutputStream output = new ByteArrayOutputStream();
((StreamingOutput) entity).write(output); ((StreamingOutput) entity).write(output);
......
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