001package ca.uhn.fhir.rest.param; 002 003import ca.uhn.fhir.context.FhirContext; 004import ca.uhn.fhir.model.api.IQueryParameterType; 005import ca.uhn.fhir.rest.api.Constants; 006import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 007 008import static org.apache.commons.lang3.StringUtils.defaultString; 009 010/* 011 * #%L 012 * HAPI FHIR - Core Library 013 * %% 014 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 015 * %% 016 * Licensed under the Apache License, Version 2.0 (the "License"); 017 * you may not use this file except in compliance with the License. 018 * You may obtain a copy of the License at 019 * 020 * http://www.apache.org/licenses/LICENSE-2.0 021 * 022 * Unless required by applicable law or agreed to in writing, software 023 * distributed under the License is distributed on an "AS IS" BASIS, 024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 025 * See the License for the specific language governing permissions and 026 * limitations under the License. 027 * #L% 028 */ 029 030/** 031 * Implementation of the _has method parameter 032 */ 033public class HasParam extends BaseParam implements IQueryParameterType { 034 035 private static final long serialVersionUID = 1L; 036 037 private String myReferenceFieldName; 038 private String myParameterName; 039 private String myParameterValue; 040 private String myTargetResourceType; 041 042 public HasParam() { 043 super(); 044 } 045 046 047 public HasParam(String theTargetResourceType, String theReferenceFieldName, String theParameterName, String theParameterValue) { 048 this(); 049 myTargetResourceType = theTargetResourceType; 050 myReferenceFieldName = theReferenceFieldName; 051 myParameterName = theParameterName; 052 myParameterValue = theParameterValue; 053 } 054 055 056 @Override 057 String doGetQueryParameterQualifier() { 058 return ':' + myTargetResourceType + ':' + myReferenceFieldName + ':' + myParameterName; 059 } 060 061 @Override 062 String doGetValueAsQueryToken(FhirContext theContext) { 063 return myParameterValue; 064 } 065 066 @Override 067 void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 068 String qualifier = defaultString(theQualifier); 069 if (!qualifier.startsWith(":")) { 070 throwInvalidSyntaxException(Constants.PARAM_HAS + qualifier); 071 } 072 int colonIndex0 = qualifier.indexOf(':', 1); 073 validateColon(qualifier, colonIndex0); 074 int colonIndex1 = qualifier.indexOf(':', colonIndex0 + 1); 075 validateColon(qualifier, colonIndex1); 076 077 myTargetResourceType = qualifier.substring(1, colonIndex0); 078 myReferenceFieldName = qualifier.substring(colonIndex0 + 1, colonIndex1); 079 myParameterName = qualifier.substring(colonIndex1 + 1); 080 myParameterValue = theValue; 081 } 082 083 public String getReferenceFieldName() { 084 return myReferenceFieldName; 085 } 086 087 public String getParameterName() { 088 return myParameterName; 089 } 090 091 public String getParameterValue() { 092 return myParameterValue; 093 } 094 095 public String getTargetResourceType() { 096 return myTargetResourceType; 097 } 098 099 private static void validateColon(String theParameterName, int colonIndex) { 100 if (colonIndex == -1) { 101 throwInvalidSyntaxException(theParameterName); 102 } 103 } 104 105 106 private static void throwInvalidSyntaxException(String theParameterName) { 107 throw new InvalidRequestException("Invalid _has parameter syntax: " + theParameterName); 108 } 109 110}