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<String>(); 053 argNames.addAll(endpoint.getEndpointPropertyNames()); 054 055 propertyNamesInterceptor.interceptPropertyNames(argNames); 056 057 final String[] argNamesArray = argNames.toArray(new String[argNames.size()]); 058 List<ApiMethod> filteredMethods = endpoint.methodHelper.filterMethods( 059 endpoint.getCandidates(), ApiMethodHelper.MatchType.SUPER_SET, argNamesArray); 060 061 if (filteredMethods.isEmpty()) { 062 ApiMethodHelper<? extends ApiMethod> methodHelper = endpoint.getMethodHelper(); 063 throw new IllegalArgumentException( 064 String.format("Missing properties for %s/%s, need one or more from %s", 065 endpoint.getApiName().getName(), endpoint.getMethodName(), 066 methodHelper.getMissingProperties(endpoint.getMethodName(), argNames))); 067 } else if (filteredMethods.size() == 1) { 068 // single match 069 result = filteredMethods.get(0); 070 } else { 071 result = ApiMethodHelper.getHighestPriorityMethod(filteredMethods); 072 LOG.warn(String.format("Using highest priority operation %s from operations %s for endpoint %s", 073 result, filteredMethods, endpoint.getEndpointUri())); 074 } 075 076 return result; 077 } 078 079 /** 080 * Utility method for Consumers to process API method invocation result. 081 * @param consumer Consumer that wants to process results. 082 * @param result result of API method invocation. 083 * @param splitResult true if the Consumer wants to split result using {@link org.apache.camel.util.component.ResultInterceptor#splitResult(Object)} method. 084 * @param <T> Consumer class that extends DefaultConsumer and implements {@link org.apache.camel.util.component.ResultInterceptor}. 085 * @return number of result exchanges processed. 086 * @throws Exception on error. 087 */ 088 public static <T extends DefaultConsumer & ResultInterceptor> int getResultsProcessed( 089 T consumer, Object result, boolean splitResult) throws Exception { 090 091 // process result according to type 092 if (result != null && splitResult) { 093 // try to split the result 094 final Object resultArray = consumer.splitResult(result); 095 096 if (resultArray != result && resultArray.getClass().isArray()) { 097 // create an exchange for every element 098 final int length = Array.getLength(resultArray); 099 for (int i = 0; i < length; i++) { 100 processResult(consumer, result, Array.get(resultArray, i)); 101 } 102 return length; 103 } 104 } 105 106 processResult(consumer, result, result); 107 return 1; // number of messages polled 108 } 109 110 private static <T extends DefaultConsumer & ResultInterceptor> void processResult(T consumer, Object methodResult, Object result) 111 throws Exception { 112 113 Exchange exchange = consumer.getEndpoint().createExchange(); 114 exchange.getIn().setBody(result); 115 116 consumer.interceptResult(methodResult, exchange); 117 try { 118 // send message to next processor in the route 119 consumer.getProcessor().process(exchange); 120 } finally { 121 // log exception if an exception occurred and was not handled 122 final Exception exception = exchange.getException(); 123 if (exception != null) { 124 consumer.getExceptionHandler().handleException("Error processing exchange", exchange, exception); 125 } 126 } 127 } 128}