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