Skip to content

Commit

Permalink
o JENKINS-8565: add validation of user input on the settings page
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup@39714 71c3de6d-444a-0410-be80-ed276b4c234a
  • Loading branch information
msteinkogler committed Aug 4, 2011
1 parent 66a8e98 commit 7763d5f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
Expand Up @@ -22,6 +22,8 @@

import java.io.File;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -124,38 +126,30 @@ public String getExcludedFilesRegex() {
}

public FormValidation doCheckBackupPath(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("value") final String value) {
if ((value == null) || value.isEmpty()) {
return FormValidation.error("'Backup Path' is not mandatory.");
@QueryParameter("value") final String path) {
if ((path == null) || path.trim().isEmpty()) {
return FormValidation.error("Backup path must not be empty.");
}

final File backupdir = new File(value);
final File backupdir = new File(path);
if (!backupdir.exists()) {
return FormValidation.warning("The given directory does not exist, it will be created during the first run.");
return FormValidation.warning("The directory does not exist, but will be created before the first run.");
}
if (!backupdir.isDirectory()) {
return FormValidation.error("A file with this name already exists.");
}
return FormValidation.ok();
}

public FormValidation doCheckFullBackupSchedule(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("fullBackupSchedule") final String backupSchedule) {
return validateCronSchedule(backupSchedule);
}

public FormValidation doCheckDiffBackupSchedule(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("diffBackupSchedule") final String backupSchedule) {
return validateCronSchedule(backupSchedule);
return FormValidation.ok();
}

private FormValidation validateCronSchedule(final String backupTime) {
if ((backupTime != null) && !backupTime.isEmpty()) {
public FormValidation doCheckBackupSchedule(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("value") final String schedule) {
if ((schedule != null) && !schedule.isEmpty()) {
String message;
try {
message = new CronTab(backupTime).checkSanity();
message = new CronTab(schedule).checkSanity();
} catch (final ANTLRException e) {
return FormValidation.error(e.getMessage());
return FormValidation.error("Invalid cron schedule. " + e.getMessage());
}
if (message != null) {
return FormValidation.warning(message);
Expand All @@ -167,4 +161,29 @@ private FormValidation validateCronSchedule(final String backupTime) {
}
}

public FormValidation doCheckExcludedFilesRegex(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("value") final String regex) {

if ((regex == null) || (regex.isEmpty())) {
return FormValidation.ok();
}

try {
Pattern.compile(excludedFilesRegex);
} catch (final PatternSyntaxException pse) {
return FormValidation.error("Regex syntax is invalid.");
}

if (regex.trim().isEmpty()) {
return FormValidation.warning("Regex is valid, but consists entirely of whitespaces - is this intentional?");
}

if (!regex.trim().equals(regex)) {
return FormValidation
.warning("Regex is valid, but contains starting and/or trailing whitespaces - is this intentional?");
}

return FormValidation.ok();
}

}
Expand Up @@ -87,17 +87,18 @@ public HudsonBackup(final File backupRoot, final File hudsonHome, final BackupTy
this.moveOldBackupsToZipFile = moveOldBackupsToZipFile;
this.nrMaxStoredFull = nrMaxStoredFull;
this.backupBuildResults = backupBuildResults;
final String tmpExpression = (excludedFilesRegex != null) ? excludedFilesRegex : "";
try {
excludedFilesRegexPattern = Pattern.compile(tmpExpression);
} catch (final PatternSyntaxException pse) {
LOGGER.log(Level.SEVERE, "Regex pattern for excluding files is invalid.", pse);
excludedFilesRegexPattern = null;
if ((excludedFilesRegex != null) && !excludedFilesRegex.isEmpty()) {
try {
excludedFilesRegexPattern = Pattern.compile(excludedFilesRegex);
} catch (final PatternSyntaxException pse) {
LOGGER.log(Level.SEVERE, "Regex pattern for excluding files is invalid, and will be disregarded.", pse);
excludedFilesRegexPattern = null;
}
}

this.backupRoot = backupRoot;
if (!backupRoot.exists()) {
backupRoot.mkdir();
backupRoot.mkdirs();
}

latestFullBackupDate = getLatestFullBackupDate();
Expand Down
Expand Up @@ -31,27 +31,30 @@
<f:entry title="Backup directory" field="backupPath"
help="/plugin/thinBackup/help/help-backupPath.html">
<f:textbox value="${it.configuration.backupPath}" name="backupPath"
checkURL="'${rootUrl}/plugin/thinBackup/checkBackupPath?value='+escape(this.value)"/>
checkUrl="'${rootUrl}/plugin/thinBackup/checkBackupPath?value='+escape(this.value)"/>
</f:entry>

<f:entry title="Backup schedule for full backups" field="fullBackupSchedule"
help="/plugin/thinBackup/help/help-fullBackupSchedule.html">
<f:textbox value="${it.configuration.fullBackupSchedule}" name="fullBackupSchedule"/>
<f:textbox value="${it.configuration.fullBackupSchedule}" name="fullBackupSchedule"
checkUrl="'${rootUrl}/plugin/thinBackup/checkBackupSchedule?value='+escape(this.value)"/>
</f:entry>

<f:entry title="Backup schedule for differential backups" field="diffBackupSchedule"
help="/plugin/thinBackup/help/help-diffBackupSchedule.html">
<f:textbox value="${it.configuration.diffBackupSchedule}" name="diffBackupSchedule"/>
<f:textbox value="${it.configuration.diffBackupSchedule}" name="diffBackupSchedule"
checkUrl="'${rootUrl}/plugin/thinBackup/checkBackupSchedule?value='+escape(this.value)"/>
</f:entry>

<f:entry title="Max number of backup sets" field="nrMaxStoredFull"
help="/plugin/thinBackup/help/help-nrMaxStoredFull.html">
<f:textbox value="${it.configuration.nrMaxStoredFull}" name="nrMaxStoredFull"/>
<f:textbox value="${it.configuration.nrMaxStoredFull}" name="nrMaxStoredFull" />
</f:entry>

<f:entry title="Files excluded from backup (regular expression)" field="excludedFilesRegex"
help="/plugin/thinBackup/help/help-excludedFilesRegex.html">
<f:textbox value="${it.configuration.excludedFilesRegex}" name="excludedFilesRegex"/>
<f:textbox value="${it.configuration.excludedFilesRegex}" name="excludedFilesRegex"
checkUrl="'${rootUrl}/plugin/thinBackup/checkExcludedFilesRegex?value='+escape(this.value)"/>
</f:entry>

<f:optionalBlock title="Backup build results"
Expand Down

0 comments on commit 7763d5f

Please sign in to comment.