1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
105
106
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
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
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
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
160 }
161 }