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    package org.apache.camel.dataformat.bindy.kvp;
018    
019    import java.io.InputStream;
020    import java.io.InputStreamReader;
021    import java.io.OutputStream;
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Scanner;
027    
028    import org.apache.camel.Exchange;
029    import org.apache.camel.dataformat.bindy.BindyAbstractDataFormat;
030    import org.apache.camel.dataformat.bindy.BindyAbstractFactory;
031    import org.apache.camel.dataformat.bindy.BindyKeyValuePairFactory;
032    import org.apache.camel.dataformat.bindy.util.Converter;
033    import org.apache.camel.spi.DataFormat;
034    import org.apache.camel.spi.PackageScanClassResolver;
035    import org.apache.camel.util.IOHelper;
036    import org.apache.camel.util.ObjectHelper;
037    import org.apache.commons.logging.Log;
038    import org.apache.commons.logging.LogFactory;
039    
040    /**
041     * A <a href="http://camel.apache.org/data-format.html">data format</a> (
042     * {@link DataFormat}) using Bindy to marshal to and from CSV files
043     */
044    public class BindyKeyValuePairDataFormat extends BindyAbstractDataFormat {
045    
046        private static final transient Log LOG = LogFactory.getLog(BindyKeyValuePairDataFormat.class);
047    
048        public BindyKeyValuePairDataFormat() {
049        }
050    
051        public BindyKeyValuePairDataFormat(String... packages) {
052            super(packages);
053        }
054    
055        @SuppressWarnings("unchecked")
056        public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
057            BindyAbstractFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
058            List<Map<String, Object>> models = (ArrayList<Map<String, Object>>)body;
059            byte[] crlf;
060    
061            // Get CRLF
062            crlf = Converter.getByteReturn(factory.getCarriageReturn());
063    
064            for (Map<String, Object> model : models) {
065                String result = factory.unbind(model);
066                byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
067                outputStream.write(bytes);
068    
069                // Add a carriage return
070                outputStream.write(crlf);
071            }
072        }
073    
074        public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
075            BindyKeyValuePairFactory factory = (BindyKeyValuePairFactory)getFactory(exchange.getContext().getPackageScanClassResolver());
076    
077            // List of Pojos
078            List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
079    
080            // Pojos of the model
081            Map<String, Object> model;
082    
083            InputStreamReader in = new InputStreamReader(inputStream);
084    
085            // Scanner is used to read big file
086            Scanner scanner = new Scanner(in);
087    
088            // Retrieve the pair separator defined to split the record
089            ObjectHelper.notNull(factory.getPairSeparator(), "The pair separator property of the annotation @Message");
090            String separator = factory.getPairSeparator();
091    
092            int count = 0;
093            try {
094                while (scanner.hasNextLine()) {
095                    // Read the line
096                    String line = scanner.nextLine().trim();
097    
098                    if (ObjectHelper.isEmpty(line)) {
099                        // skip if line is empty
100                        continue;
101                    }
102    
103                    // Increment counter
104                    count++;
105    
106                    // Create POJO
107                    model = factory.factory();
108    
109                    // Split the message according to the pair separator defined in
110                    // annotated class @Message
111                    List<String> result = Arrays.asList(line.split(separator));
112    
113                    if (result.size() == 0 || result.isEmpty()) {
114                        throw new java.lang.IllegalArgumentException("No records have been defined in the KVP !");
115                    }
116    
117                    if (result.size() > 0) {
118    
119                        // Bind data from message with model classes
120                        // Counter is used to detect line where error occurs
121                        factory.bind(result, model, count);
122    
123                        // Link objects together
124                        factory.link(model);
125    
126                        // Add objects graph to the list
127                        models.add(model);
128    
129                        if (LOG.isDebugEnabled()) {
130                            LOG.debug("Graph of objects created : " + model);
131                        }
132                    }
133    
134                }
135    
136                // Test if models list is empty or not
137                // If this is the case (correspond to an empty stream, ...)
138                if (models.size() == 0) {
139                    throw new java.lang.IllegalArgumentException("No records have been defined in the KVP !");
140                } else {
141                    return models;
142                }
143    
144            } finally {
145                scanner.close();
146                IOHelper.close(in, "in", LOG);
147            }
148        }
149    
150        protected BindyAbstractFactory createModelFactory(PackageScanClassResolver resolver) throws Exception {
151            return new BindyKeyValuePairFactory(resolver, getPackages());
152        }
153    }