From 4b294f98e89c745ca9a02f9ff11ea50472b4f61f Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Tue, 29 Sep 2020 11:03:21 +0100 Subject: [PATCH 1/7] FISH-370 Added teams notifier --- pom.xml | 2 + teams-notifier-console-plugin/pom.xml | 62 ++++++++ .../notifier/teams/EmailNotifierPlugin.java | 36 +++++ .../META-INF/admingui/console-config.xml | 28 ++++ .../notifier/teams/Strings.properties | 50 +++++++ .../teams/teamsNotifierConfiguration.jsf | 136 ++++++++++++++++++ .../resources/teams/teamsNotifierTabs.jsf | 29 ++++ teams-notifier-core/pom.xml | 66 +++++++++ .../GetTeamsNotifierConfigurationCommand.java | 85 +++++++++++ .../SetTeamsNotifierConfigurationCommand.java | 98 +++++++++++++ .../notifiers/teams/TeamsNotifier.java | 93 ++++++++++++ .../teams/TeamsNotifierConfiguration.java | 65 +++++++++ 12 files changed, 750 insertions(+) create mode 100644 teams-notifier-console-plugin/pom.xml create mode 100644 teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java create mode 100644 teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml create mode 100644 teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties create mode 100644 teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf create mode 100644 teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierTabs.jsf create mode 100644 teams-notifier-core/pom.xml create mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java create mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java create mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java create mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java diff --git a/pom.xml b/pom.xml index bfd063a..3578709 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,8 @@ snmp-notifier-console-plugin xmpp-notifier-core xmpp-notifier-console-plugin + teams-notifier-core + teams-notifier-console-plugin diff --git a/teams-notifier-console-plugin/pom.xml b/teams-notifier-console-plugin/pom.xml new file mode 100644 index 0000000..b53992e --- /dev/null +++ b/teams-notifier-console-plugin/pom.xml @@ -0,0 +1,62 @@ + + + + 4.0.0 + + + fish.payara.extensions.notifiers + notifiers-parent + 1.0 + + + teams-notifier-console-plugin + 1.0 + glassfish-jar + + Microsoft Teams Notifier Console Plugin + + + + fish.payara.server.internal.admingui + console-plugin-service + + + diff --git a/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java b/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java new file mode 100644 index 0000000..bbcf73c --- /dev/null +++ b/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java @@ -0,0 +1,36 @@ +/* + + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + + Copyright (c) 2017 Payara Foundation. All rights reserved. + + The contents of this file are subject to the terms of the Common Development + and Distribution License("CDDL") (collectively, the "License"). You + may not use this file except in compliance with the License. You can + obtain a copy of the License at + https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html + or packager/legal/LICENSE.txt. See the License for the specific + language governing permissions and limitations under the License. + + When distributing the software, include this License Header Notice in each + file and include the License file at packager/legal/LICENSE.txt. + */ + +package fish.payara.admingui.notifier.teams; + +import java.net.URL; +import org.glassfish.api.admingui.ConsoleProvider; +import org.jvnet.hk2.annotations.Service; + +/** + * + * @author Jonathan Coustick + */ +@Service +public class EmailNotifierPlugin implements ConsoleProvider { + + @Override + public URL getConfiguration() { + return null; + } +} diff --git a/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml b/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml new file mode 100644 index 0000000..50f8e64 --- /dev/null +++ b/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml @@ -0,0 +1,28 @@ + + + + + diff --git a/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties b/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties new file mode 100644 index 0000000..d61d075 --- /dev/null +++ b/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties @@ -0,0 +1,50 @@ +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright (c) 2017 Payara Foundation and/or its affiliates. All rights reserved. +# +# The contents of this file are subject to the terms of either the GNU +# General Public License Version 2 only ("GPL") or the Common Development +# and Distribution License("CDDL") (collectively, the "License"). You +# may not use this file except in compliance with the License. You can +# obtain a copy of the License at +# https://github.com/payara/Payara/blob/master/LICENSE.txt +# See the License for the specific +# language governing permissions and limitations under the License. +# +# When distributing the software, include this License Header Notice in each +# file and include the License file at glassfish/legal/LICENSE.txt. +# +# GPL Classpath Exception: +# The Payara Foundation designates this particular file as subject to the "Classpath" +# exception as provided by the Payara Foundation in the GPL Version 2 section of the License +# file that accompanied this code. +# +# Modifications: +# If applicable, add the following below the License Header, with the fields +# enclosed by brackets [] replaced by your own identifying information: +# "Portions Copyright [year] [name of copyright owner]" +# +# Contributor(s): +# If you wish your version of this file to be governed by only the CDDL or +# only the GPL Version 2, indicate your decision by adding "[Contributor] +# elects to include this software in this distribution under the [CDDL or GPL +# Version 2] license." If you don't indicate a single choice of license, a +# recipient has the option to distribute your version of this file under +# either the CDDL, the GPL Version 2 or to extend the choice of license to +# its licensees as provided above. However, if you add GPL Version 2 code +# and therefore, elected the GPL Version 2 license, then the option applies +# only if the new code is made subject to such option by the copyright +# holder. + +notifier.teams.tabs.tabText=Teams +notifier.teams.tabs.tabToolTip=Microsoft Teams Notification Configuration +notifier.teams.configuration.pageTitle=Teams Notifier Configuration +notifier.teams.configuration.pageHelpText=Enable and configure a Teamsnotifier +notifier.teams.configuration.enabledLabel=Enabled +notifier.teams.configuration.enabledLabelHelpText=Enables/Disables the Teams notifier +notifier.teams.configuration.jndiNameLabel=JavaMail JNDI Name +notifier.teams.configuration.jndiNameLabelHelpText=The JNDI name of the JavaMail session +notifier.teams.configuration.toLabel=Target Address +notifier.teams.configuration.toLabelHelpText=Teams address that notifications are to be sent to +notifier.teams.msg.success=Success +notifier.teams.buttons.test=Test \ No newline at end of file diff --git a/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf new file mode 100644 index 0000000..6dd33da --- /dev/null +++ b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf @@ -0,0 +1,136 @@ + + + + + + + + + +#include "/common/shared/alertMsg_1.inc" +#include "/payaraExtras/notification/notificationConfigTabs.inc" + + + + + + + + + + + + + + +#include "/common/shared/configNameSection.inc" + + + + + + + + + + + + + + + + + + + + + + diff --git a/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierTabs.jsf b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierTabs.jsf new file mode 100644 index 0000000..1591a4c --- /dev/null +++ b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierTabs.jsf @@ -0,0 +1,29 @@ + + + + + + diff --git a/teams-notifier-core/pom.xml b/teams-notifier-core/pom.xml new file mode 100644 index 0000000..24062e1 --- /dev/null +++ b/teams-notifier-core/pom.xml @@ -0,0 +1,66 @@ + + + + 4.0.0 + + + fish.payara.extensions.notifiers + notifiers-parent + 1.0 + + + teams-notifier-core + glassfish-jar + + Microsoft Teams Notifier Implementation + + + + fish.payara.server.internal.common + internal-api + + + jakarta.ws.rs + jakarta.ws.rs-api + 2.1.6 + + + diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java new file mode 100644 index 0000000..9a00d66 --- /dev/null +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java @@ -0,0 +1,85 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.extensions.notifiers.teams; + +import java.util.Map; + +import org.glassfish.api.admin.CommandLock; +import org.glassfish.api.admin.ExecuteOn; +import org.glassfish.api.admin.RestEndpoint; +import org.glassfish.api.admin.RestEndpoints; +import org.glassfish.api.admin.RuntimeType; +import org.glassfish.config.support.CommandTarget; +import org.glassfish.config.support.TargetType; +import org.glassfish.hk2.api.PerLookup; +import org.jvnet.hk2.annotations.Service; + +import fish.payara.internal.notification.admin.BaseGetNotifierConfigurationCommand; +import fish.payara.internal.notification.admin.NotificationServiceConfiguration; + +/** + * @author jonathan coustick + */ +@Service(name = "get-teams-notifier-configuration") +@PerLookup +@CommandLock(CommandLock.LockType.NONE) +@ExecuteOn({RuntimeType.DAS, RuntimeType.INSTANCE}) +@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG}) +@RestEndpoints({ + @RestEndpoint(configBean = NotificationServiceConfiguration.class, + opType = RestEndpoint.OpType.GET, + path = "get-teams-notifier-configuration", + description = "Lists Teams Notifier Configuration") +}) +public class GetTeamsNotifierConfigurationCommand extends BaseGetNotifierConfigurationCommand { + + @Override + protected Map getNotifierConfiguration(TeamsNotifierConfiguration configuration) { + Map map = super.getNotifierConfiguration(configuration); + + if (configuration != null) { + map.put("GroupID", configuration.getGroupID()); + map.put("channelID", configuration.getChannelID()); + } + + return map; + } + +} \ No newline at end of file diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java new file mode 100644 index 0000000..5aee933 --- /dev/null +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java @@ -0,0 +1,98 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.extensions.notifiers.teams; + +import java.beans.PropertyVetoException; + +import org.glassfish.api.Param; +import org.glassfish.api.admin.CommandLock; +import org.glassfish.api.admin.ExecuteOn; +import org.glassfish.api.admin.RestEndpoint; +import org.glassfish.api.admin.RestEndpoints; +import org.glassfish.api.admin.RuntimeType; +import org.glassfish.config.support.CommandTarget; +import org.glassfish.config.support.TargetType; +import org.glassfish.hk2.api.PerLookup; +import org.jvnet.hk2.annotations.Service; + +import fish.payara.internal.notification.admin.BaseSetNotifierConfigurationCommand; +import fish.payara.internal.notification.admin.NotificationServiceConfiguration; +import java.util.Base64; + +/** + * @author jonathan coustick + */ +@Service(name = "set-teams-notifier-configuration") +@PerLookup +@CommandLock(CommandLock.LockType.NONE) +@ExecuteOn({RuntimeType.DAS, RuntimeType.INSTANCE}) +@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG}) +@RestEndpoints({ + @RestEndpoint(configBean = NotificationServiceConfiguration.class, + opType = RestEndpoint.OpType.POST, + path = "set-teams-notifier-configuration", + description = "Configures Teams Notification Service") +}) +public class SetTeamsNotifierConfigurationCommand extends BaseSetNotifierConfigurationCommand { + + @Param(name = "groupID") + private String groupID; + + @Param + private String channelID; + + @Param + private String username; + + @Param(password = true) + private String teamsPassword; + + @Override + protected void applyValues(TeamsNotifierConfiguration configuration) throws PropertyVetoException { + super.applyValues(configuration); + configuration.setGroupID(groupID); + configuration.setChannelID(channelID); + byte[] authBytes = (username + ":" + teamsPassword).getBytes(); + String encodedString = Base64.getEncoder().encodeToString(authBytes); + configuration.setAuthorization(encodedString); + + } + +} diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java new file mode 100644 index 0000000..c4cb0e9 --- /dev/null +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java @@ -0,0 +1,93 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.extensions.notifiers.teams; + +import org.jvnet.hk2.annotations.Service; + +import fish.payara.internal.notification.PayaraConfiguredNotifier; +import fish.payara.internal.notification.PayaraNotification; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +/** + * Notifier to integrate with Microsoft Teams + * @author jonathan coustick + */ +@Service(name = "teams-notifier") +public class TeamsNotifier extends PayaraConfiguredNotifier { + + private static final Logger LOGGER = Logger.getLogger(TeamsNotifier.class.getPackage().toString()); + + Client client; + + private static final String TEAMS_BASE_ENDPOINT = "https://graph.microsoft.com/v1.0/teams/"; + private static final String CHANNELS = "/channels/"; + private static final String MESSAGES = "/messages"; + + private static final String AUTHORIZATION = "Authorization"; + private static final String BASIC = "Basic "; + + @Override + public void handleNotification(PayaraNotification event) { + WebTarget target = client.target(TEAMS_BASE_ENDPOINT + configuration.getGroupID() + CHANNELS + configuration.getChannelID() + MESSAGES); + Builder targetBuilder = target.request(MediaType.APPLICATION_JSON); + targetBuilder.header(AUTHORIZATION, BASIC + configuration.getAuthorization()); + Response response = targetBuilder.post(Entity.json(event.getData())); + if (response.getStatus() == Response.Status.OK.getStatusCode()) { + LOGGER.log(Level.INFO, response.readEntity(String.class)); + } else { + LOGGER.log(Level.SEVERE, response.readEntity(String.class)); + } + } + + @Override + public void bootstrap() { + LOGGER.log(Level.SEVERE, "Bootstrapping teams notifier"); + client = ClientBuilder.newClient(); + } + +} \ No newline at end of file diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java new file mode 100644 index 0000000..566aa06 --- /dev/null +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java @@ -0,0 +1,65 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.extensions.notifiers.teams; + +import java.beans.PropertyVetoException; + +import org.jvnet.hk2.config.Attribute; +import org.jvnet.hk2.config.Configured; + +import fish.payara.internal.notification.PayaraNotifierConfiguration; + +@Configured +public interface TeamsNotifierConfiguration extends PayaraNotifierConfiguration { + + @Attribute + String getGroupID(); + void setGroupID(String value) throws PropertyVetoException; + + @Attribute + String getChannelID(); + void setChannelID(String value) throws PropertyVetoException; + + @Attribute + String getAuthorization(); + void setAuthorization(String value) throws PropertyVetoException; + + +} \ No newline at end of file From 1989b4ed344d60f5a951b13f66b249fd1ac28201 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Tue, 29 Sep 2020 15:53:03 +0100 Subject: [PATCH 2/7] FISH-370 Created integration as an application for Azure AD --- ...erPlugin.java => TeamsNotifierPlugin.java} | 2 +- .../META-INF/admingui/console-config.xml | 49 ++++--- .../notifier/teams/Strings.properties | 4 +- .../teams/teamsNotifierConfiguration.jsf | 32 ++--- .../resources/teams/teamsNotifierTabs.jsf | 54 +++++--- teams-notifier-core/pom.xml | 5 + .../GetTeamsNotifierConfigurationCommand.java | 1 + .../SetTeamsNotifierConfigurationCommand.java | 19 +-- .../notifiers/teams/TeamsNotifier.java | 121 ++++++++++++++++-- .../teams/TeamsNotifierConfiguration.java | 17 ++- 10 files changed, 227 insertions(+), 77 deletions(-) rename teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/{EmailNotifierPlugin.java => TeamsNotifierPlugin.java} (94%) diff --git a/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java b/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java similarity index 94% rename from teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java rename to teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java index bbcf73c..780a88f 100644 --- a/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/EmailNotifierPlugin.java +++ b/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java @@ -27,7 +27,7 @@ and Distribution License("CDDL") (collectively, the "License"). You * @author Jonathan Coustick */ @Service -public class EmailNotifierPlugin implements ConsoleProvider { +public class TeamsNotifierPlugin implements ConsoleProvider { @Override public URL getConfiguration() { diff --git a/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml b/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml index 50f8e64..0546c5b 100644 --- a/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml +++ b/teams-notifier-console-plugin/src/main/resources/META-INF/admingui/console-config.xml @@ -1,28 +1,47 @@ - + diff --git a/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties b/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties index d61d075..bd1230c 100644 --- a/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties +++ b/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties @@ -1,6 +1,6 @@ # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. # -# Copyright (c) 2017 Payara Foundation and/or its affiliates. All rights reserved. +# Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved. # # The contents of this file are subject to the terms of either the GNU # General Public License Version 2 only ("GPL") or the Common Development @@ -39,7 +39,7 @@ notifier.teams.tabs.tabText=Teams notifier.teams.tabs.tabToolTip=Microsoft Teams Notification Configuration notifier.teams.configuration.pageTitle=Teams Notifier Configuration -notifier.teams.configuration.pageHelpText=Enable and configure a Teamsnotifier +notifier.teams.configuration.pageHelpText=Enable and configure a Teams notifier notifier.teams.configuration.enabledLabel=Enabled notifier.teams.configuration.enabledLabelHelpText=Enables/Disables the Teams notifier notifier.teams.configuration.jndiNameLabel=JavaMail JNDI Name diff --git a/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf index 6dd33da..9e67366 100644 --- a/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf +++ b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf @@ -1,5 +1,5 @@ - +Modifications: +If applicable, add the following below the License Header, with the fields +enclosed by brackets [] replaced by your own identifying information: +"Portions Copyright [year] [name of copyright owner]" + +Contributor(s): +If you wish your version of this file to be governed by only the CDDL or +only the GPL Version 2, indicate your decision by adding "[Contributor] +elects to include this software in this distribution under the [CDDL or GPL +Version 2] license." If you don't indicate a single choice of license, a +recipient has the option to distribute your version of this file under +either the CDDL, the GPL Version 2 or to extend the choice of license to +its licensees as provided above. However, if you add GPL Version 2 code +and therefore, elected the GPL Version 2 license, then the option applies +only if the new code is made subject to such option by the copyright +holder. +--> + diff --git a/teams-notifier-core/pom.xml b/teams-notifier-core/pom.xml index 24062e1..1e76c4e 100644 --- a/teams-notifier-core/pom.xml +++ b/teams-notifier-core/pom.xml @@ -62,5 +62,10 @@ jakarta.ws.rs-api 2.1.6 + + jakarta.json + jakarta.json-api + 1.1.6 + diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java index 9a00d66..c813f19 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java @@ -75,6 +75,7 @@ protected Map getNotifierConfiguration(TeamsNotifierConfiguratio Map map = super.getNotifierConfiguration(configuration); if (configuration != null) { + map.put("Tenant", configuration.getTenantID()); map.put("GroupID", configuration.getGroupID()); map.put("channelID", configuration.getChannelID()); } diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java index 5aee933..3176031 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java @@ -72,26 +72,29 @@ }) public class SetTeamsNotifierConfigurationCommand extends BaseSetNotifierConfigurationCommand { - @Param(name = "groupID") + @Param + private String tenantID; + + @Param private String groupID; @Param - private String channelID; + private String applicationID; @Param - private String username; + private String applicationSecret; - @Param(password = true) - private String teamsPassword; + @Param + private String channelID; @Override protected void applyValues(TeamsNotifierConfiguration configuration) throws PropertyVetoException { super.applyValues(configuration); + configuration.setTenantID(tenantID); configuration.setGroupID(groupID); configuration.setChannelID(channelID); - byte[] authBytes = (username + ":" + teamsPassword).getBytes(); - String encodedString = Base64.getEncoder().encodeToString(authBytes); - configuration.setAuthorization(encodedString); + configuration.setApplicationID(applicationID); + configuration.setApplicationSecret(applicationSecret); } diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java index c4cb0e9..769b2b9 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java @@ -43,8 +43,20 @@ import fish.payara.internal.notification.PayaraConfiguredNotifier; import fish.payara.internal.notification.PayaraNotification; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.time.Instant; import java.util.logging.Level; import java.util.logging.Logger; +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; @@ -61,33 +73,116 @@ public class TeamsNotifier extends PayaraConfiguredNotifier { private static final Logger LOGGER = Logger.getLogger(TeamsNotifier.class.getPackage().toString()); - - Client client; + private static final String AZURE_LOGIN_ENDPOINT_BASE = "https://login.microsoftonline.com/"; + private static final String AZURE_LOGIN_ENDPOINT_END = "/oauth2/v2.0/token"; + private static final String GRANT_TYPE = "grant_type"; + private static final String CLIENT_CREDENTIALS = "client_credentials"; + private static final String SCOPE = "scope"; + private static final String SCOPE_VALUE = "https://graph.microsoft.com/.default"; + private static final String CLIENT_ID = "client_id"; + private static final String CLIENT_SECRET = "client_secret"; + private static final String TEAMS_BASE_ENDPOINT = "https://graph.microsoft.com/v1.0/teams/"; private static final String CHANNELS = "/channels/"; private static final String MESSAGES = "/messages"; private static final String AUTHORIZATION = "Authorization"; - private static final String BASIC = "Basic "; + private static final String BEARER = "Bearer "; + private static final String CONTENT_TYPE = "Content-Type"; + private static final String POST = "POST"; + + private String token; + private Instant expires; @Override public void handleNotification(PayaraNotification event) { - WebTarget target = client.target(TEAMS_BASE_ENDPOINT + configuration.getGroupID() + CHANNELS + configuration.getChannelID() + MESSAGES); - Builder targetBuilder = target.request(MediaType.APPLICATION_JSON); - targetBuilder.header(AUTHORIZATION, BASIC + configuration.getAuthorization()); - Response response = targetBuilder.post(Entity.json(event.getData())); - if (response.getStatus() == Response.Status.OK.getStatusCode()) { - LOGGER.log(Level.INFO, response.readEntity(String.class)); - } else { - LOGGER.log(Level.SEVERE, response.readEntity(String.class)); - } + + try { + URL url = new URL(TEAMS_BASE_ENDPOINT + configuration.getGroupID() + CHANNELS + configuration.getChannelID() + MESSAGES); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(POST); + connection.setRequestProperty(AUTHORIZATION, BEARER + token); + connection.setRequestProperty(CONTENT_TYPE, MediaType.APPLICATION_JSON); + connection.setDoOutput(true); + connection.setDoInput(true); + + + try (OutputStream stream = connection.getOutputStream()) { + stream.write(messageBody(event)); + stream.flush(); + } + + int responseCode = connection.getResponseCode(); + if (responseCode == Response.Status.OK.getStatusCode()) { + JsonReader reader = Json.createReader(connection.getInputStream()); + JsonObject response = reader.readObject(); + int expireSeconds = response.getInt("expires_in"); + expires = Instant.now().plusSeconds(expireSeconds); + token = response.getString("acess_token"); + LOGGER.log(Level.INFO, response.toString()); + } else { + JsonReader reader = Json.createReader(connection.getErrorStream()); + JsonObject response = reader.readObject(); + LOGGER.log(Level.SEVERE, response.toString()); + } + + } catch (IOException ex) { + LOGGER.log(Level.SEVERE, null, ex); + } + } @Override public void bootstrap() { LOGGER.log(Level.SEVERE, "Bootstrapping teams notifier"); - client = ClientBuilder.newClient(); + try { + URL url = new URL(AZURE_LOGIN_ENDPOINT_BASE + configuration.getTenantID() + AZURE_LOGIN_ENDPOINT_END); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(POST); + connection.setRequestProperty(CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); + connection.setDoOutput(true); + connection.setDoInput(true); + + try (OutputStream stream = connection.getOutputStream()) { + stream.write(requestTokenBytes()); + stream.flush(); + } + + int responseCode = connection.getResponseCode(); + + if (responseCode == Response.Status.OK.getStatusCode()) { + JsonReader reader = Json.createReader(connection.getInputStream()); + JsonObject response = reader.readObject(); + int expireSeconds = response.getInt("expires_in"); + expires = Instant.now().plusSeconds(expireSeconds); + token = response.getString("access_token"); + LOGGER.log(Level.INFO, response.toString()); + } else { + JsonReader reader = Json.createReader(connection.getErrorStream()); + JsonObject response = reader.readObject(); + LOGGER.log(Level.SEVERE, response.toString()); + } + + } catch (IOException ex) { + LOGGER.log(Level.SEVERE, null, ex); + } + + + } + + private byte[] requestTokenBytes() { + String parameters = GRANT_TYPE + "=" + CLIENT_CREDENTIALS + "&" + SCOPE + "=" + SCOPE_VALUE + "&"; + parameters += CLIENT_ID + "=" + configuration.getApplicationID() + "&" + CLIENT_SECRET + "=" + configuration.getApplicationSecret(); + return parameters.getBytes(); + } + + private byte[] messageBody(PayaraNotification event) { + JsonObjectBuilder contentBuilder = Json.createObjectBuilder(); + contentBuilder.add("content", event.getMessage()); + JsonObjectBuilder body = Json.createObjectBuilder(); + body.add("body", contentBuilder); + return body.build().toString().getBytes(); } } \ No newline at end of file diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java index 566aa06..cecf388 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java @@ -48,6 +48,18 @@ @Configured public interface TeamsNotifierConfiguration extends PayaraNotifierConfiguration { + + @Attribute + String getTenantID(); + void setTenantID(String value) throws PropertyVetoException; + + @Attribute + String getApplicationID(); + void setApplicationID(String value); + + @Attribute + String getApplicationSecret(); + void setApplicationSecret(String value); @Attribute String getGroupID(); @@ -57,9 +69,4 @@ public interface TeamsNotifierConfiguration extends PayaraNotifierConfiguration String getChannelID(); void setChannelID(String value) throws PropertyVetoException; - @Attribute - String getAuthorization(); - void setAuthorization(String value) throws PropertyVetoException; - - } \ No newline at end of file From 5dec87699796fddf474df97d670d96aa9f808dfe Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Tue, 29 Sep 2020 17:44:38 +0100 Subject: [PATCH 3/7] FISH-370 Try using a webhook instead --- teams-notifier-core/pom.xml | 6 + .../GetTeamsNotifierConfigurationCommand.java | 4 +- .../SetTeamsNotifierConfigurationCommand.java | 20 +- .../notifiers/teams/TeamsNotifier.java | 192 +++++++++--------- .../teams/TeamsNotifierConfiguration.java | 20 +- .../notifiers/teams/TeamsSerializer.java | 81 ++++++++ 6 files changed, 187 insertions(+), 136 deletions(-) create mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsSerializer.java diff --git a/teams-notifier-core/pom.xml b/teams-notifier-core/pom.xml index 1e76c4e..77419bb 100644 --- a/teams-notifier-core/pom.xml +++ b/teams-notifier-core/pom.xml @@ -67,5 +67,11 @@ jakarta.json-api 1.1.6 + + jakarta.json.bind + jakarta.json.bind-api + 1.0.2 + jar + diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java index c813f19..5e959ca 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java @@ -75,9 +75,7 @@ protected Map getNotifierConfiguration(TeamsNotifierConfiguratio Map map = super.getNotifierConfiguration(configuration); if (configuration != null) { - map.put("Tenant", configuration.getTenantID()); - map.put("GroupID", configuration.getGroupID()); - map.put("channelID", configuration.getChannelID()); + map.put("Webhook URL", configuration.getWebhookUrl());; } return map; diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java index 3176031..3de5a8d 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java @@ -73,28 +73,12 @@ public class SetTeamsNotifierConfigurationCommand extends BaseSetNotifierConfigurationCommand { @Param - private String tenantID; - - @Param - private String groupID; - - @Param - private String applicationID; - - @Param - private String applicationSecret; - - @Param - private String channelID; + private String webhookURL; @Override protected void applyValues(TeamsNotifierConfiguration configuration) throws PropertyVetoException { super.applyValues(configuration); - configuration.setTenantID(tenantID); - configuration.setGroupID(groupID); - configuration.setChannelID(channelID); - configuration.setApplicationID(applicationID); - configuration.setApplicationSecret(applicationSecret); + configuration.setWebhookUrl(webhookURL); } diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java index 769b2b9..b9d28c6 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java @@ -47,23 +47,27 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.StringReader; +import java.math.BigDecimal; +import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.ProtocolException; import java.net.URL; -import java.time.Instant; +import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import javax.json.Json; +import javax.json.JsonArrayBuilder; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; import javax.json.JsonReader; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation.Builder; -import javax.ws.rs.client.WebTarget; +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; /** * Notifier to integrate with Microsoft Teams @@ -73,116 +77,110 @@ public class TeamsNotifier extends PayaraConfiguredNotifier { private static final Logger LOGGER = Logger.getLogger(TeamsNotifier.class.getPackage().toString()); - - private static final String AZURE_LOGIN_ENDPOINT_BASE = "https://login.microsoftonline.com/"; - private static final String AZURE_LOGIN_ENDPOINT_END = "/oauth2/v2.0/token"; - private static final String GRANT_TYPE = "grant_type"; - private static final String CLIENT_CREDENTIALS = "client_credentials"; - private static final String SCOPE = "scope"; - private static final String SCOPE_VALUE = "https://graph.microsoft.com/.default"; - private static final String CLIENT_ID = "client_id"; - private static final String CLIENT_SECRET = "client_secret"; - - private static final String TEAMS_BASE_ENDPOINT = "https://graph.microsoft.com/v1.0/teams/"; - private static final String CHANNELS = "/channels/"; - private static final String MESSAGES = "/messages"; + + private static final String USER_AGENT = "Payara-Teams-Notifier"; - private static final String AUTHORIZATION = "Authorization"; - private static final String BEARER = "Bearer "; - private static final String CONTENT_TYPE = "Content-Type"; - private static final String POST = "POST"; - - private String token; - private Instant expires; + private Jsonb jsonb; + private URL url; @Override public void handleNotification(PayaraNotification event) { - + if (url == null) { + LOGGER.fine("Teams notifier received notification, but no URL was available."); + return; + } + try { - URL url = new URL(TEAMS_BASE_ENDPOINT + configuration.getGroupID() + CHANNELS + configuration.getChannelID() + MESSAGES); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod(POST); - connection.setRequestProperty(AUTHORIZATION, BEARER + token); - connection.setRequestProperty(CONTENT_TYPE, MediaType.APPLICATION_JSON); connection.setDoOutput(true); - connection.setDoInput(true); + connection.setRequestMethod(HttpMethod.POST); + connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + connection.addRequestProperty(HttpHeaders.USER_AGENT, USER_AGENT); - try (OutputStream stream = connection.getOutputStream()) { - stream.write(messageBody(event)); - stream.flush(); + try (OutputStream outputStream = connection.getOutputStream()) { + outputStream.write(eventToCard(event)); + outputStream.flush(); } + connection.connect(); - int responseCode = connection.getResponseCode(); - if (responseCode == Response.Status.OK.getStatusCode()) { - JsonReader reader = Json.createReader(connection.getInputStream()); - JsonObject response = reader.readObject(); - int expireSeconds = response.getInt("expires_in"); - expires = Instant.now().plusSeconds(expireSeconds); - token = response.getString("acess_token"); - LOGGER.log(Level.INFO, response.toString()); + if (connection.getResponseCode() == 200) { + LOGGER.log(Level.FINE, "Message sent successfully"); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String response = reader.lines().collect(Collectors.joining("\n")); + LOGGER.log(Level.FINEST, response); } else { - JsonReader reader = Json.createReader(connection.getErrorStream()); - JsonObject response = reader.readObject(); - LOGGER.log(Level.SEVERE, response.toString()); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); + String response = reader.lines().collect(Collectors.joining("\n")); + LOGGER.log(Level.WARNING, response); + LOGGER.log(Level.SEVERE, "Error occurred while connecting to Microsoft Teams. Check your tokens. HTTP response code: {0}", connection.getResponseCode()); } - - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); - } - + } catch (MalformedURLException e) { + LOGGER.log(Level.SEVERE, "Error occurred while accessing URL: " + url.toString(), e); + } catch (ProtocolException e) { + LOGGER.log(Level.SEVERE, "Specified URL is not accepting protocol defined: " + HttpMethod.POST, e); + } catch (UnknownHostException e) { + LOGGER.log(Level.SEVERE, "Check your network connection. Cannot access URL: " + url.toString(), e); + } catch (ConnectException e) { + LOGGER.log(Level.SEVERE, "Error occurred while connecting URL: " + url.toString(), e); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "IO Error while accessing URL: " + url.toString(), e); + } } @Override public void bootstrap() { - LOGGER.log(Level.SEVERE, "Bootstrapping teams notifier"); try { - URL url = new URL(AZURE_LOGIN_ENDPOINT_BASE + configuration.getTenantID() + AZURE_LOGIN_ENDPOINT_END); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod(POST); - connection.setRequestProperty(CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); - connection.setDoOutput(true); - connection.setDoInput(true); - - try (OutputStream stream = connection.getOutputStream()) { - stream.write(requestTokenBytes()); - stream.flush(); - } - - int responseCode = connection.getResponseCode(); - - if (responseCode == Response.Status.OK.getStatusCode()) { - JsonReader reader = Json.createReader(connection.getInputStream()); - JsonObject response = reader.readObject(); - int expireSeconds = response.getInt("expires_in"); - expires = Instant.now().plusSeconds(expireSeconds); - token = response.getString("access_token"); - LOGGER.log(Level.INFO, response.toString()); - } else { - JsonReader reader = Json.createReader(connection.getErrorStream()); - JsonObject response = reader.readObject(); - LOGGER.log(Level.SEVERE, response.toString()); - } - - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); - } - - - } - - private byte[] requestTokenBytes() { - String parameters = GRANT_TYPE + "=" + CLIENT_CREDENTIALS + "&" + SCOPE + "=" + SCOPE_VALUE + "&"; - parameters += CLIENT_ID + "=" + configuration.getApplicationID() + "&" + CLIENT_SECRET + "=" + configuration.getApplicationSecret(); - return parameters.getBytes(); + this.url = new URL(configuration.getWebhookUrl()); + } catch (MalformedURLException e) { + LOGGER.log(Level.SEVERE, "Error occurred while accessing URL: " + url, e); + } + jsonb = JsonbBuilder.create(); } - private byte[] messageBody(PayaraNotification event) { - JsonObjectBuilder contentBuilder = Json.createObjectBuilder(); - contentBuilder.add("content", event.getMessage()); - JsonObjectBuilder body = Json.createObjectBuilder(); - body.add("body", contentBuilder); - return body.build().toString().getBytes(); + private byte[] eventToCard(PayaraNotification event) { + JsonObjectBuilder mainBuilder = Json.createObjectBuilder(); + + mainBuilder.add("@type", "MessageCard"); + mainBuilder.add("@context", "http://schema.org/extensions"); + mainBuilder.add("themeColor", "004462"); + if (event.getEventType() != null) { + mainBuilder.add("summary", event.getEventType()); + } else { + mainBuilder.add("summary", event.getSubject()); + } + + JsonArrayBuilder sectionArray = Json.createArrayBuilder(); + JsonObjectBuilder section = Json.createObjectBuilder(); + section.add("activityTitle", event.getSubject()); + + JsonArrayBuilder factsArray = Json.createArrayBuilder(); + + JsonObjectBuilder domainObject = Json.createObjectBuilder(); + domainObject.add("Domain", event.getDomainName()); + factsArray.add(domainObject); + + JsonObjectBuilder hostObject = Json.createObjectBuilder(); + hostObject.add("Host", event.getHostName()); + factsArray.add(hostObject); + + JsonObjectBuilder instanceObject = Json.createObjectBuilder(); + instanceObject.add("Instance", event.getInstanceName()); + factsArray.add(instanceObject); + + JsonObjectBuilder subjectObject = Json.createObjectBuilder(); + subjectObject.add("Message", event.getSubject()); + factsArray.add(subjectObject); + + JsonObjectBuilder messageObject = Json.createObjectBuilder(); + messageObject.add("Message", event.getMessage()); + factsArray.add(messageObject); + + + section.add("facts", factsArray); + sectionArray.add(section); + mainBuilder.add("sections", sectionArray); + return mainBuilder.build().toString().getBytes(); } } \ No newline at end of file diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java index cecf388..4d9e276 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifierConfiguration.java @@ -50,23 +50,7 @@ public interface TeamsNotifierConfiguration extends PayaraNotifierConfiguration { @Attribute - String getTenantID(); - void setTenantID(String value) throws PropertyVetoException; - - @Attribute - String getApplicationID(); - void setApplicationID(String value); - - @Attribute - String getApplicationSecret(); - void setApplicationSecret(String value); - - @Attribute - String getGroupID(); - void setGroupID(String value) throws PropertyVetoException; - - @Attribute - String getChannelID(); - void setChannelID(String value) throws PropertyVetoException; + String getWebhookUrl(); + void setWebhookUrl(String value) throws PropertyVetoException; } \ No newline at end of file diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsSerializer.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsSerializer.java new file mode 100644 index 0000000..e2f4b82 --- /dev/null +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsSerializer.java @@ -0,0 +1,81 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.extensions.notifiers.teams; + +import fish.payara.internal.notification.PayaraNotification; +import javax.json.bind.serializer.JsonbSerializer; +import javax.json.bind.serializer.SerializationContext; +import javax.json.stream.JsonGenerator; + +/** + * A custom serializer that determines what to write to the Microsoft Teams endpoint + * + * @author jonathan coustick + */ +public class TeamsSerializer implements JsonbSerializer { + + @Override + public void serialize(PayaraNotification event, JsonGenerator jg, SerializationContext sc) { + jg.write("@type", "MessageCard"); + jg.write("@context", "http://schema.org/extensions"); + jg.write("themeColor", "004462"); + jg.write("summary", event.getEventType()); + + jg.writeStartArray("sections"); + jg.writeStartObject(); + + jg.writeStartArray("facts"); + + jg.writeEnd(); + jg.writeEnd(); + + jg.write("host", event.getHostName()); + } + + private static String getDetailedSubject(PayaraNotification event) { + return String.format("%s. (host: %s, server: %s, domain: %s, instance: %s)", + event.getSubject(), + event.getHostName(), + event.getServerName(), + event.getDomainName(), + event.getInstanceName()); + } + +} \ No newline at end of file From fcf184bb3e91c398446caf3054e09f6127bd0c2a Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Wed, 30 Sep 2020 17:15:17 +0100 Subject: [PATCH 4/7] FISH-370 Finally working teams notifier --- .../notifier/teams/Strings.properties | 6 +- .../teams/teamsNotifierConfiguration.jsf | 18 ++--- .../notifiers/teams/TeamsNotifier.java | 46 ++--------- .../notifiers/teams/TeamsSerializer.java | 81 ------------------- 4 files changed, 14 insertions(+), 137 deletions(-) delete mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsSerializer.java diff --git a/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties b/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties index bd1230c..2444f77 100644 --- a/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties +++ b/teams-notifier-console-plugin/src/main/resources/fish/payara/admingui/notifier/teams/Strings.properties @@ -42,9 +42,7 @@ notifier.teams.configuration.pageTitle=Teams Notifier Configuration notifier.teams.configuration.pageHelpText=Enable and configure a Teams notifier notifier.teams.configuration.enabledLabel=Enabled notifier.teams.configuration.enabledLabelHelpText=Enables/Disables the Teams notifier -notifier.teams.configuration.jndiNameLabel=JavaMail JNDI Name -notifier.teams.configuration.jndiNameLabelHelpText=The JNDI name of the JavaMail session -notifier.teams.configuration.toLabel=Target Address -notifier.teams.configuration.toLabelHelpText=Teams address that notifications are to be sent to +notifier.teams.configuration.webhookUrlLabel=Webhook URL +notifier.teams.configuration.webhookUrlLabelHelpText=The URL that the webhook should send notifications to notifier.teams.msg.success=Success notifier.teams.buttons.test=Test \ No newline at end of file diff --git a/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf index 9e67366..f9ec8e3 100644 --- a/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf +++ b/teams-notifier-console-plugin/src/main/resources/teams/teamsNotifierConfiguration.jsf @@ -114,21 +114,13 @@ holder. helpText="$resource{i18nn.notification.configuration.notifier.dynamicHelp}"> - - + - - - - diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java index b9d28c6..b840cc1 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java @@ -47,8 +47,6 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.StringReader; -import java.math.BigDecimal; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.MalformedURLException; @@ -59,12 +57,7 @@ import java.util.logging.Logger; import java.util.stream.Collectors; import javax.json.Json; -import javax.json.JsonArrayBuilder; -import javax.json.JsonObject; import javax.json.JsonObjectBuilder; -import javax.json.JsonReader; -import javax.json.bind.Jsonb; -import javax.json.bind.JsonbBuilder; import javax.ws.rs.HttpMethod; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; @@ -80,7 +73,6 @@ public class TeamsNotifier extends PayaraConfiguredNotifier { - - @Override - public void serialize(PayaraNotification event, JsonGenerator jg, SerializationContext sc) { - jg.write("@type", "MessageCard"); - jg.write("@context", "http://schema.org/extensions"); - jg.write("themeColor", "004462"); - jg.write("summary", event.getEventType()); - - jg.writeStartArray("sections"); - jg.writeStartObject(); - - jg.writeStartArray("facts"); - - jg.writeEnd(); - jg.writeEnd(); - - jg.write("host", event.getHostName()); - } - - private static String getDetailedSubject(PayaraNotification event) { - return String.format("%s. (host: %s, server: %s, domain: %s, instance: %s)", - event.getSubject(), - event.getHostName(), - event.getServerName(), - event.getDomainName(), - event.getInstanceName()); - } - -} \ No newline at end of file From e97d3cbc9e0e86e23c69e818db5884b76f88a194 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Wed, 30 Sep 2020 17:17:27 +0100 Subject: [PATCH 5/7] FISH-370 Clean up pom --- teams-notifier-core/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/teams-notifier-core/pom.xml b/teams-notifier-core/pom.xml index 77419bb..1e76c4e 100644 --- a/teams-notifier-core/pom.xml +++ b/teams-notifier-core/pom.xml @@ -67,11 +67,5 @@ jakarta.json-api 1.1.6 - - jakarta.json.bind - jakarta.json.bind-api - 1.0.2 - jar - From fd40399d3d197b86b267d1c7fad2baa9da50e8b8 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Thu, 1 Oct 2020 11:31:58 +0100 Subject: [PATCH 6/7] FISH-370 Made requested chnaged and added man pages --- pom.xml | 4 +- .../notifier/teams/TeamsNotifierPlugin.java | 54 +++++++++++++------ teams-notifier-core/pom.xml | 11 ++-- .../GetTeamsNotifierConfigurationCommand.java | 1 + .../SetTeamsNotifierConfigurationCommand.java | 4 +- .../notifiers/teams/TeamsNotifier.java | 10 ++-- .../notifiers/teams/package-info.java | 45 ++++++++++++++++ .../teams/get-teams-notifier-configuration.1 | 40 ++++++++++++++ .../teams/set-teams-notifier-configuration.1 | 44 +++++++++++++++ 9 files changed, 181 insertions(+), 32 deletions(-) create mode 100644 teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/package-info.java create mode 100644 teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/get-teams-notifier-configuration.1 create mode 100644 teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/set-teams-notifier-configuration.1 diff --git a/pom.xml b/pom.xml index 3578709..60552af 100644 --- a/pom.xml +++ b/pom.xml @@ -72,10 +72,10 @@ slack-notifier-console-plugin snmp-notifier-core snmp-notifier-console-plugin - xmpp-notifier-core - xmpp-notifier-console-plugin teams-notifier-core teams-notifier-console-plugin + xmpp-notifier-core + xmpp-notifier-console-plugin diff --git a/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java b/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java index 780a88f..efac2cd 100644 --- a/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java +++ b/teams-notifier-console-plugin/src/main/java/fish/payara/admingui/notifier/teams/TeamsNotifierPlugin.java @@ -1,19 +1,41 @@ /* - - DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - - Copyright (c) 2017 Payara Foundation. All rights reserved. - - The contents of this file are subject to the terms of the Common Development - and Distribution License("CDDL") (collectively, the "License"). You - may not use this file except in compliance with the License. You can - obtain a copy of the License at - https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - or packager/legal/LICENSE.txt. See the License for the specific - language governing permissions and limitations under the License. - - When distributing the software, include this License Header Notice in each - file and include the License file at packager/legal/LICENSE.txt. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.admingui.notifier.teams; @@ -23,7 +45,7 @@ and Distribution License("CDDL") (collectively, the "License"). You import org.jvnet.hk2.annotations.Service; /** - * + * Plugin for the Payara Server admin console for the teams notifer * @author Jonathan Coustick */ @Service diff --git a/teams-notifier-core/pom.xml b/teams-notifier-core/pom.xml index 1e76c4e..dd4efac 100644 --- a/teams-notifier-core/pom.xml +++ b/teams-notifier-core/pom.xml @@ -49,6 +49,7 @@ teams-notifier-core glassfish-jar + 1.0 Microsoft Teams Notifier Implementation @@ -58,14 +59,8 @@ internal-api - jakarta.ws.rs - jakarta.ws.rs-api - 2.1.6 - - - jakarta.json - jakarta.json-api - 1.1.6 + jakarta.platform + jakarta.jakartaee-api diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java index 5e959ca..a5a8988 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/GetTeamsNotifierConfigurationCommand.java @@ -55,6 +55,7 @@ import fish.payara.internal.notification.admin.NotificationServiceConfiguration; /** + * Gets the configuration of the Microsoft Teams notifier * @author jonathan coustick */ @Service(name = "get-teams-notifier-configuration") diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java index 3de5a8d..ad59191 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/SetTeamsNotifierConfigurationCommand.java @@ -54,9 +54,9 @@ import fish.payara.internal.notification.admin.BaseSetNotifierConfigurationCommand; import fish.payara.internal.notification.admin.NotificationServiceConfiguration; -import java.util.Base64; /** + * Command to set the configuration of Microsoft Teams notifier. * @author jonathan coustick */ @Service(name = "set-teams-notifier-configuration") @@ -72,7 +72,7 @@ }) public class SetTeamsNotifierConfigurationCommand extends BaseSetNotifierConfigurationCommand { - @Param + @Param(primary = true) private String webhookURL; @Override diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java index b840cc1..1811c23 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java @@ -87,7 +87,7 @@ public void handleNotification(PayaraNotification event) { connection.setDoOutput(true); connection.setRequestMethod(HttpMethod.POST); connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); - connection.addRequestProperty(HttpHeaders.USER_AGENT, USER_AGENT); + //connection.addRequestProperty(HttpHeaders.USER_AGENT, USER_AGENT); try (OutputStream outputStream = connection.getOutputStream()) { @@ -98,9 +98,11 @@ public void handleNotification(PayaraNotification event) { if (connection.getResponseCode() == 200) { LOGGER.log(Level.FINE, "Message sent successfully"); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - String response = reader.lines().collect(Collectors.joining("\n")); - LOGGER.log(Level.FINEST, response); + if (LOGGER.isLoggable(Level.FINEST)) { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String response = reader.lines().collect(Collectors.joining("\n")); + LOGGER.log(Level.FINEST, response); + } } else { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); String response = reader.lines().collect(Collectors.joining("\n")); diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/package-info.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/package-info.java new file mode 100644 index 0000000..62301c4 --- /dev/null +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/package-info.java @@ -0,0 +1,45 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ + +/** + * Extension for Payara Notification Service to enable + * sending notifications to teams. + */ +package fish.payara.extensions.notifiers.teams; diff --git a/teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/get-teams-notifier-configuration.1 b/teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/get-teams-notifier-configuration.1 new file mode 100644 index 0000000..af159d3 --- /dev/null +++ b/teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/get-teams-notifier-configuration.1 @@ -0,0 +1,40 @@ +get-teams-notifier-configuration(1) Payara Notificaton Service Extension get-teams-notifier-configuration(1) + +NAME + get-teams-notifier-configuration - get the configuration of the Teams notifier + +SYNOPSIS + get-teams-notifier-configuration [--help] + +DESCRIPTION + The get-teams-notifier-configuration subcommand displays the configuration + of the Microsoft Teams notifier. + +OPTIONS + --help, -? + Displays the help text for the subcommand. + +EXAMPLES + Example 1, Listing the configuration + This example lists the current configuration for the notifier. + + asadmin> get-teams-notifier-configuration + Enabled Noisy Webhook URL + true true https://outlook.office.com/webhook/12345@67890/IncomingWebHook/98765/4321 + + Command get-teams-notifier-configuration executed successfully. + +EXIT STATUS + 0 + subcommand executed successfully + + 1 + error in executing the subcommand + +SEE ALSO + list-commands(1), list-components(1), list-modules(1) + + asadmin(1M) + +Jakarta EE 8 1 Oct 2020 get-teams-notifier-configuration(1) + diff --git a/teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/set-teams-notifier-configuration.1 b/teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/set-teams-notifier-configuration.1 new file mode 100644 index 0000000..237caaa --- /dev/null +++ b/teams-notifier-core/src/main/resources/fish/payara/extensions/notifiers/teams/set-teams-notifier-configuration.1 @@ -0,0 +1,44 @@ +set-teams-notifier-configuration(1) Payara Notificaton Service Extension set-teams-notifier-configuration(1) + +NAME + set-teams-notifier-configuration - set the configuration of the Teams notifier + +SYNOPSIS + set-teams-notifier-configuration [--help] + [--enabled={true|false}] [--dynamic={true|false}] + [webhookUrl] + + +DESCRIPTION + The set-teams-notifier-configuration subcommand sets the configuration + of the Microsoft Teams notifier. + +OPTIONS + --help, -? + Displays the help text for the subcommand. + +EXAMPLES + Example 1, Setting the configuration + This example sets configuration for the notifier + to be enabled dynamically + + asadmin> set-teams-notifier-configuration --enabled true --dynamic true https://outlook.office.com/webhook/12345@67890/IncomingWebHook/98765/4321 + + + Command set-teams-notifier-configuration executed successfully. + +EXIT STATUS + 0 + subcommand executed successfully + + 1 + error in executing the subcommand + +SEE ALSO + list-commands(1), list-components(1), list-modules(1) + + asadmin(1M) + +Jakarta EE 8 1 Oct 2020 set-teams-notifier-configuration(1) + + From 1a15b364667768e89690bf6eece1dc32dcf5e309 Mon Sep 17 00:00:00 2001 From: Jonathan Coustick Date: Fri, 2 Oct 2020 14:00:10 +0100 Subject: [PATCH 7/7] FISH-370 requested change --- .../extensions/notifiers/teams/TeamsNotifier.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java index 1811c23..247949b 100644 --- a/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java +++ b/teams-notifier-core/src/main/java/fish/payara/extensions/notifiers/teams/TeamsNotifier.java @@ -70,9 +70,7 @@ public class TeamsNotifier extends PayaraConfiguredNotifier { private static final Logger LOGGER = Logger.getLogger(TeamsNotifier.class.getPackage().toString()); - - private static final String USER_AGENT = "Payara-Teams-Notifier"; - + private URL url; @Override @@ -87,14 +85,12 @@ public void handleNotification(PayaraNotification event) { connection.setDoOutput(true); connection.setRequestMethod(HttpMethod.POST); connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); - //connection.addRequestProperty(HttpHeaders.USER_AGENT, USER_AGENT); - + connection.connect(); try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(eventToCard(event)); outputStream.flush(); } - connection.connect(); if (connection.getResponseCode() == 200) { LOGGER.log(Level.FINE, "Message sent successfully"); @@ -153,4 +149,4 @@ private byte[] eventToCard(PayaraNotification event) { return mainBuilder.build().toString().getBytes(); } -} \ No newline at end of file +}