View Javadoc
1   package ca.uhn.fhir.jpa.util.jsonpatch;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2019 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.context.FhirContext;
24  import ca.uhn.fhir.parser.DataFormatException;
25  import ca.uhn.fhir.parser.IParser;
26  import ca.uhn.fhir.parser.StrictErrorHandler;
27  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
28  import com.fasterxml.jackson.core.JsonFactory;
29  import com.fasterxml.jackson.core.JsonParser;
30  import com.fasterxml.jackson.databind.JsonNode;
31  import com.fasterxml.jackson.databind.ObjectMapper;
32  import com.github.fge.jsonpatch.JsonPatch;
33  import com.github.fge.jsonpatch.JsonPatchException;
34  import org.hl7.fhir.instance.model.api.IBaseResource;
35  import org.intellij.lang.annotations.Language;
36  
37  import java.io.IOException;
38  
39  import static org.apache.commons.lang3.StringUtils.defaultString;
40  
41  public class JsonPatchUtils {
42  
43  	public static <T extends IBaseResource> T apply(FhirContext theCtx, T theResourceToUpdate, @Language("JSON") String thePatchBody) {
44  		// Parse the patch
45  		ObjectMapper mapper = new ObjectMapper();
46  		mapper.configure(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION, false);
47  
48  		JsonFactory factory = mapper.getFactory();
49  
50  		final JsonPatch patch;
51  		try {
52  			com.fasterxml.jackson.core.JsonParser parser = factory.createParser(thePatchBody);
53  			JsonNode jsonPatchNode = mapper.readTree(parser);
54  			patch = JsonPatch.fromJson(jsonPatchNode);
55  
56  			JsonNode originalJsonDocument = mapper.readTree(theCtx.newJsonParser().encodeResourceToString(theResourceToUpdate));
57  			JsonNode after = patch.apply(originalJsonDocument);
58  
59  			@SuppressWarnings("unchecked")
60  			Class<T> clazz = (Class<T>) theResourceToUpdate.getClass();
61  
62  			String postPatchedContent = mapper.writeValueAsString(after);
63  
64  			IParser fhirJsonParser = theCtx.newJsonParser();
65  			fhirJsonParser.setParserErrorHandler(new StrictErrorHandler());
66  
67  			T retVal;
68  			try {
69  				retVal = fhirJsonParser.parseResource(clazz, postPatchedContent);
70  			} catch (DataFormatException e) {
71  				String resourceId = theResourceToUpdate.getIdElement().toUnqualifiedVersionless().getValue();
72  				String resourceType = theCtx.getResourceDefinition(theResourceToUpdate).getName();
73  				resourceId = defaultString(resourceId, resourceType);
74  				String msg = theCtx.getLocalizer().getMessage(JsonPatchUtils.class, "failedToApplyPatch", resourceId, e.getMessage());
75  				throw new InvalidRequestException(msg);
76  			}
77  			return retVal;
78  
79  		} catch (IOException | JsonPatchException theE) {
80  			throw new InvalidRequestException(theE.getMessage());
81  		}
82  
83  	}
84  
85  }