001/*- 002 * #%L 003 * HAPI FHIR Subscription Server 004 * %% 005 * Copyright (C) 2014 - 2024 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.subscription.match.deliver.websocket; 021 022import ca.uhn.fhir.jpa.subscription.match.registry.ActiveSubscription; 023import ca.uhn.fhir.jpa.subscription.match.registry.SubscriptionRegistry; 024import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscriptionChannelType; 025import jakarta.annotation.Nonnull; 026import org.hl7.fhir.r4.model.IdType; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.springframework.beans.factory.annotation.Autowired; 030 031public class WebsocketConnectionValidator { 032 private static Logger ourLog = LoggerFactory.getLogger(WebsocketConnectionValidator.class); 033 034 @Autowired 035 SubscriptionRegistry mySubscriptionRegistry; 036 037 /** 038 * Constructor 039 */ 040 public WebsocketConnectionValidator() { 041 super(); 042 } 043 044 public WebsocketValidationResponse validate(@Nonnull IdType id) { 045 if (!id.hasIdPart() || !id.isIdPartValid()) { 046 return WebsocketValidationResponse.INVALID_RESPONSE( 047 "Invalid bind request - No ID included: " + id.getValue()); 048 } 049 050 if (!id.hasResourceType()) { 051 id = id.withResourceType("Subscription"); 052 } 053 054 ActiveSubscription activeSubscription = mySubscriptionRegistry.get(id.getIdPart()); 055 056 if (activeSubscription == null) { 057 return WebsocketValidationResponse.INVALID_RESPONSE( 058 "Invalid bind request - Unknown subscription: " + id.getValue()); 059 } 060 061 if (activeSubscription.getSubscription().getChannelType() != CanonicalSubscriptionChannelType.WEBSOCKET) { 062 return WebsocketValidationResponse.INVALID_RESPONSE("Subscription " + id.getValue() + " is not a " 063 + CanonicalSubscriptionChannelType.WEBSOCKET + " subscription"); 064 } 065 066 return WebsocketValidationResponse.VALID_RESPONSE(activeSubscription); 067 } 068}