Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ class DefaultLinkGenerator implements LinkGenerator, PluginManagerAware{
def writer = new StringBuilder()
// prefer URI attribute
if (attrs.get(ATTRIBUTE_URI) != null) {
final base = handleAbsolute(attrs)
if (base != null) {
writer << base
}
else {
final cp = attrs.get(ATTRIBUTE_CONTEXT_PATH)
if (cp == null) cp = getContextPath()
if (cp != null)
writer << cp
}
final uriPath = attrs.get(ATTRIBUTE_URI).toString()
if(!isUriAbsolute(uriPath)){
final base = handleAbsolute(attrs)
if (base != null) {
writer << base
}
else {
final cp = attrs.get(ATTRIBUTE_CONTEXT_PATH)
if (cp == null) cp = getContextPath()
if (cp != null)
writer << cp
}
}
writer << uriPath
}
else {
Expand Down Expand Up @@ -244,6 +246,15 @@ class DefaultLinkGenerator implements LinkGenerator, PluginManagerAware{
}
}

private boolean isUriAbsolute(String uri){
try{
return new URI(uri).absolute
}catch(Exception e){
// assume unparseable URIs are absolute
return true
}
}

/**
* Get the declared URL of the server from config, or guess at localhost for non-production.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ class LinkGeneratorSpec extends Specification {

def baseUrl = "http://myserver.com/foo"
def context = "/bar"
def someAbsoluteUrl = "http://www.grails.org/"
def resource = null
def linkParams = [:]
def pluginManager

def mainCssResource = [dir:'css', file:'main.css']

def "Test absolute link"() {
when:
linkParams.uri = someAbsoluteUrl
linkParams.absolute = true
then:
link == someAbsoluteUrl

when:
linkParams.uri = someAbsoluteUrl

then:
link == someAbsoluteUrl
}

def "Test create link with root URI"() {
when:
linkParams.uri = '/'
Expand Down