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.Component;
020    import org.apache.camel.Consumer;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.ExchangePattern;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.impl.DefaultEndpoint;
026    import org.jsmpp.bean.AlertNotification;
027    import org.jsmpp.bean.DataSm;
028    import org.jsmpp.bean.DeliverSm;
029    
030    /**
031     * A SMPP Endpoint
032     * 
033     * @version 
034     */
035    public class SmppEndpoint extends DefaultEndpoint {
036    
037        private SmppBinding binding;
038        private SmppConfiguration configuration;
039    
040        public SmppEndpoint(String endpointUri, Component component, SmppConfiguration configuration) {
041            super(endpointUri, component);
042            this.configuration = configuration;
043        }
044    
045        public boolean isSingleton() {
046            return true;
047        }
048        
049        @Override
050        protected String createEndpointUri() {
051            return getConnectionString();
052        }
053        
054        @Override
055        public boolean isLenientProperties() {
056            return true;
057        }
058    
059        public Consumer createConsumer(Processor processor) throws Exception {
060            return new SmppConsumer(this, configuration, processor);
061        }
062    
063        public Producer createProducer() throws Exception {
064            return new SmppProducer(this, configuration);
065        }
066    
067        /**
068         * Create a new exchange for communicating with this endpoint from a SMSC
069         *
070         * @param alertNotification the received message from the SMSC
071         * @return a new exchange
072         */
073        public Exchange createOnAcceptAlertNotificationExchange(AlertNotification alertNotification) {
074            return createOnAcceptAlertNotificationExchange(getExchangePattern(), alertNotification);
075        }
076        
077        /**
078         * Create a new exchange for communicating with this endpoint from a SMSC
079         * with the specified {@link ExchangePattern} such as whether its going
080         * to be an {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut} exchange
081         *
082         * @param exchangePattern the message exchange pattern for the exchange
083         * @param alertNotification the received message from the SMSC
084         * @return a new exchange
085         */
086        public Exchange createOnAcceptAlertNotificationExchange(ExchangePattern exchangePattern,
087                                                                AlertNotification alertNotification) {
088            Exchange exchange = createExchange(exchangePattern);
089            exchange.setProperty(Exchange.BINDING, getBinding());
090            exchange.setIn(getBinding().createSmppMessage(alertNotification));
091            return exchange;
092        }
093    
094        /**
095         * Create a new exchange for communicating with this endpoint from a SMSC
096         *
097         * @param deliverSm the received message from the SMSC
098         * @return a new exchange
099         */
100        public Exchange createOnAcceptDeliverSmExchange(DeliverSm deliverSm) throws Exception {
101            return createOnAcceptDeliverSmExchange(getExchangePattern(), deliverSm);
102        }
103        
104        /**
105         * Create a new exchange for communicating with this endpoint from a SMSC
106         * with the specified {@link ExchangePattern} such as whether its going
107         * to be an {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut} exchange
108         *
109         * @param exchangePattern the message exchange pattern for the exchange
110         * @param deliverSm the received message from the SMSC
111         * @return a new exchange
112         */
113        public Exchange createOnAcceptDeliverSmExchange(ExchangePattern exchangePattern,
114                                                        DeliverSm deliverSm) throws Exception {
115            Exchange exchange = createExchange(exchangePattern);
116            exchange.setProperty(Exchange.BINDING, getBinding());
117            exchange.setIn(getBinding().createSmppMessage(deliverSm));
118            return exchange;
119        }
120        
121        /**
122         * Create a new exchange for communicating with this endpoint from a SMSC
123         *
124         * @param dataSm the received message from the SMSC
125         * @param smppMessageId the smpp message id which will be used in the response
126         * @return a new exchange
127         */
128        public Exchange createOnAcceptDataSm(DataSm dataSm, String smppMessageId) {
129            return createOnAcceptDataSm(getExchangePattern(), dataSm, smppMessageId);
130        }
131        
132        /**
133         * Create a new exchange for communicating with this endpoint from a SMSC
134         * with the specified {@link ExchangePattern} such as whether its going
135         * to be an {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut} exchange
136         *
137         * @param exchangePattern the message exchange pattern for the exchange
138         * @param dataSm the received message from the SMSC
139         * @param smppMessageId the smpp message id which will be used in the response
140         * @return a new exchange
141         */
142        public Exchange createOnAcceptDataSm(ExchangePattern exchangePattern, DataSm dataSm, String smppMessageId) {
143            Exchange exchange = createExchange(exchangePattern);
144            exchange.setProperty(Exchange.BINDING, getBinding());
145            exchange.setIn(getBinding().createSmppMessage(dataSm, smppMessageId));
146            return exchange;
147        }
148    
149        /**
150         * Returns the connection string for the current connection which has the form:
151         * smpp://<user>@<host>:<port>
152         * 
153         * @return the connection string
154         */
155        public String getConnectionString() {
156            return (configuration.getUsingSSL() ? "smpps://" : "smpp://") 
157                    + getConfiguration().getSystemId() + "@"
158                    + getConfiguration().getHost() + ":"
159                    + getConfiguration().getPort();
160        }
161    
162        /**
163         * Returns the smpp configuration
164         * 
165         * @return the configuration
166         */
167        public SmppConfiguration getConfiguration() {
168            return configuration;
169        }
170    
171        public SmppBinding getBinding() {
172            if (binding == null) {
173                binding = new SmppBinding(getConfiguration());
174            }
175            return binding;
176        }
177    
178        public void setBinding(SmppBinding binding) {
179            this.binding = binding;
180        }
181    }