Skip to content

Commit

Permalink
Adds legacy chime, slack, custom webhook messages, request/response f (
Browse files Browse the repository at this point in the history
…opensearch-project#53)

* Adds legacy chime, slack, custom webhook messages, request/response for publishing legacy notifications, and method for executing transport action

Signed-off-by: Drew Baugher <[email protected]>

* Fixes import and removes username/password that is not used by ISM

Signed-off-by: Drew Baugher <[email protected]>

* Throws error for toXContent for legacy notification response

Signed-off-by: Drew Baugher <[email protected]>

* Renames legacy destination types to have legacy prefix

Signed-off-by: Drew Baugher <[email protected]>

* Obfuscates message to remove from logs in toString method

Signed-off-by: Drew Baugher <[email protected]>

* Makes destinationt type private and updates places to use getter

Signed-off-by: Drew Baugher <[email protected]>

* Inlines destination type

Signed-off-by: Drew Baugher <[email protected]>

* Makes base message content final

Signed-off-by: Drew Baugher <[email protected]>

* Requires url to be defined in LegacyCustomWebhookMessage for use across transport wire and only writes the full url instead of each individual part

Signed-off-by: Drew Baugher <[email protected]>
  • Loading branch information
dbbaughe authored Aug 13, 2021
1 parent 32fb39b commit 4548bdc
Show file tree
Hide file tree
Showing 18 changed files with 1,516 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2020 Amazon.com, 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

package org.opensearch.commons.destination.message;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import org.apache.http.client.utils.URIBuilder;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;

/**
* This class holds the generic parameters required for a
* message.
*/
public abstract class LegacyBaseMessage implements Writeable {

private final LegacyDestinationType destinationType;
protected String destinationName;
protected String url;
private final String content;

LegacyBaseMessage(final LegacyDestinationType destinationType, final String destinationName, final String content) {
if (destinationType == null) {
throw new IllegalArgumentException("Channel type must be defined");
}
if (!Strings.hasLength(destinationName)) {
throw new IllegalArgumentException("Channel name must be defined");
}
this.destinationType = destinationType;
this.destinationName = destinationName;
this.content = content;
}

LegacyBaseMessage(final LegacyDestinationType destinationType, final String destinationName, final String content, final String url) {
this(destinationType, destinationName, content);
if (url == null) {
throw new IllegalArgumentException("url is invalid or empty");
}
this.url = url;
}

LegacyBaseMessage(StreamInput streamInput) throws IOException {
this.destinationType = streamInput.readEnum(LegacyDestinationType.class);
this.destinationName = streamInput.readString();
this.url = streamInput.readOptionalString();
this.content = streamInput.readString();
}

public void setUrl(String url) {
this.url = url;
}

public LegacyDestinationType getChannelType() {
return destinationType;
}

public String getChannelName() {
return destinationName;
}

public String getMessageContent() {
return content;
}

public String getUrl() {
return url;
}

public URI getUri() {
return buildUri(getUrl().trim(), null, null, -1, null, null);
}

protected URI buildUri(String endpoint, String scheme, String host, int port, String path, Map<String, String> queryParams) {
try {
if (Strings.isNullOrEmpty(endpoint)) {
if (Strings.isNullOrEmpty(scheme)) {
scheme = "https";
}
URIBuilder uriBuilder = new URIBuilder();
if (queryParams != null) {
for (Map.Entry<String, String> e : queryParams.entrySet())
uriBuilder.addParameter(e.getKey(), e.getValue());
}
return uriBuilder.setScheme(scheme).setHost(host).setPort(port).setPath(path).build();
}
return new URIBuilder(endpoint).build();
} catch (URISyntaxException exception) {
throw new IllegalStateException("Error creating URI");
}
}

@Override
public void writeTo(StreamOutput streamOutput) throws IOException {
streamOutput.writeEnum(destinationType);
streamOutput.writeString(destinationName);
streamOutput.writeOptionalString(url);
streamOutput.writeString(content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2020 Amazon.com, 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

package org.opensearch.commons.destination.message;

import java.io.IOException;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;

/**
* This class holds the contents of an Chime message
*/
public class LegacyChimeMessage extends LegacyBaseMessage {
private final String message;

private LegacyChimeMessage(final String destinationName, final String url, final String message) {
super(LegacyDestinationType.LEGACY_CHIME, destinationName, message, url);

if (Strings.isNullOrEmpty(message)) {
throw new IllegalArgumentException("Message content is missing");
}

this.message = message;
}

public LegacyChimeMessage(StreamInput streamInput) throws IOException {
super(streamInput);
this.message = super.getMessageContent();
}

@Override
public String toString() {
return "DestinationType: " + getChannelType() + ", DestinationName:" + destinationName + ", Url: " + url + ", Message: <...>";
}

public static class Builder {
private String message;
private final String destinationName;
private String url;

public Builder(String destinationName) {
this.destinationName = destinationName;
}

public LegacyChimeMessage.Builder withMessage(String message) {
this.message = message;
return this;
}

public LegacyChimeMessage.Builder withUrl(String url) {
this.url = url;
return this;
}

public LegacyChimeMessage build() {
return new LegacyChimeMessage(this.destinationName, this.url, this.message);
}
}

public String getMessage() {
return message;
}

public String getUrl() {
return url;
}
}
Loading

0 comments on commit 4548bdc

Please sign in to comment.