public BoundStaticSubscipriptionState(WebSocketSession theSession, ActiveSubscription theActiveSubscription) { mySession = theSession; myActiveSubscription = theActiveSubscription; theActiveSubscription.register(this); }
public void unregisterAll() { for (MessageHandler messageHandler : myDeliveryHandlerSet) { unregister(messageHandler); } }
public void remove(String theSubscriptionId) { Validate.notBlank(theSubscriptionId); ActiveSubscription activeSubscription = myCache.get(theSubscriptionId); if (activeSubscription == null) { return; } activeSubscription.unregisterAll(); myCache.remove(theSubscriptionId); }
SubscriptionMatchResult matchResult = mySubscriptionMatcher.match(nextActiveSubscription.getSubscription(), theMsg); if (!matchResult.matched()) { continue; nextActiveSubscription.getSubscription().getIdElement(myFhirContext).getValue(), resourceId.toUnqualifiedVersionless().getValue(), matchResult.isInMemory() ? "in-memory" : "by querying the repository"); deliveryMsg.setSubscription(nextActiveSubscription.getSubscription()); deliveryMsg.setOperationType(theMsg.getOperationType()); if (payload == null) { MessageChannel deliveryChannel = nextActiveSubscription.getSubscribableChannel(); if (deliveryChannel != null) { deliveryChannel.send(wrappedMsg); } else { ourLog.warn("Do not have delivery channel for subscription {}", nextActiveSubscription.getIdElement(myFhirContext));
private String getId(ActiveSubscription theActiveSubscription) { return theActiveSubscription.getIdElement(myFhirContext).toUnqualifiedVersionless().getValue(); }
@Override public void handleMessage(Message<?> theMessage) { if (!(theMessage.getPayload() instanceof ResourceDeliveryMessage)) { return; } try { ResourceDeliveryMessage msg = (ResourceDeliveryMessage) theMessage.getPayload(); if (myActiveSubscription.getSubscription().equals(msg.getSubscription())) { deliver(); } } catch (Exception e) { ourLog.error("Failure handling subscription payload", e); throw new MessagingException(theMessage, "Failure handling subscription payload", e); } }
private boolean validCriteria(ActiveSubscription theActiveSubscription, IIdType theResourceId) { String criteriaString = theActiveSubscription.getCriteriaString(); String subscriptionId = getId(theActiveSubscription); String resourceType = theResourceId.getResourceType(); if (StringUtils.isBlank(criteriaString)) { return false; } // see if the criteria matches the created object ourLog.trace("Checking subscription {} for {} with criteria {}", subscriptionId, resourceType, criteriaString); String criteriaResource = criteriaString; int index = criteriaResource.indexOf("?"); if (index != -1) { criteriaResource = criteriaResource.substring(0, criteriaResource.indexOf("?")); } if (resourceType != null && !criteriaResource.equals(resourceType)) { ourLog.trace("Skipping subscription search for {} because it does not match the criteria {}", resourceType, criteriaString); return false; } return true; } }
@SuppressWarnings("UnusedReturnValue") private CanonicalSubscription registerSubscription(IIdType theId, IBaseResource theSubscription) { Validate.notNull(theId); String subscriptionId = theId.getIdPart(); Validate.notBlank(subscriptionId); Validate.notNull(theSubscription); CanonicalSubscription canonicalized = mySubscriptionCanonicalizer.canonicalize(theSubscription); SubscribableChannel deliveryChannel; Optional<MessageHandler> deliveryHandler; if (myModelConfig.isSubscriptionMatchingEnabled()) { deliveryChannel = mySubscriptionDeliveryChannelFactory.newDeliveryChannel(subscriptionId, canonicalized.getChannelType().toCode().toLowerCase()); deliveryHandler = mySubscriptionDeliveryHandlerFactory.createDeliveryHandler(canonicalized); } else { deliveryChannel = null; deliveryHandler = Optional.empty(); } ActiveSubscription activeSubscription = new ActiveSubscription(canonicalized, deliveryChannel); deliveryHandler.ifPresent(activeSubscription::register); myActiveSubscriptionCache.put(subscriptionId, activeSubscription); // Interceptor call: SUBSCRIPTION_AFTER_ACTIVE_SUBSCRIPTION_REGISTERED myInterceptorBroadcaster.callHooks(Pointcut.SUBSCRIPTION_AFTER_ACTIVE_SUBSCRIPTION_REGISTERED, canonicalized); return canonicalized; }
private void deliver() { try { String payload = "ping " + myActiveSubscription.getIdElement(myCtx).getIdPart(); ourLog.info("Sending WebSocket message: {}", payload); mySession.sendMessage(new TextMessage(payload)); } catch (IOException e) { handleFailure(e); } }
@Override public void handleMessage(Message theMessage) throws MessagingException { if (!(theMessage.getPayload() instanceof ResourceDeliveryMessage)) { ourLog.warn("Unexpected payload type: {}", theMessage.getPayload()); return; } String subscriptionId = "(unknown?)"; try { ResourceDeliveryMessage msg = (ResourceDeliveryMessage) theMessage.getPayload(); subscriptionId = msg.getSubscription().getIdElement(myFhirContext).getValue(); ActiveSubscription updatedSubscription = mySubscriptionRegistry.get(msg.getSubscription().getIdElement(myFhirContext).getIdPart()); if (updatedSubscription != null) { msg.setSubscription(updatedSubscription.getSubscription()); } handleMessage(msg); } catch (Exception e) { String msg = "Failure handling subscription payload for subscription: " + subscriptionId; ourLog.error(msg, e); throw new MessagingException(theMessage, msg, e); } }
@Override public void closing() { myActiveSubscription.unregister(this); }