diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/RocketChatNotifier.java b/src/main/java/jenkins/plugins/rocketchatnotifier/RocketChatNotifier.java index a8ee0e98..b2f251f6 100644 --- a/src/main/java/jenkins/plugins/rocketchatnotifier/RocketChatNotifier.java +++ b/src/main/java/jenkins/plugins/rocketchatnotifier/RocketChatNotifier.java @@ -1,4 +1,5 @@ package jenkins.plugins.rocketchatnotifier; + import edu.umd.cs.findbugs.annotations.SuppressWarnings; import hudson.EnvVars; import hudson.Extension; @@ -19,6 +20,7 @@ import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.export.Exported; +import sun.security.validator.ValidatorException; import java.io.IOException; import java.util.Map; @@ -328,7 +330,7 @@ public FormValidation doTestConnection(@QueryParameter("rocketServer") final Str } RocketChatClient rocketChatClient = getRocketChatClient(targetServerUrl, targetUsername, targetPassword); String message = "RocketChat/Jenkins plugin: you're all set on " + targetBuildServerUrl; - boolean success = false; + boolean success; try { rocketChatClient.send(targetChannel, message); success = true; @@ -336,8 +338,10 @@ public FormValidation doTestConnection(@QueryParameter("rocketServer") final Str success = false; } return success ? FormValidation.ok("Success") : FormValidation.error("Failure"); + } catch (ValidatorException e) { + return FormValidation.error(e, "SSL error"); } catch (Exception e) { - return FormValidation.error("Client error : " + e.getMessage()); + return FormValidation.error(e, "Client error : " + e.getMessage()); } } } diff --git a/src/main/java/jenkins/plugins/rocketchatnotifier/RocketClientImpl.java b/src/main/java/jenkins/plugins/rocketchatnotifier/RocketClientImpl.java index ade3e061..7074b8fa 100644 --- a/src/main/java/jenkins/plugins/rocketchatnotifier/RocketClientImpl.java +++ b/src/main/java/jenkins/plugins/rocketchatnotifier/RocketClientImpl.java @@ -11,18 +11,22 @@ import jenkins.plugins.rocketchatnotifier.model.Room; import jenkins.plugins.rocketchatnotifier.model.Rooms; import org.json.JSONObject; +import sun.security.validator.ValidatorException; import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.logging.Logger; /** * Created by mreinhardt on 08.09.16. */ public class RocketClientImpl implements RocketClient { + private static final Logger LOG = Logger.getLogger(RocketClientImpl.class.getName()); + public static final String API_PATH = "/api/"; private RocketChatClient client; @@ -37,8 +41,11 @@ public RocketClientImpl(String serverUrl, String user, String password, String c public boolean publish(String message) { try { + LOG.fine("Starting sending message to channel " + this.channel); this.client.send(this.channel, message); return true; + } catch (ValidatorException e) { + return false; } catch (IOException e) { return false; } @@ -94,11 +101,11 @@ private T authenticatedGet(String method, Class reponseClass) throws IOEx } } - private void authenticatedPost(String method, Object request) throws IOException { + private void authenticatedPost(String method, Object request) throws ValidatorException, IOException { this.authenticatedPost(method, request, (Class) null); } - private T authenticatedPost(String method, Object request, Class reponseClass) throws IOException { + private T authenticatedPost(String method, Object request, Class reponseClass) throws ValidatorException, IOException { try { HttpResponse e = Unirest.post(this.serverUrl + method).header("X-Auth-Token", this.xAuthToken).header("X-User-Id", this.xUserId).header("Content-Type", "application/json").body(this.jacksonObjectMapper.writeValueAsString(request)).asString(); if (e.getStatus() == 401) { @@ -151,7 +158,7 @@ private JSONObject getVersions() throws IOException { return this.lazyVersions; } - public void send(String roomName, String message) throws IOException { + public void send(String roomName, String message) throws ValidatorException, IOException { Room room = this.getRoom(roomName); if (room == null) { throw new IOException(String.format("unknown room : %s", new Object[]{roomName})); @@ -160,7 +167,7 @@ public void send(String roomName, String message) throws IOException { } } - public void send(Room room, String message) throws IOException { + public void send(Room room, String message) throws ValidatorException, IOException { this.authenticatedPost("rooms/" + room.get_id() + "/send", new Message(message)); }