1 /* 2 * Copyright 2002-2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 module hunt.stomp.support.AbstractHeaderMapper; 18 19 import hunt.stomp.MessageHeaders; 20 import hunt.collection.Map; 21 import hunt.logging; 22 23 24 /** 25 * A base {@link HeaderMapper} implementation. 26 * 27 * @author Stephane Nicoll 28 * @since 4.1 29 * @param (T) type of the instance to and from which headers will be mapped 30 */ 31 abstract class AbstractHeaderMapper(T) : HeaderMapper!(T) { 32 33 private string inboundPrefix = ""; 34 35 private string outboundPrefix = ""; 36 37 38 /** 39 * Specify a prefix to be appended to the message header name for any 40 * user-defined property that is being mapped into the MessageHeaders. 41 * The default is an empty string (no prefix). 42 */ 43 void setInboundPrefix(string inboundPrefix) { 44 this.inboundPrefix = (inboundPrefix !is null ? inboundPrefix : ""); 45 } 46 47 /** 48 * Specify a prefix to be appended to the protocol property name for any 49 * user-defined message header that is being mapped into the protocol-specific 50 * Message. The default is an empty string (no prefix). 51 */ 52 void setOutboundPrefix(string outboundPrefix) { 53 this.outboundPrefix = (outboundPrefix !is null ? outboundPrefix : ""); 54 } 55 56 57 /** 58 * Generate the name to use to set the header defined by the specified 59 * {@code headerName} to the protocol specific message. 60 * @see #setOutboundPrefix 61 */ 62 protected string fromHeaderName(string headerName) { 63 string propertyName = headerName; 64 if (StringUtils.hasText(this.outboundPrefix) && !propertyName.startsWith(this.outboundPrefix)) { 65 propertyName = this.outboundPrefix + headerName; 66 } 67 return propertyName; 68 } 69 70 /** 71 * Generate the name to use to set the header defined by the specified 72 * {@code propertyName} to the {@link MessageHeaders} instance. 73 * @see #setInboundPrefix(string) 74 */ 75 protected string toHeaderName(string propertyName) { 76 string headerName = propertyName; 77 if (StringUtils.hasText(this.inboundPrefix) && !headerName.startsWith(this.inboundPrefix)) { 78 headerName = this.inboundPrefix + propertyName; 79 } 80 return headerName; 81 } 82 83 /** 84 * Return the header value, or {@code null} if it does not exist 85 * or does not match the requested {@code type}. 86 */ 87 88 protected V getHeaderIfAvailable(V)(Map!(string, Object) headers, string name) { 89 Object value = headers.get(name); 90 if (value is null) { 91 return V.init; 92 } 93 94 static if(is(T == Nullable!T)) { 95 Nullable!T o = cast(Nullable!T)value; 96 return o.value; 97 } else { 98 return cast(T) value; 99 } 100 101 // if (!type.isAssignableFrom(value.getClass())) { 102 // version(HUNT_DEBUG) { 103 // trace("Skipping header '" ~ name ~ "': expected type [" ~ type ~ "], but got [" ~ 104 // value.getClass() ~ "]"); 105 // } 106 // return null; 107 // } 108 // else { 109 // return type.cast(value); 110 // } 111 } 112 113 }