Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added regex support. Also, see existing JIRA ticket. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,13 @@ public final Publisher newInstance(final StaplerRequest request,

Object sitesObject = json.get("sites");
if (sitesObject instanceof JSONObject) {
for (Object siteObject : json.getJSONObject("sites").values()) {
String url = String.valueOf(siteObject);
sites.add(new Site(url));
}
Site site = toSite((JSONObject) sitesObject);
sites.add(site);
} else if (sitesObject instanceof JSONArray) {
for (Object siteObject : (JSONArray) sitesObject) {
if (siteObject instanceof JSONObject) {
String url = ((JSONObject) siteObject).getString("url");
sites.add(new Site(url));
Site site = toSite((JSONObject) siteObject);
sites.add(site);
}
}
} else {
Expand All @@ -170,6 +168,19 @@ public final Publisher newInstance(final StaplerRequest request,
return new SiteMonitorRecorder(sites);
}

/**
* Converts the json object to the Site.
* @param siteObject
* the siteObject submitted
* @return the new Site
*/
private Site toSite(JSONObject siteObject) {
String url = siteObject.getString("url");
String regex = siteObject.getString("regularExpression");
boolean regexFlag = siteObject.getBoolean("failWhenRegexNotFound");
return new Site(url, regex, regexFlag);
}

/**
* Handles SiteMonitor global configuration per Jenkins instance.
* @param request
Expand Down Expand Up @@ -205,6 +216,15 @@ public final FormValidation doCheckUrl(@QueryParameter final String value) {
return mValidator.validateUrl(value);
}

/**
* @param value
* the value to validate
* @return true if value is a valid Regex, false otherwise
*/
public final FormValidation doCheckRegex(@QueryParameter final String value) {
return mValidator.validateRegex(value);
}

/**
* @param value
* the value to validate
Expand All @@ -225,4 +245,13 @@ public final FormValidation doCheckTimeout(
@QueryParameter final String value) {
return mValidator.validateTimeout(value);
}

/**
* @return the url to the help fule
*/
@Override
public String getHelpFile() {
return "/plugin/sitemonitor/url.html";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
Expand All @@ -48,8 +47,12 @@
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.io.IOUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
* Performs the web site monitoring process.
Expand Down Expand Up @@ -161,6 +164,13 @@ public final boolean perform(final AbstractBuild<?, ?> build,
} else {
status = Status.ERROR;
}

if (status == Status.UP && site.getRegularExpressionPattern() != null) {
Result regexResult = validateWithRegularExpression(site, connection);
note = regexResult.getNote();
status = regexResult.getStatus();
}

} catch (SocketTimeoutException ste) {
listener.getLogger().println(ste + " - " + ste.getMessage());
status = Status.DOWN;
Expand Down Expand Up @@ -203,6 +213,42 @@ public final boolean perform(final AbstractBuild<?, ?> build,
return !hasFailure;
}

/**
* @param site
* the Site configuration object
* @param connection
* the connection to the site
* @return the Result with the status and note
* @throws IOException
* When any IO fails
*/
private Result validateWithRegularExpression(Site site, HttpURLConnection connection) throws IOException {
String page = IOUtils.toString(connection.getInputStream());
Matcher matcher = site.getRegularExpressionPattern().matcher(page);
boolean found = matcher.find();
String foundString = null;
boolean exact = false;
if (found) {
foundString = matcher.group();
if (site.getRegularExpression().equals(foundString)) {
exact = true;
}
}
Status status = Status.UP;
if (site.isFailWhenRegexNotFound() != found) {
status = Status.DOWN;
}
String note;
if (exact) {
note = Messages.SiteMonitor_Status_RegularExpressionExactMatch(foundString);
} else if (found) {
note = Messages.SiteMonitor_Status_RegularExpressionFound(site.getRegularExpression(), foundString);
} else {
note = Messages.SiteMonitor_Status_RegularExpressionNotFound(site.getRegularExpression());
}
return new Result(null, 0, status, note);
}

/**
* Gets the required monitor service.
* @return the BuildStepMonitor
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/hudson/plugins/sitemonitor/SiteMonitorValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
Expand Down Expand Up @@ -60,6 +61,24 @@ public final FormValidation validateUrl(final String url) {
return validation;
}

/**
* Validates a regex.
* @param regex
* the regex
* @return false when regex is malformed, true otherwise
*/
public final FormValidation validateRegex(final String regex) {
FormValidation validation = FormValidation.ok();
if (StringUtils.isNotBlank(regex)) {
try {
Pattern.compile(regex);
} catch (Exception e) {
validation = FormValidation.error(e, e.getLocalizedMessage());
}
}
return validation;
}

/**
* Validates HTTP connection timeout value.
* @param timeout
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/hudson/plugins/sitemonitor/model/Site.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
*/
package hudson.plugins.sitemonitor.model;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
* This class keeps the details of the web site to be monitored.
* @author cliffano
Expand All @@ -32,6 +36,37 @@ public class Site {
*/
private String mUrl;

/**
* The regular expression to check.
*/
private String mRegularExpression;

/**
* The pattern for the expression to check.
*/
transient private Pattern mRegularExpressionPattern;

/**
* The flag for checking the regex.
*/
private boolean mFailWhenRegexNotFound = true;

/**
* Constructs a Site with specified details.
* @param url
* the web site URL
* @param regularExpression
* the regular expression to check
* @param failWhenRegexNotFound
* the flag for checking the regex
*/
public Site(final String url, final String regularExpression, final boolean failWhenRegexNotFound) {
mUrl = url;
mRegularExpression = regularExpression;
mFailWhenRegexNotFound = failWhenRegexNotFound;
setRegexPattern();
}

/**
* Constructs a Site with specified details.
* @param url
Expand All @@ -41,10 +76,51 @@ public Site(final String url) {
mUrl = url;
}

/**
* Sets the regex pattern based on the regex.
*/
private void setRegexPattern() {
if (!StringUtils.isEmpty(mRegularExpression)) {
mRegularExpressionPattern = Pattern.compile(mRegularExpression);
} else {
mRegularExpressionPattern = null;
}
}

/**
* Part of the serialization strategy.
* @return this
*/
Object readResolve() {
setRegexPattern();
return this;
}

/**
* @return the web site URL
*/
public final String getUrl() {
return mUrl;
}

/**
* @return the regular expression to check
*/
public final String getRegularExpression() {
return mRegularExpression;
}

/**
* @return the Pattern for the regular expression to check
*/
public final Pattern getRegularExpressionPattern() {
return mRegularExpressionPattern;
}

/**
* @return the flag for checking the regex
*/
public final boolean isFailWhenRegexNotFound() {
return mFailWhenRegexNotFound;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/sitemonitor/Messages.properties
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ SiteMonitor.Error.PrefixOfURL=URL must start with http:// or https://
SiteMonitor.Error.TimeoutIsBlank=Timeout value must be provided
SiteMonitor.Error.TimeoutIsNotDigit=Timeout value must be a number
SiteMonitor.Error.InvalidResponseCode=Invalid response code(s):
SiteMonitor.Status.RegularExpressionNotFound=Regular expression "{0}" was not found.
SiteMonitor.Status.RegularExpressionExactMatch=Exact text "{0}" was found.
SiteMonitor.Status.RegularExpressionFound=Regular expression "{0}" was found.<br/>Matching text was "{1}".
SiteMonitor.Console.URL=URL:
SiteMonitor.Console.ResponseCode=response code:
SiteMonitor.Console.Status=status:
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
<f:entry title="">
<f:repeatable var="site" name="sites" items="${instance.sites}" noAddButton="true" minimum="1">
<table style="width: 100%;">
<f:entry title="${%URL}" help="/plugin/sitemonitor/url.html">
<f:entry title="${%URL}">
<f:textbox name="url" value="${site.url}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkUrl?value='+encode(this.value)"/>
<div style="text-align: right;">
<input type="button" value="${%Delete}" class="repeatable-delete show-if-not-only"/>
</f:entry>
<f:advanced>
<f:entry title="${%Validate with regular expression}">
<f:textbox name="regularExpression" value="${site.regularExpression}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkRegex?value='+encode(this.value)"/>
</f:entry>
<f:entry title="${%Fail when Regular Expression is NOT found}">
<f:checkbox name="failWhenRegexNotFound" field="failWhenRegexNotFound" default="true" />
</f:entry>
</f:advanced>
<f:entry title="">
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:entry>
<f:entry>
Expand Down
7 changes: 6 additions & 1 deletion src/main/webapp/url.html
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
The URL address of the web site to be monitored. URL should start with http:// or https:// .
Provides monitoring of one or more sites. Each site can be configured with
<ul>
<li>The URL address of the web site to be monitored. URL should start with http:// or https:// .</li>
<li>An optional Regular Expression to check for on the website's page.</li>
<li>A flag indicating whether to fail/succeed when the regular expression is found.</li>
</ul>
12 changes: 12 additions & 0 deletions src/test/java/hudson/plugins/sitemonitor/model/SiteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ public void testGetUrlShouldGiveExpectedUrlValue() {
site = new Site("http://hudson-ci.org");
assertEquals("http://hudson-ci.org", site.getUrl());
}

public void testGetRegularExpressionShouldGiveExpectedUrlValue() {
site = new Site(null, "regex1", false);
assertEquals("regex1", site.getRegularExpression());
assertEquals("regex1", site.getRegularExpressionPattern().pattern());
}

public void testGetRegularExpressionFlagShouldGiveExpectedUrlValue() {
site = new Site(null, null, false);
assertEquals(false, site.isFailWhenRegexNotFound());
}

}