/** * Optionally sets the channel of the payload based on the repository. * * @param repository * @param payload */ public void setChannel(RepositoryModel repository, Payload payload) { boolean useProjectChannels = runtimeManager.getSettings().getBoolean(Plugin.SETTING_USE_PROJECT_CHANNELS, false); if (!useProjectChannels) { return; } if (StringUtils.isEmpty(repository.projectPath)) { return; } String defaultChannel = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_CHANNEL, null); if (!StringUtils.isEmpty(defaultChannel)) { payload.setChannel(defaultChannel + "-" + repository.projectPath); } else { payload.setChannel(repository.projectPath); } }
/** * Send a payload message. * * @param payload * @throws IOException */ public void send(Payload payload) throws IOException { String slackUrl = getURL(); payload.setUnfurlLinks(true); if (StringUtils.isEmpty(payload.getUsername())) { payload.setUsername(Constants.NAME); } String defaultChannel = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_CHANNEL, null); if (!StringUtils.isEmpty(defaultChannel) && StringUtils.isEmpty(payload.getChannel())) { // specify the default channel if (defaultChannel.charAt(0) != '#' && defaultChannel.charAt(0) != '@') { defaultChannel = "#" + defaultChannel; } // channels must be lowercase payload.setChannel(defaultChannel.toLowerCase()); } String defaultEmoji = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_EMOJI, null); if (!StringUtils.isEmpty(defaultEmoji)) { if (StringUtils.isEmpty(payload.getIconEmoji()) && StringUtils.isEmpty(payload.getIconUrl())) { // specify the default emoji payload.setIconEmoji(defaultEmoji); } } Gson gson = new GsonBuilder().create(); String json = gson.toJson(payload); log.debug(json); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(slackUrl); post.getParams() .setParameter(CoreProtocolPNames.USER_AGENT, Constants.NAME + "/" + Constants.getVersion()); post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 5000); client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 5000); List<NameValuePair> nvps = new ArrayList<NameValuePair>(1); nvps.add(new BasicNameValuePair("payload", json)); post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); HttpResponse response = client.execute(post); int rc = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == rc) { // replace this with post.closeConnection() after JGit updates to HttpClient 4.2 post.abort(); } else { String result = null; InputStream is = response.getEntity().getContent(); try { byte[] buffer = new byte[8192]; ByteArrayOutputStream os = new ByteArrayOutputStream(); int len = 0; while ((len = is.read(buffer)) > -1) { os.write(buffer, 0, len); } result = os.toString("UTF-8"); } finally { if (is != null) { is.close(); } } log.error("Slack plugin sent:"); log.error(json); log.error("Slack returned:"); log.error(result); throw new IOException(String.format("Slack Error (%s): %s", rc, result)); } }