Search in sources :

Example 1 with FacebookMethodsType

use of org.apache.camel.component.facebook.data.FacebookMethodsType in project camel by apache.

the class FacebookEndpoint method initState.

private void initState() {
    // get endpoint property names
    final Set<String> arguments = new HashSet<String>();
    arguments.addAll(getEndpointPropertyNames(configuration));
    // add inBody argument for producers
    if (inBody != null) {
        arguments.add(inBody);
    }
    final String[] argNames = arguments.toArray(new String[arguments.size()]);
    candidates = new ArrayList<FacebookMethodsType>();
    candidates.addAll(getCandidateMethods(method, argNames));
    if (!candidates.isEmpty()) {
        // found an exact name match, allows disambiguation if needed
        this.nameStyle = FacebookNameStyle.EXACT;
    } else {
        // also search for long forms of method name, both get* and search*
        // Note that this set will be further sorted by Producers and Consumers
        // producers will prefer get* forms, and consumers should prefer search* forms
        candidates.addAll(getCandidateMethods(convertToGetMethod(method), argNames));
        if (!candidates.isEmpty()) {
            this.nameStyle = FacebookNameStyle.GET;
        }
        int nGetMethods = candidates.size();
        candidates.addAll(getCandidateMethods(convertToSearchMethod(method), argNames));
        // error if there are no candidates
        if (candidates.isEmpty()) {
            throw new IllegalArgumentException(String.format("No matching operation for %s, with arguments %s", method, arguments));
        }
        if (nameStyle == null) {
            // no get* methods found
            nameStyle = FacebookNameStyle.SEARCH;
        } else if (candidates.size() > nGetMethods) {
            // get* and search* methods found
            nameStyle = FacebookNameStyle.GET_AND_SEARCH;
        }
    }
    // log missing/extra properties for debugging
    if (LOG.isDebugEnabled()) {
        final Set<String> missing = getMissingProperties(method, nameStyle, arguments);
        if (!missing.isEmpty()) {
            LOG.debug("Method {} could use one or more properties from {}", method, missing);
        }
    }
}
Also used : FacebookMethodsType(org.apache.camel.component.facebook.data.FacebookMethodsType) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) HashSet(java.util.HashSet)

Example 2 with FacebookMethodsType

use of org.apache.camel.component.facebook.data.FacebookMethodsType in project camel by apache.

the class FacebookProducer method findMethod.

private FacebookMethodsType findMethod(Exchange exchange, Map<String, Object> properties) {
    FacebookMethodsType method = null;
    final List<FacebookMethodsType> candidates = endpoint.getCandidates();
    if (processInBody(exchange, properties)) {
        // filter candidates based on endpoint and exchange properties
        final Set<String> argNames = properties.keySet();
        final List<FacebookMethodsType> filteredMethods = filterMethods(candidates, MatchType.SUPER_SET, argNames.toArray(new String[argNames.size()]));
        // get the method to call
        if (filteredMethods.isEmpty()) {
            final Set<String> missing = getMissingProperties(endpoint.getMethod(), endpoint.getNameStyle(), argNames);
            throw new RuntimeCamelException(String.format("Missing properties for %s, need one or more from %s", endpoint.getMethod(), missing));
        } else if (filteredMethods.size() == 1) {
            // found an exact match
            method = filteredMethods.get(0);
        } else {
            method = FacebookMethodsTypeHelper.getHighestPriorityMethod(filteredMethods);
            LOG.warn("Calling highest priority method {} from methods {}", method, filteredMethods);
        }
    }
    return method;
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) FacebookMethodsType(org.apache.camel.component.facebook.data.FacebookMethodsType)

Example 3 with FacebookMethodsType

use of org.apache.camel.component.facebook.data.FacebookMethodsType in project camel by apache.

the class FacebookProducer 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>();
    getExchangeProperties(exchange, properties);
    FacebookPropertiesHelper.configureReadingProperties(endpoint.getConfiguration(), properties);
    getEndpointProperties(endpoint.getConfiguration(), properties);
    // decide which method to invoke
    final FacebookMethodsType method = findMethod(exchange, properties);
    if (method == null) {
        // synchronous failure
        callback.done(true);
        return true;
    }
    // create a runnable invocation task to be submitted on a background thread pool
    // this way we avoid blocking the current thread for long running operations
    Runnable invocation = new Runnable() {

        @Override
        public void run() {
            try {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Invoking method {} with {}", method.getName(), properties.keySet());
                }
                // also check whether we need to get Raw JSON
                Object result;
                String rawJSON = null;
                if (endpoint.getConfiguration().getJsonStoreEnabled() == null || !endpoint.getConfiguration().getJsonStoreEnabled()) {
                    result = FacebookMethodsTypeHelper.invokeMethod(endpoint.getConfiguration().getFacebook(), method, properties);
                } else {
                    final Facebook facebook = endpoint.getConfiguration().getFacebook();
                    // lock out the underlying Facebook object from other threads
                    synchronized (facebook) {
                        result = FacebookMethodsTypeHelper.invokeMethod(facebook, method, properties);
                        rawJSON = DataObjectFactory.getRawJSON(result);
                    }
                }
                // producer returns a single response, even for methods with List return types
                exchange.getOut().setBody(result);
                // copy headers
                exchange.getOut().setHeaders(exchange.getIn().getHeaders());
                if (rawJSON != null) {
                    exchange.getOut().setHeader(FacebookConstants.FACEBOOK_PROPERTY_PREFIX + "rawJSON", rawJSON);
                }
            } catch (Throwable t) {
                exchange.setException(ObjectHelper.wrapRuntimeCamelException(t));
            } finally {
                callback.done(false);
            }
        }
    };
    getExecutorService(getEndpoint().getCamelContext()).submit(invocation);
    return false;
}
Also used : HashMap(java.util.HashMap) Facebook(facebook4j.Facebook) FacebookMethodsType(org.apache.camel.component.facebook.data.FacebookMethodsType)

Example 4 with FacebookMethodsType

use of org.apache.camel.component.facebook.data.FacebookMethodsType in project camel by apache.

the class FacebookConsumer method findMethod.

private FacebookMethodsType findMethod() {
    FacebookMethodsType result;
    // find one that takes the largest subset of endpoint parameters
    final Set<String> argNames = new HashSet<String>();
    argNames.addAll(FacebookPropertiesHelper.getEndpointPropertyNames(endpoint.getConfiguration()));
    // add reading property for polling, if it doesn't already exist!
    argNames.add(READING_PROPERTY);
    final String[] argNamesArray = argNames.toArray(new String[argNames.size()]);
    List<FacebookMethodsType> filteredMethods = filterMethods(endpoint.getCandidates(), MatchType.SUPER_SET, argNamesArray);
    if (filteredMethods.isEmpty()) {
        throw new IllegalArgumentException(String.format("Missing properties for %s, need one or more from %s", endpoint.getMethod(), getMissingProperties(endpoint.getMethod(), endpoint.getNameStyle(), argNames)));
    } else if (filteredMethods.size() == 1) {
        // single match
        result = filteredMethods.get(0);
    } else {
        result = getHighestPriorityMethod(filteredMethods);
        LOG.warn("Using highest priority method {} from methods {}", method, filteredMethods);
    }
    return result;
}
Also used : FacebookMethodsType(org.apache.camel.component.facebook.data.FacebookMethodsType) HashSet(java.util.HashSet)

Aggregations

FacebookMethodsType (org.apache.camel.component.facebook.data.FacebookMethodsType)4 HashSet (java.util.HashSet)2 Facebook (facebook4j.Facebook)1 HashMap (java.util.HashMap)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)1 UriEndpoint (org.apache.camel.spi.UriEndpoint)1