001package ca.uhn.fhir.rest.param;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
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.Set;
024
025public class QualifierDetails {
026
027        private String myColonQualifier;
028        private String myDotQualifier;
029        private String myParamName;
030        private String myWholeQualifier;
031
032        public boolean passes(Set<String> theQualifierWhitelist, Set<String> theQualifierBlacklist) {
033                if (theQualifierWhitelist != null) {
034                        if (!theQualifierWhitelist.contains(".*")) {
035                                if (myDotQualifier != null) {
036                                        if (!theQualifierWhitelist.contains(myDotQualifier)) {
037                                                return false;
038                                        }
039                                } else {
040                                        if (!theQualifierWhitelist.contains(".")) {
041                                                return false;
042                                        }
043                                }
044                        }
045                }
046                if (theQualifierBlacklist != null) {
047                        if (myDotQualifier != null) {
048                                if (theQualifierBlacklist.contains(myDotQualifier)) {
049                                        return false;
050                                }
051                        }
052                        if (myColonQualifier != null) {
053                                if (theQualifierBlacklist.contains(myColonQualifier)) {
054                                        return false;
055                                }
056                        }
057                }
058
059                return true;
060        }
061
062        public void setParamName(String theParamName) {
063                myParamName = theParamName;
064        }
065
066        public String getParamName() {
067                return myParamName;
068        }
069
070        public void setColonQualifier(String theColonQualifier) {
071                myColonQualifier = theColonQualifier;
072        }
073
074        public void setDotQualifier(String theDotQualifier) {
075                myDotQualifier = theDotQualifier;
076        }
077
078        public String getWholeQualifier() {
079                return myWholeQualifier;
080        }
081
082        public void setWholeQualifier(String theWholeQualifier) {
083                myWholeQualifier = theWholeQualifier;
084        }
085
086        
087        public static QualifierDetails extractQualifiersFromParameterName(String theParamName) {
088                QualifierDetails retVal = new QualifierDetails();
089                if (theParamName == null || theParamName.length() == 0) {
090                        return retVal;
091                }
092
093                int dotIdx = -1;
094                int colonIdx = -1;
095                for (int idx = 0; idx < theParamName.length(); idx++) {
096                        char nextChar = theParamName.charAt(idx);
097                        if (nextChar == '.' && dotIdx == -1) {
098                                dotIdx = idx;
099                        } else if (nextChar == ':' && colonIdx == -1) {
100                                colonIdx = idx;
101                        }
102                }
103
104                if (dotIdx != -1 && colonIdx != -1) {
105                        if (dotIdx < colonIdx) {
106                                retVal.setDotQualifier(theParamName.substring(dotIdx, colonIdx));
107                                retVal.setColonQualifier(theParamName.substring(colonIdx));
108                                retVal.setParamName(theParamName.substring(0, dotIdx));
109                                retVal.setWholeQualifier(theParamName.substring(dotIdx));
110                        } else {
111                                retVal.setColonQualifier(theParamName.substring(colonIdx, dotIdx));
112                                retVal.setDotQualifier(theParamName.substring(dotIdx));
113                                retVal.setParamName(theParamName.substring(0, colonIdx));
114                                retVal.setWholeQualifier(theParamName.substring(colonIdx));
115                        }
116                } else if (dotIdx != -1) {
117                        retVal.setDotQualifier(theParamName.substring(dotIdx));
118                        retVal.setParamName(theParamName.substring(0, dotIdx));
119                        retVal.setWholeQualifier(theParamName.substring(dotIdx));
120                } else if (colonIdx != -1) {
121                        retVal.setColonQualifier(theParamName.substring(colonIdx));
122                        retVal.setParamName(theParamName.substring(0, colonIdx));
123                        retVal.setWholeQualifier(theParamName.substring(colonIdx));
124                } else {
125                        retVal.setParamName(theParamName);
126                        retVal.setColonQualifier(null);
127                        retVal.setDotQualifier(null);
128                        retVal.setWholeQualifier(null);
129                }
130
131                return retVal;
132        }
133
134
135        
136}