Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
onuba committed Dec 6, 2015
1 parent ca288d6 commit 4bd2c49
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 31 deletions.
59 changes: 47 additions & 12 deletions src/main/java/hudson/plugins/sitemonitor/SiteMonitorDescriptor.java
Expand Up @@ -153,13 +153,13 @@ public final Publisher newInstance(final StaplerRequest request,
final Object sitesObject = json.get("sites");
if (sitesObject instanceof JSONObject) {

sites.add(JsonToSiteMapper.INSTANCE.apply(json.getJSONObject("sites")));
addSite(sites, json.getJSONObject("sites"));

} else if (sitesObject instanceof JSONArray) {

for (Object siteObject : (JSONArray) sitesObject) {
if (siteObject instanceof JSONObject) {
sites.add(JsonToSiteMapper.INSTANCE.apply((JSONObject) siteObject));
addSite(sites, (JSONObject) siteObject);
}
}
} else {
Expand All @@ -169,14 +169,17 @@ public final Publisher newInstance(final StaplerRequest request,
return new SiteMonitorRecorder(sites);
}

/*private String checkUrl(String url) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
return url;
}*/
/**
* Ignore sites with blank url
* @param sites sites list
* @param siteObject site object
*/
private void addSite(List<Site> sites, JSONObject siteObject) {

if (!StringUtils.isBlank(siteObject.getString("timeout"))) {
sites.add(JsonToSiteMapper.INSTANCE.apply(siteObject));
}
}

/**
* Handles SiteMonitor global configuration per Jenkins instance.
Expand Down Expand Up @@ -215,7 +218,7 @@ public final FormValidation doCheckUrl(@QueryParameter final String value) {
* @return true if value is a valid comma-separated response codes, false
* otherwise
*/
public final FormValidation doCheckResponseCodes(
public final FormValidation doCheckGlobalResponseCodes(
@QueryParameter final String value) {
return mValidator.validateResponseCodes(value);
}
Expand All @@ -225,8 +228,40 @@ public final FormValidation doCheckResponseCodes(
* the value to validate
* @return true if value is a valid timeout, false otherwise
*/
public final FormValidation doCheckTimeout(
public final FormValidation doCheckGlobalTimeout(
@QueryParameter final String value) {
return mValidator.validateTimeout(value);
}

/**
* @param value
* the value to validate
* @return true if value is a valid comma-separated response codes, false
* otherwise
*/
public final FormValidation doCheckResponseCodes(
@QueryParameter final String value) {

if (StringUtils.isNotBlank(value)) {
return mValidator.validateResponseCodes(value);
}

return FormValidation.ok();
}

/**
* @param value
* the value to validate
* @return true if value is a valid timeout, false otherwise
*/
public final FormValidation doCheckTimeout(
@QueryParameter final String value) {

if (StringUtils.isNotBlank(value)) {
return mValidator.validateTimeout(value);
}

return FormValidation.ok();

}
}
36 changes: 26 additions & 10 deletions src/main/java/hudson/plugins/sitemonitor/SiteMonitorValidator.java
Expand Up @@ -33,35 +33,52 @@

/**
* This class provides validation functions.
*
* @author cliffano
*/
public class SiteMonitorValidator {

/**
* Validates a URL.
*
* @param url
* the web site URL
* @return false when URL is malformed, true otherwise
*/
public final FormValidation validateUrl(final String url) {
FormValidation validation = FormValidation.ok();
if (StringUtils.isNotBlank(url)) {
if (url.startsWith("http://") || url.startsWith("https://")) {

// If a protocol is defined, it has to be valid
if (url.contains("://")) {

if (!url.startsWith("http://") && !url.startsWith("https://")) {
validation = FormValidation.error(Messages.SiteMonitor_Error_PrefixOfURL());
}
} else {

String internalUrl = url;

// By default use http://, the http:// will be added on newInstance once the
// configuration is saved
if (!url.startsWith("http://") && !url.startsWith("https://")) {
internalUrl = "http://" + internalUrl;
}

try {
new URL(url);
new URL(internalUrl);
} catch (MalformedURLException mue) {
validation = FormValidation.error(Messages.SiteMonitor_Error_MalformedURL());
}
} else {
validation = FormValidation
.error(Messages.SiteMonitor_Error_PrefixOfURL());
}
}
}

return validation;
}

/**
* Validates HTTP connection timeout value.
*
* @param timeout
* the time out value in seconds
* @return true when timeout value is valid, false otherwise
Expand All @@ -78,12 +95,12 @@ public final FormValidation validateTimeout(final String timeout) {

/**
* Validates a comma-separated HTTP response codes.
*
* @param responseCodes
* the response codes
* @return true if all response codes are valid, false otherwise
*/
public final FormValidation validateResponseCodes(
final String responseCodes) {
public final FormValidation validateResponseCodes(final String responseCodes) {
FormValidation validation = FormValidation.ok();
List<String> invalidResponseCodes = new ArrayList<String>();
if (StringUtils.isNotBlank(responseCodes)) {
Expand All @@ -94,8 +111,7 @@ public final FormValidation validateResponseCodes(
}
}
if (!invalidResponseCodes.isEmpty()) {
StringBuffer errorMessage = new StringBuffer(
Messages.SiteMonitor_Error_InvalidResponseCode());
StringBuffer errorMessage = new StringBuffer(Messages.SiteMonitor_Error_InvalidResponseCode());
for (String invalidResponseCode : invalidResponseCodes) {
errorMessage.append(invalidResponseCode).append(" ");
}
Expand Down
@@ -1,5 +1,29 @@
/**
* Copyright (c) 2015 Francisco Hernandez Suarez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.sitemonitor.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import hudson.plugins.sitemonitor.model.Site;
import hudson.plugins.sitemonitor.model.Site.SiteBuilder;
import net.sf.json.JSONObject;
Expand All @@ -8,7 +32,7 @@

/**
* Transform a json site information into Site object
* @author fhernandez
* @author onuba
*
*/
public enum JsonToSiteMapper implements Function<JSONObject, Site>{
Expand All @@ -17,9 +41,19 @@ public enum JsonToSiteMapper implements Function<JSONObject, Site>{

public Site apply(JSONObject json) {

final SiteBuilder siteBuilder = Site.builder(((JSONObject) json).getString("url")).
timeout(((JSONObject) json).getInt("timeout")).
successResponseCodes(JsonToSuccessResponseList.INSTANCE.apply((JSONObject) json));
String url = json.getString("url");

if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}

final SiteBuilder siteBuilder = Site.builder(url);

if (!StringUtils.isBlank(json.getString("timeout")) && NumberUtils.isDigits(json.getString("timeout"))) {
siteBuilder.timeout(((JSONObject) json).getInt("timeout"));
}

siteBuilder.successResponseCodes(JsonToSuccessResponseList.INSTANCE.apply((JSONObject) json));

return siteBuilder.build();
}
Expand Down
@@ -1,8 +1,30 @@
/**
* Copyright (c) 2015 Francisco Hernandez Suarez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.sitemonitor.mapper;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import net.sf.json.JSONObject;

Expand All @@ -26,7 +48,9 @@ public List<Integer> apply(JSONObject json) {
if (!StringUtils.isBlank(json.getString("successResponseCodes"))) {

for (String responseCode : json.getString("successResponseCodes").split(",")) {
successResponseCodes.add(Integer.parseInt(responseCode.trim()));
if (NumberUtils.isDigits(responseCode)) {
successResponseCodes.add(Integer.parseInt(responseCode.trim()));
}
}
}

Expand Down
@@ -1,3 +1,24 @@
/**
* Copyright (c) 2015 Francisco Hernandez Suarez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.sitemonitor.mapper;

import java.util.List;
Expand Down
Expand Up @@ -8,7 +8,7 @@
<f:entry title="${%Success Response Codes}" help="/plugin/sitemonitor/successresponsecodes_sites.html">
<f:textbox name="successResponseCodes" value="${site.successResponseCodesCsv}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkResponseCodes?value='+encode(this.value)"/>
</f:entry>
<f:entry title="${%Timeout in seconds}" help="/plugin/sitemonitor/timeout.html">
<f:entry title="${%Timeout in seconds}" help="/plugin/sitemonitor/timeout_sites.html">
<f:textbox name="timeout" value="${site.timeout}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkTimeout?value='+encode(this.value)"/>
</f:entry>
<f:entry>
Expand Down
Expand Up @@ -2,12 +2,12 @@
<f:section title="${%Site Monitor}">
<f:entry title="${%Success Response Codes}" help="/plugin/sitemonitor/successresponsecodes.html">
<table style="width: 100%;">
<f:textbox name="successResponseCodes" value="${descriptor.successResponseCodesCsv}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkResponseCodes?value='+encode(this.value)"/>
<f:textbox name="successResponseCodes" value="${descriptor.successResponseCodesCsv}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkGlobalResponseCodes?value='+encode(this.value)"/>
</table>
</f:entry>
<f:entry title="${%Timeout in seconds}" help="/plugin/sitemonitor/timeout.html">
<table style="width: 100%;">
<f:textbox name="timeout" value="${descriptor.timeout}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkTimeout?value='+encode(this.value)"/>
<f:textbox name="timeout" value="${descriptor.timeout}" checkUrl="'${rootURL}/publisher/SiteMonitorRecorder/checkGlobalTimeout?value='+encode(this.value)"/>
</table>
</f:entry>
</f:section>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/timeout_sites.html
@@ -0,0 +1 @@
The HTTP connection timeout value in seconds. This timeout will apply to this site, if you not specify nothing here the values from global configuration will be used.
3 changes: 2 additions & 1 deletion src/main/webapp/url.html
@@ -1 +1,2 @@
The URL address of the web site to be monitored. URL should start with http:// or https:// .
The URL address of the web site to be monitored. URL should start with http:// or https://.
By default http:// will be used.

0 comments on commit 4bd2c49

Please sign in to comment.