@Override public Object invokeClient( String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException { IParser parser = createAppropriateParserForParsingResponse( theResponseMimeType, theResponseReader, theResponseStatusCode); switch (getReturnType()) { case BUNDLE: { Bundle bundle; if (myResourceType != null) { bundle = parser.parseBundle(myResourceType, theResponseReader); } else { bundle = parser.parseBundle(theResponseReader); } switch (getMethodReturnType()) { case BUNDLE: return bundle; case LIST_OF_RESOURCES: List<IResource> listOfResources; if (myResourceListCollectionType != null) { listOfResources = new ArrayList<IResource>(); for (IResource next : bundle.toListOfResources()) { if (!myResourceListCollectionType.isAssignableFrom(next.getClass())) { ourLog.debug( "Not returning resource of type {} because it is not a subclass or instance of {}", next.getClass(), myResourceListCollectionType); continue; } listOfResources.add(next); } } else { listOfResources = bundle.toListOfResources(); } return listOfResources; case RESOURCE: List<IResource> list = bundle.toListOfResources(); if (list.size() == 0) { return null; } else if (list.size() == 1) { return list.get(0); } else { throw new InvalidResponseException( theResponseStatusCode, "FHIR server call returned a bundle with multiple resources, but this method is only able to returns one."); } case BUNDLE_PROVIDER: throw new IllegalStateException( "Return type of " + IBundleProvider.class.getSimpleName() + " is not supported in clients"); } break; } case RESOURCE: { IResource resource; if (myResourceType != null) { resource = parser.parseResource(myResourceType, theResponseReader); } else { resource = parser.parseResource(theResponseReader); } MethodUtil.parseClientRequestResourceHeaders(null, theHeaders, resource); switch (getMethodReturnType()) { case BUNDLE: return Bundle.withSingleResource(resource); case LIST_OF_RESOURCES: return Collections.singletonList(resource); case RESOURCE: return resource; case BUNDLE_PROVIDER: throw new IllegalStateException( "Return type of " + IBundleProvider.class.getSimpleName() + " is not supported in clients"); } break; } } throw new IllegalStateException("Should not get here!"); }