📄 listener.java
字号:
package com.openwave.mms.mm7;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import javax.mail.internet.InternetHeaders;import javax.mail.internet.MimeMultipart;import javax.mail.MessagingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.io.PrintStream;import java.util.StringTokenizer;import java.util.Random;import java.util.HashMap;import org.apache.log4j.Logger;import org.apache.log4j.Level;import org.apache.log4j.NDC;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.SOAPEnvelope;import com.openwave.mms.mm7.soap.SOAPException;import com.openwave.mms.mm7.soap.SOAPHeader;import com.openwave.mms.mm7.soap.SOAPMethod;import com.openwave.mms.mm7.soap.SOAPParser;class Listener implements Runnable { public Listener( RelayConnection relayConn, ServerSocket server ) { this.relayConn = relayConn; this.server = server; secret = new byte[20]; new Random().nextBytes( secret ); } public synchronized void startListener( ) { newListener(); } public synchronized void setAuthenticators( HashMap authenticators ) { this.authenticators = authenticators; } public synchronized void setAuthType( RelayConnection.AuthenticationType authType ) { this.authType = authType; } public void run() { Socket socket = null; // accept a connection try { if( logger.isInfoEnabled() ) { logger.info( "MM7Receiver listening for connections on port " + server.getLocalPort() ); } socket = server.accept(); socket.setSoTimeout( 15 * 1000 ); // 15 secs } catch( IOException e ) { try { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( e.getMessage() ); } if( socket != null ) socket.close(); } catch( IOException ioe ) { //cant do much! if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ioe.getMessage() ); } } NDC.remove(); return; } // create a new thread to accept the next connection newListener(); try { if( logger.isInfoEnabled() ) { logger.info( "MM7Receiver got a connection" ); } OutputStream out = socket.getOutputStream(); try { InputStream in = socket.getInputStream(); if( logger.isInfoEnabled() ) { RecordedInputStream recorder = new RecordedInputStream( in ); logger.debug( "[Begin Incoming Request From Relay]" ); logger.debug( recorder.getBuffer() ); logger.debug( "[End Incoming Request From Relay]" ); in = recorder; } InternetHeaders headers = new InternetHeaders( ); if( authenticators != null ) { boolean authSuccess = checkAuth( in, out, headers ); if( ! authSuccess ) return; } else headers.load( in ); // the following should be part of a dispatcher class // which can be used in a Servlet String contentTypes[] = headers.getHeader( "content-type" ); Response response = RelayConnection.dispatchInternal( in, contentTypes[0], relayConn.getMessageListener() ); writeResponse( response, out ); } catch( APIException ae ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ae.getLocalizedMessage() ); } FaultResponse res = new FaultResponse( ae.getMessage(), ae.getErrorCode() ); writeResponse( res, out ); } catch( IOException ioe ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ioe.getMessage() ); } // write out error response FaultResponse res = new FaultResponse( ioe.getMessage(), ErrorCode.SERVER_ERROR ); writeResponse( res, out ); } catch( MessageProcessingException mpe ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( mpe.getLocalizedMessage() ); } // write dummy response FaultResponse res = new FaultResponse( "Client", "Client Error", mpe.getMessage(), mpe.getErrorCode() ); writeResponse( res, out ); } out.flush(); } catch( Exception ex ) { // catch all. most probably this exception occurred when processing // other exceptions so just log it if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ex.getMessage() ); } } finally { try { socket.close(); } catch( IOException ioe ) { //cant do much! if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ioe.getMessage() ); } } NDC.remove(); } } private void writeHeaders( OutputStream out ) throws IOException { out.write("HTTP/1.0 200 OK\r\n".getBytes()); out.write("Content-Type: text/xml\r\n".getBytes()); if( logger.isDebugEnabled() ) { logger.debug( "[Begin Outgoing Response To Relay]\n" ); logger.debug( "HTTP/1.0 200 OK\r\n" ); logger.debug( "Content-Type: text/xml\r\n" ); } } private void newListener() { (new Thread(this)).start(); } private void writeEmptyResponse( OutputStream outStream ) throws IOException { writeHeaders( outStream ); outStream.write( "Content-length: 0\r\n\r\n".getBytes() ); if( logger.isDebugEnabled() ) { logger.debug( "Content-length: 0\r\n\r\n".getBytes() ); logger.debug( "\n[End Outgoing Response To Relay]" ); } } class Authorization { public String userName; public String realm; public String nonce; public String cNonce; public String nonceCount; public String response; public String qop; public String uri; } private boolean checkAuth( InputStream inStream, OutputStream outStream, InternetHeaders headers ) throws APIException, IOException, MessagingException { headers.load( inStream ); String authHeaders[] = headers.getHeader( "authorization" ); String authHeader = null; if( authHeaders != null && ( authHeader = authHeaders[ authHeaders.length - 1 ] ) != null ) { //parse the header and verify Auth int spaceIndex = authHeader.indexOf( " " ); if( spaceIndex == -1 ) throw new APIException( ErrorCode.CLIENT_ERROR, "malformed-authorization-header" ); String authType = authHeader.substring( 0, spaceIndex ); if( authType.equalsIgnoreCase( "basic" ) ) { if( this.authType == RelayConnection.AuthenticationType.DIGEST ) { drainInputStream( inStream, headers ); sendAuthHeader( outStream ); return false; } String base64String = authHeader.substring( spaceIndex + 1 ); byte[] decodedString = Base64.decode( base64String );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -