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.DeliverSm;
028    
029    /**
030     * A SMPP Endpoint
031     * 
032     * @version $Revision: 889123 $
033     * @author muellerc
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         * Returns the connection string for the current connection which has the form:
123         * smpp://<user>@<host>:<port>
124         * 
125         * @return the connection string
126         */
127        public String getConnectionString() {
128            return "smpp://" + getConfiguration().getSystemId() + "@"
129                    + getConfiguration().getHost() + ":"
130                    + getConfiguration().getPort();
131        }
132    
133        /**
134         * Returns the smpp configuration
135         * 
136         * @return the configuration
137         */
138        public SmppConfiguration getConfiguration() {
139            return configuration;
140        }
141    
142        public SmppBinding getBinding() {
143            if (binding == null) {
144                binding = new SmppBinding(getConfiguration());
145            }
146            return binding;
147        }
148    
149        public void setBinding(SmppBinding binding) {
150            this.binding = binding;
151        }
152    }