MMS Transport patch

View: New views
3 Messages — Rating Filter:   Alert me  

MMS Transport patch

by Anjana Fernando :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello ,

I have finished one part of my GSoC project,  which is to implement
the MMS transport in MIRAE. I've attached a patch file that contains
the changes I have made.

* I've made a new package as "org.apache.mirae.transport" it contains
the new transport handler classes.

Here are the classes I've made:

TransportHandler: This is the base class for all the transport handler
classes , this class contains a static method to get the suitable
TransportHandler class.

HttpTransportHandler: This is the handler class that has the HTTP
protocol functionality , it contains most of the code that was
previously in the "Dispatch" class.

MessageTransportHandler: This contains the new MMS transport handler.
It takes care of sending and receiving of MMS messages.

* I've modified " Dispatch " class to select the suitable protocol
handler class when it's sending the message.

Regards,
Anjana.

Index: src/mirae/ws/org/apache/mirae/transport/MessageTransportHandler.java
===================================================================
--- src/mirae/ws/org/apache/mirae/transport/MessageTransportHandler.java (revision 0)
+++ src/mirae/ws/org/apache/mirae/transport/MessageTransportHandler.java (revision 0)
@@ -0,0 +1,312 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.transport;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import javax.microedition.io.Connector;
+import javax.wireless.messaging.Message;
+import javax.wireless.messaging.MessageConnection;
+import javax.wireless.messaging.MessagePart;
+import javax.wireless.messaging.MultipartMessage;
+
+/**
+ * provides MMS transport handler functionality
+ */
+public class MessageTransportHandler extends TransportHandler {
+
+ private String appID; // application id used to communicate
+ private InputStream in = null;
+ private OutputStream out = null;
+
+ protected MessageTransportHandler() {
+
+ }
+
+ protected MessageTransportHandler(String url, String soapAction,
+            String userName, String password)
+            throws IOException {
+ this.url = url;
+ /*
+ *  resolve the application id from the url
+ */
+ int index = url.indexOf(":");
+ index = url.indexOf(":", index+1);
+ if (index == -1) {
+ appID = "WS_MMS"; // default application id
+ }
+ else {
+    appID = url.substring(index+1);
+ }
+
+ this.soapAction = soapAction;
+ this.userName = userName;
+ this.password = password;
+ }
+
+ /**
+ *  @see javax.microedition.io.InputConnection#openInputStream()
+ */
+ public InputStream openInputStream() throws IOException {
+ in = new MessageInputStream();
+ return in;
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openOutputStream()
+ */
+ public OutputStream openOutputStream() throws IOException {
+ out = new MessageOutputStream();
+ return out;
+ }
+
+ /**
+ * @see javax.microedition.io.Connection#close()
+ */
+ public void close() throws IOException {
+ if (in != null) {
+ in.close();
+ }
+ if (out != null) {
+ out.close();
+ }
+ }
+
+ /**
+ * provides the input data  from the MMS packet
+ */
+ public class MessageInputStream extends InputStream {
+
+ private boolean dataReady = false;
+ private byte[] data;  // the data buffer that keeps the message content
+ private ByteArrayInputStream byteIn;
+
+ protected MessageInputStream() {
+
+ }
+
+ /**
+ * reads the data from the MMS packet
+ */
+ private void readAll() throws IOException {
+ MessageConnection connection = (MessageConnection)Connector.open(
+                "mms://:" + appID);
+ Message msg = connection.receive();
+ if (msg instanceof MultipartMessage) {
+ MultipartMessage mpm = (MultipartMessage)msg;
+ MessagePart mp = mpm.getMessageParts()[0];
+ data = mp.getContent();
+ }
+ else {
+ throw new IOException("Invalid message type");
+ }
+ byteIn = new ByteArrayInputStream(data);
+ connection.close();
+ dataReady = true; // sets that data is ready to read
+ }
+
+ /**
+ * @see java.io.InputStream#available()
+ */
+ public int available() {
+ if (!dataReady) {   // checks if data has been read
+ try {
+    readAll();
+ }
+ catch (IOException ioe) {
+ return -1;
+ }
+ }
+ return byteIn.available();
+ }
+
+ /**
+ * @see java.io.InputStream#close()
+ */
+    public void close() throws IOException {
+     if (byteIn != null)
+        byteIn.close();
+     dataReady = false;
+    }
+    
+    /**
+     * @see java.io.InputStream#mark(int)
+     */
+    public synchronized void mark(int readlimit) {
+     if (!dataReady) {
+     try {
+    readAll();
+     }
+     catch (IOException ioe) {  
+     }
+ }
+     byteIn.mark(readlimit);
+    }
+
+    /**
+     * @see java.io.InputStream#markSupported()
+     */
+    public boolean markSupported() {
+     if (!dataReady) {
+     try {
+    readAll();
+     }
+     catch (IOException ioe) {
+     return false;
+     }
+ }
+     return byteIn.markSupported();
+    }
+
+    /**
+     * @see java.io.InputStream#read()
+     */
+    public int read() throws IOException {
+ if (!dataReady) {
+ readAll();
+ }
+ return byteIn.read();
+ }
+
+    /**
+     * @see java.io.InputStream#read(byte[])
+     */
+    public int read(byte[] b) throws IOException {
+     if (!dataReady) {
+ readAll();
+ }
+     return byteIn.read(b);
+    }
+    
+    /**
+     * @see java.io.InputStream#read(byte[], int, int)
+     */
+    public int read(byte[] b, int off, int len) throws IOException {
+     if (!dataReady) {
+ readAll();
+ }
+     return byteIn.read(b, off, len);
+    }
+
+    /**
+     * @see java.io.InputStream#reset()
+     */
+    public synchronized void reset() {
+     if (byteIn != null) {
+     byteIn.reset();
+     }
+    }
+    
+    /**
+     * @see java.io.InputStream#skip(long)
+     */
+    public long skip(long n) {
+     if (!dataReady) {
+     try {
+    readAll();
+     }
+     catch (IOException ioe) {
+     return -1;
+     }
+ }
+     return byteIn.skip(n);
+    }
+
+ }
+
+ /**
+ * provides the functionality of outputting the data as a MMS packet
+ */
+ public class MessageOutputStream extends OutputStream {
+
+ private ByteArrayOutputStream byteOut;
+ boolean flushed;
+
+ private MessageOutputStream() {
+ byteOut = new ByteArrayOutputStream();
+ flushed = false;
+ }
+
+ /**
+ * @see java.io.OutputStream#close()
+ */
+ public void close() throws IOException {
+ if (!flushed) {
+ flush();
+ }
+ byteOut.close();
+ }
+
+ /**
+ * Flushes the data and writes the MMS packet to the destination.
+ * @see java.io.OutputStream#flush()
+ */
+    public void flush() throws IOException {    
+     MessageConnection connection;
+     int index = url.indexOf(":");
+     index = url.indexOf(":", index+1);
+    
+     if (index != -1) {
+     connection = (MessageConnection)Connector.open(url);
+     }
+     else {
+     connection = (MessageConnection)Connector.open(
+                      url + ":" + appID);
+     }
+        
+     MultipartMessage message = (MultipartMessage)connection.newMessage(
+                          MessageConnection.MULTIPART_MESSAGE);
+     MessagePart messagePart = new MessagePart(byteOut.toByteArray(),
+     "text/xml", "id1", "location", null);
+     /*
+     * the web service is identified by the message subject
+     */
+     message.setSubject(soapAction);    
+     message.addMessagePart(messagePart);
+     connection.send(message);  
+     connection.close();    
+     flushed = true;    
+    }
+
+    /**
+     * @see java.io.OutputStream#write(int)
+     */
+    public void write(int b) throws IOException {
+     byteOut.write(b);
+    }
+    
+    /*
+     * @see java.io.OutputStream#write(byte[])
+     */
+    public void write(byte[] b) throws IOException {
+     byteOut.write(b);
+    }
+    
+    /**
+     * @see java.io.OutputStream#write(byte[], int, int)
+     */
+    public void write(byte[] b, int off, int len) throws IOException {
+     byteOut.write(b, off, len);
+    }
+
+ }
+
+}
Index: src/mirae/ws/org/apache/mirae/transport/HttpTransportHandler.java
===================================================================
--- src/mirae/ws/org/apache/mirae/transport/HttpTransportHandler.java (revision 0)
+++ src/mirae/ws/org/apache/mirae/transport/HttpTransportHandler.java (revision 0)
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.transport;
+
+import java.io.DataOutputStream;
+import java.io.DataInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpConnection;
+
+import org.apache.mirae.ws.Dispatch;
+import org.apache.mirae.ws.util.Base64;
+
+/**
+ * provides HTTP transport handler functionality
+ */
+public class HttpTransportHandler extends TransportHandler {
+
+ private HttpConnection httpConnection;
+
+ protected HttpTransportHandler() {
+
+ }
+
+ protected HttpTransportHandler(String url, String soapAction,
+                                   String userName, String password)
+                               throws IOException {
+ this.url = url;
+ this.soapAction = soapAction;
+ this.userName = userName;
+ this.password = password;
+
+ httpConnection = (HttpConnection) Connector.open(url,
+                          Connector.READ_WRITE, true);
+ if (soapAction == null) {
+            // WS-I BP 1.0 R2745
+            soapAction = "";
+        }
+        // WS-I BP 1.0 R2744
+        soapAction = "\"" + soapAction + "\"";
+        httpConnection.setRequestProperty("SOAPAction", soapAction);
+        httpConnection.setRequestProperty("Content-Type", "text/xml");
+        // connection.setRequestProperty("Content-Type",
+        // "application/fastinfoset");
+
+        httpConnection.setRequestProperty("User-Agent", "mirae/1.0");
+        if (userName != null) {
+            StringBuffer tmpBuf = new StringBuffer();
+            tmpBuf.append(userName).append(":").append(
+                    (password == null) ? "" : password);
+            String value = " Basic " + Base64.encode(
+                      tmpBuf.toString().getBytes());
+            httpConnection.setRequestProperty(
+              Dispatch.HEADER_AUTHORIZATION, value);
+        }
+
+        httpConnection.setRequestMethod(HttpConnection.POST);        
+ }
+
+ /**
+ * @see javax.microedition.io.InputConnection#openInputStream()
+ */
+ public InputStream openInputStream() throws IOException {
+ return httpConnection.openInputStream();
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openOutputStream()
+ */
+ public OutputStream openOutputStream() throws IOException {
+ return httpConnection.openOutputStream();
+ }
+
+ /**
+ * @see javax.microedition.io.InputConnection#openDataInputStream()
+ */
+ public DataInputStream openDataInputStream() throws IOException {
+ return httpConnection.openDataInputStream();
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openDataOutputStream()
+ */
+ public DataOutputStream openDataOutputStream() throws IOException {
+ return httpConnection.openDataOutputStream();
+ }
+
+    /**
+     * @see javax.microedition.io.Connection#close()
+     */
+ public void close() throws IOException {
+ httpConnection.close();
+ }
+}
Index: src/mirae/ws/org/apache/mirae/transport/TransportHandler.java
===================================================================
--- src/mirae/ws/org/apache/mirae/transport/TransportHandler.java (revision 0)
+++ src/mirae/ws/org/apache/mirae/transport/TransportHandler.java (revision 0)
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.transport;
+
+import java.io.DataOutputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import javax.microedition.io.StreamConnection;
+
+public abstract class TransportHandler implements StreamConnection {
+
+ protected String url;
+ protected String soapAction;
+ protected String userName;
+ protected String password;
+
+ /**
+ * checks the url and builds the necessory TransportHandler with the
+ * other information given by the parameters.
+ * @param url the web sevice end point
+ * @param soapAction the web service soap action
+ * @param username identifies the user that accesses the web service
+ * @param password the password to authenticate the user
+ * @return the TransportHandler associated with the protocol used.
+ * @throws IOException
+ */
+ public static TransportHandler getTransportHandler(String url,
+                                           String soapAction,
+                                           String username,
+                                           String password)
+                                                   throws IOException {
+ /*
+ * The Transport handler is mapped to the pattern of the url.
+ */
+ if (url.startsWith("http://")) {
+ return new HttpTransportHandler(url, soapAction,
+                        username, password);
+ }
+ else if (url.startsWith("mms://")) {
+ return new MessageTransportHandler(url, soapAction,
+                           username, password);
+ }
+ else {
+ throw new IOException("Unsupported protocol");
+ }
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openDataOutputStream()
+ */
+ public DataOutputStream openDataOutputStream() throws IOException {
+ return new DataOutputStream(openOutputStream());
+ }
+
+ /**
+ * @see javax.microedition.io.InputConnection#openDataInputStream()
+ */
+ public DataInputStream openDataInputStream() throws IOException {
+ return new DataInputStream(openInputStream());
+ }
+
+ /**
+ * @return the url associated with the TranportHandler
+ */
+ public String getURL() {
+ return url;
+ }
+
+ /**
+ * @return the soap action associated with the TranportHandler
+ */
+ public String getSoapAction() {
+ return soapAction;
+ }
+
+ /**
+ * @return the user name associated with the TranportHandler
+ */
+ public String getUserName() {
+ return userName;
+ }
+
+ /**
+ * @return the password associated with the TranportHandler
+ */
+ public String getPassword() {
+ return password;
+ }
+
+}
Index: src/mirae/ws/org/apache/mirae/transport/HttpTransportHandler.java
===================================================================
--- src/mirae/ws/org/apache/mirae/transport/HttpTransportHandler.java (revision 0)
+++ src/mirae/ws/org/apache/mirae/transport/HttpTransportHandler.java (revision 0)
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.transport;
+
+import java.io.DataOutputStream;
+import java.io.DataInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpConnection;
+
+import org.apache.mirae.ws.Dispatch;
+import org.apache.mirae.ws.util.Base64;
+
+/**
+ * provides HTTP transport handler functionality
+ */
+public class HttpTransportHandler extends TransportHandler {
+
+ private HttpConnection httpConnection;
+
+ protected HttpTransportHandler() {
+
+ }
+
+ protected HttpTransportHandler(String url, String soapAction,
+                                   String userName, String password)
+                               throws IOException {
+ this.url = url;
+ this.soapAction = soapAction;
+ this.userName = userName;
+ this.password = password;
+
+ httpConnection = (HttpConnection) Connector.open(url,
+                          Connector.READ_WRITE, true);
+ if (soapAction == null) {
+            // WS-I BP 1.0 R2745
+            soapAction = "";
+        }
+        // WS-I BP 1.0 R2744
+        soapAction = "\"" + soapAction + "\"";
+        httpConnection.setRequestProperty("SOAPAction", soapAction);
+        httpConnection.setRequestProperty("Content-Type", "text/xml");
+        // connection.setRequestProperty("Content-Type",
+        // "application/fastinfoset");
+
+        httpConnection.setRequestProperty("User-Agent", "mirae/1.0");
+        if (userName != null) {
+            StringBuffer tmpBuf = new StringBuffer();
+            tmpBuf.append(userName).append(":").append(
+                    (password == null) ? "" : password);
+            String value = " Basic " + Base64.encode(
+                      tmpBuf.toString().getBytes());
+            httpConnection.setRequestProperty(
+              Dispatch.HEADER_AUTHORIZATION, value);
+        }
+
+        httpConnection.setRequestMethod(HttpConnection.POST);        
+ }
+
+ /**
+ * @see javax.microedition.io.InputConnection#openInputStream()
+ */
+ public InputStream openInputStream() throws IOException {
+ return httpConnection.openInputStream();
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openOutputStream()
+ */
+ public OutputStream openOutputStream() throws IOException {
+ return httpConnection.openOutputStream();
+ }
+
+ /**
+ * @see javax.microedition.io.InputConnection#openDataInputStream()
+ */
+ public DataInputStream openDataInputStream() throws IOException {
+ return httpConnection.openDataInputStream();
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openDataOutputStream()
+ */
+ public DataOutputStream openDataOutputStream() throws IOException {
+ return httpConnection.openDataOutputStream();
+ }
+
+    /**
+     * @see javax.microedition.io.Connection#close()
+     */
+ public void close() throws IOException {
+ httpConnection.close();
+ }
+}
Index: src/mirae/ws/org/apache/mirae/transport/MessageTransportHandler.java
===================================================================
--- src/mirae/ws/org/apache/mirae/transport/MessageTransportHandler.java (revision 0)
+++ src/mirae/ws/org/apache/mirae/transport/MessageTransportHandler.java (revision 0)
@@ -0,0 +1,312 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.transport;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import javax.microedition.io.Connector;
+import javax.wireless.messaging.Message;
+import javax.wireless.messaging.MessageConnection;
+import javax.wireless.messaging.MessagePart;
+import javax.wireless.messaging.MultipartMessage;
+
+/**
+ * provides MMS transport handler functionality
+ */
+public class MessageTransportHandler extends TransportHandler {
+
+ private String appID; // application id used to communicate
+ private InputStream in = null;
+ private OutputStream out = null;
+
+ protected MessageTransportHandler() {
+
+ }
+
+ protected MessageTransportHandler(String url, String soapAction,
+            String userName, String password)
+            throws IOException {
+ this.url = url;
+ /*
+ *  resolve the application id from the url
+ */
+ int index = url.indexOf(":");
+ index = url.indexOf(":", index+1);
+ if (index == -1) {
+ appID = "WS_MMS"; // default application id
+ }
+ else {
+    appID = url.substring(index+1);
+ }
+
+ this.soapAction = soapAction;
+ this.userName = userName;
+ this.password = password;
+ }
+
+ /**
+ *  @see javax.microedition.io.InputConnection#openInputStream()
+ */
+ public InputStream openInputStream() throws IOException {
+ in = new MessageInputStream();
+ return in;
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openOutputStream()
+ */
+ public OutputStream openOutputStream() throws IOException {
+ out = new MessageOutputStream();
+ return out;
+ }
+
+ /**
+ * @see javax.microedition.io.Connection#close()
+ */
+ public void close() throws IOException {
+ if (in != null) {
+ in.close();
+ }
+ if (out != null) {
+ out.close();
+ }
+ }
+
+ /**
+ * provides the input data  from the MMS packet
+ */
+ public class MessageInputStream extends InputStream {
+
+ private boolean dataReady = false;
+ private byte[] data;  // the data buffer that keeps the message content
+ private ByteArrayInputStream byteIn;
+
+ protected MessageInputStream() {
+
+ }
+
+ /**
+ * reads the data from the MMS packet
+ */
+ private void readAll() throws IOException {
+ MessageConnection connection = (MessageConnection)Connector.open(
+                "mms://:" + appID);
+ Message msg = connection.receive();
+ if (msg instanceof MultipartMessage) {
+ MultipartMessage mpm = (MultipartMessage)msg;
+ MessagePart mp = mpm.getMessageParts()[0];
+ data = mp.getContent();
+ }
+ else {
+ throw new IOException("Invalid message type");
+ }
+ byteIn = new ByteArrayInputStream(data);
+ connection.close();
+ dataReady = true; // sets that data is ready to read
+ }
+
+ /**
+ * @see java.io.InputStream#available()
+ */
+ public int available() {
+ if (!dataReady) {   // checks if data has been read
+ try {
+    readAll();
+ }
+ catch (IOException ioe) {
+ return -1;
+ }
+ }
+ return byteIn.available();
+ }
+
+ /**
+ * @see java.io.InputStream#close()
+ */
+    public void close() throws IOException {
+     if (byteIn != null)
+        byteIn.close();
+     dataReady = false;
+    }
+    
+    /**
+     * @see java.io.InputStream#mark(int)
+     */
+    public synchronized void mark(int readlimit) {
+     if (!dataReady) {
+     try {
+    readAll();
+     }
+     catch (IOException ioe) {  
+     }
+ }
+     byteIn.mark(readlimit);
+    }
+
+    /**
+     * @see java.io.InputStream#markSupported()
+     */
+    public boolean markSupported() {
+     if (!dataReady) {
+     try {
+    readAll();
+     }
+     catch (IOException ioe) {
+     return false;
+     }
+ }
+     return byteIn.markSupported();
+    }
+
+    /**
+     * @see java.io.InputStream#read()
+     */
+    public int read() throws IOException {
+ if (!dataReady) {
+ readAll();
+ }
+ return byteIn.read();
+ }
+
+    /**
+     * @see java.io.InputStream#read(byte[])
+     */
+    public int read(byte[] b) throws IOException {
+     if (!dataReady) {
+ readAll();
+ }
+     return byteIn.read(b);
+    }
+    
+    /**
+     * @see java.io.InputStream#read(byte[], int, int)
+     */
+    public int read(byte[] b, int off, int len) throws IOException {
+     if (!dataReady) {
+ readAll();
+ }
+     return byteIn.read(b, off, len);
+    }
+
+    /**
+     * @see java.io.InputStream#reset()
+     */
+    public synchronized void reset() {
+     if (byteIn != null) {
+     byteIn.reset();
+     }
+    }
+    
+    /**
+     * @see java.io.InputStream#skip(long)
+     */
+    public long skip(long n) {
+     if (!dataReady) {
+     try {
+    readAll();
+     }
+     catch (IOException ioe) {
+     return -1;
+     }
+ }
+     return byteIn.skip(n);
+    }
+
+ }
+
+ /**
+ * provides the functionality of outputting the data as a MMS packet
+ */
+ public class MessageOutputStream extends OutputStream {
+
+ private ByteArrayOutputStream byteOut;
+ boolean flushed;
+
+ private MessageOutputStream() {
+ byteOut = new ByteArrayOutputStream();
+ flushed = false;
+ }
+
+ /**
+ * @see java.io.OutputStream#close()
+ */
+ public void close() throws IOException {
+ if (!flushed) {
+ flush();
+ }
+ byteOut.close();
+ }
+
+ /**
+ * Flushes the data and writes the MMS packet to the destination.
+ * @see java.io.OutputStream#flush()
+ */
+    public void flush() throws IOException {    
+     MessageConnection connection;
+     int index = url.indexOf(":");
+     index = url.indexOf(":", index+1);
+    
+     if (index != -1) {
+     connection = (MessageConnection)Connector.open(url);
+     }
+     else {
+     connection = (MessageConnection)Connector.open(
+                      url + ":" + appID);
+     }
+        
+     MultipartMessage message = (MultipartMessage)connection.newMessage(
+                          MessageConnection.MULTIPART_MESSAGE);
+     MessagePart messagePart = new MessagePart(byteOut.toByteArray(),
+     "text/xml", "id1", "location", null);
+     /*
+     * the web service is identified by the message subject
+     */
+     message.setSubject(soapAction);    
+     message.addMessagePart(messagePart);
+     connection.send(message);  
+     connection.close();    
+     flushed = true;    
+    }
+
+    /**
+     * @see java.io.OutputStream#write(int)
+     */
+    public void write(int b) throws IOException {
+     byteOut.write(b);
+    }
+    
+    /*
+     * @see java.io.OutputStream#write(byte[])
+     */
+    public void write(byte[] b) throws IOException {
+     byteOut.write(b);
+    }
+    
+    /**
+     * @see java.io.OutputStream#write(byte[], int, int)
+     */
+    public void write(byte[] b, int off, int len) throws IOException {
+     byteOut.write(b, off, len);
+    }
+
+ }
+
+}
Index: src/mirae/ws/org/apache/mirae/transport/TransportHandler.java
===================================================================
--- src/mirae/ws/org/apache/mirae/transport/TransportHandler.java (revision 0)
+++ src/mirae/ws/org/apache/mirae/transport/TransportHandler.java (revision 0)
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.transport;
+
+import java.io.DataOutputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import javax.microedition.io.StreamConnection;
+
+public abstract class TransportHandler implements StreamConnection {
+
+ protected String url;
+ protected String soapAction;
+ protected String userName;
+ protected String password;
+
+ /**
+ * checks the url and builds the necessory TransportHandler with the
+ * other information given by the parameters.
+ * @param url the web sevice end point
+ * @param soapAction the web service soap action
+ * @param username identifies the user that accesses the web service
+ * @param password the password to authenticate the user
+ * @return the TransportHandler associated with the protocol used.
+ * @throws IOException
+ */
+ public static TransportHandler getTransportHandler(String url,
+                                           String soapAction,
+                                           String username,
+                                           String password)
+                                                   throws IOException {
+ /*
+ * The Transport handler is mapped to the pattern of the url.
+ */
+ if (url.startsWith("http://")) {
+ return new HttpTransportHandler(url, soapAction,
+                        username, password);
+ }
+ else if (url.startsWith("mms://")) {
+ return new MessageTransportHandler(url, soapAction,
+                           username, password);
+ }
+ else {
+ throw new IOException("Unsupported protocol");
+ }
+ }
+
+ /**
+ * @see javax.microedition.io.OutputConnection#openDataOutputStream()
+ */
+ public DataOutputStream openDataOutputStream() throws IOException {
+ return new DataOutputStream(openOutputStream());
+ }
+
+ /**
+ * @see javax.microedition.io.InputConnection#openDataInputStream()
+ */
+ public DataInputStream openDataInputStream() throws IOException {
+ return new DataInputStream(openInputStream());
+ }
+
+ /**
+ * @return the url associated with the TranportHandler
+ */
+ public String getURL() {
+ return url;
+ }
+
+ /**
+ * @return the soap action associated with the TranportHandler
+ */
+ public String getSoapAction() {
+ return soapAction;
+ }
+
+ /**
+ * @return the user name associated with the TranportHandler
+ */
+ public String getUserName() {
+ return userName;
+ }
+
+ /**
+ * @return the password associated with the TranportHandler
+ */
+ public String getPassword() {
+ return password;
+ }
+
+}
Index: src/mirae/ws/org/apache/mirae/ws/Dispatch.java
===================================================================
--- src/mirae/ws/org/apache/mirae/ws/Dispatch.java (revision 424174)
+++ src/mirae/ws/org/apache/mirae/ws/Dispatch.java (working copy)
@@ -1,125 +1,105 @@
-package org.apache.mirae.ws;
-
-import java.io.IOException;
-
-import javax.microedition.io.Connector;
-import javax.microedition.io.HttpConnection;
-import javax.microedition.io.StreamConnection;
-import javax.xml.rpc.NamespaceConstants;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamConstants;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamWriter;
-
-import org.apache.mirae.ws.util.Base64;
-
-public class Dispatch {
-    private StreamConnection connection;
-
-    public Dispatch(String endpointUrl, String soapAction) throws IOException {
-        this(endpointUrl, soapAction, null, null);
-    }
-    
-    public Dispatch(StreamConnection connection) {
-        this.connection = connection;
-    }
-    
-    public Dispatch(String endpointUrl, String soapAction, String username,
-            String password) throws IOException {
-            HttpConnection con = (HttpConnection) Connector.open(endpointUrl,
-                    Connector.READ_WRITE, true);
-            if (soapAction == null) {
-                // WS-I BP 1.0 R2745
-                soapAction = "";
-            }
-            // WS-I BP 1.0 R2744
-            soapAction = "\"" + soapAction + "\"";
-            con.setRequestProperty("SOAPAction", soapAction);
-            con.setRequestProperty("Content-Type", "text/xml");
-            // connection.setRequestProperty("Content-Type",
-            // "application/fastinfoset");
-
-            con.setRequestProperty("User-Agent", "mirae/1.0");
-            if (username != null) {
-                StringBuffer tmpBuf = new StringBuffer();
-
-                tmpBuf.append(username).append(":").append(
-                        (password == null) ? "" : password);
-                String value = " Basic "
-                        + Base64.encode(tmpBuf.toString().getBytes());
-                con.setRequestProperty(
-                        OperationStax.HEADER_AUTHORIZATION, value);
-            }
-
-            con.setRequestMethod(HttpConnection.POST);
-            connection = con;
-    }
-
-    public XMLStreamWriter getResponseWriter() throws XMLStreamException, IOException {
-        return getRequestWriter();
-    }
-    
-    public XMLStreamWriter getRequestWriter() throws XMLStreamException, IOException {
-        // XMLOutputFactory outputFactory = new StAXOutputFactory();
-        // outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
-        // Boolean.TRUE);
-        return XMLOutputFactory.newInstance().createXMLStreamWriter(connection.openOutputStream());
-    }
-
-    public void closeConnection() {
-        if (connection != null) {
-            try {
-                connection.close();
-            } catch (IOException e) {
-            }
-            connection = null;
-        }
-        
-    }
-    
-    public XMLStreamReader getRequestReader() throws XMLStreamException, IOException {
-        return getResponseReader();
-    }
-
-    public XMLStreamReader getResponseReader() throws XMLStreamException, IOException {
-        return XMLInputFactory.newInstance()
-        .createXMLStreamReader(connection.openInputStream());
-    }
-    
-    public void writeBeforeBody(XMLStreamWriter writer) throws XMLStreamException {
-        writer.writeStartDocument();
-        writer.writeStartElement("v", "Envelope", NamespaceConstants.NSURI_SOAP_ENVELOPE);
-        writer.writeNamespace("i", NamespaceConstants.NSURI_SCHEMA_XSI);
-        writer.writeNamespace("d", NamespaceConstants.NSURI_SCHEMA_XSD);
-        writer.writeNamespace("c", NamespaceConstants.NSURI_SOAP_ENCODING);
-        writer.writeNamespace("v", NamespaceConstants.NSURI_SOAP_ENVELOPE);
-        writer.setPrefix("v", NamespaceConstants.NSURI_SOAP_ENVELOPE);
-        writer.writeStartElement(NamespaceConstants.NSURI_SOAP_ENVELOPE, "Body");
-        
-    }
-    
-    public void writeAfterBody(XMLStreamWriter writer) throws XMLStreamException {
-        writer.writeEndElement();
-        writer.writeEndElement();
-        writer.writeEndDocument();        
-    }
-    
-    public void readBeforeBody(XMLStreamReader reader) throws XMLStreamException {
-        if (reader.next() != XMLStreamConstants.START_ELEMENT) {
-            reader.nextTag();
-        }
-        reader.require(XMLStreamConstants.START_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Envelope");
-        reader.nextTag();
-        reader.require(XMLStreamConstants.START_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Body");
-        reader.nextTag();        
-    }
-    
-    public void readAfterBody(XMLStreamReader reader) throws XMLStreamException {
-        reader.nextTag();
-        reader.require(XMLStreamConstants.END_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Body");
-        reader.nextTag();
-        reader.require(XMLStreamConstants.END_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Envelope");        
-    }
-}
+package org.apache.mirae.ws;
+
+import java.io.IOException;
+
+import javax.microedition.io.StreamConnection;
+import javax.xml.rpc.NamespaceConstants;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.mirae.transport.TransportHandler;
+
+public class Dispatch {
+    /** header for security (username/password) */
+    public static final String HEADER_AUTHORIZATION = "Authorization";
+
+    private StreamConnection connection;
+
+    public Dispatch(String endpointUrl, String soapAction) throws IOException {
+        this(endpointUrl, soapAction, null, null);
+    }
+    
+    public Dispatch(StreamConnection connection) {
+        this.connection = connection;
+    }
+    
+    public Dispatch(String endpointUrl, String soapAction, String username,
+            String password) throws IOException {    
+            connection = TransportHandler.getTransportHandler(
+             endpointUrl, soapAction,username, password);
+    }
+
+    public XMLStreamWriter getResponseWriter() throws XMLStreamException, IOException {
+        return getRequestWriter();
+    }
+    
+    public XMLStreamWriter getRequestWriter() throws XMLStreamException, IOException {
+        // XMLOutputFactory outputFactory = new StAXOutputFactory();
+        // outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
+        // Boolean.TRUE);
+        return XMLOutputFactory.newInstance().createXMLStreamWriter(connection.openOutputStream());
+    }
+
+    public void closeConnection() {
+        if (connection != null) {
+            try {
+                connection.close();
+            } catch (IOException e) {
+            }
+            connection = null;
+        }
+        
+    }
+    
+    public XMLStreamReader getRequestReader() throws XMLStreamException, IOException {
+        return getResponseReader();
+    }
+
+    public XMLStreamReader getResponseReader() throws XMLStreamException, IOException {
+        return XMLInputFactory.newInstance()
+        .createXMLStreamReader(connection.openInputStream());
+    }
+    
+    public void writeBeforeBody(XMLStreamWriter writer) throws XMLStreamException {
+        writer.writeStartDocument();
+        writer.writeStartElement("v", "Envelope", NamespaceConstants.NSURI_SOAP_ENVELOPE);
+        writer.writeNamespace("i", NamespaceConstants.NSURI_SCHEMA_XSI);
+        writer.writeNamespace("d", NamespaceConstants.NSURI_SCHEMA_XSD);
+        writer.writeNamespace("c", NamespaceConstants.NSURI_SOAP_ENCODING);
+        writer.writeNamespace("v", NamespaceConstants.NSURI_SOAP_ENVELOPE);
+        writer.setPrefix("v", NamespaceConstants.NSURI_SOAP_ENVELOPE);
+        writer.writeStartElement(NamespaceConstants.NSURI_SOAP_ENVELOPE, "Body");
+        
+    }
+    
+    public void writeAfterBody(XMLStreamWriter writer) throws XMLStreamException {
+        writer.writeEndElement();
+        writer.writeEndElement();
+        writer.writeEndDocument();        
+    }
+    
+    public void readBeforeBody(XMLStreamReader reader) throws XMLStreamException {
+        if (reader.next() != XMLStreamConstants.START_ELEMENT) {
+            reader.nextTag();
+        }
+        reader.require(XMLStreamConstants.START_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Envelope");
+        reader.nextTag();
+        reader.require(XMLStreamConstants.START_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Header");
+        reader.nextTag();
+        reader.require(XMLStreamConstants.END_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Header");
+        reader.nextTag();
+        reader.require(XMLStreamConstants.START_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Body");
+        reader.nextTag();        
+    }
+    
+    public void readAfterBody(XMLStreamReader reader) throws XMLStreamException {
+        reader.nextTag();
+        reader.require(XMLStreamConstants.END_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Body");
+        reader.nextTag();
+        reader.require(XMLStreamConstants.END_ELEMENT, NamespaceConstants.NSURI_SOAP_ENVELOPE, "Envelope");        
+    }
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: mirae-dev-unsubscribe@...
For additional commands, e-mail: mirae-dev-help@...

Re: MMS Transport patch

by Changshin Lee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I'm very glad to see this work. We'll take some time to review the
code, and start voting on adding this new feature to Mirae.

Thank you,

Ias

On 7/22/06, Anjana Fernando <lafernando@...> wrote:

> Hello ,
>
> I have finished one part of my GSoC project,  which is to implement
> the MMS transport in MIRAE. I've attached a patch file that contains
> the changes I have made.
>
> * I've made a new package as "org.apache.mirae.transport" it contains
> the new transport handler classes.
>
> Here are the classes I've made:
>
> TransportHandler: This is the base class for all the transport handler
> classes , this class contains a static method to get the suitable
> TransportHandler class.
>
> HttpTransportHandler: This is the handler class that has the HTTP
> protocol functionality , it contains most of the code that was
> previously in the "Dispatch" class.
>
> MessageTransportHandler: This contains the new MMS transport handler.
> It takes care of sending and receiving of MMS messages.
>
> * I've modified " Dispatch " class to select the suitable protocol
> handler class when it's sending the message.
>
> Regards,
> Anjana.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: mirae-dev-unsubscribe@...
> For additional commands, e-mail: mirae-dev-help@...
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: mirae-dev-unsubscribe@...
For additional commands, e-mail: mirae-dev-help@...


Re: MMS Transport patch

by Anjana Fernando :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's great ..  :)

- Anjana

On 7/24/06, Changshin Lee <iasandcb@...> wrote:

> Hi,
>
> I'm very glad to see this work. We'll take some time to review the
> code, and start voting on adding this new feature to Mirae.
>
> Thank you,
>
> Ias
>
> On 7/22/06, Anjana Fernando <lafernando@...> wrote:
> > Hello ,
> >
> > I have finished one part of my GSoC project,  which is to implement
> > the MMS transport in MIRAE. I've attached a patch file that contains
> > the changes I have made.
> >
> > * I've made a new package as "org.apache.mirae.transport" it contains
> > the new transport handler classes.
> >
> > Here are the classes I've made:
> >
> > TransportHandler: This is the base class for all the transport handler
> > classes , this class contains a static method to get the suitable
> > TransportHandler class.
> >
> > HttpTransportHandler: This is the handler class that has the HTTP
> > protocol functionality , it contains most of the code that was
> > previously in the "Dispatch" class.
> >
> > MessageTransportHandler: This contains the new MMS transport handler.
> > It takes care of sending and receiving of MMS messages.
> >
> > * I've modified " Dispatch " class to select the suitable protocol
> > handler class when it's sending the message.
> >
> > Regards,
> > Anjana.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: mirae-dev-unsubscribe@...
> > For additional commands, e-mail: mirae-dev-help@...
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: mirae-dev-unsubscribe@...
> For additional commands, e-mail: mirae-dev-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: mirae-dev-unsubscribe@...
For additional commands, e-mail: mirae-dev-help@...

LightInTheBox - Buy quality products at wholesale price!