Java コレクションとしてのサブスクリプションフィルタポリシー - Amazon Simple Notification Service


Java コレクションとしてのサブスクリプションフィルタポリシー

を使用してサブスクリプションフィルタポリシーを Amazon SNS クライアントに提供するには、AWS SDK for Javaこの文字列を渡す必要があります。以下のプロセスを使用して文字列として渡す必要があります。

  1. ポリシーをコレクションとして定義するには、各属性名を値のリストと関連付けるマップを作成します。

  2. サブスクリプションにポリシーを割り当てるには、マップのコンテンツからポリシーの文字列バージョンを作成します。

  3. この文字列を Amazon SNS クライアントに渡します。

Java の使用例

次の Java コード例は、サブスクリプションフィルタポリシーを Amazon SNS クライアントに提供するプロセスを示しています。

/* * Copyright 2010-2021, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. * */ import; import; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import; // The class stores the filterPolicy field as a map. public class SNSMessageFilterPolicy { private final Map<String, Attribute> filterPolicy = new HashMap<>(); // You can use the addAttribute(), addAttributePrefix(), and addAttributeAnythingBut() // methods to add attributes to your policy. These methods accept the attribute name as a string, // and they are specialized to accept different value types. You can pass values as strings, // lists of strings, numbers, or number ranges and you can also add the attributes anything-but // and prefix. public void addAttribute(final String attributeName, final String attributeValue) { filterPolicy.put(attributeName, new Attribute<>(AttributeType.String, attributeValue)); } public void addAttribute(final String attributeName, final ArrayList<String> attributeValues) { final ArrayList<Attribute> attributes = new ArrayList<>(); for (final String s : attributeValues) { attributes.add(new Attribute<>(AttributeType.String, s)); } filterPolicy.put(attributeName, new Attribute<>(AttributeType.List, attributes)); } public void addAttributePrefix(final String attributeName, final String prefix) { filterPolicy.put(attributeName, new Attribute<>(AttributeType.Prefix, prefix)); } public void addAttributeAnythingBut(final String attributeName, final String value) { filterPolicy.put(attributeName, new Attribute<>(AttributeType.AnythingBut, value)); } public <T extends Number> void addAttribute(final String attributeName, final String op, final T value) { filterPolicy.put(attributeName, new Attribute<>(AttributeType.Numeric, new NumericValue<>(op, value))); } public <T extends Number> void addAttributeRange( final String attributeName, final String lowerOp, final T lower, final String upperOp, final T upper) { filterPolicy.put(attributeName, new Attribute<>(AttributeType.Numeric, new NumericValue<>(lowerOp, lower, upperOp, upper))); } // To apply your policy to a subscription, use the apply() method, providing the client and the // subscription ARN. This method produces a policy string from the contents of the filterPolicy map, // and then applies the policy to the specified subscription. public void apply(final AmazonSNS snsClient, final String subscriptionArn) { final SetSubscriptionAttributesRequest request = new SetSubscriptionAttributesRequest(subscriptionArn, "FilterPolicy", formatFilterPolicy()); snsClient.setSubscriptionAttributes(request); } private String formatFilterPolicy() { return filterPolicy.entrySet() .stream() .map(entry -> "\"" + entry.getKey() + "\": [" + entry.getValue() + "]") .collect(Collectors.joining(", ", "{", "}")); } private enum AttributeType { String, Numeric, Prefix, List, AnythingBut } private class Attribute<T> { final T value; final AttributeType type; Attribute(final AttributeType type, final T value) { this.value = value; this.type = type; } public String toString() { switch (type) { case Prefix: return String.format("{\"prefix\":\"%s\"}", value.toString()); case Numeric: return String.format("{\"numeric\":%s}", value.toString()); case List: final List list = (List)value; final ArrayList<T> values = new ArrayList<T>(list); return values .stream() .map(Object::toString) .collect(Collectors.joining(",")); case AnythingBut: return String.format("{\"anything-but\":\"%s\"}", value); default: return String.format("\"%s\"", value); } } } private class NumericValue<T extends Number> { private final T lower; private final T upper; private final String lowerOp; private final String upperOp; NumericValue(final String op, final T value) { lower = value; lowerOp = op; upper = null; upperOp = null; } NumericValue(final String lowerOp, final T lower, final String upperOp, final T upper) { this.lower = lower; this.lowerOp = lowerOp; this.upper = upper; this.upperOp = upperOp; } public String toString() { final StringBuilder s = new StringBuilder("[") .append('\"') .append(lowerOp) .append("\",") .append(lower); if (upper != null) { s.append(",\"") .append(upperOp). append("\","). append(upper); } s.append("]"); return s.toString(); } } }

次の Java コードの抜粋は、SNSMessageFilterPolicy クラスの例を初期化して使用する方法を示しています。

// Initialize the example filter policy class. final SNSMessageFilterPolicy fp = new SNSMessageFilterPolicy(); // Add a filter policy attribute with a single value. fp.addAttribute("store", "example_corp"); fp.addAttribute("event", "order_placed"); // Add a prefix attribute. filterPolicy.addAttributePrefix("customer_interests", "bas"); // Add an anything-but attribute. filterPolicy.addAttributeAnythingBut("customer_interests", "baseball"); // Add a filter policy attribute with a list of values. final ArrayList<String> attributeValues = new ArrayList<>(); attributeValues.add("rugby"); attributeValues.add("soccer"); attributeValues.add("hockey"); fp.addAttribute("customer_interests", attributeValues); // Add a numeric attribute. filterPolicy.addAttribute("price_usd", "=", 0); // Add a numeric attribute with a range. filterPolicy.addAttributeRange("price_usd", ">", 0, "<=", 100); // Apply the filter policy attributes to an Amazon SNS subscription. fp.apply(snsClient, subscriptionArn);