Fix LGTM warning

This commit is contained in:
jamesagnew 2020-06-30 09:38:16 -04:00
parent b650fdb9ea
commit 11c4f9defb
1 changed files with 64 additions and 64 deletions
hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/match/deliver/resthook

View File

@ -46,7 +46,6 @@ import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.util.TransactionBuilder; import ca.uhn.fhir.util.TransactionBuilder;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor; import org.apache.commons.text.StringSubstitutor;
import org.apache.http.NameValuePair;
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;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -55,6 +54,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.messaging.MessagingException; import org.springframework.messaging.MessagingException;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -92,79 +92,80 @@ public class SubscriptionDeliveringRestHookSubscriber extends BaseSubscriptionDe
IClientExecutable<?, ?> operation; IClientExecutable<?, ?> operation;
if (isNotBlank(theSubscription.getPayloadSearchResult())) { if (isNotBlank(theSubscription.getPayloadSearchResult())) {
String resType = theSubscription.getPayloadSearchResult().substring(0, theSubscription.getPayloadSearchResult().indexOf('?')); operation = createDeliveryRequestTransaction(theSubscription, theClient, thePayloadResource);
IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resType); } else if (thePayloadType != null) {
RuntimeResourceDefinition resourceDefinition = myFhirContext.getResourceDefinition(resType); operation = createDeliveryRequestNormal(theMsg, theClient, thePayloadResource);
String payloadUrl = theSubscription.getPayloadSearchResult();
Map<String, String> valueMap = new HashMap<>(1);
valueMap.put("matched_resource_id", thePayloadResource.getIdElement().toUnqualifiedVersionless().getValue());
payloadUrl = new StringSubstitutor(valueMap).replace(payloadUrl);
SearchParameterMap payloadSearchMap = myMatchUrlService.translateMatchUrl(payloadUrl, resourceDefinition, MatchUrlService.processIncludes());
payloadSearchMap.setLoadSynchronous(true);
IBundleProvider searchResults = dao.search(payloadSearchMap);
TransactionBuilder builder = new TransactionBuilder(myFhirContext);
for (IBaseResource next : searchResults.getResources(0, searchResults.size())) {
builder.addUpdateEntry(next);
}
operation = theClient.transaction().withBundle(builder.getBundle());
} else { } else {
sendNotification(theMsg);
operation = null;
}
switch (theMsg.getOperationType()) { if (operation != null) {
case CREATE:
case UPDATE: if (thePayloadType != null) {
if (thePayloadResource == null || thePayloadResource.isEmpty()) { operation.encoded(thePayloadType);
if (thePayloadType != null) { }
operation = theClient.create().resource(thePayloadResource);
} else { String payloadId = thePayloadResource.getIdElement().toUnqualified().getValue();
sendNotification(theMsg); ourLog.info("Delivering {} rest-hook payload {} for {}", theMsg.getOperationType(), payloadId, theSubscription.getIdElement(myFhirContext).toUnqualifiedVersionless().getValue());
return;
} try {
} else { operation.execute();
if (thePayloadType != null) { } catch (ResourceNotFoundException e) {
operation = theClient.update().resource(thePayloadResource); ourLog.error("Cannot reach {} ", theMsg.getSubscription().getEndpointUrl());
} else { ourLog.error("Exception: ", e);
sendNotification(theMsg); throw e;
return;
}
}
break;
case DELETE:
operation = theClient.delete().resourceById(theMsg.getPayloadId(myFhirContext));
break;
default:
ourLog.warn("Ignoring delivery message of type: {}", theMsg.getOperationType());
return;
} }
} }
}
if (thePayloadType != null) { @Nullable
operation.encoded(thePayloadType); private IClientExecutable<?, ?> createDeliveryRequestNormal(ResourceDeliveryMessage theMsg, IGenericClient theClient, IBaseResource thePayloadResource) {
IClientExecutable<?, ?> operation;
switch (theMsg.getOperationType()) {
case CREATE:
case UPDATE:
operation = theClient.update().resource(thePayloadResource);
break;
case DELETE:
operation = theClient.delete().resourceById(theMsg.getPayloadId(myFhirContext));
break;
default:
ourLog.warn("Ignoring delivery message of type: {}", theMsg.getOperationType());
operation = null;
break;
}
return operation;
}
private IClientExecutable<?, ?> createDeliveryRequestTransaction(CanonicalSubscription theSubscription, IGenericClient theClient, IBaseResource thePayloadResource) {
IClientExecutable<?, ?> operation;
String resType = theSubscription.getPayloadSearchResult().substring(0, theSubscription.getPayloadSearchResult().indexOf('?'));
IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resType);
RuntimeResourceDefinition resourceDefinition = myFhirContext.getResourceDefinition(resType);
String payloadUrl = theSubscription.getPayloadSearchResult();
Map<String, String> valueMap = new HashMap<>(1);
valueMap.put("matched_resource_id", thePayloadResource.getIdElement().toUnqualifiedVersionless().getValue());
payloadUrl = new StringSubstitutor(valueMap).replace(payloadUrl);
SearchParameterMap payloadSearchMap = myMatchUrlService.translateMatchUrl(payloadUrl, resourceDefinition, MatchUrlService.processIncludes());
payloadSearchMap.setLoadSynchronous(true);
IBundleProvider searchResults = dao.search(payloadSearchMap);
TransactionBuilder builder = new TransactionBuilder(myFhirContext);
for (IBaseResource next : searchResults.getResources(0, searchResults.size())) {
builder.addUpdateEntry(next);
} }
String payloadId = null; operation = theClient.transaction().withBundle(builder.getBundle());
if (thePayloadResource != null) { return operation;
payloadId = thePayloadResource.getIdElement().toUnqualified().getValue();
}
ourLog.info("Delivering {} rest-hook payload {} for {}", theMsg.getOperationType(), payloadId, theSubscription.getIdElement(myFhirContext).toUnqualifiedVersionless().getValue());
try {
operation.execute();
} catch (ResourceNotFoundException e) {
ourLog.error("Cannot reach {} ", theMsg.getSubscription().getEndpointUrl());
ourLog.error("Exception: ", e);
throw e;
}
} }
public IBaseResource getResource(IIdType payloadId) throws ResourceGoneException { public IBaseResource getResource(IIdType payloadId) throws ResourceGoneException {
RuntimeResourceDefinition resourceDef = myFhirContext.getResourceDefinition(payloadId.getResourceType()); RuntimeResourceDefinition resourceDef = myFhirContext.getResourceDefinition(payloadId.getResourceType());
IFhirResourceDao dao = myDaoRegistry.getResourceDao(resourceDef.getImplementingClass()); IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resourceDef.getImplementingClass());
return dao.read(payloadId.toVersionless()); return dao.read(payloadId.toVersionless());
} }
@ -272,8 +273,7 @@ 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()); ourLog.error("Error trying to reach {}: {}", theMsg.getSubscription().getEndpointUrl(), e.toString());
e.printStackTrace();
throw new ResourceNotFoundException(e.getMessage()); throw new ResourceNotFoundException(e.getMessage());
} }
} }