diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/field/HyperlinkField.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/field/HyperlinkField.java index 5b3017c6a22480fb7130ad0616243a3cbc8b2c36..38039e760765c479b4a0507fdfa79fdf77df9297 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/field/HyperlinkField.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/field/HyperlinkField.java @@ -16,25 +16,72 @@ package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.field; +import com.extjs.gxt.ui.client.event.ComponentEvent; import com.extjs.gxt.ui.client.widget.form.TextField; +import com.extjs.gxt.ui.client.widget.form.Validator; -import ch.systemsx.cisd.openbis.generic.shared.basic.CommonValidationConstants; +import ch.systemsx.cisd.openbis.generic.shared.basic.ValidationUtilities.HyperlinkValidationHelper; /** * A {@link TextField} extension for registering text (<code>String</code>) that will be rendered as - * a link. The text characters are validated. + * a link. Default protocol ("http://") is added on blur if user doesn't provide one.<br> + * The text characters are validated. * * @author Piotr Buczek */ public class HyperlinkField extends VarcharField { + public HyperlinkField(final String label, final boolean mandatory) { super(label, mandatory); - setRegex(CommonValidationConstants.HYPERLINK_REGEXP); + // default auto validation is bad if we want to add a prefix automatically + // we use validation on blur here but the other option would be to increase validation delay + setAutoValidate(false); + setValidator(new HyperlinkValidator()); + setValidateOnBlur(true); setEmptyText("Hyperlink"); getMessages().setBlankText("Hyperlink required"); - getMessages().setRegexText("Hyperlink has improper format"); } + @Override + protected void onFocus(ComponentEvent be) + { + // clearing invalid messages on focus is needed because automatic validation is turned off + super.onFocus(be); + clearInvalid(); + } + + /** {@link Validator} for external hyperlink value. */ + private class HyperlinkValidator implements Validator<String, HyperlinkField> + { + private final static String PROTOCOL_PART = "://"; + + private final static String DEFAULT_PROTOCOL = "http://"; + + public String validate(HyperlinkField field, final String fieldValue) + { + // add default protocol if none is provided + String validatedValue = fieldValue; + if (validatedValue.contains(PROTOCOL_PART) == false) + { + validatedValue = DEFAULT_PROTOCOL + fieldValue; + field.setRawValue(validatedValue); + } + + // validate protocols and format + if (HyperlinkValidationHelper.isProtocolValid(validatedValue) == false) + { + return "Hyperlink should start with one of the following protocols: " + + HyperlinkValidationHelper.getValidProtocolsAsString(); + } + if (HyperlinkValidationHelper.isFormatValid(validatedValue) == false) + { + return "Hyperlink has improper format"; + } + // validated value is valid + return null; + } + + } } \ No newline at end of file diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidator.java index e818093190d12e187edd9de74608caa3af9e77ec..2f097e5ae5cdece1cb86e2ea1c30158a496f8ba3 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidator.java @@ -33,7 +33,7 @@ import ch.systemsx.cisd.common.collections.IToStringConverter; import ch.systemsx.cisd.common.exceptions.UserFailureException; import ch.systemsx.cisd.common.utilities.PropertyUtils; import ch.systemsx.cisd.common.utilities.PropertyUtils.Boolean; -import ch.systemsx.cisd.openbis.generic.shared.basic.CommonValidationConstants; +import ch.systemsx.cisd.openbis.generic.shared.basic.ValidationUtilities.HyperlinkValidationHelper; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialIdentifier; import ch.systemsx.cisd.openbis.generic.shared.dto.PropertyTypePE; import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyPE; @@ -301,14 +301,22 @@ public final class PropertyValidator implements IPropertyValueValidator public final String validate(final String value) throws UserFailureException { assert value != null : "Unspecified value."; - if (value.matches(CommonValidationConstants.HYPERLINK_REGEXP)) + + // validate protocols and format + if (HyperlinkValidationHelper.isProtocolValid(value) == false) { - return value; - } else + throw UserFailureException.fromTemplate( + "Hyperlink '%s' should start with one of the following protocols: '%s'", + value, HyperlinkValidationHelper.getValidProtocolsAsString()); + } + if (HyperlinkValidationHelper.isFormatValid(value) == false) { throw UserFailureException.fromTemplate( "Hyperlink value '%s' has improper format.", value); } + + // validated value is valid + return value; } } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/CommonValidationConstants.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/CommonValidationConstants.java deleted file mode 100644 index 5006337b1bae1f6e1108cdb61fa2d402f614f313..0000000000000000000000000000000000000000 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/CommonValidationConstants.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2009 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.openbis.generic.shared.basic; - -/** - * Common constants to be used both on client and server side for field validation. - * - * @author Piotr Buczek - */ -public class CommonValidationConstants -{ - - // A complete regexp for hyperlink may be very complex like this one: - // http://internet.ls-la.net/folklore/url-regexpr.html - // We should rather use sth simple for "safe" hyperlink - with no <>()[] - /** a simple regular expression for "safe" hyperlinks */ - public static final String HYPERLINK_REGEXP = "[^<>()\\[\\]]*"; -} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/ValidationUtilities.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/ValidationUtilities.java new file mode 100644 index 0000000000000000000000000000000000000000..e0ce199360ded6592f3f6feab6159e754eab68c5 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/ValidationUtilities.java @@ -0,0 +1,66 @@ +/* + * Copyright 2009 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.shared.basic; + +import java.util.Arrays; + +/** + * Utility class to be used both on client and server side for field validation. + * + * @author Piotr Buczek + */ +public class ValidationUtilities +{ + + /** A helper class for external hyperlink validation. */ + public static class HyperlinkValidationHelper + { + // A complete regexp for hyperlink may be very complex like this one: + // http://internet.ls-la.net/folklore/url-regexpr.html + // We should rather use sth simple for "safe" hyperlink - with no <>()[] + /** a simple regular expression for "safe" hyperlinks */ + private static final String HYPERLINK_REGEXP = "[^<>()\\[\\]]*"; + + private static final String[] HYPERLINK_VALID_PROTOCOLS = + { "http://", "https://", "ftp://" }; + + /** @return does given <var>string</var> start with a valid external hyperlink protocol */ + public static final boolean isProtocolValid(String string) + { + for (String protocol : HYPERLINK_VALID_PROTOCOLS) + { + if (string.indexOf(protocol) == 0) + { + return true; + } + } + return false; + } + + /** @return does given <var>string</var> contain a hyperlink value in a proper format */ + public static final boolean isFormatValid(String string) + { + return string.matches(HyperlinkValidationHelper.HYPERLINK_REGEXP); + } + + public static final String getValidProtocolsAsString() + { + return Arrays.toString(HYPERLINK_VALID_PROTOCOLS); + } + + } +} \ No newline at end of file