Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
OLAT CI-CD Testing Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lars Oliver Dam
OLAT CI-CD Testing Project
Commits
41a6fb48
Commit
41a6fb48
authored
13 years ago
by
srosse
Browse files
Options
Downloads
Patches
Plain Diff
OO-77: slightly better URL validation
parent
a946a281
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/org/olat/user/propertyhandlers/URLPropertyHandler.java
+28
-4
28 additions, 4 deletions
...va/org/olat/user/propertyhandlers/URLPropertyHandler.java
src/test/java/org/olat/user/UserPropertiesTest.java
+62
-0
62 additions, 0 deletions
src/test/java/org/olat/user/UserPropertiesTest.java
with
90 additions
and
4 deletions
src/main/java/org/olat/user/propertyhandlers/URLPropertyHandler.java
+
28
−
4
View file @
41a6fb48
...
@@ -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
;
}
}
}
}
This diff is collapsed.
Click to expand it.
src/test/java/org/olat/user/UserPropertiesTest.java
0 → 100644
+
62
−
0
View file @
41a6fb48
/**
* <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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment