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     */
017    package org.apache.camel.component.smpp;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.spi.ExceptionHandler;
022    import org.jsmpp.bean.AlertNotification;
023    import org.jsmpp.bean.DataSm;
024    import org.jsmpp.bean.DeliverSm;
025    import org.jsmpp.extra.ProcessRequestException;
026    import org.jsmpp.session.DataSmResult;
027    import org.jsmpp.session.MessageReceiverListener;
028    import org.jsmpp.session.Session;
029    import org.jsmpp.util.MessageIDGenerator;
030    import org.jsmpp.util.MessageId;
031    import org.jsmpp.util.RandomMessageIDGenerator;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    public class MessageReceiverListenerImpl implements MessageReceiverListener {
036        
037        private static final transient Logger LOG = LoggerFactory.getLogger(MessageReceiverListenerImpl.class);
038    
039        private MessageIDGenerator messageIDGenerator = new RandomMessageIDGenerator();
040        private SmppEndpoint endpoint;
041        private Processor processor;
042        private ExceptionHandler exceptionHandler;
043        
044        public MessageReceiverListenerImpl(SmppEndpoint endpoint, Processor processor, ExceptionHandler exceptionHandler) {
045            this.endpoint = endpoint;
046            this.processor = processor;
047            this.exceptionHandler = exceptionHandler;
048        }
049    
050        public void onAcceptAlertNotification(AlertNotification alertNotification) {
051            LOG.debug("Received an alertNotification {}", alertNotification);
052    
053            try {
054                Exchange exchange = endpoint.createOnAcceptAlertNotificationExchange(alertNotification);
055    
056                LOG.trace("Processing the new smpp exchange...");
057                processor.process(exchange);
058                LOG.trace("Processed the new smpp exchange");
059            } catch (Exception e) {
060                exceptionHandler.handleException(e);
061            }
062        }
063    
064        public void onAcceptDeliverSm(DeliverSm deliverSm) throws ProcessRequestException {
065            LOG.debug("Received a deliverSm {}", deliverSm);
066    
067            try {
068                Exchange exchange = endpoint.createOnAcceptDeliverSmExchange(deliverSm);
069    
070                LOG.trace("processing the new smpp exchange...");
071                processor.process(exchange);
072                LOG.trace("processed the new smpp exchange");
073            } catch (Exception e) {
074                exceptionHandler.handleException(e);
075                if (e instanceof ProcessRequestException) {
076                    throw (ProcessRequestException) e;
077                }
078            }
079        }
080    
081        public DataSmResult onAcceptDataSm(DataSm dataSm, Session session) throws ProcessRequestException {
082            LOG.debug("Received a dataSm {}", dataSm);
083    
084            MessageId newMessageId = messageIDGenerator.newMessageId();
085    
086            try {
087                Exchange exchange = endpoint.createOnAcceptDataSm(dataSm, newMessageId.getValue());
088    
089                LOG.trace("processing the new smpp exchange...");
090                processor.process(exchange);
091                LOG.trace("processed the new smpp exchange");
092            } catch (Exception e) {
093                exceptionHandler.handleException(e);
094                if (e instanceof ProcessRequestException) {
095                    throw (ProcessRequestException) e;
096                }
097                throw new ProcessRequestException(e.getMessage(), 255, e);
098            }
099    
100            return new DataSmResult(newMessageId, dataSm.getOptionalParametes());
101        }
102    
103        public void setMessageIDGenerator(MessageIDGenerator messageIDGenerator) {
104            this.messageIDGenerator = messageIDGenerator;
105        }
106    }