/** * ============================================================================= * * ORCID (R) Open Source * http://orcid.org * * Copyright (c) 2012-2014 ORCID, Inc. * Licensed under an MIT-Style License (MIT) * http://orcid.org/open-source-license * * This copyright and license information (including a link to the full license) * shall be included in its entirety in all copies or substantial portion of * the software. * * ============================================================================= */ package org.orcid.core.manager.impl; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.orcid.core.manager.SlackManager; import org.orcid.core.utils.JsonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; /** * * @author Will Simpson * */ public class SlackManagerImpl implements SlackManager { @Value("${org.orcid.core.slack.webhookUrl:}") private String webhookUrl; @Value("${org.orcid.core.slack.channel}") private String channel; private Client client = Client.create(); private static final Logger LOGGER = LoggerFactory.getLogger(SlackManagerImpl.class); @Override public void sendSystemAlert(String message) { if (StringUtils.isNotBlank(webhookUrl)) { Map<String, String> bodyMap = new HashMap<>(); bodyMap.put("text", message); bodyMap.put("channel", channel); String bodyJson = JsonUtils.convertToJsonString(bodyMap); WebResource resource = client.resource(webhookUrl); ClientResponse response = resource.entity(bodyJson).post(ClientResponse.class); int status = response.getStatus(); if (status != 200) { LOGGER.warn("Unable to send message to Slack, status={}, error={}, message={}", new Object[] { status, response.getEntity(String.class), message }); } } } }