1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.services.telephony.rcs.validator; 18 19 import android.telephony.ims.SipMessage; 20 21 /** 22 * Validates a SipMessage and returns the result via an instance of {@link ValidationResult}. 23 */ 24 public interface SipMessageValidator { 25 /** 26 * Validate that the SipMessage is allowed to be sent to the remote. 27 * @param message The SipMessage being validated. 28 * @return A {@link ValidationResult} that represents whether or not the message was validated. 29 * If not validated, it also returns a reason why the SIP message was not validated. 30 */ validate(SipMessage message)31 ValidationResult validate(SipMessage message); 32 33 /** 34 * Compose a SipMessageValidator out of two validators, this validator running before the next 35 * validator. 36 * @param next The next validator that will be run if this validator validates the message 37 * successfully. 38 * @return A new SipMessageValidator composed of this validator and the next one. 39 */ andThen(SipMessageValidator next)40 default SipMessageValidator andThen(SipMessageValidator next) { 41 return (SipMessage m) -> { 42 ValidationResult result = validate(m); 43 if (!result.isValidated) return result; 44 return next.validate(m); 45 }; 46 } 47 } 48