public static void isNotBlankOrThrowUnprocessableEntity(String theString, String theMessage) { if (isBlank(theString)) { throw new UnprocessableEntityException(theMessage); } }
private boolean activateSubscription(String theActiveStatus, final IBaseResource theSubscription, String theRequestedStatus) { IFhirResourceDao subscriptionDao = myDaoRegistry.getSubscriptionDao(); IBaseResource subscription = subscriptionDao.read(theSubscription.getIdElement()); subscription.setId(subscription.getIdElement().toVersionless()); ourLog.info("Activating subscription {} from status {} to {}", subscription.getIdElement().toUnqualified().getValue(), theRequestedStatus, theActiveStatus); try { SubscriptionUtil.setStatus(myFhirContext, subscription, theActiveStatus); subscription = subscriptionDao.update(subscription).getResource(); submitResourceModifiedForUpdate(subscription); return true; } catch (final UnprocessableEntityException e) { ourLog.info("Changing status of {} to ERROR", subscription.getIdElement()); SubscriptionUtil.setStatus(myFhirContext, subscription, "error"); SubscriptionUtil.setReason(myFhirContext, subscription, e.getMessage()); subscriptionDao.update(subscription); return false; } }
ourLog.info(" - Got ID: {}", id.getValue()); } catch (UnprocessableEntityException e) { ourLog.warn("UnprocessableEntityException: " + e.toString()); } catch (BaseServerResponseException e) { ourLog.warn("Server responded HTTP " + e.getStatusCode() + ": " + e.toString());
private boolean activateSubscription(String theActiveStatus, final IBaseResource theSubscription, String theRequestedStatus) { IFhirResourceDao subscriptionDao = myDaoRegistry.getSubscriptionDao(); IBaseResource subscription = subscriptionDao.read(theSubscription.getIdElement()); subscription.setId(subscription.getIdElement().toVersionless()); ourLog.info("Activating subscription {} from status {} to {}", subscription.getIdElement().toUnqualified().getValue(), theRequestedStatus, theActiveStatus); try { SubscriptionUtil.setStatus(myFhirContext, subscription, theActiveStatus); subscription = subscriptionDao.update(subscription).getResource(); submitResourceModifiedForUpdate(subscription); return true; } catch (final UnprocessableEntityException e) { ourLog.info("Changing status of {} to ERROR", subscription.getIdElement()); SubscriptionUtil.setStatus(myFhirContext, subscription, "error"); SubscriptionUtil.setReason(myFhirContext, subscription, e.getMessage()); subscriptionDao.update(subscription); return false; } }
ourLog.info(" - Got ID: {}", id.getValue()); } catch (UnprocessableEntityException e) { ourLog.warn("UnprocessableEntityException: " + e.toString());
public void setLanguage(String theLanguage) { if (defaultString(theLanguage).length() > MAX_LANGUAGE_LENGTH) { throw new UnprocessableEntityException("Language exceeds maximum length of " + MAX_LANGUAGE_LENGTH + " chars: " + theLanguage); } myLanguage = theLanguage; }
@Test public void testSendManuallyReturningError() { try { sendManually(thisSucks()); fail("expected UnprocessableEntityException"); } catch (UnprocessableEntityException e) { assertEquals("HTTP 422 Unprocessable Entity: This sucks", e.getMessage()); } }
public void setProfile(String theProfile) { if (defaultString(theProfile).length() > MAX_PROFILE_LENGTH) { throw new UnprocessableEntityException("Profile name exceeds maximum length of " + MAX_PROFILE_LENGTH + " chars: " + theProfile); } myProfile = theProfile; }
private void verifyMandatoryFilesExist(List<String> theExpectedFilenameFragments) { List<String> notFound = notFound(theExpectedFilenameFragments); if (!notFound.isEmpty()) { throw new UnprocessableEntityException("Could not find the following mandatory files in input: " + notFound); } }
public RuntimeResourceDefinition validateCriteriaAndReturnResourceDefinition(Subscription theResource) { String query = theResource.getCriteria(); if (isBlank(query)) { throw new UnprocessableEntityException("Subscription.criteria must be populated"); } int sep = query.indexOf('?'); if (sep <= 1) { throw new UnprocessableEntityException("Subscription.criteria must be in the form \"{Resource Type}?[params]\""); } String resType = query.substring(0, sep); if (resType.contains("/")) { throw new UnprocessableEntityException("Subscription.criteria must be in the form \"{Resource Type}?[params]\""); } RuntimeResourceDefinition resDef; try { resDef = getContext().getResourceDefinition(resType); } catch (DataFormatException e) { throw new UnprocessableEntityException("Subscription.criteria contains invalid/unsupported resource type: " + resType); } return resDef; }
protected void validateChannelEndpoint(Subscription theResource) { if (isBlank(theResource.getChannel().getEndpoint())) { throw new UnprocessableEntityException("Rest-hook subscriptions must have Subscription.channel.endpoint defined"); } }
protected void validateChannelEndpoint(Subscription theResource) { if (isBlank(theResource.getChannel().getEndpoint())) { throw new UnprocessableEntityException("Rest-hook subscriptions must have Subscription.channel.endpoint defined"); } }
public void validateCriteriaAndAddStrategy(final IBaseResource theResource) { String criteria = mySubscriptionCanonicalizer.getCriteria(theResource); try { SubscriptionMatchingStrategy strategy = mySubscriptionStrategyEvaluator.determineStrategy(criteria); mySubscriptionCanonicalizer.setMatchingStrategyTag(myFhirContext, theResource, strategy); } catch (InvalidRequestException | DataFormatException e) { throw new UnprocessableEntityException("Invalid subscription criteria submitted: " + criteria + " " + e.getMessage()); } }
private void verifyActiveStatus(RestOperationTypeEnum theOperation, Subscription theSubscription, SubscriptionStatus newStatus, SubscriptionStatus theExistingStatus) { SubscriptionChannelType channelType = theSubscription.getChannel().getTypeElement().getValue(); if (channelType == null) { throw new UnprocessableEntityException("Subscription.channel.type must be populated"); } if (channelType == SubscriptionChannelType.WEBSOCKET) { return; } if (theExistingStatus != null) { throw new UnprocessableEntityException("Subscription.status can not be changed from " + describeStatus(theExistingStatus) + " to " + describeStatus(newStatus)); } if (theSubscription.getStatus() == null) { throw new UnprocessableEntityException("Can not " + theOperation.getCode().toLowerCase() + " resource: Subscription.status must be populated on this server"); } throw new UnprocessableEntityException("Subscription.status must be '" + SubscriptionStatus.OFF.toCode() + "' or '" + SubscriptionStatus.REQUESTED.toCode() + "' on a newly created subscription"); }
private void verifyActiveStatus(RestOperationTypeEnum theOperation, Subscription theSubscription, SubscriptionStatus newStatus, SubscriptionStatus theExistingStatus) { SubscriptionChannelType channelType = theSubscription.getChannel().getTypeElement().getValue(); if (channelType == null) { throw new UnprocessableEntityException("Subscription.channel.type must be populated"); } if (channelType == SubscriptionChannelType.WEBSOCKET) { return; } if (theExistingStatus != null) { throw new UnprocessableEntityException("Subscription.status can not be changed from " + describeStatus(theExistingStatus) + " to " + describeStatus(newStatus)); } if (theSubscription.getStatus() == null) { throw new UnprocessableEntityException("Can not " + theOperation.getCode().toLowerCase() + " resource: Subscription.status must be populated on this server"); } throw new UnprocessableEntityException("Subscription.status must be '" + SubscriptionStatus.OFF.toCode() + "' or '" + SubscriptionStatus.REQUESTED.toCode() + "' on a newly created subscription"); }
private void verifyActiveStatus(RestOperationTypeEnum theOperation, Subscription theSubscription, SubscriptionStatusEnum newStatus, SubscriptionStatusEnum theExistingStatus) { SubscriptionChannelTypeEnum channelType = theSubscription.getChannel().getTypeElement().getValueAsEnum(); if (channelType == null) { throw new UnprocessableEntityException("Subscription.channel.type must be populated"); } if (channelType == SubscriptionChannelTypeEnum.WEBSOCKET) { return; } if (theExistingStatus != null) { throw new UnprocessableEntityException("Subscription.status can not be changed from " + describeStatus(theExistingStatus) + " to " + describeStatus(newStatus)); } if (theSubscription.getStatus() == null) { throw new UnprocessableEntityException("Can not " + theOperation.getCode().toLowerCase() + " resource: Subscription.status must be populated on this server"); } throw new UnprocessableEntityException("Subscription.status must be '" + SubscriptionStatusEnum.OFF.getCode() + "' or '" + SubscriptionStatusEnum.REQUESTED.getCode() + "' on a newly created subscription"); }
private boolean requestAppliesToTransaction(FhirContext theContext, RuleOpEnum theOp, IBaseResource theInputResource) { if (!"Bundle".equals(theContext.getResourceDefinition(theInputResource).getName())) { return false; } IBaseBundle request = (IBaseBundle) theInputResource; String bundleType = defaultString(BundleUtil.getBundleType(theContext, request)); //noinspection EnumSwitchStatementWhichMissesCases if (theOp == RuleOpEnum.TRANSACTION) { if ("transaction".equals(bundleType) || "batch".equals(bundleType)) { return true; } else { String msg = theContext.getLocalizer().getMessage(RuleImplOp.class, "invalidRequestBundleTypeForTransaction", '"' + bundleType + '"'); throw new UnprocessableEntityException(msg); } } return false; }
protected void validateMetaCount(int theMetaCount) { if (myConfig.getResourceMetaCountHardLimit() != null) { if (theMetaCount > myConfig.getResourceMetaCountHardLimit()) { throw new UnprocessableEntityException("Resource contains " + theMetaCount + " meta entries (tag/profile/security label), maximum is " + myConfig.getResourceMetaCountHardLimit()); } } }
/** * Fail the request by throwing an {@link UnprocessableEntityException} as a result of a validation failure. * Subclasses may change this behaviour by providing alternate behaviour. */ protected void fail(RequestDetails theRequestDetails, ValidationResult theValidationResult) { throw new UnprocessableEntityException(theRequestDetails.getServer().getFhirContext(), theValidationResult.toOperationOutcome()); }
@Override protected void validateResourceForStorage(Subscription theResource, ResourceTable theEntityToSave) { super.validateResourceForStorage(theResource, theEntityToSave); RuntimeResourceDefinition resDef = validateCriteriaAndReturnResourceDefinition(theResource); if (resDef == null) { return; } IFhirResourceDao<? extends IBaseResource> dao = getDao(resDef.getImplementingClass()); if (dao == null) { throw new UnprocessableEntityException("Subscription.criteria contains invalid/unsupported resource type: " + resDef); } if (theResource.getChannel().getType() == null) { throw new UnprocessableEntityException("Subscription.channel.type must be populated on this server"); } SubscriptionStatus status = theResource.getStatus(); if (status == null) { throw new UnprocessableEntityException("Subscription.status must be populated on this server"); } }