📄 relayconnection.java
字号:
throw new APIException( "unexpected-content-type", contentTypes[0] ); return ResponseFactory.makeResponse( inStream ); } catch( IOException ioe ) { throw new APIException( "io-exception-reading-from-socket", ioe.getMessage() ); } catch( MessagingException me ) { throw new APIException( "cannot-read-headers", me.getMessage() ); } catch( SOAPException se ) { throw new APIException( ErrorCode.SERVER_ERROR, se.getMessage() ); } finally { try { if( useNewSocket ) newSocket.close(); else socket.close(); } catch( IOException ioe ) { // cant do much } } } /** * Sets a message listener in the connection object that processes incoming * requests from MMSC. Use this method to set a custom object that implements * the {@link MessageListener} interface or extends the {@link MessageListenerAdapter} * class as the listener that processes incoming requests using custom functionality. * * @param messageListener The custom object that implements the <code> * MessageListener</code> interface or extends the * <code>MessageListenerAdapter</code> class. * @exception APIException If the appliation attempts to set a message listener in * a <code>RelayConnection</code> object that was created in send-only mode. */ public void setMessageListener( MessageListener messageListener ) throws APIException { threadCheck(); if( mode == SENDER_ONLY ) throw new APIException( "connection-mode-is-sender-only" ); this.messageListener = messageListener; server.setAuthenticators( authenticators ); server.setAuthType( authType ); server.startListener(); } MessageListener getMessageListener() { return messageListener; } /** * When the API is used in a servlet-based application, this method is used by the * servlet to dispatch an incoming request to a <code>MessageListener</code>. This * method is valid only in servlet applications. * * @param request The servlet request object that encapsulates the incoming request. * @param response The servlet response object that encapsulates the outgoing response. * @param messageListener The custom object that implements the <code> * MessageListener</code> interface or extends the <code>MessageListenerAdapter * </code> class to which the API dispatches the request. * @exception APIException If the API encounters an error writing the request to * the socket. */ public static void dispatch( ServletRequest request, ServletResponse response, MessageListener messageListener ) throws APIException { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = request.getInputStream(); try { Response resp = dispatchInternal( inputStream, request.getContentType(), messageListener ); writeServletResponse( resp, response ); } catch( APIException ae ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ae.getLocalizedMessage(), ae ); } FaultResponse res = new FaultResponse( ae.getMessage(), ae.getErrorCode() ); writeServletResponse( res, response ); throw ae; } catch( SOAPException se ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( se.getMessage(), se ); } FaultResponse res = new FaultResponse( se.getMessage(), ErrorCode.CLIENT_ERROR ); writeServletResponse( res, response ); throw new APIException( ErrorCode.CLIENT_ERROR, se.getMessage() ); } catch( MessagingException me ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( me.getMessage(), me ); } FaultResponse res = new FaultResponse( me.getMessage(), ErrorCode.CLIENT_ERROR ); writeServletResponse( res, response ); throw new APIException( ErrorCode.CLIENT_ERROR, me.getMessage() ); } catch( ContentException ce ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ce.getMessage(), ce ); } FaultResponse res = new FaultResponse( ce.getMessage(), ErrorCode.CLIENT_ERROR ); writeServletResponse( res, response ); throw new APIException( ErrorCode.CLIENT_ERROR, ce.getMessage() ); } catch( MessageProcessingException mpe ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( mpe.getMessage(), mpe ); } // write dummy response FaultResponse res = new FaultResponse( "Client", "Client Error", mpe.getMessage(), mpe.getErrorCode() ); writeServletResponse( res, response ); throw new APIException( ErrorCode.CLIENT_ERROR, mpe.getMessage() ); } } catch( IOException ioe ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ioe.getMessage(), ioe ); } throw new APIException( ErrorCode.SERVER_ERROR, "io-exception-writing-to-socket", ioe.getMessage() ); } catch( SOAPException se ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( se.getMessage(), se ); } throw new APIException( ErrorCode.SERVER_ERROR, se.getMessage() ); } } /** * Sets the <code>Vector</code> of <code>PasswordAuthentication</code> objects that * contain the user name and password pairs that the API uses to authenticate incoming * requests. This method is valid only for standalone applications operating in * receive-only or send and receive mode. Connection authentication for servlet * applications is handled by the web server in which the servlet is deployed. * * @param authenticators <code>Vector</code> of <code>PasswordAuthentication</code> * objects the API uses to authenticate connection requests. * @exception ClassCastException If the <code>Vector</code> objects are not * of type <code>PasswordAuthentication</code>. * @exception APIException If the current thread does not own this * <code>RelayConnection</code> object. * */ public void setAuthenticators( Vector authenticators ) throws ClassCastException, APIException { threadCheck(); this.authenticators = new HashMap(); if( authenticators != null ) { for( int i = 0; i < authenticators.size(); i++ ) { java.net.PasswordAuthentication auth = (java.net.PasswordAuthentication) authenticators.get( i ); this.authenticators.put( auth.getUserName(), auth ); } } } /** * Sets the type of HTTP authentication used to validate incoming requests. This method * is valid only for standalone applications operating in receive-only or send and receive * mode. * * @param authType The type of HTTP authentication to use. Must be an * {@link AuthenticationType} object. * @exception APIException If the current thread does not own this * <code>RelayConnection</code> object. * */ public void setAuthType( AuthenticationType authType ) throws APIException { threadCheck(); if( mode == SENDER_ONLY ) throw new APIException( "connection-mode-is-sender-only" ); this.authType = authType; } /** * When using SSL, indicates to the API whether you are using a server certificate * that has a weak common name. * A weak common name is a host name used in a test certificate on the server * that may differ from the actual host name where the servier is running. These certificates * are used for testing purposes while waiting to a Certificate Authority to assign a * certificate. * * @param weakCN A boolean that specifies whether the server certificate has a weak * common name. Specifiy <code>true</code> if certficate has a weak common name; * otherwise specify <code>false</code>, which is the default. * @exception APIException If the current thread does not own this * <code>RelayConnection</code> object. * */ public void setWeakCN( boolean weakCN ) throws APIException { threadCheck(); this.weakCN = weakCN; } /** * Constructor for initiatng a receiver-only mode of connection. * * @param port The port on which the server will listen for connections. * @param secure True for initiating an SSL server. * @exception APIException If the API cannot create the server socket. * */ private RelayConnection( int port, boolean secure ) throws APIException { mode = RECEIVER_ONLY; this.port = port; server = new Server( this, port, secure ); owningThread = Thread.currentThread(); } /** * Constructor for initiatng a sender-only mode of * connection. * * @param url The URL at which an MMS Relay is available. * If the protocol portion of the URL is https, an SSL * connection will be initiated. * @param userName The user name to be used to connect to the URL. * @param password The password to be used to connect to the URL. * @exception MalformedURLException if url is not parseable. * @exception APIException if username/password is not supplied. * */ private RelayConnection( String url, String userName, String password ) throws MalformedURLException, APIException { if( userName == null || userName.length() == 0 || password == null || password.length() == 0 ) throw new APIException( "no-username-password" ); this.url = new URL( url ); this.userName = userName; this.password = password; mode = SENDER_ONLY; owningThread = Thread.currentThread(); } /** * Constructor for initiatng a receiver and sender mode of * connection. * * @param url The URL at which an MMS Relay is available. * If the protocol portion of the URL is https, an SSL * connection will be initiated. * @param userName The user name to be used to connect to the URL. * @param password The password to be used to connect to the URL. * @param port The port on which the receiver will listen for connections. * @param secure True for making the receiver listen for SSL connections only. * @exception MalformedURLException if Url is not parseable. * @exception APIException if username/password is not supplied. * */ private RelayConnection( String url, String userName, String password, int port, boolean secure ) throws MalformedURLException, APIException { if( userName == null || userName.length() == 0 || password == null || password.length() == 0 ) throw new APIException( "no-username-password" ); this.url = new URL( url ); this.userName = userName; this.password = password; this.port = port; mode = SENDER_AND_RECEIVER; server = new Server( this, port, secure ); owningThread = Thread.currentThread(); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -