mirror of
https://github.com/hapifhir/hapi-fhir.git
synced 2025-03-09 14:33:32 +00:00
Refactor uitlity method for reuse
This commit is contained in:
parent
aae53d6ec7
commit
76685ad691
@ -44,7 +44,6 @@ import ca.uhn.fhir.rest.gclient.IClientExecutable;
|
|||||||
import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException;
|
import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException;
|
||||||
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
|
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
|
||||||
import ca.uhn.fhir.util.BundleBuilder;
|
import ca.uhn.fhir.util.BundleBuilder;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.apache.commons.text.StringSubstitutor;
|
import org.apache.commons.text.StringSubstitutor;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||||
import org.hl7.fhir.instance.model.api.IIdType;
|
import org.hl7.fhir.instance.model.api.IIdType;
|
||||||
@ -57,10 +56,10 @@ import org.springframework.messaging.MessagingException;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||||
|
|
||||||
@ -251,21 +250,10 @@ public class SubscriptionDeliveringRestHookSubscriber extends BaseSubscriptionDe
|
|||||||
*/
|
*/
|
||||||
protected void sendNotification(ResourceDeliveryMessage theMsg) {
|
protected void sendNotification(ResourceDeliveryMessage theMsg) {
|
||||||
Map<String, List<String>> params = new HashMap<>();
|
Map<String, List<String>> params = new HashMap<>();
|
||||||
List<Header> headers = new ArrayList<>();
|
CanonicalSubscription subscription = theMsg.getSubscription();
|
||||||
if (theMsg.getSubscription().getHeaders() != null) {
|
List<Header> headers = parseHeadersFromSubscription(subscription);
|
||||||
theMsg.getSubscription().getHeaders().stream().filter(Objects::nonNull).forEach(h -> {
|
|
||||||
final int sep = h.indexOf(':');
|
|
||||||
if (sep > 0) {
|
|
||||||
final String name = h.substring(0, sep);
|
|
||||||
final String value = h.substring(sep + 1);
|
|
||||||
if (StringUtils.isNotBlank(name)) {
|
|
||||||
headers.add(new Header(name.trim(), value.trim()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder url = new StringBuilder(theMsg.getSubscription().getEndpointUrl());
|
StringBuilder url = new StringBuilder(subscription.getEndpointUrl());
|
||||||
IHttpClient client = myFhirContext.getRestfulClientFactory().getHttpClient(url, params, "", RequestTypeEnum.POST, headers);
|
IHttpClient client = myFhirContext.getRestfulClientFactory().getHttpClient(url, params, "", RequestTypeEnum.POST, headers);
|
||||||
IHttpRequest request = client.createParamRequest(myFhirContext, params, null);
|
IHttpRequest request = client.createParamRequest(myFhirContext, params, null);
|
||||||
try {
|
try {
|
||||||
@ -273,8 +261,36 @@ public class SubscriptionDeliveringRestHookSubscriber extends BaseSubscriptionDe
|
|||||||
// close connection in order to return a possible cached connection to the connection pool
|
// close connection in order to return a possible cached connection to the connection pool
|
||||||
response.close();
|
response.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ourLog.error("Error trying to reach {}: {}", theMsg.getSubscription().getEndpointUrl(), e.toString());
|
ourLog.error("Error trying to reach {}: {}", subscription.getEndpointUrl(), e.toString());
|
||||||
throw new ResourceNotFoundException(e.getMessage());
|
throw new ResourceNotFoundException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Header> parseHeadersFromSubscription(CanonicalSubscription subscription) {
|
||||||
|
List<Header> headers = null;
|
||||||
|
if (subscription != null) {
|
||||||
|
for (String h : subscription.getHeaders()) {
|
||||||
|
if (h != null) {
|
||||||
|
final int sep = h.indexOf(':');
|
||||||
|
if (sep > 0) {
|
||||||
|
final String name = h.substring(0, sep);
|
||||||
|
final String value = h.substring(sep + 1);
|
||||||
|
if (isNotBlank(name)) {
|
||||||
|
if (headers == null) {
|
||||||
|
headers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
headers.add(new Header(name.trim(), value.trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (headers == null) {
|
||||||
|
headers = Collections.emptyList();
|
||||||
|
} else {
|
||||||
|
headers = Collections.unmodifiableList(headers);
|
||||||
|
}
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user