📄 gateway.java
字号:
package gov.nist.examples.busy.gateway;import javax.mail.*;import javax.mail.internet.*;import java.util.*;import javax.sip.*;import javax.sip.message.*;import javax.sip.header.*;import javax.sip.address.*;import gov.nist.examples.busy.gateway.registrar.*;import tools.rtpgateway.*;/** 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; 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 GatewayUtilities getGatewayUtilities() { return gatewayUtilities; } public Configuration getConfiguration() { return configuration; } /** 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(); //configuration=new Configuration(); //StackConfiguration stackConfiguration=new StackConfiguration(); //configuration.stackConfigurationList.addElement(stackConfiguration); 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); requestForwarding=new RequestForwarding(this); responseForwarding=new ResponseForwarding(this); } } catch (Exception ex) { System.out.println ("ERROR: exception raised while initializing the proxy"); ex.printStackTrace(); throw new Exception ("ERROR: exception raised while initializing the proxy"); } } } /** 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 (serverTransaction==null) { String method=request.getMethod(); // Methods that creates dialogs, so that can generate transactions if ( method.equals("INVITE") || method.equals("SUBSCRIBE") || method.equals("MESSAGE") ) { try{ serverTransaction=sipProvider.getNewServerTransaction(request); } catch(TransactionAlreadyExistsException e) { if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), this request"+ " is a retransmission, we drop it!"); } } } if (GatewayDebug.debug) GatewayUtilities.printTransaction(serverTransaction); // Let's check for the REGISTER: if (request.getMethod().equals("REGISTER")) { //address translation if(request.getHeader(FromHeader.NAME).toString().indexOf(configuration.serviceGuy)!=-1){ if(!registrar.hasExpiresZero(request)) configuration.isRegistered=true; else configuration.isRegistered=false; } System.out.println("isRegistered : " +configuration.isRegistered); // we call the RegisterProcessing: registrar.processRegister(request,sipProvider,serverTransaction); return; } if ( !request.getMethod().equals("INVITE") && !request.getMethod().equals("ACK") && !request.getMethod().equals("BYE") && !request.getMethod().equals("CANCEL") //&& //!request.getMethod().equals("OPTIONS") ) { Response response=messageFactory.createResponse (Response.METHOD_NOT_ALLOWED,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); GatewayDebug.println("Proxy, processRequest(), we drop all requests"+ " except INVITE, ACK, BYE, OPTIONS... METHOD_NOT_ALLOWED replied"); return; } if ( registrar.hasRegistration(request) ) { Vector targetURIList=registrar.getContactsURI(request); if (GatewayDebug.debug) GatewayDebug.println("Proxy, processRequest(), we have the contacts"+ ", let's forward the Request!"); requestForwarding.forwardRequest(targetURIList,sipProvider, request,serverTransaction); } else { //Address Translation if the guy is not registered if ( !request.getMethod().equals(Request.REGISTER) ) { if(!configuration.isRegistered){ Vector targetURIList=null; if ( registrar.hasRegistration("sip:secretary@nist.gov") ) { targetURIList=registrar.getContactsURI("sip:secretary@nist.gov"); requestForwarding.forwardRequest(targetURIList,sipProvider, request,serverTransaction); return; } /***************** PSTN gateway *************************************************/ else if ( configuration.pstnGateway!=null ) { GatewayDebug.println ("Proxy, processRequest(), we are going to forward to the PSTN gateway..."); AddressFactory addressFactory=getAddressFactory(); SipURI sipURI=addressFactory.createSipURI(configuration.phoneNumberToCall,configuration.pstnGateway+";user=phone"); targetURIList=new Vector(); targetURIList.addElement(sipURI); Request requestToModify=(Request)request.clone(); ToHeader toHeader=(ToHeader)requestToModify.getHeader(ToHeader.NAME); URI uri=toHeader.getAddress().getURI(); if(uri.isSipURI()){ SipURI toSipURI=(SipURI)uri; toSipURI.setUser(configuration.phoneNumberToCall); toSipURI.setHost("129.6.50.176:4000;user=phone"); } System.out.println(requestToModify); requestForwarding.forwardRequest(targetURIList,sipProvider, requestToModify,serverTransaction); return; } else{ Properties props = new Properties(); // XXX - could use Session.getTransport() and Transport.connect() // XXX - assume we're using SMTP props.put("mail.smtp.host", "smtp.nist.gov"); // Get a Session object Session session = Session.getDefaultInstance(props, null); // construct the message //InternetAddress address=new InternetAddress("jean_deruelle@hotmail.com"); javax.mail.Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("jean.deruelle@nist.gov")); msg.setRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress("jean.deruelle@nist.gov") ); msg.setSubject("Automatic Message"); //msg.setText(request.getHeader(FromHeader.NAME).toString()+ " tries to call you"); msg.setHeader("X-Mailer", "Gateway-Busy"); msg.setSentDate(new Date()); msg.setContent(request.getHeader(FromHeader.NAME).toString()+ " tries to call you","text/plain"); // send the thing off Transport.send(msg); System.out.println("\nMail was sent successfully."); Response response=messageFactory.createResponse (Response.TEMPORARILY_UNAVAILABLE,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Proxy, processRequest(), unable to set "+ " the targets, 480 (Temporarily Unavailable) replied:\n"+ response.toString() ); return; } /*********************************************************************************/ /*requestForwarding.forwardRequest(targetURIList,sipProvider, request,serverTransaction); return;*/ } } else { Response response=messageFactory.createResponse (Response.TEMPORARILY_UNAVAILABLE,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Proxy, processRequest(), unable to set "+ " the targets, 480 (Temporarily Unavailable) replied:\n"+ response.toString() ); } } } catch (Exception ex){ try{ if (GatewayDebug.debug) { GatewayDebug.println("Proxy, processRequest(), internal error, "+ "exception raised:"); GatewayDebug.logException(ex); } // This is an internal error: // Let's return a 500 SERVER_INTERNAL_ERROR Response response=messageFactory.createResponse (Response.SERVER_INTERNAL_ERROR,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (GatewayDebug.debug) GatewayDebug.println("Proxy, processRequest(),"+ " 500 SERVER_INTERNAL_ERROR replied:\n"+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -