gateway.java
来自「First of all, the Applet-phone is a SIP 」· Java 代码 · 共 664 行 · 第 1/2 页
JAVA
664 行
package gov.nist.examples.bps.gateway;import java.util.*;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.sip.*;import javax.sip.message.*;import javax.sip.header.*;import javax.sip.address.*;import gov.nist.examples.bps.registrar.*;import tools.rtpgateway.*;import gov.nist.examples.bps.presenceserver.*;/** Gateway Entry point. * *@version JAIN-SIP-1.1 * *@author Olivier Deruelle <deruelle@nist.gov> (primary) * M. Ranganathan <mranga@nist.gov> (bug fixes and hacks) <br/> *<a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a> * */public class Gateway implements SipListener { protected RTPHolesManager rtpHolesManager; protected Vector sipStacksList; protected MessageFactory messageFactory; protected HeaderFactory headerFactory; protected AddressFactory addressFactory; protected Configuration configuration; protected Registrar registrar; protected GatewayUtilities gatewayUtilities; protected RequestForwarding requestForwarding; protected ResponseForwarding responseForwarding; protected PresenceServer presenceServer; protected SyncManager syncManager; public RequestForwarding getRequestForwarding() { return requestForwarding; } public ResponseForwarding getResponseForwarding() { return responseForwarding; } public AddressFactory getAddressFactory() { return addressFactory; } public MessageFactory getMessageFactory() { return messageFactory; } public HeaderFactory getHeaderFactory() { return headerFactory; } public PresenceServer getPresenceServer() { return presenceServer; } public GatewayUtilities getGatewayUtilities() { return gatewayUtilities; } public Configuration getConfiguration() { return configuration; } public SyncManager getSyncManager() { return syncManager; } /** Creates new Proxy */ public Gateway(String confFile) throws Exception{ sipStacksList=new Vector(); if (confFile==null) { System.out.println ("ERROR: Set the configuration file flag: " + "USE: -cf configuration_file_location.xml" ); } else { try { // First, let's parse the configuration file. GatewayConfigurationHandler handler= new GatewayConfigurationHandler(confFile); configuration=handler.getConfiguration(); if (configuration==null || !configuration.isValidConfiguration()) { System.out.println ("ERROR: the configuration file is not correct!"+ " Correct the errors first."); throw new Exception ("ERROR: the configuration file is not correct!"+ " Correct the errors first."); } else { gatewayUtilities=new GatewayUtilities(this); registrar=new Registrar(this); presenceServer=new PresenceServer(this); syncManager=new SyncManager(presenceServer); requestForwarding=new RequestForwarding(this); responseForwarding=new ResponseForwarding(this); } } catch (Exception ex) { System.out.println ("ERROR: exception raised while initializing the Gateway"); ex.printStackTrace(); throw new Exception ("ERROR: exception raised while initializing the Gateway"); } } } /** This is a listener method. */ public void processRequest(RequestEvent requestEvent) { Request request = requestEvent.getRequest(); SipProvider sipProvider = (SipProvider) requestEvent.getSource(); ServerTransaction serverTransaction=requestEvent.getServerTransaction(); try { if (GatewayDebug.debug) GatewayDebug.println ("\n***************************************************************"+ "\n***************************************************************"+ "\nRequest " + request.getMethod() +" received:\n"+request.toString()); if (GatewayDebug.debug) GatewayUtilities.printTransaction(serverTransaction); /******************************* REGISTER *************************************/ if (request.getMethod().equals(Request.REGISTER)) { // we use the registrar. registrar.processRegister(request,sipProvider,serverTransaction); // Subscribe to the base Readers: try{ ExtensionHeader baseReaderHeader=(ExtensionHeader)request.getHeader("Base-reader"); if (baseReaderHeader!=null) { // WE have to keep track of the base tag: presenceServer.processRegister(request); presenceServer.sendSubscribe(request,sipProvider); } } catch(Exception e) { e.printStackTrace(); } return; }/******************************* NOTIFY ***********************************/ if (request.getMethod().equals(Request.NOTIFY) ) { ExtensionHeader baseReaderHeader=(ExtensionHeader)request.getHeader("Base-reader"); if (baseReaderHeader==null) { // This NOTIFY does not belong to any base-reader, we discard it. Response response=messageFactory.createResponse (Response.METHOD_NOT_ALLOWED,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); GatewayDebug.println("Gateway, processRequest(), we drop the request, "+ "this NOTIFY is not from a base reader, METHOD_NOT_ALLOWED replied"); return; } // we use the presence server. presenceServer.processNotify(request,sipProvider,serverTransaction, baseReaderHeader.getValue()); return; }/******************************* METHODS NOT ALLOWED **************************/ if ( !request.getMethod().equals(Request.INVITE) && !request.getMethod().equals(Request.ACK) && !request.getMethod().equals(Request.BYE) && !request.getMethod().equals(Request.CANCEL) ) { Response response=messageFactory.createResponse (Response.METHOD_NOT_ALLOWED,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); GatewayDebug.println("Gateway, processRequest(), we drop the request, "+ "METHOD_NOT_ALLOWED replied"); return; }/******************************* INVITE, ACK, BYE, CANCEL *******************/ // The INVITE is submitted to a specific process where we check if the // user calling and the user to be called have badges and are not // constrained to each other. /* Algorithm: * 1. Check if the From URI has a badge bond to it. * If not, we reply "UNAUTHORIZED". Otherwise step 2. * 2. Check if the Request URI has a badge bond to it. * If not, we reply "NOT_FOUND". Otherwise step 3. * 3. Check if Request URI is registered. * If not, we reply "TEMPORARILY_UNAVAILABLE". Otherwise step 4. * 4. Get the list of constraints for Request URI. * If constraints related to people name (From URI), then reply "FORBIDDEN" * If constraints related to people presence (From URI), then reply "BUSY" * If constraints related to people rank (From URI), then reply "BUSY" * If constraints related to places (get the location information * from the presence server), then reply "BUSY" * Otherwise, forward the INVITE. */ if (request.getMethod().equals(Request.INVITE)) { // STEP 1. /* if ( ! getSyncManager().isSenderAllowed(request) ) { Response response=messageFactory.createResponse (Response.UNAUTHORIZED,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), "+ " STEP 1 failed: the sender does not have a badge.\n"+ " 401 (UNAUTHORIZED) replied:\n"+ response.toString() ); return; } */ // STEP 2: if ( ! getSyncManager().isReceiverAllowed(request) ) { Response response=messageFactory.createResponse (Response.NOT_FOUND,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), "+ " STEP 2 failed: the receiver does not have a badge.\n"+ " 404 (NOT_FOUND) replied:\n"+ response.toString() ); return; } // STEP 3: if ( ! registrar.hasRegistration(request) ) { Response response=messageFactory.createResponse (Response.TEMPORARILY_UNAVAILABLE,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), "+ " STEP 3 failed: the receiver is not registered.\n"+ " 480 (TEMPORARILY_UNAVAILABLE) replied:\n"+ response.toString() ); return; } // STEP 4: int status=getSyncManager().checkConstraints(request); if ( status==Constraint.FAILED_ON_PEOPLE_NAME ) { Response response=messageFactory.createResponse (Response.FORBIDDEN,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), constraints "+ " check has failed on PEOPLE_NAME, 403 (FORBIDDEN) replied:\n"+ response.toString() ); return; } if ( status==Constraint.FAILED_ON_PEOPLE_PRESENCE || status==Constraint.FAILED_ON_PEOPLE_RANK || status==Constraint.FAILED_ON_LOCATION ){ if ( status==Constraint.FAILED_ON_PEOPLE_PRESENCE ) { if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), constraints "+ " check has failed on PEOPLE_PRESENCE, 486 (BUSY_HERE) replied:\n"); } if ( status==Constraint.FAILED_ON_PEOPLE_RANK ) { if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), constraints "+ " check has failed on PEOPLE_RANK, 486 (BUSY_HERE) replied:\n" ); } if ( status==Constraint.FAILED_ON_LOCATION ) { if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), constraints "+ " check has failed on PEOPLE_RANK, 486 (BUSY_HERE) replied:\n"); } Response response=messageFactory.createResponse (Response.BUSY_HERE,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); GatewayDebug.println(response.toString() ); if (response.getStatusCode() == 486) { try { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.nist.gov"); // Get a Session object Session session = Session.getDefaultInstance(props, null); // construct the message javax.mail.Message msg = new MimeMessage(session); FromHeader fromHeader = (FromHeader) response.getHeader(FromHeader.NAME); SipURI fromUri = (SipURI) fromHeader.getAddress().getURI();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?