@Override public final BaseResponse<?> handle( JsonPath jsonPath, RequestParams requestParams, RequestBody requestBody) throws Exception { String resourceName = jsonPath.getResourceName(); PathIds resourceIds = jsonPath.getIds(); RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName); if (registryEntry == null) { throw new ResourceNotFoundException(resourceName); } if (requestBody == null) { throw new RequestBodyNotFoundException(HttpMethod.POST, resourceName); } Serializable castedResourceId = getResourceId(resourceIds, registryEntry); ResourceField relationshipField = registryEntry .getResourceInformation() .findRelationshipFieldByName(jsonPath.getElementName()); if (relationshipField == null) { throw new ResourceFieldNotFoundException(jsonPath.getElementName()); } ResourceRepository resourceRepository = registryEntry.getResourceRepository(); @SuppressWarnings("unchecked") Object resource = resourceRepository.findOne(castedResourceId, requestParams); Class<?> baseRelationshipFieldClass = relationshipField.getType(); Class<?> relationshipFieldClass = Generics.getResourceClass(relationshipField.getGenericType(), baseRelationshipFieldClass); @SuppressWarnings("unchecked") Class<? extends Serializable> relationshipIdType = (Class<? extends Serializable>) resourceRegistry .getEntry(relationshipFieldClass) .getResourceInformation() .getIdField() .getType(); RelationshipRepository relationshipRepositoryForClass = registryEntry.getRelationshipRepositoryForClass(relationshipFieldClass); if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) { if (!requestBody.isMultiple()) { throw new RequestBodyException(HttpMethod.POST, resourceName, "Non-multiple data in body"); } Iterable<DataBody> dataBodies = requestBody.getMultipleData(); processToManyRelationship( resource, relationshipIdType, jsonPath.getElementName(), dataBodies, relationshipRepositoryForClass); } else { if (requestBody.isMultiple()) { throw new RequestBodyException(HttpMethod.POST, resourceName, "Multiple data in body"); } DataBody dataBody = requestBody.getSingleData(); processToOneRelationship( resource, relationshipIdType, jsonPath.getElementName(), dataBody, relationshipRepositoryForClass); } MetaInformation metaInformation = getMetaInformation(resourceRepository, Collections.singletonList(resource), requestParams); return new ResourceResponse(metaInformation); }
@Override public final boolean isAcceptable(JsonPath jsonPath, String requestType) { return !jsonPath.isCollection() && jsonPath instanceof RelationshipsPath && method().name().equals(requestType); }