From: Aron Roberts Date: Thu, 3 May 2012 21:58:45 +0000 (-0700) Subject: CSPACE-3718,CSPACE-5119: PersonValidatorHandler now extends ValidatorHandlerImpl... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=482b380a096900ad717274ebf0cc1a6c5170cc08;p=tmp%2Fjakarta-migration.git CSPACE-3718,CSPACE-5119: PersonValidatorHandler now extends ValidatorHandlerImpl. (Also performed automated reformatting of source code in the latter class for readability.) --- diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/ValidatorHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/common/document/ValidatorHandlerImpl.java index 7be27b618..eff2255e5 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/ValidatorHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/ValidatorHandlerImpl.java @@ -1,6 +1,5 @@ package org.collectionspace.services.common.document; - import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentHandler.Action; @@ -12,140 +11,142 @@ import org.slf4j.LoggerFactory; * The Class ValidatorHandlerImpl. */ public abstract class ValidatorHandlerImpl implements ValidatorHandler { - - /** The logger. */ + + /** + * The logger. + */ private final Logger logger = LoggerFactory.getLogger(ValidatorHandlerImpl.class); - private ServiceContext ctx; - + protected ServiceContext getServiceContext() { - return ctx; + return ctx; } - // gets reset by calls to setServiceContext() method protected boolean enforceAsserts = true; - + public boolean getEnforceAsserts() { - return enforceAsserts; - } - - public void setEnforceAsserts(ServiceContext ctx) { - Boolean disableAssertsAttr = ctx.getServiceBinding().isDisableAsserts(); - if (disableAssertsAttr == null) { - enforceAsserts = true; - } else { - enforceAsserts = !disableAssertsAttr.booleanValue(); - } - } - - protected void setServiceContext(ServiceContext ctx) { - this.ctx = ctx; - + return enforceAsserts; } - + + public void setEnforceAsserts(ServiceContext ctx) { + Boolean disableAssertsAttr = ctx.getServiceBinding().isDisableAsserts(); + if (disableAssertsAttr == null) { + enforceAsserts = true; + } else { + enforceAsserts = !disableAssertsAttr.booleanValue(); + } + } + + protected void setServiceContext(ServiceContext ctx) { + this.ctx = ctx; + + } + protected void CS_ASSERT(boolean expression, String errorMsg) throws AssertionError { - if (expression != true) { - if (errorMsg == null) { - errorMsg = "Validation exception occurred in: " + - this.getClass().getName(); - } - throw new AssertionError(errorMsg); - } + if (expression != true) { + if (errorMsg == null) { + errorMsg = "Validation exception occurred in: " + + this.getClass().getName(); + } + throw new AssertionError(errorMsg); + } } - + protected void CS_ASSERT(boolean expression) throws AssertionError { - CS_ASSERT(expression, null); + CS_ASSERT(expression, null); } - + private void init(ServiceContext ctx) { - setEnforceAsserts(ctx); - setServiceContext(ctx); + setEnforceAsserts(ctx); + setServiceContext(ctx); } - - /* (non-Javadoc) - * @see org.collectionspace.services.common.document.ValidatorHandler#validate(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.context.ServiceContext) - */ - @Override + + /* + * (non-Javadoc) @see + * org.collectionspace.services.common.document.ValidatorHandler#validate(org.collectionspace.services.common.document.DocumentHandler.Action, + * org.collectionspace.services.common.context.ServiceContext) + */ + @Override public void validate(Action action, ServiceContext ctx) - throws InvalidDocumentException { - init(ctx); - - switch (action) { - case CREATE: - handleCreate(); - break; - case GET: - handleGet(); - break; - case GET_ALL: - handleGetAll(); - break; - case UPDATE: - handleUpdate(); - break; - case DELETE: - handleDelete(); - break; - default: - throw new UnsupportedOperationException("ValidatorHandlerImpl: Unknow action = " + - action); - } + throws InvalidDocumentException { + init(ctx); + + switch (action) { + case CREATE: + handleCreate(); + break; + case GET: + handleGet(); + break; + case GET_ALL: + handleGetAll(); + break; + case UPDATE: + handleUpdate(); + break; + case DELETE: + handleDelete(); + break; + default: + throw new UnsupportedOperationException("ValidatorHandlerImpl: Unknown action = " + + action); + } + } + + protected boolean enforceAsserts() { + return !ctx.getServiceBinding().isDisableAsserts(); } - - protected boolean enforceAsserts() { - return !ctx.getServiceBinding().isDisableAsserts(); - } - + protected Object getCommonPart() { - Object result = null; - - try { - MultipartServiceContext multiPartCtx = (MultipartServiceContext) getServiceContext(); - result = multiPartCtx.getInputPart(ctx.getCommonPartLabel(), - getCommonPartClass()); - } catch (Exception e) { - if (logger.isDebugEnabled() == true) { - logger.debug("Could not extract common part from multipart input.", e); - } - } - - return result; - } - + Object result = null; + + try { + MultipartServiceContext multiPartCtx = (MultipartServiceContext) getServiceContext(); + result = multiPartCtx.getInputPart(ctx.getCommonPartLabel(), + getCommonPartClass()); + } catch (Exception e) { + if (logger.isDebugEnabled() == true) { + logger.debug("Could not extract common part from multipart input.", e); + } + } + + return result; + } + abstract protected Class getCommonPartClass(); - - /** - * Handle create. - * - * @param ctx the ctx - */ - abstract protected void handleCreate() throws InvalidDocumentException; - - /** - * Handle get. - * - * @param ctx the ctx - */ - abstract protected void handleGet() throws InvalidDocumentException; - - /** - * Handle get all. - * - * @param ctx the ctx - */ - abstract protected void handleGetAll() throws InvalidDocumentException; - - /** - * Handle update. - * - * @param ctx the ctx - */ - abstract protected void handleUpdate() throws InvalidDocumentException; - - /** - * Handle delete. - * - * @param ctx the ctx - */ - abstract protected void handleDelete() throws InvalidDocumentException; + + /** + * Handle create. + * + * @param ctx the ctx + */ + abstract protected void handleCreate() throws InvalidDocumentException; + + /** + * Handle get. + * + * @param ctx the ctx + */ + abstract protected void handleGet() throws InvalidDocumentException; + + /** + * Handle get all. + * + * @param ctx the ctx + */ + abstract protected void handleGetAll() throws InvalidDocumentException; + + /** + * Handle update. + * + * @param ctx the ctx + */ + abstract protected void handleUpdate() throws InvalidDocumentException; + + /** + * Handle delete. + * + * @param ctx the ctx + */ + abstract protected void handleDelete() throws InvalidDocumentException; } \ No newline at end of file diff --git a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java index 0bfb7897b..b088b4e90 100644 --- a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java +++ b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java @@ -25,14 +25,11 @@ package org.collectionspace.services.person.nuxeo; import java.util.List; import java.util.regex.Pattern; import org.collectionspace.services.common.api.Tools; -import org.collectionspace.services.person.PersonsCommon; -import org.collectionspace.services.common.context.MultipartServiceContext; -import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.document.DocumentHandler.Action; import org.collectionspace.services.common.document.InvalidDocumentException; -import org.collectionspace.services.common.document.ValidatorHandler; +import org.collectionspace.services.common.document.ValidatorHandlerImpl; import org.collectionspace.services.person.PersonTermGroup; import org.collectionspace.services.person.PersonTermGroupList; +import org.collectionspace.services.person.PersonsCommon; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,83 +41,87 @@ import org.slf4j.LoggerFactory; * * $LastChangedRevision: $ $LastChangedDate: $ */ -public class PersonValidatorHandler implements ValidatorHandler { +public class PersonValidatorHandler extends ValidatorHandlerImpl { final Logger logger = LoggerFactory.getLogger(PersonValidatorHandler.class); - private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches() + // 'Bad pattern' for shortIdentifiers matches any non-word characters + private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]"); //.matcher(input).matches() + private static final String VALIDATION_ERROR = "The record payload was invalid. See log file for more details."; + private static final String SHORT_ID_BAD_CHARS_MSG = + "shortIdentifier must only contain standard word characters"; + private static final String HAS_NO_TERMS_MSG = + "Authority items must contain at least one term."; + private static final String HAS_AN_EMPTY_TERM_MSG = + "Each term group in an authority item must contain " + + "a non-empty term name or " + + "a non-empty term display name."; + boolean invalid = false; + String msg = ""; @Override - public void validate(Action action, ServiceContext ctx) - throws InvalidDocumentException { - if (logger.isDebugEnabled()) { - logger.debug("validate() action=" + action.name()); - } - - // Bail out if the validation action is for delete. - if (action.equals(Action.DELETE)) { - return; - } - - try { - MultipartServiceContext mctx = (MultipartServiceContext) ctx; - PersonsCommon person = (PersonsCommon) mctx.getInputPart(mctx.getCommonPartLabel(), - PersonsCommon.class); - String msg = ""; - boolean invalid = false; - - if (person != null) { // No guarantee that there is a common part in every post/update. - - // Validation occurring on both creates and updates - - /* - * String displayName = person.getDisplayName(); if - * (!person.isDisplayNameComputed() && ((displayName == null) || - * displayName.trim().isEmpty())) { invalid = true; msg += - * "displayName must be non-null and non-blank if - * displayNameComputed is false!"; } - * - */ + protected Class getCommonPartClass() { + return PersonsCommon.class; + } - if (!containsAtLeastOneTerm(person)) { - invalid = true; - msg += "Authority items must contain at least one term."; + @Override + protected void handleCreate() throws InvalidDocumentException { + PersonsCommon person = (PersonsCommon) getCommonPart(); + // No guarantee that there is a common part in every post/update. + if (person != null) { + try { + String shortId = person.getShortIdentifier(); + if (shortId != null) { + CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_MSG); } - - if (!allTermsContainNameOrDisplayName(person)) { - invalid = true; - msg += "Each term group in an authority item must contain " - + "a non-empty term name or " - + "a non-empty term display name."; + CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_MSG); + CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_MSG); + } catch (AssertionError e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); } + throw new InvalidDocumentException(VALIDATION_ERROR, e); + } + } + } - // Validation specific to creates or updates - if (action.equals(Action.CREATE)) { - - // shortIdentifier value must contain only word characters - String shortId = person.getShortIdentifier(); - if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) { - invalid = true; - msg += "shortIdentifier must only contain standard word characters"; - } + @Override + protected void handleGet() throws InvalidDocumentException { + } - // Note: Per CSPACE-2215, shortIdentifier values that are null (missing) - // or the empty string are now legally accepted in create payloads. - // In either of those cases, a short identifier will be synthesized from - // a display name or supplied in another manner. + @Override + protected void handleGetAll() throws InvalidDocumentException { + } - } else if (action.equals(Action.UPDATE)) { + @Override + protected void handleUpdate() throws InvalidDocumentException { + PersonsCommon person = (PersonsCommon) getCommonPart(); + // No guarantee that there is a common part in every post/update. + if (person != null) { + try { + // shortIdentifier is among a set of fields that are + // prevented from being changed on an update, and thus + // we don't need to check its value here. + CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_MSG); + CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_MSG); + } catch (AssertionError e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); } + throw new InvalidDocumentException(VALIDATION_ERROR, e); } + } + } - if (invalid) { - logger.error(msg); - throw new InvalidDocumentException(msg); - } - } catch (InvalidDocumentException ide) { - throw ide; - } catch (Exception e) { - throw new InvalidDocumentException(e); + @Override + protected void handleDelete() throws InvalidDocumentException { + } + + private boolean shortIdentifierContainsOnlyValidChars(String shortId) { + // Check whether any characters match the 'bad' pattern + if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) { + return false; } + return true; } private boolean containsAtLeastOneTerm(PersonsCommon person) { @@ -139,11 +140,10 @@ public class PersonValidatorHandler implements ValidatorHandler { PersonTermGroupList termGroupList = person.getPersonTermGroupList(); List termGroups = termGroupList.getPersonTermGroup(); for (PersonTermGroup termGroup : termGroups) { - if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName()) ){ + if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) { return false; } } return true; } - }