001/*
002 * #%L
003 * HAPI FHIR - Server Framework
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.rest.server;
021
022import ca.uhn.fhir.rest.api.server.RequestDetails;
023import ca.uhn.fhir.rest.server.method.BaseMethodBinding;
024import ca.uhn.fhir.rest.server.method.BaseMethodBinding;
025import ca.uhn.fhir.rest.server.method.MethodMatchEnum;
026
027import java.util.LinkedList;
028import java.util.List;
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 LinkedList<BaseMethodBinding> myMethodBindings = new LinkedList<>();
039
040        /**
041         * Constructor
042         */
043        public ResourceBinding() {
044                super();
045        }
046
047        public BaseMethodBinding getMethod(RequestDetails theRequest) {
048                if (null == myMethodBindings) {
049                        ourLog.warn("No methods exist for resource: {}", resourceName);
050                        return null;
051                }
052
053                ourLog.debug("Looking for a handler for {}", theRequest);
054
055                /*
056                 * Look for the method with the highest match strength
057                 */
058
059                BaseMethodBinding matchedMethod = null;
060                MethodMatchEnum matchedMethodStrength = null;
061
062                for (BaseMethodBinding rm : myMethodBindings) {
063                        MethodMatchEnum nextMethodMatch = rm.incomingServerRequestMatchesMethod(theRequest);
064                        if (nextMethodMatch != MethodMatchEnum.NONE) {
065                                if (matchedMethodStrength == null || matchedMethodStrength.ordinal() < nextMethodMatch.ordinal()) {
066                                        matchedMethod = rm;
067                                        matchedMethodStrength = nextMethodMatch;
068                                }
069                                if (matchedMethodStrength == MethodMatchEnum.EXACT) {
070                                        break;
071                                }
072                        }
073                }
074
075                return matchedMethod;
076        }
077
078        public String getResourceName() {
079                return resourceName;
080        }
081
082        public void setResourceName(String resourceName) {
083                this.resourceName = resourceName;
084        }
085
086        public List<BaseMethodBinding> getMethodBindings() {
087                return myMethodBindings;
088        }
089
090        public void addMethod(BaseMethodBinding method) {
091                this.myMethodBindings.push(method);
092        }
093
094        @Override
095        public boolean equals(Object o) {
096                if (!(o instanceof ResourceBinding))
097                        return false;
098                return resourceName.equals(((ResourceBinding) o).getResourceName());
099        }
100
101        @Override
102        public int hashCode() {
103                return 0;
104        }
105
106}