-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new field to the CLIENT_RESPONSE pointcut in order to allow cli…
…ents to mutate an HTTP response from the BaseClient. (#5488) * Add a new field to the CLIENT_RESPONSE pointcut in order to allow clients to mutate an HTTP response from the BaseClient. * Add FhirContext to ClientResponseContext. * Introduce a ModifiedStringApacheHttpResponse. Run spotless. * Remove TDOOs, add and update javadoc. * Spotless and copyright header. * Add changelog. * Unique error message code. * Only trigger the interceptor if the expected return type is a Bundle. * Fix spotless. * Code review feedback. * Spotless. * Bump to 6.11.3-SNAPSHOT
- Loading branch information
1 parent
e15d043
commit 8fbbaef
Showing
82 changed files
with
346 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/ClientResponseContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*- | ||
* #%L | ||
* HAPI FHIR - Core Library | ||
* %% | ||
* Copyright (C) 2014 - 2023 Smile CDR, Inc. | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package ca.uhn.fhir.rest.client.api; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.interceptor.api.Pointcut; | ||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
|
||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
/** | ||
* Used to pass context to {@link Pointcut#CLIENT_RESPONSE}, including a mutable {@link IHttpResponse} | ||
*/ | ||
public class ClientResponseContext { | ||
private final IHttpRequest myHttpRequest; | ||
private IHttpResponse myHttpResponse; | ||
private final IRestfulClient myRestfulClient; | ||
private final FhirContext myFhirContext; | ||
private final Class<? extends IBaseResource> myReturnType; | ||
|
||
public ClientResponseContext( | ||
IHttpRequest myHttpRequest, | ||
IHttpResponse theHttpResponse, | ||
IRestfulClient myRestfulClient, | ||
FhirContext theFhirContext, | ||
Class<? extends IBaseResource> theReturnType) { | ||
this.myHttpRequest = myHttpRequest; | ||
this.myHttpResponse = theHttpResponse; | ||
this.myRestfulClient = myRestfulClient; | ||
this.myFhirContext = theFhirContext; | ||
this.myReturnType = theReturnType; | ||
} | ||
|
||
public IHttpRequest getHttpRequest() { | ||
return myHttpRequest; | ||
} | ||
|
||
public IHttpResponse getHttpResponse() { | ||
return myHttpResponse; | ||
} | ||
|
||
public IRestfulClient getRestfulClient() { | ||
return myRestfulClient; | ||
} | ||
|
||
public FhirContext getFhirContext() { | ||
return myFhirContext; | ||
} | ||
|
||
public Class<? extends IBaseResource> getReturnType() { | ||
return myReturnType; | ||
} | ||
|
||
public void setHttpResponse(IHttpResponse theHttpResponse) { | ||
this.myHttpResponse = theHttpResponse; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ClientResponseContext that = (ClientResponseContext) o; | ||
return Objects.equals(myHttpRequest, that.myHttpRequest) | ||
&& Objects.equals(myHttpResponse, that.myHttpResponse) | ||
&& Objects.equals(myRestfulClient, that.myRestfulClient) | ||
&& Objects.equals(myFhirContext, that.myFhirContext) | ||
&& Objects.equals(myReturnType, that.myReturnType); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(myHttpRequest, myHttpResponse, myRestfulClient, myFhirContext, myReturnType); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", ClientResponseContext.class.getSimpleName() + "[", "]") | ||
.add("myHttpRequest=" + myHttpRequest) | ||
.add("myHttpResponse=" + myHttpResponse) | ||
.add("myRestfulClient=" + myRestfulClient) | ||
.add("myFhirContext=" + myFhirContext) | ||
.add("myReturnType=" + myReturnType) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...client/src/main/java/ca/uhn/fhir/rest/client/apache/ModifiedStringApacheHttpResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* #%L | ||
* HAPI FHIR - Client Framework | ||
* %% | ||
* Copyright (C) 2014 - 2023 Smile CDR, Inc. | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package ca.uhn.fhir.rest.client.apache; | ||
|
||
import ca.uhn.fhir.i18n.Msg; | ||
import ca.uhn.fhir.rest.client.api.IHttpResponse; | ||
import ca.uhn.fhir.rest.client.impl.BaseHttpResponse; | ||
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; | ||
import ca.uhn.fhir.util.StopWatch; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Process a modified copy of an existing {@link IHttpResponse} with a String containing new content. | ||
* <p/> | ||
* Meant to be used with custom interceptors that need to hijack an existing IHttpResponse with new content. | ||
*/ | ||
public class ModifiedStringApacheHttpResponse extends BaseHttpResponse implements IHttpResponse { | ||
private static final org.slf4j.Logger ourLog = | ||
org.slf4j.LoggerFactory.getLogger(ModifiedStringApacheHttpResponse.class); | ||
private boolean myEntityBuffered = false; | ||
private final String myNewContent; | ||
private final IHttpResponse myOrigHttpResponse; | ||
private byte[] myEntityBytes = null; | ||
|
||
public ModifiedStringApacheHttpResponse( | ||
IHttpResponse theOrigHttpResponse, String theNewContent, StopWatch theResponseStopWatch) { | ||
super(theResponseStopWatch); | ||
myOrigHttpResponse = theOrigHttpResponse; | ||
myNewContent = theNewContent; | ||
} | ||
|
||
@Override | ||
public void bufferEntity() throws IOException { | ||
if (myEntityBuffered) { | ||
return; | ||
} | ||
try (InputStream respEntity = readEntity()) { | ||
if (respEntity != null) { | ||
try { | ||
myEntityBytes = IOUtils.toByteArray(respEntity); | ||
} catch (IllegalStateException exception) { | ||
throw new InternalErrorException(Msg.code(2447) + exception); | ||
} | ||
myEntityBuffered = true; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (myOrigHttpResponse instanceof CloseableHttpResponse) { | ||
try { | ||
((CloseableHttpResponse) myOrigHttpResponse).close(); | ||
} catch (IOException exception) { | ||
ourLog.debug("Failed to close response", exception); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Reader createReader() throws IOException { | ||
return new InputStreamReader(readEntity(), StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> getAllHeaders() { | ||
return myOrigHttpResponse.getAllHeaders(); | ||
} | ||
|
||
@Override | ||
public List<String> getHeaders(String theName) { | ||
return myOrigHttpResponse.getHeaders(theName); | ||
} | ||
|
||
@Override | ||
public String getMimeType() { | ||
return myOrigHttpResponse.getMimeType(); | ||
} | ||
|
||
@Override | ||
public StopWatch getRequestStopWatch() { | ||
return myOrigHttpResponse.getRequestStopWatch(); | ||
} | ||
|
||
@Override | ||
public Object getResponse() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getStatus() { | ||
return myOrigHttpResponse.getStatus(); | ||
} | ||
|
||
@Override | ||
public String getStatusInfo() { | ||
return myOrigHttpResponse.getStatusInfo(); | ||
} | ||
|
||
@Override | ||
public InputStream readEntity() { | ||
if (myEntityBuffered) { | ||
return new ByteArrayInputStream(myEntityBytes); | ||
} else { | ||
return new ByteArrayInputStream(myNewContent.getBytes()); | ||
} | ||
} | ||
} |
Oops, something went wrong.