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

Actually return a ZIP.

This version does not have any fancy features yet, i.e. neither link
rewriting nor cool file names

git-svn-id: https://develop.sub.uni-goettingen.de/repos/textgrid/trunk/services/aggregator@14098 7c539038-3410-0410-b1ec-0f2a7bf1c452
parent 2b4480b7
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
public class AggregationEntry { public class AggregationEntry {
private static Logger logger = Logger.getLogger("info.textgrid.services.aggregator.tree"); private static Logger logger = Logger.getLogger("info.textgrid.services.aggregator.tree");
private static URI baseURI = new File(".").toURI().normalize();
private final ObjectType metadata; private final ObjectType metadata;
...@@ -70,10 +72,10 @@ protected String getSimpleFileName(final boolean withExtension) { ...@@ -70,10 +72,10 @@ protected String getSimpleFileName(final boolean withExtension) {
* if true, append a content type specific extension to the last * if true, append a content type specific extension to the last
* component of the path. * component of the path.
*/ */
protected File getFileName(final boolean withExtension) { protected File getFile(final boolean withExtension) {
Optional<Aggregation> parent = getParent(); Optional<Aggregation> parent = getParent();
if (parent.isPresent()) if (parent.isPresent())
return new File(parent.get().getFileName(false), return new File(parent.get().getFile(false),
getSimpleFileName(withExtension)); getSimpleFileName(withExtension));
else else
return new File(getSimpleFileName(withExtension)); return new File(getSimpleFileName(withExtension));
...@@ -82,10 +84,11 @@ protected File getFileName(final boolean withExtension) { ...@@ -82,10 +84,11 @@ protected File getFileName(final boolean withExtension) {
/** /**
* Returns a {@link File} representing the abstract file name for this * Returns a {@link File} representing the abstract file name for this
* aggregation entry from the root of the aggregation tree. Equivalent to * aggregation entry from the root of the aggregation tree. Equivalent to
* {@code {@linkplain #getFileName(boolean) getFileName(true)}}. * {@code {@linkplain #getFile(boolean) getFileName(true)}}.
* @param withExtension TODO
*/ */
public File getFileName() { public String getFileName(boolean withExtension) {
return getFileName(true); return baseURI.relativize(getFile(true).toURI()).toString();
} }
@Override @Override
...@@ -100,4 +103,8 @@ public String toString() { ...@@ -100,4 +103,8 @@ public String toString() {
} }
} }
public URI getTextGridURI() {
return URI.create(getMetadata().getGeneric().getGenerated().getTextgridUri().getValue());
}
} }
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
import info.textgrid.services.aggregator.tree.AggregationTreeFactory; import info.textgrid.services.aggregator.tree.AggregationTreeFactory;
import java.net.URI; import java.net.URI;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
...@@ -21,8 +23,11 @@ ...@@ -21,8 +23,11 @@
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;
import org.apache.cxf.jaxrs.model.wadl.Description; import org.apache.cxf.jaxrs.model.wadl.Description;
...@@ -40,8 +45,8 @@ public ZIP(final ITextGridRep repository) { ...@@ -40,8 +45,8 @@ public ZIP(final ITextGridRep repository) {
@GET @GET
@Path(value = "/{object}") @Path(value = "/{object}")
@Produces(value = "text/html") @Produces(value = "application/zip")
public Response get( public StreamingOutput get(
@Description("The TextGrid URI of the TEI document or aggregation to transform") @Description("The TextGrid URI of the TEI document or aggregation to transform")
@PathParam("object") final URI uri, @PathParam("object") final URI uri,
@Description("Session ID to access protected resources") @Description("Session ID to access protected resources")
...@@ -51,21 +56,26 @@ public Response get( ...@@ -51,21 +56,26 @@ public Response get(
final MetadataContainerType container = crud.readMetadata(sid, null, uri.toString()); final MetadataContainerType container = crud.readMetadata(sid, null, uri.toString());
final ObjectType rootObject = container.getObject(); final ObjectType rootObject = container.getObject();
Aggregation tree = AggregationTreeFactory.create(rootObject, repository, sid); final String format = rootObject.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});
throw new WebApplicationException(javax.ws.rs.core.Response
.status(Status.BAD_REQUEST)
.entity(MessageFormat.format(
"The requested object {0} is a {1}, not an aggregation, and thus cannot be aggregated.", uri,
format)).build());
}
StringBuilder output = new StringBuilder("Following stuff (+meta) would be zipped:\n\n"); return new ZipResult(rootObject, repository, sid);
appendLS(tree, output);
return Response.ok().entity(output.toString()).type("text/plain").build();
} }
private void appendLS(Aggregation aggregation, StringBuilder output) { private void appendLS(Aggregation aggregation, StringBuilder output) {
output.append(aggregation.getFileName()).append(":\n"); output.append(aggregation.getFileName(true)).append(":\n");
for(AggregationEntry child : aggregation.getChildren()) for(AggregationEntry child : aggregation.getChildren())
if (child instanceof Aggregation) if (child instanceof Aggregation)
appendLS((Aggregation) child, output); appendLS((Aggregation) child, output);
else else
output.append(' ').append(child.getFileName()).append('\n'); output.append(' ').append(child.getFileName(true)).append('\n');
} }
} }
package info.textgrid.services.aggregator.zip;
import info.textgrid.namespaces.metadata.core._2010.ObjectType;
import info.textgrid.services.aggregator.ITextGridRep;
import info.textgrid.services.aggregator.tree.Aggregation;
import info.textgrid.services.aggregator.tree.AggregationEntry;
import info.textgrid.services.aggregator.tree.AggregationTreeFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.bind.JAXB;
import com.google.common.base.Throwables;
import com.google.common.io.ByteStreams;
public class ZipResult implements StreamingOutput {
private final ObjectType rootMetadata;
private final ITextGridRep repository;
private final String sid;
public ZipResult(ObjectType rootObject, ITextGridRep repository, String sid) {
this.rootMetadata = rootObject;
this.repository = repository;
this.sid = sid;
}
@Override
public void write(OutputStream output) throws IOException,
WebApplicationException {
ZipOutputStream zip = new ZipOutputStream(output);
zip.setComment(MessageFormat.format("# Exported from TextGrid -- www.textgrid.de\nRoot aggregation: {0}", rootMetadata.getGeneric().getGenerated().getTextgridUri().getValue()));
Aggregation root = AggregationTreeFactory.create(rootMetadata, repository, sid);
writeAggregation(zip, root);
zip.close();
}
private void writeAggregation(ZipOutputStream zip, Aggregation root) throws IOException {
writeFile(zip, root);
zip.putNextEntry(new ZipEntry(root.getFileName(false).concat("/")));
zip.closeEntry();
for (AggregationEntry child : root.getChildren()) {
if (child instanceof Aggregation)
writeAggregation(zip, (Aggregation) child);
else
writeFile(zip, child);
}
}
private void writeFile(ZipOutputStream zip, AggregationEntry aggregationEntry) throws IOException {
writeMetadata(zip, aggregationEntry);
zip.putNextEntry(new ZipEntry(aggregationEntry.getFileName(true)));
try {
InputStream content = repository.getContent(aggregationEntry.getTextGridURI(), sid);
ByteStreams.copy(content, zip);
zip.closeEntry();
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw new WebApplicationException(e);
}
}
private void writeMetadata(ZipOutputStream zip, AggregationEntry aggregationEntry) throws IOException {
zip.putNextEntry(new ZipEntry(aggregationEntry.getFileName(true).concat(".meta")));
JAXB.marshal(aggregationEntry.getMetadata(), zip);
zip.closeEntry();
}
}
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