Skip to content
Snippets Groups Projects
Commit 41a6fb48 authored by srosse's avatar srosse
Browse files

OO-77: slightly better URL validation

parent a946a281
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package org.olat.user.propertyhandlers; package org.olat.user.propertyhandlers;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
...@@ -93,17 +94,40 @@ public class URLPropertyHandler extends Generic127CharTextPropertyHandler { ...@@ -93,17 +94,40 @@ public class URLPropertyHandler extends Generic127CharTextPropertyHandler {
public boolean isValidValue(String value, ValidationError validationError, Locale locale) { public boolean isValidValue(String value, ValidationError validationError, Locale locale) {
if ( ! super.isValidValue(value, validationError, locale)) return false; if ( ! super.isValidValue(value, validationError, locale)) return false;
boolean allOk = true;
if (StringHelper.containsNonWhitespace(value)) { if (StringHelper.containsNonWhitespace(value)) {
// check url address syntax // check url address syntax
try { try {
new URL(value); URL url = new URL(value);
url.toURI();
if(!"http".equals(url.getProtocol()) && !"https".equals(url.getProtocol())) {
validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid");
allOk &= false;
}
if(!StringHelper.containsNonWhitespace(url.getAuthority())) {
validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid");
allOk &= false;
}
if(!StringHelper.containsNonWhitespace(url.getHost())) {
validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid");
allOk &= false;
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid"); validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid");
return false; allOk &= false;
} catch (URISyntaxException e) {
validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid");
allOk &= false;
}
if(!value.startsWith("http://") &&!value.startsWith("https://")) {
validationError.setErrorKey(i18nFormElementLabelKey() + ".error.valid");
allOk &= false;
} }
} }
// everthing ok
return true; return allOk;
} }
} }
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.user;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import org.olat.core.gui.components.form.ValidationError;
import org.olat.user.propertyhandlers.URLPropertyHandler;
/**
*
* Description:<br>
* Test the validation method of PropertyHandler(s)
*
* <P>
* Initial Date: 23 janv. 2012 <br>
*
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*/
public class UserPropertiesTest {
@Test
public void testURLPropertyHandlerValidation() {
URLPropertyHandler urlHandler = new URLPropertyHandler();
ValidationError error = new ValidationError();
boolean valid1 = urlHandler.isValidValue("http://www.openolat.org", error, null);
assertTrue(valid1);
boolean valid2 = urlHandler.isValidValue("http://test.ch", error, null);
assertTrue(valid2);
boolean valid3 = urlHandler.isValidValue("http://localhost", error, null);
assertTrue(valid3);
boolean invalid1 = urlHandler.isValidValue("http:www.openolat.org", error, null);
assertFalse(invalid1);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment