001package ca.uhn.fhir.rest.param;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
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                         * This was removed Sep 9 2015, as I don't see any way it could possibly be triggered.
047                        if (!theQualifierWhitelist.contains(SearchParameter.QUALIFIER_ANY_TYPE)) {
048                                if (myColonQualifier != null) {
049                                        if (!theQualifierWhitelist.contains(myColonQualifier)) {
050                                                return false;
051                                        }
052                                } else {
053                                        if (!theQualifierWhitelist.contains(":")) {
054                                                return false;
055                                        }
056                                }
057                        }
058                        */
059                }
060                if (theQualifierBlacklist != null) {
061                        if (myDotQualifier != null) {
062                                if (theQualifierBlacklist.contains(myDotQualifier)) {
063                                        return false;
064                                }
065                        }
066                        if (myColonQualifier != null) {
067                                if (theQualifierBlacklist.contains(myColonQualifier)) {
068                                        return false;
069                                }
070                        }
071                }
072
073                return true;
074        }
075
076        public void setParamName(String theParamName) {
077                myParamName = theParamName;
078        }
079
080        public String getParamName() {
081                return myParamName;
082        }
083
084        public void setColonQualifier(String theColonQualifier) {
085                myColonQualifier = theColonQualifier;
086        }
087
088        public void setDotQualifier(String theDotQualifier) {
089                myDotQualifier = theDotQualifier;
090        }
091
092        public String getWholeQualifier() {
093                return myWholeQualifier;
094        }
095
096        public void setWholeQualifier(String theWholeQualifier) {
097                myWholeQualifier = theWholeQualifier;
098        }
099
100        
101        public static QualifierDetails extractQualifiersFromParameterName(String theParamName) {
102                QualifierDetails retVal = new QualifierDetails();
103                if (theParamName == null || theParamName.length() == 0) {
104                        return retVal;
105                }
106
107                int dotIdx = -1;
108                int colonIdx = -1;
109                for (int idx = 0; idx < theParamName.length(); idx++) {
110                        char nextChar = theParamName.charAt(idx);
111                        if (nextChar == '.' && dotIdx == -1) {
112                                dotIdx = idx;
113                        } else if (nextChar == ':' && colonIdx == -1) {
114                                colonIdx = idx;
115                        }
116                }
117
118                if (dotIdx != -1 && colonIdx != -1) {
119                        if (dotIdx < colonIdx) {
120                                retVal.setDotQualifier(theParamName.substring(dotIdx, colonIdx));
121                                retVal.setColonQualifier(theParamName.substring(colonIdx));
122                                retVal.setParamName(theParamName.substring(0, dotIdx));
123                                retVal.setWholeQualifier(theParamName.substring(dotIdx));
124                        } else {
125                                retVal.setColonQualifier(theParamName.substring(colonIdx, dotIdx));
126                                retVal.setDotQualifier(theParamName.substring(dotIdx));
127                                retVal.setParamName(theParamName.substring(0, colonIdx));
128                                retVal.setWholeQualifier(theParamName.substring(colonIdx));
129                        }
130                } else if (dotIdx != -1) {
131                        retVal.setDotQualifier(theParamName.substring(dotIdx));
132                        retVal.setParamName(theParamName.substring(0, dotIdx));
133                        retVal.setWholeQualifier(theParamName.substring(dotIdx));
134                } else if (colonIdx != -1) {
135                        retVal.setColonQualifier(theParamName.substring(colonIdx));
136                        retVal.setParamName(theParamName.substring(0, colonIdx));
137                        retVal.setWholeQualifier(theParamName.substring(colonIdx));
138                } else {
139                        retVal.setParamName(theParamName);
140                        retVal.setColonQualifier(null);
141                        retVal.setDotQualifier(null);
142                        retVal.setWholeQualifier(null);
143                }
144
145                return retVal;
146        }
147
148
149        
150}