001package ca.uhn.fhir.rest.server; 002 003/* 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import java.util.ArrayList; 024import java.util.List; 025 026import ca.uhn.fhir.rest.api.server.RequestDetails; 027import ca.uhn.fhir.rest.server.method.BaseMethodBinding; 028import ca.uhn.fhir.rest.server.method.MethodMatchEnum; 029 030/** 031 * Holds all method bindings for an individual resource type 032 */ 033public class ResourceBinding { 034 035 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceBinding.class); 036 037 private String resourceName; 038 private List<BaseMethodBinding<?>> myMethodBindings = new ArrayList<>(); 039 040 /** 041 * Constructor 042 */ 043 public ResourceBinding() { 044 super(); 045 } 046 047 /** 048 * Constructor 049 */ 050 public ResourceBinding(String resourceName, List<BaseMethodBinding<?>> methods) { 051 this.resourceName = resourceName; 052 this.myMethodBindings = methods; 053 } 054 055 public BaseMethodBinding<?> getMethod(RequestDetails theRequest) { 056 if (null == myMethodBindings) { 057 ourLog.warn("No methods exist for resource: {}", resourceName); 058 return null; 059 } 060 061 ourLog.debug("Looking for a handler for {}", theRequest); 062 063 /* 064 * Look for the method with the highest match strength 065 */ 066 067 BaseMethodBinding<?> matchedMethod = null; 068 MethodMatchEnum matchedMethodStrength = null; 069 070 for (BaseMethodBinding<?> rm : myMethodBindings) { 071 MethodMatchEnum nextMethodMatch = rm.incomingServerRequestMatchesMethod(theRequest); 072 if (nextMethodMatch != MethodMatchEnum.NONE) { 073 if (matchedMethodStrength == null || matchedMethodStrength.ordinal() < nextMethodMatch.ordinal()) { 074 matchedMethod = rm; 075 matchedMethodStrength = nextMethodMatch; 076 } 077 if (matchedMethodStrength == MethodMatchEnum.EXACT) { 078 break; 079 } 080 } 081 } 082 083 return matchedMethod; 084 } 085 086 public String getResourceName() { 087 return resourceName; 088 } 089 090 public void setResourceName(String resourceName) { 091 this.resourceName = resourceName; 092 } 093 094 public List<BaseMethodBinding<?>> getMethodBindings() { 095 return myMethodBindings; 096 } 097 098 public void setMethods(List<BaseMethodBinding<?>> methods) { 099 this.myMethodBindings = methods; 100 } 101 102 public void addMethod(BaseMethodBinding<?> method) { 103 this.myMethodBindings.add(method); 104 } 105 106 @Override 107 public boolean equals(Object o) { 108 if (!(o instanceof ResourceBinding)) 109 return false; 110 return resourceName.equals(((ResourceBinding) o).getResourceName()); 111 } 112 113 @Override 114 public int hashCode() { 115 return 0; 116 } 117 118}