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.util.component; 018 019import java.lang.reflect.Array; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Set; 023 024import org.apache.camel.Exchange; 025import org.apache.camel.impl.DefaultConsumer; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029/** 030 * Utility class for API Consumers. 031 */ 032public final class ApiConsumerHelper { 033 034 private static final Logger LOG = LoggerFactory.getLogger(ApiConsumerHelper.class); 035 036 private ApiConsumerHelper() { 037 } 038 039 /** 040 * Utility method to find matching API Method for supplied endpoint's configuration properties. 041 * @param endpoint endpoint for configuration properties. 042 * @param propertyNamesInterceptor names interceptor for adapting property names, usually the consumer class itself. 043 * @param <E> ApiName enumeration. 044 * @param <T> Component configuration class. 045 * @return matching ApiMethod. 046 */ 047 public static <E extends Enum<E> & ApiName, T> ApiMethod findMethod( 048 AbstractApiEndpoint<E, T> endpoint, PropertyNamesInterceptor propertyNamesInterceptor) { 049 050 ApiMethod result; 051 // find one that takes the largest subset of endpoint parameters 052 final Set<String> argNames = new HashSet<>(endpoint.getEndpointPropertyNames()); 053 propertyNamesInterceptor.interceptPropertyNames(argNames); 054 055 List<ApiMethod> filteredMethods = endpoint.methodHelper.filterMethods( 056 endpoint.getCandidates(), ApiMethodHelper.MatchType.SUPER_SET, argNames); 057 058 if (filteredMethods.isEmpty()) { 059 ApiMethodHelper<? extends ApiMethod> methodHelper = endpoint.getMethodHelper(); 060 throw new IllegalArgumentException( 061 String.format("Missing properties for %s/%s, need one or more from %s", 062 endpoint.getApiName().getName(), endpoint.getMethodName(), 063 methodHelper.getMissingProperties(endpoint.getMethodName(), argNames))); 064 } else if (filteredMethods.size() == 1) { 065 // single match 066 result = filteredMethods.get(0); 067 } else { 068 result = ApiMethodHelper.getHighestPriorityMethod(filteredMethods); 069 LOG.warn(String.format("Using highest priority operation %s from operations %s for endpoint %s", 070 result, filteredMethods, endpoint.getEndpointUri())); 071 } 072 073 return result; 074 } 075 076 /** 077 * Utility method for Consumers to process API method invocation result. 078 * @param consumer Consumer that wants to process results. 079 * @param result result of API method invocation. 080 * @param splitResult true if the Consumer wants to split result using {@link org.apache.camel.util.component.ResultInterceptor#splitResult(Object)} method. 081 * @param <T> Consumer class that extends DefaultConsumer and implements {@link org.apache.camel.util.component.ResultInterceptor}. 082 * @return number of result exchanges processed. 083 * @throws Exception on error. 084 */ 085 public static <T extends DefaultConsumer & ResultInterceptor> int getResultsProcessed( 086 T consumer, Object result, boolean splitResult) throws Exception { 087 088 // process result according to type 089 if (result != null && splitResult) { 090 // try to split the result 091 final Object results = consumer.splitResult(result); 092 093 if (results != null) { 094 if (results instanceof List) { 095 // Optimized for lists 096 final List<?> list = (List<?>)results; 097 final int size = list.size(); 098 099 // access elements by position rather than with iterator to 100 // reduce garbage 101 for (int i = 0; i < size; i++) { 102 processResult(consumer, result, list.get(i)); 103 } 104 105 return size; 106 } else if (results instanceof Iterable) { 107 // Optimized for iterable 108 int size = 0; 109 for (Object singleResult : (Iterable<?>)results) { 110 processResult(consumer, result, singleResult); 111 size++; 112 } 113 114 return size; 115 } else if (results.getClass().isArray()) { 116 // Optimized for array 117 final int size = Array.getLength(results); 118 for (int i = 0; i < size; i++) { 119 processResult(consumer, result, Array.get(results, i)); 120 } 121 122 return size; 123 } 124 } 125 } 126 127 processResult(consumer, result, result); 128 return 1; // number of messages polled 129 } 130 131 private static <T extends DefaultConsumer & ResultInterceptor> void processResult(T consumer, Object methodResult, Object result) 132 throws Exception { 133 134 Exchange exchange = consumer.getEndpoint().createExchange(); 135 exchange.getIn().setBody(result); 136 137 consumer.interceptResult(methodResult, exchange); 138 try { 139 // send message to next processor in the route 140 consumer.getProcessor().process(exchange); 141 } finally { 142 // log exception if an exception occurred and was not handled 143 final Exception exception = exchange.getException(); 144 if (exception != null) { 145 consumer.getExceptionHandler().handleException("Error processing exchange", exchange, exception); 146 } 147 } 148 } 149}