View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.mms.transport.mm1;
36  
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import java.io.ByteArrayOutputStream;
41  import java.io.IOException;
42  import java.io.InputStream;
43  import java.io.OutputStream;
44  import java.net.HttpURLConnection;
45  import java.net.URL;
46  import java.util.Properties;
47  
48  import org.marre.mime.MimeBodyPart;
49  import org.marre.mms.MmsException;
50  import org.marre.mms.MmsHeaders;
51  import org.marre.mms.transport.MmsTransport;
52  import org.marre.util.IOUtil;
53  import org.marre.util.StringUtil;
54  
55  /***
56   * Sends mms using the mm1 protocol. 
57   * 
58   * @author Markus Eriksson
59   * @version $Id: Mm1Transport.java,v 1.9 2005/11/26 14:52:08 c95men Exp $
60   */
61  public class Mm1Transport implements MmsTransport
62  {
63      private static Logger log_ = LoggerFactory.getLogger(Mm1Transport.class);
64  
65      /*** 
66       * Content type for a mms message. 
67       */
68      public static final String CONTENT_TYPE_WAP_MMS_MESSAGE = "application/vnd.wap.mms-message";
69  
70      /***
71       * URL for the proxy gateway
72       */
73      private String myMmsProxyGatewayAddress;
74  
75      /***
76       * @see org.marre.mms.transport.MmsTransport#init(java.util.Properties)
77       */
78      public void init(Properties theProps) throws MmsException
79      {
80          myMmsProxyGatewayAddress = theProps.getProperty("smsj.mm1.proxygateway");
81  
82          if (myMmsProxyGatewayAddress == null)
83          {
84              throw new MmsException("smsj.mm1.proxygateway not set");
85          }
86      }
87  
88      /***
89       * The mm1 protocol is connection less so this method is not used.
90       * @see org.marre.mms.transport.MmsTransport#connect()
91       */
92      public void connect()
93      {
94          // Empty
95      }
96  
97      /***
98       * Sends MMS.
99       * 
100      * @see org.marre.mms.transport.MmsTransport#send(org.marre.mime.MimeBodyPart, org.marre.mms.MmsHeaders)
101      */
102     public void send(MimeBodyPart theMessage, MmsHeaders theHeaders) throws MmsException, IOException
103     {
104         // POST data to the MMSC
105         
106         // First create the data so we can find out how large it is
107         ByteArrayOutputStream baos = new ByteArrayOutputStream();
108         Mm1Encoder.writeMessageToStream(baos, theMessage, theHeaders);
109         baos.close();
110         
111         if (log_.isDebugEnabled())
112         {
113             String str = StringUtil.bytesToHexString(baos.toByteArray());
114             log_.debug("request [" + str + "]");
115         }
116         
117         URL url = new URL(myMmsProxyGatewayAddress);
118         HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
119         
120         urlConn.addRequestProperty("Content-Length", "" + baos.size());
121         urlConn.addRequestProperty("Content-Type", CONTENT_TYPE_WAP_MMS_MESSAGE);
122 
123         urlConn.setDoOutput(true);
124         urlConn.setDoInput(true);
125         urlConn.setAllowUserInteraction(false);
126         
127         // Send the data
128         OutputStream out = urlConn.getOutputStream();
129         baos.writeTo(out);
130         out.flush();
131         out.close();
132         
133         baos.reset();
134         baos = new ByteArrayOutputStream();
135         
136         // Read the response
137         InputStream response = urlConn.getInputStream();
138         
139         int responsecode = urlConn.getResponseCode();
140         log_.debug("HTTP response code : " + responsecode);
141         
142         IOUtil.copy(response, baos);
143         baos.close();
144         
145         if (log_.isDebugEnabled())
146         {
147             String str = StringUtil.bytesToHexString(baos.toByteArray());
148             log_.debug("response [" + str + "]");
149         }
150         // TODO: Parse the response
151     }
152 
153     /***
154      * The mm1 protocol is connection less so this method is not used.
155      * @see org.marre.mms.transport.MmsTransport#disconnect()
156      */
157     public void disconnect() throws IOException
158     {
159         // Empty
160     }
161 }