001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022import java.util.Map;
023import java.util.TreeMap;
024
025import javax.activation.DataHandler;
026import javax.activation.DataSource;
027
028import org.apache.camel.Attachment;
029import org.apache.camel.util.CollectionHelper;
030
031public class DefaultAttachment implements Attachment {
032    private Map<String, Object> headers;
033    private DataHandler dataHandler;
034
035    public DefaultAttachment(DataHandler dh) {
036        dataHandler = dh;
037    }
038
039    public DefaultAttachment(DataSource ds) {
040        dataHandler = new DataHandler(ds);
041    }
042
043    @Override
044    public DataHandler getDataHandler() {
045        return dataHandler;
046    }
047
048    @Override
049    public String getHeader(String name) {
050        if (headers != null) {
051            Object headerObject = headers.get(name);
052            if (headerObject instanceof String) {
053                return (String)headerObject;
054            } else if (headerObject instanceof Collection<?>) {
055                return CollectionHelper.collectionAsCommaDelimitedString((Collection<?>)headerObject);
056            }
057        }
058        return null;
059    }
060
061    @SuppressWarnings("unchecked")
062    @Override
063    public List<String> getHeaderAsList(String name) {
064        if (headers != null) {
065            Object headerObject = headers.get(name);
066            if (headerObject instanceof List<?>) {
067                return (List<String>)headerObject;
068            } else if (headerObject instanceof String) {
069                return Collections.singletonList((String)headerObject);
070            }
071        }
072        return null;
073    }
074
075    @Override
076    public void addHeader(String headerName, String headerValue) {
077        if (headers == null) {
078            headers = createHeaders();
079        }
080        CollectionHelper.appendValue(headers, headerName, headerValue);
081    }
082
083    @Override
084    public void setHeader(String headerName, String headerValue) {
085        if (headers == null) {
086            headers = createHeaders();
087        }
088        headers.put(headerName, headerValue);
089    }
090
091    @Override
092    public void removeHeader(String headerName) {
093        if (headers != null) {
094            headers.remove(headerName);
095        }
096    }
097
098    @Override
099    public Collection<String> getHeaderNames() {
100        if (headers == null) {
101            headers = createHeaders();
102        }
103        return headers.keySet();
104    }
105
106    public void clearHeaders() {
107        headers = null;
108    }
109
110    private Map<String, Object> createHeaders() {
111        return new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
112    }
113
114    public boolean equals(Object other) {
115        if (other instanceof Attachment) {
116            DataHandler otherDh = ((Attachment)other).getDataHandler();
117            return dataHandler.equals(otherDh);
118        }
119        return false;
120    }
121
122    public int hashCode() {
123        return dataHandler.hashCode();
124    }
125}