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