Search in sources :

Example 1 with ApiMethod

use of org.apache.camel.util.component.ApiMethod in project camel by apache.

the class Olingo2Producer method process.

@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    // properties for method arguments
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.putAll(endpoint.getEndpointProperties());
    propertiesHelper.getExchangeProperties(exchange, properties);
    // let the endpoint and the Producer intercept properties
    endpoint.interceptProperties(properties);
    interceptProperties(properties);
    // create response handler
    properties.put(Olingo2Endpoint.RESPONSE_HANDLER_PROPERTY, new Olingo2ResponseHandler<Object>() {

        @Override
        public void onResponse(Object response) {
            // producer returns a single response, even for methods with List return types
            exchange.getOut().setBody(response);
            // copy headers
            exchange.getOut().setHeaders(exchange.getIn().getHeaders());
            interceptResult(response, exchange);
            callback.done(false);
        }

        @Override
        public void onException(Exception ex) {
            exchange.setException(ex);
            callback.done(false);
        }

        @Override
        public void onCanceled() {
            exchange.setException(new RuntimeCamelException("OData HTTP Request cancelled!"));
            callback.done(false);
        }
    });
    // decide which method to invoke
    final ApiMethod method = findMethod(exchange, properties);
    if (method == null) {
        // synchronous failure
        callback.done(true);
        return true;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Invoking operation {} with {}", method.getName(), properties.keySet());
    }
    try {
        doInvokeMethod(method, properties);
    } catch (Throwable t) {
        exchange.setException(ObjectHelper.wrapRuntimeCamelException(t));
        callback.done(true);
        return true;
    }
    return false;
}
Also used : HashMap(java.util.HashMap) ApiMethod(org.apache.camel.util.component.ApiMethod) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 2 with ApiMethod

use of org.apache.camel.util.component.ApiMethod in project camel by apache.

the class DocumentGeneratorMojo method getDocumentContext.

private VelocityContext getDocumentContext() throws MavenReportException {
    final VelocityContext context = new VelocityContext();
    context.put("helper", this);
    // project GAV
    context.put("groupId", project.getGroupId());
    context.put("artifactId", project.getArtifactId());
    context.put("version", project.getVersion());
    // component URI format
    // look for single API, no endpoint-prefix
    @SuppressWarnings("unchecked") final Set<String> apiNames = new TreeSet<String>(collection.getApiNames());
    context.put("apiNames", apiNames);
    String suffix;
    if (apiNames.size() == 1 && ((Set) apiNames).contains("")) {
        suffix = "://endpoint?[options]";
    } else {
        suffix = "://endpoint-prefix/endpoint?[options]";
    }
    context.put("uriFormat", scheme + suffix);
    // API helpers
    final Map<String, ApiMethodHelper> apiHelpers = new TreeMap<String, ApiMethodHelper>();
    for (Object element : collection.getApiHelpers().entrySet()) {
        Map.Entry entry = (Map.Entry) element;
        apiHelpers.put(((ApiName) entry.getKey()).getName(), (ApiMethodHelper) entry.getValue());
    }
    context.put("apiHelpers", apiHelpers);
    // API methods and endpoint configurations
    final Map<String, Class<? extends ApiMethod>> apiMethods = new TreeMap<String, Class<? extends ApiMethod>>();
    final Map<String, Class<?>> apiConfigs = new TreeMap<String, Class<?>>();
    for (Object element : collection.getApiMethods().entrySet()) {
        Map.Entry entry = (Map.Entry) element;
        final String name = ((ApiName) entry.getValue()).getName();
        @SuppressWarnings("unchecked") Class<? extends ApiMethod> apiMethod = (Class<? extends ApiMethod>) entry.getKey();
        apiMethods.put(name, apiMethod);
        Class<?> configClass;
        try {
            configClass = getProjectClassLoader().loadClass(getEndpointConfigName(apiMethod));
        } catch (ClassNotFoundException e) {
            throw new MavenReportException(e.getMessage(), e);
        } catch (MojoExecutionException e) {
            throw new MavenReportException(e.getMessage(), e);
        }
        apiConfigs.put(name, configClass);
    }
    context.put("apiMethods", apiMethods);
    context.put("apiConfigs", apiConfigs);
    // API component properties
    context.put("scheme", this.scheme);
    context.put("componentName", this.componentName);
    Class<?> configClass;
    try {
        configClass = getProjectClassLoader().loadClass(getComponentConfig());
    } catch (ClassNotFoundException e) {
        throw new MavenReportException(e.getMessage(), e);
    } catch (MojoExecutionException e) {
        throw new MavenReportException(e.getMessage(), e);
    }
    context.put("componentConfig", configClass);
    // get declared and derived fields for component config
    // use get/set methods instead of fields, since this class could inherit others, that have private fields
    // so getDeclaredFields() won't work, like it does for generated endpoint config classes!!!
    final Map<String, String> configFields = new TreeMap<String, String>();
    do {
        IntrospectionSupport.ClassInfo classInfo = IntrospectionSupport.cacheClass(configClass);
        for (IntrospectionSupport.MethodInfo method : classInfo.methods) {
            if (method.isSetter) {
                configFields.put(method.getterOrSetterShorthandName, getCanonicalName(method.method.getParameterTypes()[0]));
            }
        }
        configClass = configClass.getSuperclass();
    } while (configClass != null && !configClass.equals(Object.class));
    context.put("componentConfigFields", configFields);
    return context;
}
Also used : IntrospectionSupport(org.apache.camel.util.IntrospectionSupport) VelocityContext(org.apache.velocity.VelocityContext) ApiMethod(org.apache.camel.util.component.ApiMethod) TreeSet(java.util.TreeSet) ApiMethodHelper(org.apache.camel.util.component.ApiMethodHelper) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ApiName(org.apache.camel.util.component.ApiName) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 3 with ApiMethod

use of org.apache.camel.util.component.ApiMethod in project camel by apache.

the class DocumentGeneratorMojo method getEndpoints.

public static List<EndpointInfo> getEndpoints(Class<? extends ApiMethod> apiMethod, ApiMethodHelper<?> helper, Class<?> endpointConfig) {
    // get list of valid options
    final Set<String> validOptions = new HashSet<String>();
    for (Field field : endpointConfig.getDeclaredFields()) {
        validOptions.add(field.getName());
    }
    // create method name map
    final Map<String, List<ApiMethod>> methodMap = new TreeMap<String, List<ApiMethod>>();
    for (ApiMethod method : apiMethod.getEnumConstants()) {
        String methodName = method.getName();
        List<ApiMethod> apiMethods = methodMap.get(methodName);
        if (apiMethods == null) {
            apiMethods = new ArrayList<ApiMethod>();
            methodMap.put(methodName, apiMethods);
        }
        apiMethods.add(method);
    }
    // create method name to alias name map
    final Map<String, Set<String>> aliasMap = new TreeMap<String, Set<String>>();
    final Map<String, Set<String>> aliasToMethodMap = helper.getAliases();
    for (Map.Entry<String, Set<String>> entry : aliasToMethodMap.entrySet()) {
        final String alias = entry.getKey();
        for (String method : entry.getValue()) {
            Set<String> aliases = aliasMap.get(method);
            if (aliases == null) {
                aliases = new TreeSet<String>();
                aliasMap.put(method, aliases);
            }
            aliases.add(alias);
        }
    }
    // create options map and return type map
    final Map<String, Set<String>> optionMap = new TreeMap<String, Set<String>>();
    final Map<String, Set<String>> returnType = new TreeMap<String, Set<String>>();
    for (Map.Entry<String, List<ApiMethod>> entry : methodMap.entrySet()) {
        final String name = entry.getKey();
        final List<ApiMethod> apiMethods = entry.getValue();
        // count the number of times, every valid option shows up across methods
        // and also collect return types
        final Map<String, Integer> optionCount = new TreeMap<String, Integer>();
        final TreeSet<String> resultTypes = new TreeSet<String>();
        returnType.put(name, resultTypes);
        for (ApiMethod method : apiMethods) {
            for (String arg : method.getArgNames()) {
                if (validOptions.contains(arg)) {
                    Integer count = optionCount.get(arg);
                    if (count == null) {
                        count = 1;
                    } else {
                        count += 1;
                    }
                    optionCount.put(arg, count);
                }
            }
            // wrap primitive result types
            Class<?> resultType = method.getResultType();
            if (resultType.isPrimitive()) {
                resultType = ClassUtils.primitiveToWrapper(resultType);
            }
            resultTypes.add(getCanonicalName(resultType));
        }
        // collect method options
        final TreeSet<String> options = new TreeSet<String>();
        optionMap.put(name, options);
        final Set<String> mandatory = new TreeSet<String>();
        // generate optional and mandatory lists for overloaded methods
        int nMethods = apiMethods.size();
        for (ApiMethod method : apiMethods) {
            final Set<String> optional = new TreeSet<String>();
            for (String arg : method.getArgNames()) {
                if (validOptions.contains(arg)) {
                    final Integer count = optionCount.get(arg);
                    if (count == nMethods) {
                        mandatory.add(arg);
                    } else {
                        optional.add(arg);
                    }
                }
            }
            if (!optional.isEmpty()) {
                options.add(optional.toString());
            }
        }
        if (!mandatory.isEmpty()) {
            // strip [] from mandatory options
            final String mandatoryOptions = mandatory.toString();
            options.add(mandatoryOptions.substring(1, mandatoryOptions.length() - 1));
        }
    }
    // create endpoint data
    final List<EndpointInfo> infos = new ArrayList<EndpointInfo>();
    for (Map.Entry<String, List<ApiMethod>> methodEntry : methodMap.entrySet()) {
        final String endpoint = methodEntry.getKey();
        // set endpoint name
        EndpointInfo info = new EndpointInfo();
        info.endpoint = endpoint;
        info.aliases = convertSetToString(aliasMap.get(endpoint));
        info.options = convertSetToString(optionMap.get(endpoint));
        final Set<String> resultTypes = returnType.get(endpoint);
        // get rid of void results
        resultTypes.remove("void");
        info.resultTypes = convertSetToString(resultTypes);
        infos.add(info);
    }
    return infos;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) ApiMethod(org.apache.camel.util.component.ApiMethod) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Field(java.lang.reflect.Field) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet)

Example 4 with ApiMethod

use of org.apache.camel.util.component.ApiMethod in project camel by apache.

the class LinkedInConsumer method interceptPropertyNames.

@Override
public void interceptPropertyNames(Set<String> propertyNames) {
    // do we need to add fields option
    if (!propertyNames.contains(LinkedInEndpoint.FIELDS_OPTION)) {
        final List<ApiMethod> candidates = endpoint.getCandidates();
        for (ApiMethod method : candidates) {
            if (!method.getArgNames().contains(LinkedInEndpoint.FIELDS_OPTION)) {
                return;
            }
        }
        // all candidates use fields option, so there is no ambiguity
        propertyNames.add(LinkedInEndpoint.FIELDS_OPTION);
    }
}
Also used : ApiMethod(org.apache.camel.util.component.ApiMethod)

Aggregations

ApiMethod (org.apache.camel.util.component.ApiMethod)4 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 IntrospectionSupport (org.apache.camel.util.IntrospectionSupport)1 ApiMethodHelper (org.apache.camel.util.component.ApiMethodHelper)1 ApiName (org.apache.camel.util.component.ApiName)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1 VelocityContext (org.apache.velocity.VelocityContext)1