001/* 002 * Copyright 2002-2018 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 * https://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.messaging.support; 018 019import java.util.Map; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023 024import org.springframework.messaging.MessageHeaders; 025import org.springframework.util.StringUtils; 026 027/** 028 * A base {@link HeaderMapper} implementation. 029 * 030 * @author Stephane Nicoll 031 * @since 4.1 032 */ 033public abstract class AbstractHeaderMapper<T> implements HeaderMapper<T> { 034 035 protected final Log logger = LogFactory.getLog(getClass()); 036 037 private String inboundPrefix = ""; 038 039 private String outboundPrefix = ""; 040 041 042 /** 043 * Specify a prefix to be appended to the message header name for any 044 * user-defined property that is being mapped into the MessageHeaders. 045 * The default is an empty String (no prefix). 046 */ 047 public void setInboundPrefix(String inboundPrefix) { 048 this.inboundPrefix = (inboundPrefix != null ? inboundPrefix : ""); 049 } 050 051 /** 052 * Specify a prefix to be appended to the protocol property name for any 053 * user-defined message header that is being mapped into the protocol-specific 054 * Message. The default is an empty String (no prefix). 055 */ 056 public void setOutboundPrefix(String outboundPrefix) { 057 this.outboundPrefix = (outboundPrefix != null ? outboundPrefix : ""); 058 } 059 060 061 /** 062 * Generate the name to use to set the header defined by the specified 063 * {@code headerName} to the protocol specific message. 064 * @see #setOutboundPrefix 065 */ 066 protected String fromHeaderName(String headerName) { 067 String propertyName = headerName; 068 if (StringUtils.hasText(this.outboundPrefix) && !propertyName.startsWith(this.outboundPrefix)) { 069 propertyName = this.outboundPrefix + headerName; 070 } 071 return propertyName; 072 } 073 074 /** 075 * Generate the name to use to set the header defined by the specified 076 * {@code propertyName} to the {@link MessageHeaders} instance. 077 * @see #setInboundPrefix(String) 078 */ 079 protected String toHeaderName(String propertyName) { 080 String headerName = propertyName; 081 if (StringUtils.hasText(this.inboundPrefix) && !headerName.startsWith(this.inboundPrefix)) { 082 headerName = this.inboundPrefix + propertyName; 083 } 084 return headerName; 085 } 086 087 /** 088 * Return the header value, or {@code null} if it does not exist 089 * or does not match the requested {@code type}. 090 */ 091 protected <V> V getHeaderIfAvailable(Map<String, Object> headers, String name, Class<V> type) { 092 Object value = headers.get(name); 093 if (value == null) { 094 return null; 095 } 096 if (!type.isAssignableFrom(value.getClass())) { 097 if (logger.isWarnEnabled()) { 098 logger.warn("Skipping header '" + name + "'expected type [" + type + "], but got [" + 099 value.getClass() + "]"); 100 } 101 return null; 102 } 103 else { 104 return type.cast(value); 105 } 106 } 107 108}