Skip to content
Permalink
Browse files
  • Loading branch information
onuba committed Dec 6, 2015
1 parent ca288d6 commit 4bd2c4943c1d7d475041d7ab54ce9fa166eaf129
@@ -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 {
@@ -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.
@@ -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);
}
@@ -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();

}
}
@@ -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
@@ -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)) {
@@ -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(" ");
}
@@ -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;
@@ -8,7 +32,7 @@

/**
* Transform a json site information into Site object
* @author fhernandez
* @author onuba
*
*/
public enum JsonToSiteMapper implements Function<JSONObject, Site>{
@@ -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();
}
@@ -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;

@@ -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()));
}
}
}

@@ -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;
@@ -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>
@@ -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>
@@ -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.
@@ -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.