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

📄 relayconnection.java

📁 openwave公司的用于彩信开发的MM7协议实现java原代码,决对超值.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
package com.openwave.mms.mm7;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.HashMap;import java.net.HttpURLConnection;import java.io.IOException;import java.io.InputStream;import java.security.MessageDigest;import java.net.MalformedURLException;import java.security.NoSuchAlgorithmException;import java.io.OutputStream;import java.io.PrintWriter;import java.util.Properties;import java.util.StringTokenizer;import java.net.Socket;import java.net.URL;import java.net.UnknownHostException;import java.util.Vector;import javax.security.cert.CertificateExpiredException;import javax.security.cert.CertificateNotYetValidException;import javax.mail.internet.InternetHeaders;import javax.mail.MessagingException;import javax.mail.internet.MimeMultipart;import javax.net.ssl.SSLSocket;import javax.net.ssl.SSLSocketFactory;import javax.security.cert.X509Certificate;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.apache.log4j.Logger;import org.apache.log4j.Level;import com.openwave.mms.mm7.util.Base64;import com.openwave.mms.mm7.util.InputStreamDataSource;import com.openwave.mms.mm7.util.RecordedInputStream;import com.openwave.mms.mm7.soap.SOAPConsts;import com.openwave.mms.mm7.soap.SOAPException;import com.openwave.mms.mm7.soap.SOAPMethod;import com.openwave.mms.mm7.soap.SOAPParser;import com.openwave.mms.content.ContentException;/** *  This class encapsulates the connection that applications use to *  send MM7 messages to and receive them from MMSC. Applications use *  <code>RelayConnection</code> objects to establish connections with *  MMSC in one of three modes: *  <UL> *  <LI> Send-only, *  <LI> Receive-only, *  <LI> Send and Receive, *  </UL> *  <p> *  Use this class to handle the application connection with MMSC by following these  *  guidelines:  *  <OL> *  <LI> Create a <code>RelayConnection</code> object. *  <LI> Initiate a connection with MMSC in one of the three operating modes.  * *  <LI> For standalone applications in receive-only or send and receive mode,  *       specify the type of HTTP Access Authentication supported.  * *  <LI> If using the Secure Socket Layer (SSL) protocol, specify whether your *       server certificate has a weak common name using the {@link #setWeakCN *       setWeakCN} method. * *  <LI> For standalone applications in receive-only or send and receive mode, *       create an object that implements the {@link MessageListener} interface  *       or extends the {@link MessageListenerAdapter} class and register  *       the object with the <code>RelayConnection</code> object using *       the {@link #setMessageListener setMessageListener} method. *  *  <LI> To send messages to MMSC from applications operating in send-only or  *       send and receive mode, create a {@link SubmitRequest} object that  *       contains the message and use the connection's {@link #sendRequest  *       sendRequest} method to send it. The API encapsulates the response from MMSC  *       in a {@link SubmitResponse} or {@link FaultResponse} object  *       and returns it. * *  <LI> When the API receives requests from MMSC, it converts the request to *       a {@link DeliverRequest}, {@link DeliveryReport}, or {@link ReadReply} *       object. For standalone applications, the API passes the request object  *       to the custom message listener object that was registered with the  *       <code>RelayConnection</code> object. For servlet *       applications, use the connection's {@link #dispatch dispatch} method   *       in the servlet's <code>doPost</code> method to dispatch the request to a *       MessageListener object. *       To return the appropriate response, create a response object and use it as *       the return value of the message listener method that processes the request. *       The <code>RelayConnection</code> object converts the response to SOAP format *       and returns it to MMSC.    * </OL> * <p> * For further information about using this class handle connections between your * application and MMSC, see the <em>Openwave MMS Library Developer's Guide</em>. * *  @version 3.0 */public final class RelayConnection {    /**     *  This inner class encapsulates the constants that identify     *  the type of HTTP authentication the API uses to authenticate incoming requests.     */    public static class AuthenticationType {        /**         *  Private constructor so that AuthenticationTypes are only ones created below.         *         *  @param authType The authetication type.         */        private AuthenticationType( int authType ) {            this.authType = authType;        }        /**         *  Static constant that identifies the type of HTTP authentication as          *  HTTP Basic Access Authentication.         */        public static final AuthenticationType BASIC = new AuthenticationType( 0 );            /**         *  Static constant that identifies the type of HTTP authentication as          *  HTTP Digest Access Authentication.         */        public static final AuthenticationType DIGEST = new AuthenticationType( 1 );            /**         *  Static constant that identifies the type of HTTP authentication as          *  either HTTP Basic or Digest Access Authentication.         */        public static final AuthenticationType ANY = new AuthenticationType( 2 );            private int authType;    }    /**     *   Creates a connection for communication with MMSC in receiver-only mode.     *   Applications using this type of connection can only receive requests from     *   and cannot send them to MMSC. This method should be used only in standalone      *   applications.      *     *   @param port The port on which the application listens for connections      *               from MMSC.     *   @param secure A boolean that specifies whether the communication is      *               conducted using the Secure Socket Layer (SSL) protocol.     *               <code>true</code> specifies that the application uses SSL,     *               <code>false</code> indicates it does not use SSL.     *   @exception APIException If the API cannot create the server socket.     */    public static RelayConnection createReceiver(                                     int port,                                     boolean secure ) throws APIException {        return new RelayConnection( port, secure );    }    /**     *  Creates a connection for communication with MMSC in send-only mode.     *  Applications using this type of connection can only send requests to     *  and cannot receive them from MMSC.     *     *   @param url The URL on which MMSC listens for requests and to which     *              the API sends requests.     *              If the protocol portion of the URL is <code>https</code>,      *              the API initiates an SSL connection.     *   @param userName The numerical portion of the VASP's PLMN address that     *              the application uses to connect to MMSC. For example,     *              15551234567.     *   @param password The password associated with <code>userName</code>     *              that the application uses to connect to MMSC.     *   @exception MalformedURLException If the API encounters an error parsing     *              the <code>url</code>.     *   @exception APIException If the application does not supply a value for      *              <code>userName</code> or <code>password</code>.     */    public static RelayConnection createSender(                             String url,                             String userName,                             String password ) throws MalformedURLException,                                                      APIException {        return new RelayConnection( url, userName, password );    }    /**     *  Creates a connection for communication with MMSC in send and receive mode.     *  Applications using this type of connection can send requests to     *  and receive them from MMSC. This method is valid only for standalone      *  applications.      *     *   @param url The URL on which MMSC listens for requests and to which     *              the API sends requests.     *              If the protocol portion of the URL is <code>https</code>,      *              the API initiates an SSL connection.     *   @param userName The numerical portion of the VASP's PLMN address that     *              the application uses to connect to MMSC. For example,     *              15551234567.     *   @param password The password that the application uses to connect      *              to MMSC.     *   @param port The port on which the application listens for connections      *               from MMSC.     *   @param secure A boolean that specifies whether the communication is      *              conducted using the Secure Socket Layer (SSL) protocol.     *              <code>true</code> specifies that the application uses SSL,     *              <code>false</code> indicates it does not use SSL.     *   @exception MalformedURLException If the API encounters an error parsing     *              the <code>url</code>.     *   @exception APIException If the application does not supply a value for      *              <code>userName</code> or <code>password</code>.     */    public static RelayConnection createSenderAndReceiver(                             String url,                             String userName,                             String password,                             int port,                             boolean secure ) throws MalformedURLException,                                                     APIException {        return new RelayConnection( url,                                    userName,                                    password,                                    port,                                    secure );     }    /**     *   Sends a <code>Request</code> object that contains a multimedia message     *   to MMSC for delivery to mobile subscribers and returns a     *   <code>Response</code> object that encapsulates the MMSC response.      *     *   @param request A {@link SubmitRequest} object.     *   @return A {@link SubmitResponse} object that contains the MMSC response     *           to the <code>request</code>.     *   @exception APIException If the connection to MMSC is not successful,     *           an error occurs writing the request to the socket, the request      *           contains an invalid content type or error in the content type      *           header, or an error occurs reading the response from the socket.       */    public Response sendRequest( Request request )                                 throws APIException,                                        ContentException {        threadCheck();        if( mode == RECEIVER_ONLY )            throw new APIException( "connection-mode-is-receiver-only" );        Socket socket = createSocket();        Socket newSocket = null;        boolean useNewSocket = false;        try {            int retCode = writeMessage( request, socket, null );            if( retCode == HttpURLConnection.HTTP_UNAUTHORIZED ) {                WWWAuthenticateHeader wwwAuthHeader = getWWWAuthHeader( socket.getInputStream() );                String authHeaderValue = computeAuthHeaderValue( wwwAuthHeader );                socket.close();                newSocket = createSocket();                retCode = writeMessage( request, newSocket, authHeaderValue );                useNewSocket = true;            }            if( retCode != HttpURLConnection.HTTP_OK )                throw new APIException( "http-return-code", new Integer( retCode ) );        } catch( IOException ioe ) {            try {                if( useNewSocket )                    newSocket.close();                else                    socket.close();            } catch( IOException ioe1 ) {                // cant do much            }            throw new APIException( "io-exception-writing-to-socket",                                    ioe.getMessage() );        }        try {            InputStream inStream = useNewSocket ? newSocket.getInputStream()                                                : socket.getInputStream();            if( logger.isDebugEnabled() ) {                RecordedInputStream recorder = new RecordedInputStream( inStream );                logger.debug( "[Begin Incoming Response from Relay]" );                logger.debug( recorder.getBuffer() );                logger.debug( "[End Incoming Response from Relay]" );                inStream = recorder;            }            InternetHeaders headers = new InternetHeaders( inStream );            String[] contentTypes = headers.getHeader( "content-type" );            if( contentTypes == null || contentTypes.length == 0 )                throw new APIException( "no-content-type-header" );            if( ! contentTypes[0].startsWith( "text/xml" ) )

⌨️ 快捷键说明

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