@Test public void testUpdate_ErrorIfBothAttributesAndExcludedAttributesExist() { @SuppressWarnings("unchecked") BaseResourceTypeResourceImpl<ScimUser> baseResourceImpl = Mockito.mock(BaseResourceTypeResourceImpl.class); ScimUser scimUser = null; try { scimUser = getScimUser(); } catch (PhoneNumberParseException e) { fail("Parsing phone number in getScimUser failed"); } when(baseResourceImpl.update(scimUser, "1", includedAttributeList, excludedAttributeList)).thenCallRealMethod(); Response response = baseResourceImpl.update(scimUser, "1", includedAttributeList, excludedAttributeList); assertTrue(response != null); assertTrue(response.getStatus() == Status.BAD_REQUEST.getStatusCode()); assertTrue(response.getEntity() instanceof ErrorResponse); assertTrue(((ErrorResponse)response.getEntity()).getDetail().equals("Cannot include both attributes and excluded attributes in a single request")); }
@Override public Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes) { try { Provider<T> provider = getProviderInternal(); Set<AttributeReference> attributeReferences = Optional.ofNullable(attributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); Set<AttributeReference> excludedAttributeReferences = Optional.ofNullable(excludedAttributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); if (!attributeReferences.isEmpty() && !excludedAttributeReferences.isEmpty()) { return createAmbiguousAttributeParametersResponse(); } endpointUtil.process(uriInfo); T stored; try { stored = provider.get(id); } catch (UnableToRetrieveResourceException e2) { log.error("Unable to retrieve resource with id: {}", id, e2); return createGenericExceptionResponse(e2, e2.getStatus()); } catch (Exception e) { log.error("Uncaught provider exception", e); return provider.handleException(e); } if (stored == null) { return createNotFoundResponse(id); } EntityTag backingETag = null; try { backingETag = etagGenerator.generateEtag(stored); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e1) { return createETagErrorResponse(); } ResponseBuilder evaluatePreconditionsResponse = request.evaluatePreconditions(backingETag); if (evaluatePreconditionsResponse != null) { return createPreconditionFailedResponse(id, evaluatePreconditionsResponse); } T updated; try { UpdateRequest<T> updateRequest = updateRequestInstance.get(); updateRequest.initWithResource(id, stored, resource); updated = provider.update(updateRequest); } catch (UnableToUpdateResourceException e1) { return createGenericExceptionResponse(e1, e1.getStatus()); } catch (Exception e1) { log.error("Uncaught provider exception", e1); return provider.handleException(e1); } try { updated = processFilterAttributeExtensions(provider, updated, attributeReferences, excludedAttributeReferences); } catch (ClientFilterException e1) { ErrorResponse er = new ErrorResponse(e1.getStatus(), e1.getMessage()); return er.toResponse(); } try { if (!excludedAttributeReferences.isEmpty()) { updated = attributeUtil.setExcludedAttributesForDisplay(updated, excludedAttributeReferences); } else { updated = attributeUtil.setAttributesForDisplay(updated, attributeReferences); } } catch (IllegalArgumentException | IllegalAccessException | AttributeDoesNotExistException | IOException e) { log.error("Failed to handle attribute processing in update " + e.getMessage()); } EntityTag etag = null; try { etag = etagGenerator.generateEtag(updated); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e) { log.error("Failed to generate etag for newly created entity " + e.getMessage()); } if (etag == null) { return Response.ok(updated) .location(buildLocationTag(updated)) .build(); } return Response.ok(updated) .location(buildLocationTag(updated)) .tag(etag) .build(); } catch (ScimServerException sse) { LOG.error("Error Processing SCIM Request", sse); return sse.getErrorResponse() .toResponse(); } }
BaseResourceTypeResourceImpl implements BaseResourceTypeResource<T> { @Override public Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes) { try { Provider<T> provider = getProviderInternal(); Set<AttributeReference> attributeReferences = Optional.ofNullable(attributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); Set<AttributeReference> excludedAttributeReferences = Optional.ofNullable(excludedAttributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); if (!attributeReferences.isEmpty() && !excludedAttributeReferences.isEmpty()) { return createAmbiguousAttributeParametersResponse(); } endpointUtil.process(uriInfo); T stored; try { stored = provider.get(id); } catch (UnableToRetrieveResourceException e2) { log.error("Unable to retrieve resource with id: {}", id, e2); return createGenericExceptionResponse(e2, e2.getStatus()); } catch (Exception e) { log.error("Uncaught provider exception", e); return provider.handleException(e); } if (stored == null) { return createNotFoundResponse(id); } EntityTag backingETag = null; try { backingETag = etagGenerator.generateEtag(stored); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e1) { return createETagErrorResponse(); } ResponseBuilder evaluatePreconditionsResponse = request.evaluatePreconditions(backingETag); if (evaluatePreconditionsResponse != null) { return createPreconditionFailedResponse(id, evaluatePreconditionsResponse); } T updated; try { UpdateRequest<T> updateRequest = updateRequestInstance.get(); updateRequest.initWithResource(id, stored, resource); updated = provider.update(updateRequest); } catch (UnableToUpdateResourceException e1) { return createGenericExceptionResponse(e1, e1.getStatus()); } catch (Exception e1) { log.error("Uncaught provider exception", e1); return provider.handleException(e1); } try { updated = processFilterAttributeExtensions(provider, updated, attributeReferences, excludedAttributeReferences); } catch (ClientFilterException e1) { ErrorResponse er = new ErrorResponse(e1.getStatus(), e1.getMessage()); return er.toResponse(); } try { if (!excludedAttributeReferences.isEmpty()) { updated = attributeUtil.setExcludedAttributesForDisplay(updated, excludedAttributeReferences); } else { updated = attributeUtil.setAttributesForDisplay(updated, attributeReferences); } } catch (IllegalArgumentException | IllegalAccessException | AttributeDoesNotExistException | IOException e) { log.error("Failed to handle attribute processing in update " + e.getMessage()); } EntityTag etag = null; try { etag = etagGenerator.generateEtag(updated); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e) { log.error("Failed to generate etag for newly created entity " + e.getMessage()); } if (etag == null) { return Response.ok(updated) .location(buildLocationTag(updated)) .build(); } return Response.ok(updated) .location(buildLocationTag(updated)) .tag(etag) .build(); } catch (ScimServerException sse) { LOG.error("Error Processing SCIM Request", sse); return sse.getErrorResponse() .toResponse(); } } }
BaseResourceTypeResourceImpl implements BaseResourceTypeResource<T> { @Override public Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes) { try { Provider<T> provider = getProviderInternal(); Set<AttributeReference> attributeReferences = Optional.ofNullable(attributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); Set<AttributeReference> excludedAttributeReferences = Optional.ofNullable(excludedAttributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); if (!attributeReferences.isEmpty() && !excludedAttributeReferences.isEmpty()) { return createAmbiguousAttributeParametersResponse(); } endpointUtil.process(uriInfo); T stored; try { stored = provider.get(id); } catch (UnableToRetrieveResourceException e2) { log.error("Unable to retrieve resource with id: {}", id, e2); return createGenericExceptionResponse(e2, e2.getStatus()); } catch (Exception e) { log.error("Uncaught provider exception", e); return provider.handleException(e); } if (stored == null) { return createNotFoundResponse(id); } EntityTag backingETag = null; try { backingETag = etagGenerator.generateEtag(stored); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e1) { return createETagErrorResponse(); } ResponseBuilder evaluatePreconditionsResponse = request.evaluatePreconditions(backingETag); if (evaluatePreconditionsResponse != null) { return createPreconditionFailedResponse(id, evaluatePreconditionsResponse); } T updated; try { UpdateRequest<T> updateRequest = updateRequestInstance.get(); updateRequest.initWithResource(id, stored, resource); updated = provider.update(updateRequest); } catch (UnableToUpdateResourceException e1) { return createGenericExceptionResponse(e1, e1.getStatus()); } catch (Exception e1) { log.error("Uncaught provider exception", e1); return provider.handleException(e1); } try { updated = processFilterAttributeExtensions(provider, updated, attributeReferences, excludedAttributeReferences); } catch (ClientFilterException e1) { ErrorResponse er = new ErrorResponse(e1.getStatus(), e1.getMessage()); return er.toResponse(); } try { if (!excludedAttributeReferences.isEmpty()) { updated = attributeUtil.setExcludedAttributesForDisplay(updated, excludedAttributeReferences); } else { updated = attributeUtil.setAttributesForDisplay(updated, attributeReferences); } } catch (IllegalArgumentException | IllegalAccessException | AttributeDoesNotExistException | IOException e) { log.error("Failed to handle attribute processing in update " + e.getMessage()); } EntityTag etag = null; try { etag = etagGenerator.generateEtag(updated); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e) { log.error("Failed to generate etag for newly created entity " + e.getMessage()); } if (etag == null) { return Response.ok(updated) .location(buildLocationTag(updated)) .build(); } return Response.ok(updated) .location(buildLocationTag(updated)) .tag(etag) .build(); } catch (ScimServerException sse) { LOG.error("Error Processing SCIM Request", sse); return sse.getErrorResponse() .toResponse(); } }  }
BaseResourceTypeResourceImpl implements BaseResourceTypeResource<T> { @Override public Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes) { try { Provider<T> provider = getProviderInternal(); Set<AttributeReference> attributeReferences = Optional.ofNullable(attributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); Set<AttributeReference> excludedAttributeReferences = Optional.ofNullable(excludedAttributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); if (!attributeReferences.isEmpty() && !excludedAttributeReferences.isEmpty()) { return createAmbiguousAttributeParametersResponse(); } endpointUtil.process(uriInfo); T stored; try { stored = provider.get(id); } catch (UnableToRetrieveResourceException e2) { log.error("Unable to retrieve resource with id: {}", id, e2); return createGenericExceptionResponse(e2, e2.getStatus()); } catch (Exception e) { log.error("Uncaught provider exception", e); return provider.handleException(e); } if (stored == null) { return createNotFoundResponse(id); } EntityTag backingETag = null; try { backingETag = etagGenerator.generateEtag(stored); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e1) { return createETagErrorResponse(); } ResponseBuilder evaluatePreconditionsResponse = request.evaluatePreconditions(backingETag); if (evaluatePreconditionsResponse != null) { return createPreconditionFailedResponse(id, evaluatePreconditionsResponse); } T updated; try { UpdateRequest<T> updateRequest = updateRequestInstance.get(); updateRequest.initWithResource(id, stored, resource); updated = provider.update(updateRequest); } catch (UnableToUpdateResourceException e1) { return createGenericExceptionResponse(e1, e1.getStatus()); } catch (Exception e1) { log.error("Uncaught provider exception", e1); return provider.handleException(e1); } try { updated = processFilterAttributeExtensions(provider, updated, attributeReferences, excludedAttributeReferences); } catch (ClientFilterException e1) { ErrorResponse er = new ErrorResponse(e1.getStatus(), e1.getMessage()); return er.toResponse(); } try { if (!excludedAttributeReferences.isEmpty()) { updated = attributeUtil.setExcludedAttributesForDisplay(updated, excludedAttributeReferences); } else { updated = attributeUtil.setAttributesForDisplay(updated, attributeReferences); } } catch (IllegalArgumentException | IllegalAccessException | AttributeDoesNotExistException | IOException e) { log.error("Failed to handle attribute processing in update " + e.getMessage()); } EntityTag etag = null; try { etag = etagGenerator.generateEtag(updated); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e) { log.error("Failed to generate etag for newly created entity " + e.getMessage()); } if (etag == null) { return Response.ok(updated) .location(buildLocationTag(updated)) .build(); } return Response.ok(updated) .location(buildLocationTag(updated)) .tag(etag) .build(); } catch (ScimServerException sse) { LOG.error("Error Processing SCIM Request", sse); return sse.getErrorResponse() .toResponse(); } }  abstract Provider<T> getProvider(); @Override Response getById(String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response query(AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes, FilterWrapper filter, AttributeReference sortBy, SortOrder sortOrder, Integer startIndex, Integer count); @Override Response create(T resource, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response find(SearchRequest request); @Override Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response patch(PatchRequest patchRequest, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response delete(String id); static Response createGenericExceptionResponse(Throwable e1, Status status); }
BaseResourceTypeResourceImpl implements BaseResourceTypeResource<T> { @Override public Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes) { try { Provider<T> provider = getProviderInternal(); Set<AttributeReference> attributeReferences = Optional.ofNullable(attributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); Set<AttributeReference> excludedAttributeReferences = Optional.ofNullable(excludedAttributes) .map(wrapper -> wrapper.getAttributeReferences()) .orElse(Collections.emptySet()); if (!attributeReferences.isEmpty() && !excludedAttributeReferences.isEmpty()) { return createAmbiguousAttributeParametersResponse(); } endpointUtil.process(uriInfo); T stored; try { stored = provider.get(id); } catch (UnableToRetrieveResourceException e2) { log.error("Unable to retrieve resource with id: {}", id, e2); return createGenericExceptionResponse(e2, e2.getStatus()); } catch (Exception e) { log.error("Uncaught provider exception", e); return provider.handleException(e); } if (stored == null) { return createNotFoundResponse(id); } EntityTag backingETag = null; try { backingETag = etagGenerator.generateEtag(stored); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e1) { return createETagErrorResponse(); } ResponseBuilder evaluatePreconditionsResponse = request.evaluatePreconditions(backingETag); if (evaluatePreconditionsResponse != null) { return createPreconditionFailedResponse(id, evaluatePreconditionsResponse); } T updated; try { UpdateRequest<T> updateRequest = updateRequestInstance.get(); updateRequest.initWithResource(id, stored, resource); updated = provider.update(updateRequest); } catch (UnableToUpdateResourceException e1) { return createGenericExceptionResponse(e1, e1.getStatus()); } catch (Exception e1) { log.error("Uncaught provider exception", e1); return provider.handleException(e1); } try { updated = processFilterAttributeExtensions(provider, updated, attributeReferences, excludedAttributeReferences); } catch (ClientFilterException e1) { ErrorResponse er = new ErrorResponse(e1.getStatus(), e1.getMessage()); return er.toResponse(); } try { if (!excludedAttributeReferences.isEmpty()) { updated = attributeUtil.setExcludedAttributesForDisplay(updated, excludedAttributeReferences); } else { updated = attributeUtil.setAttributesForDisplay(updated, attributeReferences); } } catch (IllegalArgumentException | IllegalAccessException | AttributeDoesNotExistException | IOException e) { log.error("Failed to handle attribute processing in update " + e.getMessage()); } EntityTag etag = null; try { etag = etagGenerator.generateEtag(updated); } catch (JsonProcessingException | NoSuchAlgorithmException | UnsupportedEncodingException e) { log.error("Failed to generate etag for newly created entity " + e.getMessage()); } if (etag == null) { return Response.ok(updated) .location(buildLocationTag(updated)) .build(); } return Response.ok(updated) .location(buildLocationTag(updated)) .tag(etag) .build(); } catch (ScimServerException sse) { LOG.error("Error Processing SCIM Request", sse); return sse.getErrorResponse() .toResponse(); } }  abstract Provider<T> getProvider(); @Override Response getById(String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response query(AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes, FilterWrapper filter, AttributeReference sortBy, SortOrder sortOrder, Integer startIndex, Integer count); @Override Response create(T resource, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response find(SearchRequest request); @Override Response update(T resource, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response patch(PatchRequest patchRequest, String id, AttributeReferenceListWrapper attributes, AttributeReferenceListWrapper excludedAttributes); @Override Response delete(String id); static Response createGenericExceptionResponse(Throwable e1, Status status);  }