|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.mom.webhook; |
| 19 | + |
| 20 | +import java.util.Date; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.apache.cloudstack.api.Identity; |
| 24 | +import org.apache.cloudstack.api.InternalIdentity; |
| 25 | + |
| 26 | +public interface WebhookFilter extends Identity, InternalIdentity { |
| 27 | + |
| 28 | + enum Type { |
| 29 | + EventType |
| 30 | + } |
| 31 | + |
| 32 | + enum Mode { |
| 33 | + Include, Exclude |
| 34 | + } |
| 35 | + |
| 36 | + enum MatchType { |
| 37 | + Exact, Prefix, Suffix, Contains |
| 38 | + } |
| 39 | + |
| 40 | + long getId(); |
| 41 | + long getWebhookId(); |
| 42 | + Type getType(); |
| 43 | + Mode getMode(); |
| 44 | + MatchType getMatchType(); |
| 45 | + String getValue(); |
| 46 | + Date getCreated(); |
| 47 | + |
| 48 | + static boolean overlaps(WebhookFilter.MatchType oldMatchType, String oldValue, WebhookFilter.MatchType newMatchType, String newValue) { |
| 49 | + switch (oldMatchType) { |
| 50 | + case Exact: |
| 51 | + switch (newMatchType) { |
| 52 | + case Exact: |
| 53 | + return oldValue.equals(newValue); |
| 54 | + } |
| 55 | + break; |
| 56 | + |
| 57 | + case Prefix: |
| 58 | + switch (newMatchType) { |
| 59 | + case Exact: |
| 60 | + case Prefix: |
| 61 | + return newValue.startsWith(oldValue); |
| 62 | + } |
| 63 | + break; |
| 64 | + |
| 65 | + case Suffix: |
| 66 | + switch (newMatchType) { |
| 67 | + case Exact: |
| 68 | + case Suffix: |
| 69 | + return newValue.endsWith(oldValue); |
| 70 | + } |
| 71 | + break; |
| 72 | + |
| 73 | + case Contains: |
| 74 | + switch (newMatchType) { |
| 75 | + case Exact: |
| 76 | + case Prefix: |
| 77 | + case Suffix: |
| 78 | + case Contains: |
| 79 | + return newValue.contains(oldValue); |
| 80 | + } |
| 81 | + break; |
| 82 | + |
| 83 | + default: |
| 84 | + break; |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + default WebhookFilter getConflicting(List<? extends WebhookFilter> existing) { |
| 90 | + for (WebhookFilter f : existing) { |
| 91 | + if (f.getType() != this.getType()) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + // 1. Duplicate entry (same mode, match type, and value) |
| 96 | + if (f.getMode() == this.getMode() |
| 97 | + && f.getMatchType() == this.getMatchType() |
| 98 | + && f.getValue().equalsIgnoreCase(this.getValue())) { |
| 99 | + return f; |
| 100 | + } |
| 101 | + |
| 102 | + // 2. Opposite mode (INCLUDE vs EXCLUDE) — check for overlap |
| 103 | + if (f.getMode() != this.getMode()) { |
| 104 | + String oldVal = f.getValue().toUpperCase(); |
| 105 | + String newVal = this.getValue().toUpperCase(); |
| 106 | + |
| 107 | + if (overlaps(f.getMatchType(), oldVal, this.getMatchType(), newVal)) { |
| 108 | + return f; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | +} |
0 commit comments