Skip to content

Commit

Permalink
#402: possibility to add outgoing HTTP headers in FHIR producer
Browse files Browse the repository at this point in the history
  • Loading branch information
unixoid committed Aug 29, 2023
1 parent 509e65e commit be18192
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public interface Constants {
String HTTP_CLIENT_IP_ADDRESS = "FhirHttpClientIpAddress";
String HTTP_LOCALES = "FhirHttpAcceptLanguage";
String HTTP_USER = "FhirHttpUserPrincipal";
String HTTP_HEADERS = "FhirHttpHeaders";
String HTTP_INCOMING_HEADERS = "FhirHttpHeaders";
String HTTP_OUTGOING_HEADERS = "FhirHttpOutgoingHeaders";
/**
* @deprecated use {@link #HTTP_INCOMING_HEADERS} instead
*/
@Deprecated
String HTTP_HEADERS = HTTP_INCOMING_HEADERS;
String HTTP_X509_CERTIFICATES = "FhirHttpCertificates";

String FHIR_RESOURCE_TYPE_HEADER = "RESOURCE_TYPE_HEADER";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected Map<String, Object> enrichParameters(FhirSearchParameters parameters,
enriched.put(Constants.HTTP_USER, httpServletRequest.getUserPrincipal());

var headers = extractHttpHeaders(httpServletRequest);
enriched.put(Constants.HTTP_HEADERS, headers);
enriched.put(Constants.HTTP_INCOMING_HEADERS, headers);

var cipherSuite = (String) httpServletRequest.getAttribute("javax.servlet.request.cipher_suite");
if (cipherSuite != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
import ca.uhn.fhir.rest.gclient.IClientExecutable;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultProducer;
Expand All @@ -28,12 +29,14 @@
import org.openehealth.ipf.platform.camel.core.util.Exchanges;
import org.openehealth.ipf.platform.camel.ihe.fhir.core.intercept.producer.HapiClientAuditInterceptor;

import java.util.List;
import java.util.Map;

/**
* @author Christian Ohr
* @since 3.1
*/
@Slf4j
public class FhirProducer<AuditDatasetType extends FhirAuditDataset> extends DefaultProducer {

public FhirProducer(Endpoint endpoint) {
Expand Down Expand Up @@ -94,6 +97,17 @@ public void process(Exchange exchange) {
getClient(exchange),
exchange.getIn().getBody(),
exchange.getIn().getHeaders());

Object httpHeadersObject = exchange.getIn().getHeader(Constants.HTTP_OUTGOING_HEADERS);
if (httpHeadersObject instanceof Map) {
Map<String, List<String>> headers = (Map<String, List<String>>) httpHeadersObject;
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
executableClient.withAdditionalHeader(entry.getKey(), value);
}
}
}

var result = executableClient.execute();
var resultMessage = Exchanges.resultMessage(exchange);
resultMessage.setBody(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.openehealth.ipf.platform.camel.ihe.fhir.test.FhirTestContainer;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -68,6 +69,10 @@ private Exchange send(Object request, String httpMethod) throws Exception {
Exchange exchange = new DefaultExchange(camelContext, ExchangePattern.InOut);
exchange.getMessage().setBody(request);
exchange.getMessage().setHeader(Constants.HTTP_METHOD, httpMethod);
exchange.getMessage().setHeader(Constants.HTTP_OUTGOING_HEADERS, Map.of(
"Authorization", List.of("Bearer d2h5IGFyZSB5b3UgcmVhZGluZyB0aGlzPw=="),
"Header2", List.of("Value1", "Value2", "Value3")
));
exchange = producerTemplate.send("ch-ppq3://localhost:" + DEMO_APP_PORT, exchange);
Exception exception = Exchanges.extractException(exchange);
if (exception != null) {
Expand Down Expand Up @@ -113,6 +118,10 @@ public void testUpdate2() throws Exception {
.resource(consent)
.conditional()
.where(Consent.IDENTIFIER.exactly().identifier(createUuid()))
.withAdditionalHeader("Header2", "Value2")
.withAdditionalHeader("Authorization", "Bearer d2h5IGFyZSB5b3UgcmVhZGluZyB0aGlzPw==")
.withAdditionalHeader("Header2", "Value3")
.withAdditionalHeader("Header2", "Value1")
.execute();

List<AuditMessage> auditMessages = auditSender.getMessages();
Expand All @@ -126,6 +135,10 @@ public void testDelete1() throws Exception {
MethodOutcome methodOutcome = client.delete()
.resourceConditionalByType(Consent.class)
.where(Consent.IDENTIFIER.exactly().identifier(createUuid()))
.withAdditionalHeader("Header2", "Value2")
.withAdditionalHeader("Authorization", "Bearer d2h5IGFyZSB5b3UgcmVhZGluZyB0aGlzPw==")
.withAdditionalHeader("Header2", "Value3")
.withAdditionalHeader("Header2", "Value1")
.execute();

List<AuditMessage> auditMessages = auditSender.getMessages();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import ca.uhn.fhir.rest.api.MethodOutcome;
import org.apache.camel.builder.RouteBuilder;
import org.hl7.fhir.r4.model.Consent;
import org.hl7.fhir.r4.model.IdType;
import static org.junit.jupiter.api.Assertions.*;
import org.openehealth.ipf.commons.ihe.fhir.Constants;
import org.openehealth.ipf.platform.camel.core.adapter.ValidatorAdapter;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.openehealth.ipf.platform.camel.ihe.fhir.core.FhirCamelValidators.*;
Expand All @@ -36,6 +38,10 @@ public void configure() throws Exception {
.setHeader(VALIDATION_MODE, constant(MODEL))
.process(itiRequestValidator())
.process(exchange -> {
Map<String, List<String>> httpHeaders = (Map<String, List<String>>) exchange.getMessage().getHeader(Constants.HTTP_INCOMING_HEADERS);
assertEquals(1, httpHeaders.get("Authorization").size());
assertEquals(3, httpHeaders.get("Header2").size());

//Consent request = exchange.getMessage().getMandatoryBody(Consent.class);
log.info("Method = {}", exchange.getMessage().getHeader(Constants.HTTP_METHOD));
exchange.getMessage().setBody(new MethodOutcome(new IdType(UUID.randomUUID().toString())));
Expand Down

0 comments on commit be18192

Please sign in to comment.