Skip to content

Commit

Permalink
Test and fix for GRAILS-7444
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Aug 13, 2011
1 parent 94b8cd9 commit cdcb784
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Expand Up @@ -57,6 +57,12 @@ class RenderDynamicMethodTests extends AbstractGrailsControllerTests {
assertEquals "<foo>bar</foo>", response.contentAsString
}

void testRenderTextWithContentTypeAndCharset() {
testCtrl.renderTextWithContentTypeAndCharset()
assertEquals "text/xml;charset=utf-16", response.contentType
assertEquals "<foo>bar</foo>", response.contentAsString
}

void testRenderXml() {
testCtrl.renderXml()
assertEquals "text/xml;charset=utf-8", response.contentType
Expand Down Expand Up @@ -105,6 +111,10 @@ class RenderDynamicMethodTestController {
render(text:"<foo>bar</foo>",contentType:"text/xml", encoding:"utf-16")
}

def renderTextWithContentTypeAndCharset = {
render(text:"<foo>bar</foo>",contentType:"text/xml;charset=utf-16")
}

def renderXml = {
render(contentType:"text/xml") {
foo {
Expand Down
10 changes: 8 additions & 2 deletions grails-web/src/main/groovy/grails/util/GrailsWebUtil.java
Expand Up @@ -19,6 +19,7 @@

import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class GrailsWebUtil {

public static final String DEFAULT_ENCODING = "UTF-8";
private static final String CHARSET_ATTRIBUTE = ";charset=";
private static final Pattern CHARSET_IN_CONTENT_TYPE_REGEXP = Pattern.compile(";\\s*charset\\s*=", Pattern.CASE_INSENSITIVE);

/**
* Looks up a GrailsApplication instance from the ServletContext.
Expand Down Expand Up @@ -178,7 +180,11 @@ public static GroovyObject getControllerFromRequest(HttpServletRequest request)
}

public static String getContentType(String name, String encoding) {
if (StringUtils.isBlank(encoding)) encoding = DEFAULT_ENCODING;
return name + CHARSET_ATTRIBUTE + encoding;
if(CHARSET_IN_CONTENT_TYPE_REGEXP.matcher(name).find()) {
return name;
} else {
if (StringUtils.isBlank(encoding)) encoding = DEFAULT_ENCODING;
return name + CHARSET_ATTRIBUTE + encoding;
}
}
}

0 comments on commit cdcb784

Please sign in to comment.