gateway.java
来自「First of all, the Applet-phone is a SIP 」· Java 代码 · 共 664 行 · 第 1/2 页
JAVA
664 行
String fromAddr = fromUri.getUser() + "@" + fromUri.getHost(); msg.setFrom(new InternetAddress(fromAddr)); ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME); SipURI toUri = (SipURI) toHeader.getAddress().getURI(); String toAddr = toUri.getUser() + "@" + toUri.getHost(); msg.setRecipient( javax.mail.Message.RecipientType.TO, new InternetAddress(toAddr)); 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(" I tried to call you", "text/plain"); // send the thing off Transport.send(msg); System.out.println("\nMail was sent successfully."); } catch (Exception e) { e.printStackTrace(); } } return; } } Vector targetsURI=registrar.getContactsURI(request); if (targetsURI==null || targetsURI.isEmpty() ) { GatewayDebug.println("Gateway, processRequest(), "+ " No contact URI found, request is dropped!"); return; } if (GatewayDebug.debug) GatewayDebug.println("Gateway, processRequest(), "+ " ALL checks passed. The request is getting forwarded to the contact"+ " URI"); requestForwarding.forwardRequest((URI)targetsURI.firstElement(),sipProvider, request,serverTransaction); } catch (Exception ex){ try{ if (GatewayDebug.debug) { GatewayDebug.println("Gateway, 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("Gateway, processRequest(),"+ " 500 SERVER_INTERNAL_ERROR replied:\n"+ response.toString()); } catch (Exception e){ e.printStackTrace(); } } } /** This is a listener method. */ public void processResponse(ResponseEvent responseEvent) { try{ Response response = responseEvent.getResponse(); SipProvider sipProvider = (SipProvider) responseEvent.getSource(); ClientTransaction clientTransaction=responseEvent.getClientTransaction(); GatewayDebug.println ("\n***************************************************************"+ "\n***************************************************************"+ "\nResponse "+response.getStatusCode() + " "+response.getReasonPhrase() +" received:\n"+response.toString() ); GatewayDebug.println("Processing Response in progress"); if (GatewayDebug.debug) GatewayUtilities.printTransaction(clientTransaction); CSeqHeader cseqHeader=(CSeqHeader)response.getHeader(CSeqHeader.NAME); if ( response.getStatusCode() == 200 && cseqHeader.getMethod().equals("SUBSCRIBE") ){ GatewayDebug.println("200 OK related to the SUBSCRIBE the presence server"+ " sent. We ignore it."); return; } responseForwarding.forwardResponse(sipProvider, response,clientTransaction); } catch (Exception ex) { if (GatewayDebug.debug) { GatewayDebug.println("Gateway, processResponse(), internal error, "+ "exception raised:"); GatewayDebug.logException(ex); } } } /** JAIN Listener method. */ public void processTimeout(TimeoutEvent timeOutEvent) { GatewayDebug.println("TimeoutEvent received"); } /*********************** Methods for *************** * starting and stopping the proxy * ************************************************************/ /** Start the proxy, this method has to be called after the init method * throws Exception that which can be caught by the upper application */ public void start() throws Exception { if (configuration!=null && configuration.isValidConfiguration()) { Properties properties=new Properties(); // LOGGING property: if (configuration.enableDebug) { GatewayDebug.debug=true; GatewayDebug.setProxyOutputFile(configuration.outputProxy); GatewayDebug.println("DEBUG properties set!"); if (configuration.badMessageLogFile!=null) properties.setProperty("gov.nist.javax.sip.BAD_MESSAGE_LOG", configuration.badMessageLogFile); if (configuration.debugLogFile!=null) { properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", configuration.debugLogFile); } if (configuration.serverLogFile!=null) properties.setProperty("gov.nist.javax.sip.SERVER_LOG", configuration.serverLogFile); } else { System.out.println("DEBUG properties not set!"); } /********************************************************************************************* SIP STACKS ****************************************************** ********************************************************************************/ SipFactory sipFactory = SipFactory.getInstance(); sipFactory.setPathName("gov.nist"); headerFactory = sipFactory.createHeaderFactory(); addressFactory = sipFactory.createAddressFactory(); messageFactory = sipFactory.createMessageFactory(); if (configuration.enableRTPGateway) { GatewayDebug.println("Gateway, RTP gateway enabled between:" +configuration.minRTPHolePort+" and "+configuration.maxRTPHolePort+ " and a max. packet size of: "+configuration.maxRTPPacketSize); rtpHolesManager=new RTPHolesManager(configuration.minRTPHolePort, configuration.maxRTPHolePort,configuration.maxRTPPacketSize); } // Create SipStack objects: for (int i=0;i<configuration.stackConfigurationList.size();i++) { StackConfiguration stackConfiguration=(StackConfiguration) configuration.stackConfigurationList.elementAt(i); //System.out.println(stackConfiguration.stackIPAddress); properties.setProperty("javax.sip.IP_ADDRESS", stackConfiguration.stackIPAddress); properties.setProperty("javax.sip.STACK_NAME", stackConfiguration.stackName); if (configuration.check(stackConfiguration.outboundProxy)) properties.setProperty("javax.sip.OUTBOUND_PROXY", stackConfiguration.outboundProxy); if (configuration.check(stackConfiguration.routerPath)) properties.setProperty("javax.sip.ROUTER_PATH", stackConfiguration.routerPath); // This has to be hardcoded to true. for the proxy. properties.setProperty("javax.sip.RETRANSMISSION_FILTER", "on"); SipStack sipStack = sipFactory.createSipStack(properties); sipStacksList.addElement(sipStack); // We create the Listening points: Vector lps=stackConfiguration.getListeningPoints(); for ( int j=0;lps!=null && j<lps.size();j++) { Association a=(Association)lps.elementAt(j); try { GatewayDebug.println("Gateway, transport:" + a.transport); GatewayDebug.println("Gateway, port:" + Integer.valueOf(a.port).intValue()); ListeningPoint lp=sipStack.createListeningPoint (Integer.valueOf(a.port).intValue(), a.transport); SipProvider sipProvider = sipStack.createSipProvider(lp); sipProvider.addSipListener( this ); } catch(Exception e) { GatewayDebug.println ("Gateway, ERROR: listening point not created "); GatewayDebug.logException(e); } } } } else { System.out.println("Gateway, ERROR: the configuration file is not correct!"+ " Correct the errors first."); } } /** Stop the Gateway, this method has to be called after the start method * throws Exception that which can be caught by the upper application */ public void stop() throws Exception { } public ViaHeader getStackViaHeader(SipStack sipStack) { try { ListeningPoint lp = (ListeningPoint)sipStack.getListeningPoints().next(); String host = sipStack.getIPAddress(); int port = lp.getPort(); String transport = lp.getTransport(); // branch id is assigned by the transaction layer. return headerFactory.createViaHeader (host,port,transport,null); } catch (Exception e) { if (GatewayDebug.debug) { GatewayDebug.println("Gateway, getStackViaHeader(), internal error, "+ "exception raised:"); GatewayDebug.logException(e); } return null; } } public SipStack getSipStack(String toHostName) { try { GatewayDebug.println("Gateway, getSipStack(), we try to use the "+ " the appropriate stack for the destination:"+toHostName); for (int i=0;i<sipStacksList.size();i++) { SipStack sipStack=(SipStack)sipStacksList.elementAt(i); String stackIPAddress=sipStack.getIPAddress(); if (stackIPAddress.equals(toHostName) ) { GatewayDebug.println("Gateway, getSipStack(), we return "+ " the appropriate stack"); return sipStack; } } GatewayDebug.println("Gateway, getSipStack(), we return the default stack"); return (SipStack)sipStacksList.firstElement(); } catch(Exception e) { if (GatewayDebug.debug) { GatewayDebug.println("Gateway, getSipStack(), internal error, "+ "exception raised:"); GatewayDebug.logException(e); } return null; } } public SipStack getOppositeSipStack(String stackIPAddr) { try { GatewayDebug.println("Gateway, getOppositeSipStack(), we try to get "+ " the opposite stack for the destination:"+stackIPAddr); for (int i=0;i<sipStacksList.size();i++) { SipStack sipStack=(SipStack)sipStacksList.elementAt(i); String stackIPAddress=sipStack.getIPAddress(); if (!stackIPAddress.equals(stackIPAddr) ) { GatewayDebug.println("Gateway, getOppositeSipStack(), we return "+ " the opposite stack"); return sipStack; } } GatewayDebug.println("Gateway, getOppositeSipStack(), we return the default stack"); return ((SipStack)sipStacksList.firstElement()); } catch(Exception e) { if (GatewayDebug.debug) { GatewayDebug.println("Gateway, getOppositeSipStack(), internal error, "+ "exception raised:"); GatewayDebug.logException(e); } return null; } } /*************************************************************************/ /************ The main method: to launch the proxy *************/ /************************************************************************/ public static void main(String args[]) { try{ // the Proxy: //String confFile= (String) args[1]; //String confFile= "./src/gov/nist/examples/bps/configuration/configuration_bps.xml"; String confFile= "./gov/nist/examples/bps/configuration/configuration_bps.xml"; Gateway gateway=new Gateway(confFile); gateway.start(); GatewayDebug.println("Gateway ready to work"); } catch(Exception e) { System.out.println ("ERROR: Set the configuration file flag: " + "USE: -cf configuration_file_location.xml" ); System.out.println("ERROR, the gateway can not be started, " + " exception raised:\n"); e.printStackTrace(); } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?