
001/*- 002 * #%L 003 * HAPI FHIR Subscription Server 004 * %% 005 * Copyright (C) 2014 - 2023 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 org.hl7.fhir.r4.model.IdType; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028import org.springframework.beans.factory.annotation.Autowired; 029 030import javax.annotation.Nonnull; 031 032public class WebsocketConnectionValidator { 033 private static Logger ourLog = LoggerFactory.getLogger(WebsocketConnectionValidator.class); 034 035 @Autowired 036 SubscriptionRegistry mySubscriptionRegistry; 037 038 /** 039 * Constructor 040 */ 041 public WebsocketConnectionValidator() { 042 super(); 043 } 044 045 public WebsocketValidationResponse validate(@Nonnull IdType id) { 046 if (!id.hasIdPart() || !id.isIdPartValid()) { 047 return WebsocketValidationResponse.INVALID_RESPONSE( 048 "Invalid bind request - No ID included: " + id.getValue()); 049 } 050 051 if (!id.hasResourceType()) { 052 id = id.withResourceType("Subscription"); 053 } 054 055 ActiveSubscription activeSubscription = mySubscriptionRegistry.get(id.getIdPart()); 056 057 if (activeSubscription == null) { 058 return WebsocketValidationResponse.INVALID_RESPONSE( 059 "Invalid bind request - Unknown subscription: " + id.getValue()); 060 } 061 062 if (activeSubscription.getSubscription().getChannelType() != CanonicalSubscriptionChannelType.WEBSOCKET) { 063 return WebsocketValidationResponse.INVALID_RESPONSE("Subscription " + id.getValue() + " is not a " 064 + CanonicalSubscriptionChannelType.WEBSOCKET + " subscription"); 065 } 066 067 return WebsocketValidationResponse.VALID_RESPONSE(activeSubscription); 068 } 069}