Search in sources :

Example 46 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class SipPublisher method process.

public void process(Exchange exchange) throws Exception {
    String requestMethod = exchange.getIn().getHeader("REQUEST_METHOD", String.class);
    if (requestMethod == null) {
        throw new CamelExchangeException("Missing mandatory Header: REQUEST_HEADER", exchange);
    }
    Object body = exchange.getIn().getBody();
    Request request = configuration.createSipRequest(sequenceNumber, requestMethod, body);
    provider.sendRequest(request);
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) Request(javax.sip.message.Request)

Example 47 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class SearchProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    long myLastId = lastId;
    // KEYWORDS
    // keywords from header take precedence
    String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class);
    if (keywords == null) {
        keywords = endpoint.getProperties().getKeywords();
    }
    if (keywords == null) {
        throw new CamelExchangeException("No keywords to use for query", exchange);
    }
    Query query = new Query(keywords);
    // filter of older tweets
    if (endpoint.getProperties().isFilterOld() && myLastId != 0) {
        query.setSinceId(myLastId);
    }
    // since id
    Long sinceId = exchange.getIn().getHeader(TwitterConstants.TWITTER_SINCEID, Long.class);
    if (sinceId == null) {
        sinceId = endpoint.getProperties().getSinceId();
    }
    if (ObjectHelper.isNotEmpty(sinceId)) {
        query.setSinceId(sinceId);
    }
    // max id
    Long maxId = exchange.getIn().getHeader(TwitterConstants.TWITTER_MAXID, Long.class);
    if (ObjectHelper.isNotEmpty(maxId)) {
        query.setMaxId(maxId);
    }
    // language
    String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class);
    if (lang == null) {
        lang = endpoint.getProperties().getLang();
    }
    if (ObjectHelper.isNotEmpty(lang)) {
        query.setLang(lang);
    }
    // number of elements per page
    Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class);
    if (count == null) {
        count = endpoint.getProperties().getCount();
    }
    if (ObjectHelper.isNotEmpty(count)) {
        query.setCount(count);
    }
    // number of pages
    Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class);
    if (numberOfPages == null) {
        numberOfPages = endpoint.getProperties().getNumberOfPages();
    }
    Twitter twitter = endpoint.getProperties().getTwitter();
    log.debug("Searching twitter with keywords: {}", keywords);
    QueryResult results = twitter.search(query);
    List<Status> list = results.getTweets();
    for (int i = 1; i < numberOfPages; i++) {
        if (!results.hasNext()) {
            break;
        }
        log.debug("Fetching page");
        results = twitter.search(results.nextQuery());
        list.addAll(results.getTweets());
    }
    if (endpoint.getProperties().isFilterOld()) {
        for (Status t : list) {
            long newId = t.getId();
            if (newId > myLastId) {
                myLastId = newId;
            }
        }
    }
    exchange.getIn().setBody(list);
    // update the lastId after finished the processing
    if (myLastId > lastId) {
        lastId = myLastId;
    }
}
Also used : Status(twitter4j.Status) CamelExchangeException(org.apache.camel.CamelExchangeException) QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Twitter(twitter4j.Twitter) TwitterEndpoint(org.apache.camel.component.twitter.TwitterEndpoint)

Example 48 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class NettyProducer method processWithBody.

private boolean processWithBody(final Exchange exchange, Object body, BodyReleaseCallback callback) {
    // set the exchange encoding property
    if (getConfiguration().getCharsetName() != null) {
        exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getConfiguration().getCharsetName()));
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Pool[active={}, idle={}]", pool.getNumActive(), pool.getNumIdle());
    }
    // get a channel from the pool
    ChannelFuture channelFuture = null;
    Channel channel = null;
    try {
        if (getConfiguration().isReuseChannel()) {
            channel = exchange.getProperty(NettyConstants.NETTY_CHANNEL, Channel.class);
        }
        if (channel == null) {
            channelFuture = pool.borrowObject();
            if (channelFuture != null) {
                LOG.trace("Got channel request from pool {}", channelFuture);
            }
        } else {
            channelFuture = channel.newSucceededFuture();
        }
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // we must have a channel
    if (channelFuture == null) {
        exchange.setException(new CamelExchangeException("Cannot get channel from pool", exchange));
        callback.done(true);
        return true;
    }
    channelFuture.addListener(new ChannelConnectedListener(exchange, callback, body));
    return false;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CamelExchangeException(org.apache.camel.CamelExchangeException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollDatagramChannel(io.netty.channel.epoll.EpollDatagramChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) Channel(io.netty.channel.Channel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ConnectException(java.net.ConnectException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 49 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class OpenShiftProducer method doRemoveEmbeddedCartridge.

protected void doRemoveEmbeddedCartridge(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String embeddedCartridgeName = exchange.getIn().getHeader(OpenShiftConstants.EMBEDDED_CARTRIDGE_NAME, getEndpoint().getApplication(), String.class);
        if (ObjectHelper.isNotEmpty(embeddedCartridgeName)) {
            IEmbeddableCartridge removingCartridge = (new LatestEmbeddableCartridge(embeddedCartridgeName)).get(app);
            for (IEmbeddedCartridge cartridge : app.getEmbeddedCartridges()) {
                if (cartridge.equals(removingCartridge)) {
                    cartridge.destroy();
                    exchange.getIn().setBody(cartridge.getDisplayName());
                }
            }
        } else {
            throw new CamelExchangeException("Cartridge not specified", exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication) LatestEmbeddableCartridge(com.openshift.client.cartridge.query.LatestEmbeddableCartridge) IEmbeddedCartridge(com.openshift.client.cartridge.IEmbeddedCartridge) IEmbeddableCartridge(com.openshift.client.cartridge.IEmbeddableCartridge)

Example 50 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class OpenShiftProducer method doAddEnvironmentVariable.

protected void doAddEnvironmentVariable(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
        String variableValue = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_VALUE, getEndpoint().getApplication(), String.class);
        if (!app.canUpdateEnvironmentVariables()) {
            throw new CamelExchangeException("The application with id " + name + " can't update Environment Variables", exchange);
        }
        if (ObjectHelper.isNotEmpty(variableName) && ObjectHelper.isNotEmpty(variableValue)) {
            IEnvironmentVariable result = app.addEnvironmentVariable(variableName, variableValue);
            exchange.getIn().setBody(result.getName());
        } else {
            throw new CamelExchangeException("Environment variable not correctly specified", exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication) IEnvironmentVariable(com.openshift.client.IEnvironmentVariable)

Aggregations

CamelExchangeException (org.apache.camel.CamelExchangeException)82 IApplication (com.openshift.client.IApplication)23 Exchange (org.apache.camel.Exchange)17 IOException (java.io.IOException)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 InputStream (java.io.InputStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AsyncCallback (org.apache.camel.AsyncCallback)5 CamelExecutionException (org.apache.camel.CamelExecutionException)5 Message (org.apache.camel.Message)5 File (java.io.File)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Processor (org.apache.camel.Processor)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Serializable (java.io.Serializable)3 URI (java.net.URI)3 List (java.util.List)3