From e46cd083c6f36515f977fa124134642ac5aee853 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 7 Oct 2009 02:08:10 +0000 Subject: [PATCH] CSPACE-429: Additional minor code clean-up in the ID Service in preparation for work on CSPACE-497, adding type awareness. Continued selectively replacing inappropriate Java Exceptions with CSpace-defined Exception types. --- .../services/id/IDResource.java | 14 +++++----- .../services/id/IDService.java | 6 ++-- .../services/id/IDServiceJdbcImpl.java | 28 +++++++++++-------- .../services/id/UUIDGeneratorPart.java | 1 - .../id/test/IDServiceJdbcImplTest.java | 16 +++++------ .../id/test/StringIDGeneratorPartTest.java | 2 -- .../id/test/UUIDGeneratorPartTest.java | 2 -- 7 files changed, 36 insertions(+), 33 deletions(-) diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java index 39e6f6af0..251745c4b 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDResource.java @@ -23,9 +23,6 @@ package org.collectionspace.services.id; -import java.util.Collections; -import java.util.List; - import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -34,11 +31,10 @@ import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Marshaller; // May at some point instead use // org.jboss.resteasy.spi.NotFoundException +import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.common.repository.DocumentNotFoundException; import org.slf4j.Logger; @@ -139,7 +135,11 @@ public class IDResource { } catch (DocumentNotFoundException dnfe) { response = Response.status(Response.Status.NOT_FOUND) .entity(dnfe.getMessage()).type(MediaType.TEXT_PLAIN).build(); - + + } catch (BadRequestException bre) { + response = Response.status(Response.Status.BAD_REQUEST) + .entity(bre.getMessage()).type(MediaType.TEXT_PLAIN).build(); + } catch (IllegalStateException ise) { response = Response.status(Response.Status.BAD_REQUEST) .entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build(); @@ -206,7 +206,7 @@ public class IDResource { @Produces(MediaType.APPLICATION_XML) public Response readIDGenerator(@PathParam("csid") String csid) { - logger.debug("> in readIDGenerator(String)"); + logger.debug("> in readIDGenerator(String)"); Response response = null; response = response.ok().build(); diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java index 783c85ce9..05a41c049 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDService.java @@ -20,6 +20,7 @@ package org.collectionspace.services.id; // May at some point instead use // org.jboss.resteasy.spi.NotFoundException import org.collectionspace.services.common.repository.DocumentNotFoundException; +import org.collectionspace.services.common.repository.BadRequestException; /** @@ -43,7 +44,7 @@ public interface IDService { // Generates and returns a new ID from the specified ID generator. public String createID(String csid) throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException; + BadRequestException, IllegalArgumentException, IllegalStateException; // Returns the last-generated ID associated with the specified ID generator. public String readLastID(String csid) @@ -69,7 +70,8 @@ public interface IDService { // Update (may need to check for changes in the ID generator structure) public void updateIDGenerator(String csid, String serializedIDGenerator) - throws IllegalArgumentException, IllegalStateException; + throws DocumentNotFoundException, BadRequestException, + IllegalArgumentException, IllegalStateException; // Delete (possibly not permitted - deactivate instead?) diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java b/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java index d5ac20554..88ea343b2 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/IDServiceJdbcImpl.java @@ -86,6 +86,7 @@ import java.sql.Statement; // May at some point instead use // org.jboss.resteasy.spi.NotFoundException +import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.common.repository.DocumentNotFoundException; import org.slf4j.Logger; @@ -190,7 +191,7 @@ public class IDServiceJdbcImpl implements IDService { */ @Override public String createID(String csid) throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException { + BadRequestException, IllegalArgumentException, IllegalStateException { logger.debug("> in createID"); @@ -220,7 +221,7 @@ public class IDServiceJdbcImpl implements IDService { // Guard code - should not be needed. if (serializedGenerator == null || serializedGenerator.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "ID generator " + "\'" + csid + "\'" + " could not be found."); } @@ -555,9 +556,10 @@ public class IDServiceJdbcImpl implements IDService { * * @throws IllegalStateException if a storage-related error occurred. */ + @Override public String readIDGenerator (String csid) throws DocumentNotFoundException, IllegalArgumentException, - IllegalStateException { + IllegalStateException { logger.debug("> in readIDGenerator"); @@ -618,14 +620,14 @@ public class IDServiceJdbcImpl implements IDService { * @throws IllegalStateException if a storage-related error occurred. */ public void updateIDGenerator(String csid, SettableIDGenerator generator) - throws IllegalArgumentException, IllegalStateException { + throws BadRequestException, IllegalArgumentException, IllegalStateException, DocumentNotFoundException { logger.debug("> in updateIDGenerator(String, SettableIDGenerator)"); // @TODO: Add checks for authorization to perform this operation. if (generator == null) { - throw new IllegalArgumentException( + throw new BadRequestException( "ID generator provided in update operation cannot be null."); } @@ -638,7 +640,9 @@ public class IDServiceJdbcImpl implements IDService { try { updateIDGenerator(csid, serializedGenerator); - } catch (IllegalArgumentException e ) { + } catch (DocumentNotFoundException e ) { + throw e; + } catch (BadRequestException e ) { throw e; } catch (IllegalStateException e ) { throw e; @@ -665,14 +669,15 @@ public class IDServiceJdbcImpl implements IDService { */ @Override public void updateIDGenerator(String csid, String serializedGenerator) - throws IllegalArgumentException, IllegalStateException { + throws DocumentNotFoundException, BadRequestException, + IllegalArgumentException, IllegalStateException { logger.debug("> in updateIDGenerator(String, String)"); // @TODO: Add checks for authorization to perform this operation. if (serializedGenerator == null || serializedGenerator.equals("")) { - throw new IllegalArgumentException( + throw new BadRequestException( "Could not understand or parse this representation of an ID generator."); } @@ -728,7 +733,7 @@ public class IDServiceJdbcImpl implements IDService { // Otherwise, throw an exception, which indicates that the requested // ID generator was not found. } else { - throw new IllegalArgumentException( + throw new DocumentNotFoundException( "Error updating ID generator '" + csid + "': generator could not be found in the database."); } // end if (idGeneratorFound) @@ -759,7 +764,8 @@ public class IDServiceJdbcImpl implements IDService { * @throws IllegalStateException if a storage-related error occurred. */ public void deleteIDGenerator(String csid) - throws IllegalArgumentException, IllegalStateException { + throws DocumentNotFoundException, IllegalArgumentException, + IllegalStateException { logger.debug("> in deleteIDGenerator"); @@ -803,7 +809,7 @@ public class IDServiceJdbcImpl implements IDService { // Otherwise, throw an exception, which indicates that the requested // ID generator was not found. } else { - throw new IllegalArgumentException( + throw new DocumentNotFoundException( "Error deleting ID generator '" + csid + "': generator could not be found in the database."); } // end if (idGeneratorFound) diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java b/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java index 49ad98a2f..83ec2c7a2 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/UUIDGeneratorPart.java @@ -26,7 +26,6 @@ package org.collectionspace.services.id; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; /** * UUIDGeneratorPart diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java index dc9f1ed64..c786ab360 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/IDServiceJdbcImplTest.java @@ -23,8 +23,7 @@ package org.collectionspace.services.id.test; -// May at some point instead use -// org.jboss.resteasy.spi.NotFoundException +import org.collectionspace.services.common.repository.BadRequestException; import org.collectionspace.services.common.repository.DocumentNotFoundException; import org.collectionspace.services.id.*; @@ -89,7 +88,7 @@ public class IDServiceJdbcImplTest { @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator", "readIDGenerator"}) public void updateIDGenerator() throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException { + BadRequestException, IllegalArgumentException, IllegalStateException { final String NEW_DESCRIPTION = "new description"; @@ -99,9 +98,9 @@ public class IDServiceJdbcImplTest { generator = IDGeneratorSerializer.deserialize(serializedGenerator); generator.setDescription(NEW_DESCRIPTION); serializedGenerator = IDGeneratorSerializer.serialize(generator); - + jdbc.updateIDGenerator(DEFAULT_CSID, serializedGenerator); - + serializedGenerator = jdbc.readIDGenerator(DEFAULT_CSID); generator = IDGeneratorSerializer.deserialize(serializedGenerator); @@ -111,14 +110,15 @@ public class IDServiceJdbcImplTest { @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator", "readIDGenerator", "updateIDGenerator"}) - public void deleteIDGenerator() { + public void deleteIDGenerator() throws DocumentNotFoundException { jdbc.deleteIDGenerator(DEFAULT_CSID); } @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator", "readIDGenerator", "updateIDGenerator", "deleteIDGenerator"}) public void createID() throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException { + BadRequestException, IllegalArgumentException, + IllegalStateException { try { jdbc.deleteIDGenerator(DEFAULT_CSID); @@ -162,7 +162,7 @@ public class IDServiceJdbcImplTest { @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createID"}, expectedExceptions = DocumentNotFoundException.class) public void createIDNonExistentGenerator() throws DocumentNotFoundException, - IllegalArgumentException, IllegalStateException { + BadRequestException, IllegalArgumentException, IllegalStateException { nextId = service.createID("non-existent identifier"); } diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java index e899b2c26..5cdbe788b 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/StringIDGeneratorPartTest.java @@ -26,8 +26,6 @@ package org.collectionspace.services.id.test; import junit.framework.TestCase; import org.collectionspace.services.id.StoredValueIDGeneratorPart; import org.collectionspace.services.id.StringIDGeneratorPart; -import org.collectionspace.services.id.StoredValueIDGeneratorPart; -import org.collectionspace.services.id.StringIDGeneratorPart; /** * StringIDGeneratorPartTest diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/test/UUIDGeneratorPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/test/UUIDGeneratorPartTest.java index 8376a37ad..e2dc50ff0 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/test/UUIDGeneratorPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/test/UUIDGeneratorPartTest.java @@ -24,8 +24,6 @@ package org.collectionspace.services.id.test; import junit.framework.TestCase; -import org.collectionspace.services.id.IDGeneratorPart; -import org.collectionspace.services.id.UUIDGeneratorPart; import org.collectionspace.services.id.IDGeneratorPart; import org.collectionspace.services.id.UUIDGeneratorPart; -- 2.47.3