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)
| 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. |