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