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.impl.DefaultConsumer;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.jsmpp.DefaultPDUReader;
025 import org.jsmpp.DefaultPDUSender;
026 import org.jsmpp.SynchronizedPDUSender;
027 import org.jsmpp.bean.AlertNotification;
028 import org.jsmpp.bean.BindType;
029 import org.jsmpp.bean.DeliverSm;
030 import org.jsmpp.bean.NumberingPlanIndicator;
031 import org.jsmpp.bean.TypeOfNumber;
032 import org.jsmpp.session.BindParameter;
033 import org.jsmpp.session.MessageReceiverListener;
034 import org.jsmpp.session.SMPPSession;
035 import org.jsmpp.util.DefaultComposer;
036
037 /**
038 * An implementation of @{link Consumer} which use the SMPP protocol
039 *
040 * @version $Revision: 941133 $
041 * @author muellerc
042 */
043 public class SmppConsumer extends DefaultConsumer {
044
045 private static final transient Log LOG = LogFactory.getLog(SmppConsumer.class);
046
047 private SmppConfiguration configuration;
048 private SMPPSession session;
049
050 /**
051 * The constructor which gets a smpp endpoint, a smpp configuration and a processor
052 */
053 public SmppConsumer(SmppEndpoint endpoint, SmppConfiguration configuration, Processor processor) {
054 super(endpoint, processor);
055 this.configuration = configuration;
056 }
057
058 @Override
059 protected void doStart() throws Exception {
060 if (LOG.isDebugEnabled()) {
061 LOG.debug("Connecting to: " + getEndpoint().getConnectionString() + "...");
062 }
063
064 super.doStart();
065
066 session = createSMPPSession();
067 session.setEnquireLinkTimer(this.configuration.getEnquireLinkTimer());
068 session.setTransactionTimer(this.configuration.getTransactionTimer());
069 session.setMessageReceiverListener(new MessageReceiverListener() {
070 public void onAcceptAlertNotification(AlertNotification alertNotification) {
071 if (LOG.isDebugEnabled()) {
072 LOG.debug("Received an alertNotification " + alertNotification);
073 }
074
075 try {
076 Exchange exchange = getEndpoint().createOnAcceptAlertNotificationExchange(alertNotification);
077
078 LOG.trace("Processing the new smpp exchange...");
079 getProcessor().process(exchange);
080 LOG.trace("Processed the new smpp exchange");
081 } catch (Exception e) {
082 getExceptionHandler().handleException(e);
083 }
084 }
085
086 public void onAcceptDeliverSm(DeliverSm deliverSm) {
087 if (LOG.isDebugEnabled()) {
088 LOG.debug("Received a deliverSm " + deliverSm);
089 }
090
091 try {
092 Exchange exchange = getEndpoint().createOnAcceptDeliverSmExchange(deliverSm);
093
094 LOG.trace("processing the new smpp exchange...");
095 getProcessor().process(exchange);
096 LOG.trace("processed the new smpp exchange");
097 } catch (Exception e) {
098 getExceptionHandler().handleException(e);
099 }
100 }
101
102 });
103
104 session.connectAndBind(
105 this.configuration.getHost(),
106 this.configuration.getPort(),
107 new BindParameter(
108 BindType.BIND_RX,
109 this.configuration.getSystemId(),
110 this.configuration.getPassword(),
111 this.configuration.getSystemType(),
112 TypeOfNumber.UNKNOWN,
113 NumberingPlanIndicator.UNKNOWN,
114 ""));
115
116 LOG.info("Connected to: " + getEndpoint().getConnectionString());
117 }
118
119 /**
120 * Factory method to easily instantiate a mock SMPPSession
121 *
122 * @return the SMPPSession
123 */
124 SMPPSession createSMPPSession() {
125 if (configuration.getUsingSSL()) {
126 return new SMPPSession(new SynchronizedPDUSender(new DefaultPDUSender(new DefaultComposer())),
127 new DefaultPDUReader(), SmppSSLConnectionFactory.getInstance());
128 } else {
129 return new SMPPSession();
130 }
131 }
132
133 @Override
134 protected void doStop() throws Exception {
135 LOG.debug("Disconnecting from: " + getEndpoint().getConnectionString() + "...");
136
137 super.doStop();
138
139 if (session != null) {
140 session.close();
141 session = null;
142 }
143
144 LOG.info("Disconnected from: " + getEndpoint().getConnectionString());
145 }
146
147 @Override
148 public String toString() {
149 return "SmppConsumer[" + getEndpoint().getConnectionString() + "]";
150 }
151
152 @Override
153 public SmppEndpoint getEndpoint() {
154 return (SmppEndpoint) super.getEndpoint();
155 }
156
157 /**
158 * Returns the smpp configuration
159 *
160 * @return the configuration
161 */
162 public SmppConfiguration getConfiguration() {
163 return configuration;
164 }
165
166 }