Skip to content

Commit

Permalink
Better implementation of JENKINS-30386
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsNielsen committed Sep 10, 2015
1 parent 8671c16 commit f8d7db8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
Expand Up @@ -78,8 +78,6 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
String expandedHubAddress = build.getEnvironment(listener).expand(Util.fixNull(hubAddress));
String expandedProjectName = build.getEnvironment(listener).expand(Util.fixNull(projectName));

httpService.setListener(listener);

if (expandedHubAddress.isEmpty()) {
throw new AbortException("Hub address not provided");
}
Expand Down
Expand Up @@ -3,37 +3,28 @@
import hudson.AbortException;
import hudson.model.BuildListener;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.client.fluent.Request;

/**
*
* @author Andrius
*/
public class HttpService implements Serializable {
private transient BuildListener listener;

public String getContentFromUrlAsString(String url) throws AbortException {
log(String.format("Request sent to %s", url));


private static final Logger logger = Logger.getLogger(HttpService.class.getName());

public String getContentFromUrlAsString(String url) throws AbortException {
logger.fine(String.format("Request sent to %s", url));
String output;
try {
output = Request.Get(url).execute().returnContent().asString();
} catch (Exception e) {
log(String.format("Error on url: %s", url));
log(String.format("Exception message is: %s", e.getMessage()));
throw new AbortException(e.getMessage());
logger.log(Level.SEVERE, String.format("[CodeSonar] Error on url: %s", url), e);
throw new AbortException(String.format("[CodeSonar] Error on url: %s%n[CodeSonar] Message is: %s", url, e.getMessage()));
}

return output;
}

public void setListener(BuildListener listener) {
this.listener = listener;
}

private void log(String content) {
if(listener != null) {
listener.getLogger().println("[CodeSonar] "+content);
}
}
}
Expand Up @@ -104,6 +104,7 @@ public void testLogging() throws Exception {

// assert that we have a message in the console log
String log = FileUtils.readFileToString(build.getLogFile());
assertTrue(log.contains("[CodeSonar] Request sent to http://10.10.10.10/index.xml"));
assertTrue(log.contains("[CodeSonar] Error on url: http://10.10.10.10/index.xml"));
assertTrue(log.contains("[CodeSonar] Message is: "));
}
}

0 comments on commit f8d7db8

Please sign in to comment.