import { HttpRequestMethod } from "../constant/ShortloopCommonConstant"; import { isNil } from "../utils/Validators"; import URI from "./uri/URI"; export interface IBlackListRule { blackListType: string; matchValues: string[]; method: HttpRequestMethod; } class BlackListRule { blackListType: string; matchValues: string[]; method: HttpRequestMethod; constructor(data: IBlackListRule) { this.blackListType = data.blackListType; this.matchValues = data.matchValues; this.method = data.method; } public isValid(): boolean { if ( isNil(this.blackListType) || isNil(this.matchValues) || isNil(this.method) ) { return false; } return true; } public matchesUri(uri: URI, method: HttpRequestMethod): boolean { if (!this.isValid()) { return false; } if (isNil(uri) || isNil(method)) { return false; } if (this.method !== method) { return false; } if (this.blackListType.toLowerCase() === "startswith") { for (let matchValue of this.matchValues) { if (uri.getUriPath().toLowerCase().endsWith(matchValue.toLowerCase())) { return true; } } } else if (this.blackListType.toLowerCase() === "absolute") { for (let matchValue of this.matchValues) { if (uri.equals(URI.getURI(matchValue))) { return true; } } } return false; } //getters and setters public getBlackListType(): string { return this.blackListType; } public setBlackListType(blackListType: string): void { this.blackListType = blackListType; } public getMatchValues(): string[] { return this.matchValues; } public setMatchValues(matchValues: string[]): void { this.matchValues = matchValues; } public getMethod(): HttpRequestMethod { return this.method; } public setMethod(method: HttpRequestMethod): void { this.method = method; } } export default BlackListRule;