⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connector.java

📁 Messaging- MIDP的是一个彩信客户端
💻 JAVA
字号:
/*
 *  JVending - J2ME MMS Client
 *  Copyright (C) 2004  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jvending.messaging.io;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;

import javax.microedition.io.*;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.BinaryMessage;

import org.jvending.messaging.impl.mms.MultimediaParser;
import org.jvending.messaging.impl.io.PeekInputStream;
import org.jvending.messaging.MessageConnection;
import org.jvending.messaging.MultimediaMessage;



/**
 * @author Shane Isbell
 * @version 1.0.2a
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.MessageListener;

 * @since 04/03/28
 */

public class Connector {

    public static final int READ = 0;

    public static final int READ_WRITE = 1;

    public static final int WRITE = 2;



    private Connector() {
    }

    /**
     * @param url
     * @return
     * @throws IOException
     * @throws ConnectionNotFoundException
     * @throws IllegalArgumentException
     * @throws SecurityException
     */
    public static Connection open(String url) throws IOException,
            ConnectionNotFoundException, IllegalArgumentException, SecurityException {
        MessageConnection mc = new MessageConnectionImpl();
        mc.setUrlConnection(url);
        return mc;
    }
    private static class MessageConnectionImpl implements MessageConnection {

        private String urlConnection;

        private javax.wireless.messaging.MessageConnection messageConnection;

        private HttpConnection httpConnection;

        /**
         * If value is already set, this operation will be silently ignored. There should never be a case
         * where two different urls are set within the system.
         *
         * @param urlConnection
         */
        public void setUrlConnection(String urlConnection) throws IOException {
            if (this.urlConnection != null) return;

            if (urlConnection.startsWith("http:")) {
                httpConnection = (HttpConnection) javax.microedition.io.Connector.open(urlConnection);
            } else if (urlConnection.startsWith("sms:")) {
                messageConnection = (javax.wireless.messaging.MessageConnection)
                        javax.microedition.io.Connector.open(urlConnection);
            } else {
                //TODO: Support https
                throw new IOException("Unrecognized Message Type");
            }
            this.urlConnection = urlConnection;
        }

        /**
         * TODO: Only returns MULTIPART_MIXED_MESSAGE
         *
         * @param type
         * @return
         * @throws IllegalArgumentException
         */
        public Message newMessage(String type) throws IllegalArgumentException {
            if (type.equals(MULTIPART_MIXED_MESSAGE)) return new MultimediaMessage();
            else throw new IllegalArgumentException();
        }

        /**
         * @param type
         * @param address
         * @return
         * @throws IllegalArgumentException
         */
        public Message newMessage(String type, String address) throws IllegalArgumentException {
            if (type.equals(MULTIPART_MIXED_MESSAGE)) {
                MultimediaMessage mm = new MultimediaMessage();
                mm.setAddress(address);
                return mm;
            } else throw new IllegalArgumentException("Unsupported Content Type");
        }

        /**
         * @param message
         * @return
         */
        public int numberOfSegments(Message message) {
            return 1;//don't care
        }

        /**
         * @return
         * @throws java.io.IOException
         * @throws java.io.InterruptedIOException
         * @throws SecurityException
         */
        public Message receive() throws IOException, InterruptedIOException, SecurityException {
            if (urlConnection == null) throw new IOException("The URL has not been set");
            InputStream is = null;
            ByteArrayInputStream bais = null;
            if (urlConnection.startsWith("http:")) {
                is = httpConnection.openInputStream();
            } else if (urlConnection.startsWith("sms:")) {
                BinaryMessage bm = (BinaryMessage) messageConnection.receive();
                byte[] data = bm.getPayloadData();
                is = (InputStream) new ByteArrayInputStream(data);
            } else {
                //TODO: Support https
                throw new IOException("Unrecognized Message Type");
            }

            PeekInputStream pis = new PeekInputStream(is);
            MultimediaMessage mm = new MultimediaMessage();
            MultimediaParser mp = new MultimediaParser();
            mp.parse(pis, mm);

            if (is != null) is.close();
            if (bais != null) bais.close();

            return mm;
        }

        /**
         * TODO: Not Implemented
         *
         * @param message
         * @throws IOException
         * @throws InterruptedException
         * @throws SecurityException
         * @throws IllegalArgumentException
         * @throws NullPointerException
         */
        public void send(Message message) throws IOException, InterruptedException,
                SecurityException, IllegalArgumentException, NullPointerException {
            if (message == null) throw new IllegalArgumentException("Null message");
            if (!(message instanceof org.jvending.messaging.MultimediaMessage))
                throw new IllegalArgumentException("Incorrect message type");
            String address = message.getAddress();
            if (address == null || address.trim().equals("")) throw new IOException("No sending address");
            //open connection to address:: what is the protocol?
            //We should do: HTTP to web server, SMS to another device and MM1 protocol to MMSC.
            //for now, POST to web and send SMS (MMS Notification) with URI to another device.
            //send bytes over connection
        }

        /**
         * This method only works on MessageConnections, not HttpConnections.
         *
         * @param messageListener
         * @throws SecurityException
         * @throws IOException
         */
        public void setMessageListener(MessageListener messageListener) throws SecurityException, IOException {
            if (messageConnection != null) messageConnection.setMessageListener(messageListener);
            else if (httpConnection != null) throw new IOException("Cannot attach listeners to HTTP Messages");
            else throw new IOException("Connection not set");
        }

        /**
         * @throws IOException
         */
        public void close() throws IOException {
            if (httpConnection != null) httpConnection.close();
            if (messageConnection != null) messageConnection.close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -