001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.web; 018 019import java.nio.charset.Charset; 020import java.util.Locale; 021import java.util.Map; 022 023import org.springframework.boot.context.properties.ConfigurationProperties; 024 025/** 026 * Configuration properties for http encoding. 027 * 028 * @author Stephane Nicoll 029 * @author Brian Clozel 030 * @since 1.2.0 031 */ 032@ConfigurationProperties(prefix = "spring.http.encoding") 033public class HttpEncodingProperties { 034 035 public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 036 037 /** 038 * Charset of HTTP requests and responses. Added to the "Content-Type" header if not 039 * set explicitly. 040 */ 041 private Charset charset = DEFAULT_CHARSET; 042 043 /** 044 * Force the encoding to the configured charset on HTTP requests and responses. 045 */ 046 private Boolean force; 047 048 /** 049 * Force the encoding to the configured charset on HTTP requests. Defaults to true 050 * when "force" has not been specified. 051 */ 052 private Boolean forceRequest; 053 054 /** 055 * Force the encoding to the configured charset on HTTP responses. 056 */ 057 private Boolean forceResponse; 058 059 /** 060 * Locale to Encoding mapping. 061 */ 062 private Map<Locale, Charset> mapping; 063 064 public Charset getCharset() { 065 return this.charset; 066 } 067 068 public void setCharset(Charset charset) { 069 this.charset = charset; 070 } 071 072 public boolean isForce() { 073 return Boolean.TRUE.equals(this.force); 074 } 075 076 public void setForce(boolean force) { 077 this.force = force; 078 } 079 080 public boolean isForceRequest() { 081 return Boolean.TRUE.equals(this.forceRequest); 082 } 083 084 public void setForceRequest(boolean forceRequest) { 085 this.forceRequest = forceRequest; 086 } 087 088 public boolean isForceResponse() { 089 return Boolean.TRUE.equals(this.forceResponse); 090 } 091 092 public void setForceResponse(boolean forceResponse) { 093 this.forceResponse = forceResponse; 094 } 095 096 public Map<Locale, Charset> getMapping() { 097 return this.mapping; 098 } 099 100 public void setMapping(Map<Locale, Charset> mapping) { 101 this.mapping = mapping; 102 } 103 104 boolean shouldForce(Type type) { 105 Boolean force = (type == Type.REQUEST ? this.forceRequest : this.forceResponse); 106 if (force == null) { 107 force = this.force; 108 } 109 if (force == null) { 110 force = (type == Type.REQUEST); 111 } 112 return force; 113 } 114 115 enum Type { 116 117 REQUEST, RESPONSE 118 119 } 120 121}