From 6c449bdcf7b51b73b8ca02f89c8da9eb90e016b1 Mon Sep 17 00:00:00 2001 From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de> Date: Mon, 14 Jan 2013 11:57:57 +0000 Subject: [PATCH] Error handling cleanup git-svn-id: https://develop.sub.uni-goettingen.de/repos/textgrid/trunk/services/aggregator@13729 7c539038-3410-0410-b1ec-0f2a7bf1c452 --- .../aggregator/teicorpus/TEICorpus.java | 58 +++++++------------ .../aggregator/teicorpus/TEICorpusTest.java | 14 +++-- 2 files changed, 31 insertions(+), 41 deletions(-) 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 e7552f0..9acade4 100644 --- a/src/main/java/info/textgrid/services/aggregator/teicorpus/TEICorpus.java +++ b/src/main/java/info/textgrid/services/aggregator/teicorpus/TEICorpus.java @@ -70,46 +70,32 @@ public Response get(@PathParam("aggregation") final URI uri, @QueryParam("attach") @DefaultValue("true") final boolean attach, @QueryParam("flat") @DefaultValue("false") final boolean flat, @QueryParam("sid") final String sid) - throws URISyntaxException { + throws URISyntaxException, ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault { logger.fine("TEIcorpus called for root aggregation: " + uri); final TGCrudService crud = repository.getCRUDService(); logger.finest("Yo, clients are there."); - try { - final MetadataContainerType rootAggregationMetadata = crud - .readMetadata(sid, null, uri.toString()); - logger.finer("CRUD request for root aggregation successful"); - final AggregationTreeWalker serializer = new TEICorpusSerializer( - rootAggregationMetadata.getObject(), flat, sid); - 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}); - 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()); - } - final ResponseBuilder builder = Response.ok(serializer, "text/xml"); - if (attach) { - final String fileName = rootAggregationMetadata.getObject() - .getGeneric().getProvided().getTitle().get(0) - + (flat ? ".flat" : "") + ".xml"; - RESTUtils.addAttachmentFilename(builder, fileName); - } - return builder.build(); - } catch (final ObjectNotFoundFault e) { - logger.log(Level.SEVERE, "CRUD: (Root aggregation) not found", e); - throw new WebApplicationException(e, javax.ws.rs.core.Response.status(Status.NOT_FOUND).entity(e.toString()).build()); - } catch (final MetadataParseFault e) { - logger.log(Level.SEVERE, "CRUD: Could not parse metadata", e); - throw new WebApplicationException(e); - } catch (final IoFault e) { - logger.log(Level.SEVERE, "CRUD: IO Fault", e); - throw new WebApplicationException(e); - } catch (final AuthFault e) { - logger.log(Level.SEVERE, "CRUD: (Root aggregation) AuthFault", e); - throw new WebApplicationException(e, Status.FORBIDDEN); + final MetadataContainerType rootAggregationMetadata = crud + .readMetadata(sid, null, uri.toString()); + logger.finer("CRUD request for root aggregation successful"); + final AggregationTreeWalker serializer = new TEICorpusSerializer( + rootAggregationMetadata.getObject(), flat, sid); + 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}); + 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()); } + final ResponseBuilder builder = Response.ok(serializer, "text/xml"); + if (attach) { + final String fileName = rootAggregationMetadata.getObject() + .getGeneric().getProvided().getTitle().get(0) + + (flat ? ".flat" : "") + ".xml"; + RESTUtils.addAttachmentFilename(builder, fileName); + } + return builder.build(); // final Response response = search.listAggregation(uri.toString()); // for (final ResultType result : response.getResult()) { diff --git a/src/test/java/info/textgrid/services/aggregator/teicorpus/TEICorpusTest.java b/src/test/java/info/textgrid/services/aggregator/teicorpus/TEICorpusTest.java index ddc4ef7..b162765 100644 --- a/src/test/java/info/textgrid/services/aggregator/teicorpus/TEICorpusTest.java +++ b/src/test/java/info/textgrid/services/aggregator/teicorpus/TEICorpusTest.java @@ -1,5 +1,9 @@ package info.textgrid.services.aggregator.teicorpus; +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.services.aggregator.TextGridRepProvider; import java.io.ByteArrayOutputStream; @@ -27,18 +31,18 @@ public void setUp() throws Exception { teiCorpus = new TEICorpus(TextGridRepProvider.getInstance()); } - @Test(expected = WebApplicationException.class) - public void testGet404() throws URISyntaxException { + @Test(expected = ObjectNotFoundFault.class) + public void testGet404() throws URISyntaxException, ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault { final Response response = teiCorpus.get(URI.create("textgrid:doesnotexist"), - true, false, null); + true, false, null); Assert.assertEquals(404, response.getStatus()); } @Test public void testGet() throws URISyntaxException, WebApplicationException, - IOException { + IOException, ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault { final Response response = teiCorpus.get(URI.create("textgrid:jmzg.0"), true, - false, null); + false, null); final Object entity = response.getEntity(); final ByteArrayOutputStream output = new ByteArrayOutputStream(); ((StreamingOutput) entity).write(output); -- GitLab