MCPcopy Index your code
hub / github.com/apache/tomcat / setContentType

Method setContentType

java/org/apache/coyote/Response.java:669–709  ·  view source on GitHub ↗

Sets the content type. This method must preserve any response charset that may already have been set via a call to response.setContentType(), response.setLocale(), or response.setCharacterEncoding(). @param type the content type

(String type)

Source from the content-addressed store, hash-verified

667 * @param type the content type
668 */
669 public void setContentType(String type) {
670
671 if (type == null) {
672 this.contentType = null;
673 return;
674 }
675
676 MediaType m = null;
677 try {
678 m = MediaType.parseMediaType(new StringReader(type));
679 } catch (IOException ignore) {
680 // Ignore - null test below handles this
681 }
682 if (m == null) {
683 // Invalid - Assume no charset and just pass through whatever
684 // the user provided.
685 this.contentType = type;
686 return;
687 }
688
689 String charsetValue = m.getCharset();
690
691 if (charsetValue == null) {
692 // No charset and we know value is valid as parser was successful
693 // Pass-through user provided value in case user-agent is buggy and
694 // requires specific format
695 this.contentType = type;
696 } else {
697 // There is a charset so have to rebuild content-type without it
698 this.contentType = m.toStringNoCharset();
699 charsetValue = charsetValue.trim();
700 if (!charsetValue.isEmpty()) {
701 charsetHolder = CharsetHolder.getInstance(charsetValue);
702 try {
703 charsetHolder.validate();
704 } catch (UnsupportedEncodingException e) {
705 log.warn(sm.getString("response.encoding.invalid", charsetValue), e);
706 }
707 }
708 }
709 }
710
711 /**
712 * Set the content type without charset.

Callers 10

testSetContentType01Method · 0.95
testSetContentType02Method · 0.95
testSetContentType03Method · 0.95
testSetContentType04Method · 0.95
testSetContentType05Method · 0.95
testSetContentType06Method · 0.95
testSetLocale08Method · 0.95
checkSpecialHeaderMethod · 0.95

Calls 9

parseMediaTypeMethod · 0.95
getCharsetMethod · 0.95
toStringNoCharsetMethod · 0.95
getInstanceMethod · 0.95
validateMethod · 0.65
warnMethod · 0.65
getStringMethod · 0.65
trimMethod · 0.45
isEmptyMethod · 0.45

Tested by 9

testSetContentType01Method · 0.76
testSetContentType02Method · 0.76
testSetContentType03Method · 0.76
testSetContentType04Method · 0.76
testSetContentType05Method · 0.76
testSetContentType06Method · 0.76
testSetLocale08Method · 0.76