View Javadoc
1   package ca.uhn.fhir.rest.param;
2   
3   /*-
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.Set;
24  
25  public class QualifierDetails {
26  
27  	private String myColonQualifier;
28  	private String myDotQualifier;
29  	private String myParamName;
30  	private String myWholeQualifier;
31  
32  	public boolean passes(Set<String> theQualifierWhitelist, Set<String> theQualifierBlacklist) {
33  		if (theQualifierWhitelist != null) {
34  			if (!theQualifierWhitelist.contains(".*")) {
35  				if (myDotQualifier != null) {
36  					if (!theQualifierWhitelist.contains(myDotQualifier)) {
37  						return false;
38  					}
39  				} else {
40  					if (!theQualifierWhitelist.contains(".")) {
41  						return false;
42  					}
43  				}
44  			}
45  			/*
46  			 * This was removed Sep 9 2015, as I don't see any way it could possibly be triggered.
47  			if (!theQualifierWhitelist.contains(SearchParameter.QUALIFIER_ANY_TYPE)) {
48  				if (myColonQualifier != null) {
49  					if (!theQualifierWhitelist.contains(myColonQualifier)) {
50  						return false;
51  					}
52  				} else {
53  					if (!theQualifierWhitelist.contains(":")) {
54  						return false;
55  					}
56  				}
57  			}
58  			*/
59  		}
60  		if (theQualifierBlacklist != null) {
61  			if (myDotQualifier != null) {
62  				if (theQualifierBlacklist.contains(myDotQualifier)) {
63  					return false;
64  				}
65  			}
66  			if (myColonQualifier != null) {
67  				if (theQualifierBlacklist.contains(myColonQualifier)) {
68  					return false;
69  				}
70  			}
71  		}
72  
73  		return true;
74  	}
75  
76  	public void setParamName(String theParamName) {
77  		myParamName = theParamName;
78  	}
79  
80  	public String getParamName() {
81  		return myParamName;
82  	}
83  
84  	public void setColonQualifier(String theColonQualifier) {
85  		myColonQualifier = theColonQualifier;
86  	}
87  
88  	public void setDotQualifier(String theDotQualifier) {
89  		myDotQualifier = theDotQualifier;
90  	}
91  
92  	public String getWholeQualifier() {
93  		return myWholeQualifier;
94  	}
95  
96  	public void setWholeQualifier(String theWholeQualifier) {
97  		myWholeQualifier = theWholeQualifier;
98  	}
99  
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 }