001/* 002 * #%L 003 * HAPI FHIR - Core Library 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.param; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.model.api.IQueryParameterType; 025import ca.uhn.fhir.rest.api.Constants; 026import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 027 028import static org.apache.commons.lang3.StringUtils.defaultString; 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(Msg.code(1942) + "Invalid _has parameter syntax: " + theParameterName); 108 } 109 110}