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     */
017    
018    package org.apache.camel.component.syslog;
019    
020    import java.io.InputStream;
021    import java.io.OutputStream;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.spi.DataFormat;
025    import org.apache.camel.util.ExchangeHelper;
026    
027    public class Rfc3164SyslogDataFormat implements DataFormat {
028        public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
029            SyslogMessage message = ExchangeHelper.convertToMandatoryType(exchange, SyslogMessage.class, body);
030            stream.write(Rfc3164SyslogConverter.toString(message).getBytes());
031        }
032    
033        public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
034    
035            String body = ExchangeHelper.convertToMandatoryType(exchange, String.class, inputStream);
036            SyslogMessage message = Rfc3164SyslogConverter.parseMessage(body.getBytes());
037    
038            exchange.getOut().setHeader(SyslogConstants.SYSLOG_FACILITY, message.getFacility());
039            exchange.getOut().setHeader(SyslogConstants.SYSLOG_SEVERITY, message.getSeverity());
040            exchange.getOut().setHeader(SyslogConstants.SYSLOG_HOSTNAME, message.getHostname());
041            exchange.getOut().setHeader(SyslogConstants.SYSLOG_TIMESTAMP, message.getTimestamp());
042    
043            //Since we are behind the fact of being in an Endpoint...
044            //We need to pull in the remote/local via either Mina or Netty.
045    
046            if (exchange.getIn().getHeader("CamelMinaLocalAddress") != null) {
047                message.setLocalAddress(exchange.getIn().getHeader("CamelMinaLocalAddress", String.class));
048                exchange.getOut().setHeader(SyslogConstants.SYSLOG_LOCAL_ADDRESS, message.getLocalAddress());
049            }
050    
051            if (exchange.getIn().getHeader("CamelMinaRemoteAddress") != null) {
052                message.setRemoteAddress(exchange.getIn().getHeader("CamelMinaRemoteAddress", String.class));
053                exchange.getOut().setHeader(SyslogConstants.SYSLOG_REMOTE_ADDRESS, message.getRemoteAddress());
054            }
055    
056            if (exchange.getIn().getHeader("CamelNettyRemoteAddress") != null) {
057                message.setRemoteAddress(exchange.getIn().getHeader("CamelMinaRemoteAddress", String.class));
058                exchange.getOut().setHeader(SyslogConstants.SYSLOG_REMOTE_ADDRESS, message.getRemoteAddress());
059            }
060    
061            return message;
062        }
063    }