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.component.file;
018    
019    import java.util.Comparator;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.impl.UriEndpointComponent;
026    import org.apache.camel.util.CastUtils;
027    import org.apache.camel.util.EndpointHelper;
028    import org.apache.camel.util.ObjectHelper;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    import static org.apache.camel.util.ObjectHelper.isNotEmpty;
033    
034    /**
035     * Base class file component. To be extended.
036     */
037    public abstract class GenericFileComponent<T> extends UriEndpointComponent {
038    
039        protected Logger log = LoggerFactory.getLogger(getClass());
040    
041        public GenericFileComponent() {
042            super(GenericFileEndpoint.class);
043        }
044    
045        public GenericFileComponent(CamelContext context) {
046            super(context, GenericFileEndpoint.class);
047        }
048    
049        protected GenericFileEndpoint<T> createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
050    
051            // create the correct endpoint based on the protocol
052            final GenericFileEndpoint<T> endpoint;
053    
054            // call to subclasses to build their custom version of a GenericFileEndpoint
055            endpoint = buildFileEndpoint(uri, remaining, parameters);
056    
057            // sort by using file language
058            String sortBy = getAndRemoveParameter(parameters, "sortBy", String.class);
059            if (isNotEmpty(sortBy) && !EndpointHelper.isReferenceParameter(sortBy)) {
060                // we support nested sort groups so they should be chained
061                String[] groups = sortBy.split(";");
062                Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(groups));
063                Comparator<Exchange> comparator = createSortByComparator(it);
064                endpoint.setSortBy(comparator);
065            }
066            setProperties(endpoint.getConfiguration(), parameters);
067            setProperties(endpoint, parameters);
068    
069            afterPropertiesSet(endpoint);
070    
071            return endpoint;
072        }
073    
074        /**
075         * A factory method for derived file components to create the endpoint
076         *
077         * @param uri the full URI of the endpoint
078         * @param remaining the remaining part of the URI without the query
079         *                parameters or component prefix
080         * @param parameters the optional parameters passed in
081         * @return a newly created endpoint or null if the endpoint cannot be
082         *         created based on the inputs
083         * @throws Exception can be thrown
084         */
085        protected abstract GenericFileEndpoint<T> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception;
086    
087        /**
088         * A factory method for derived file components to perform validation of properties
089         *
090         * @param endpoint the endpoint
091         * @throws Exception can be thrown in case of validation errors
092         */
093        protected abstract void afterPropertiesSet(GenericFileEndpoint<T> endpoint) throws Exception;
094    
095        /**
096         * Helper to create a sort comparator
097         *
098         * @param it iterator
099         * @return Comparator<Exchange>
100         */
101        private Comparator<Exchange> createSortByComparator(Iterator<String> it) {
102            if (!it.hasNext()) {
103                return null;
104            }
105    
106            String group = it.next();
107    
108            boolean reverse = group.startsWith("reverse:");
109            String reminder = reverse ? ifStartsWithReturnRemainder("reverse:", group) : group;
110    
111            boolean ignoreCase = reminder.startsWith("ignoreCase:");
112            reminder = ignoreCase ? ifStartsWithReturnRemainder("ignoreCase:", reminder) : reminder;
113    
114            ObjectHelper.notEmpty(reminder, "sortBy expression", this);
115    
116            // recursive add nested sorters
117            return GenericFileDefaultSorter.sortByFileLanguage(getCamelContext(), 
118                reminder, reverse, ignoreCase, createSortByComparator(it));
119        }
120    }