📄 registerprocessing.java
字号:
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2008 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.openfire.sip.tester.stack;
import org.jivesoftware.openfire.sip.tester.security.SipSecurityException;
import org.jivesoftware.openfire.sip.tester.comm.CommunicationsException;
import org.jivesoftware.openfire.sip.tester.Log;
import javax.sip.message.Request;
import javax.sip.message.Response;
import javax.sip.*;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.*;
import java.util.Timer;
import java.util.ArrayList;
import java.util.TimerTask;
import java.text.ParseException;
/**
* Title: SIP Register Tester
* Description:JAIN-SIP Test application
*
* @author Thiago Rocha Camargo (thiago@jivesoftware.com)
*/
class RegisterProcessing {
private SipManager sipManCallback = null;
private Request registerRequest = null;
private boolean isRegistered = false;
private boolean isUnregistering = false;
private Timer reRegisterTimer = null;
private int keepAlivePort = 0;
private Timer keepAliveTimer = null;
RegisterProcessing(SipManager sipManCallback) {
this.sipManCallback = sipManCallback;
}
void setSipManagerCallBack(SipManager sipManCallback) {
this.sipManCallback = sipManCallback;
}
void processOK(ClientTransaction clientTransatcion, Response response) {
isRegistered = true;
FromHeader fromHeader = ((FromHeader) response
.getHeader(FromHeader.NAME));
Address address = fromHeader.getAddress();
int expires = 0;
if (!isUnregistering) {
ContactHeader contactHeader = (ContactHeader) response
.getHeader(ContactHeader.NAME);
// TODO check if the registrar created the contact address
if (contactHeader != null) {
expires = contactHeader.getExpires();
} else {
ExpiresHeader expiresHeader = response.getExpires();
if (expiresHeader != null) {
expires = expiresHeader.getExpires();
}
}
}
// expires may be null
// fix by Luca Bincoletto <Luca.Bincoletto@tilab.com>
if (expires == 0) {
isUnregistering = false;
sipManCallback.fireUnregistered(address.toString());
} else {
if (reRegisterTimer != null)
reRegisterTimer.cancel();
if (keepAliveTimer != null)
keepAliveTimer.cancel();
reRegisterTimer = new Timer();
keepAliveTimer = new Timer();
// if (expires > 0 && expires < 60) {
// [issue 2] Schedule re registrations
// bug reported by LynlvL@netscape.com
// use the value returned by the server to reschedule
// registration
SipURI uri = (SipURI) address.getURI();
scheduleReRegistration(uri.getHost(), uri.getPort(), uri
.getTransportParam(), expires);
// }
/*
* else{ SipURI uri = (SipURI) address.getURI();
* scheduleReRegistration(uri.getHost(), uri.getPort(),
* uri.getTransportParam(), expires); }
*/
sipManCallback.fireRegistered(address.toString());
}
}
void processTimeout(Transaction transatcion, Request request) {
isRegistered = true;
FromHeader fromHeader = ((FromHeader) request
.getHeader(FromHeader.NAME));
Address address = fromHeader.getAddress();
sipManCallback.fireUnregistered("Request timeouted for: "
+ address.toString());
}
void processNotImplemented(ClientTransaction transatcion, Response response) {
isRegistered = true;
FromHeader fromHeader = ((FromHeader) response
.getHeader(FromHeader.NAME));
Address address = fromHeader.getAddress();
sipManCallback.fireUnregistered("Server returned NOT_IMPLEMENTED. "
+ address.toString());
}
/**
* Attempts to re-ogenerate the corresponding request with the proper
* credentials and terminates the call if it fails.
*
* @param clientTransaction the corresponding transaction
* @param response the challenge
*/
void processAuthenticationChallenge(ClientTransaction clientTransaction,
Response response) {
try {
ClientTransaction retryTran = sipManCallback.sipSecurityManager
.handleChallenge(response, clientTransaction);
retryTran.sendRequest();
}
catch (SipSecurityException exc) {
// tell the others we couldn't register
sipManCallback.fireUnregistered(((FromHeader) clientTransaction
.getRequest().getHeader(FromHeader.NAME)).getAddress()
.toString());
sipManCallback.fireCommunicationsError(new CommunicationsException(
"Authorization failed!", exc));
}
catch (Exception exc) {
// tell the others we couldn't register
sipManCallback.fireUnregistered(((FromHeader) clientTransaction
.getRequest().getHeader(FromHeader.NAME)).getAddress()
.toString());
sipManCallback.fireCommunicationsError(new CommunicationsException(
"Failed to resend a request "
+ "after a security challenge!", exc));
}
}
synchronized void register(String registrarAddress, int registrarPort,
String registrarTransport, int expires)
throws CommunicationsException {
try {
isUnregistering = false;
// From
FromHeader fromHeader = sipManCallback.getFromHeader(true);
Address fromAddress = fromHeader.getAddress();
sipManCallback.fireRegistering(fromAddress.toString());
// Request URI
SipURI requestURI = null;
try {
requestURI = sipManCallback.addressFactory.createSipURI(null,
registrarAddress);
}
catch (ParseException ex) {
throw new CommunicationsException("Bad registrar address:"
+ registrarAddress, ex);
}
catch (NullPointerException ex) {
// Do not throw an exc, we should rather silently notify the
// user
// throw new CommunicationsException("A registrar address was
// not specified!", ex);
sipManCallback.fireUnregistered(fromAddress.getURI().toString()
+ " (registrar not specified)");
return;
}
requestURI.setPort(registrarPort);
try {
requestURI.setTransportParam(registrarTransport);
}
catch (ParseException ex) {
throw new CommunicationsException(registrarTransport
+ " is not a valid transport!", ex);
}
// Call ID Header
CallIdHeader callIdHeader = sipManCallback.sipProvider
.getNewCallId();
// CSeq Header
CSeqHeader cSeqHeader = null;
try {
cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1,
Request.REGISTER);
}
catch (ParseException ex) {
// Should never happen
Log.error("register", ex);
}
catch (InvalidArgumentException ex) {
// Should never happen
Log.error("register", ex);
}
// To Header
ToHeader toHeader = null;
try {
toHeader = sipManCallback.headerFactory.createToHeader(
fromAddress, null);
}
catch (ParseException ex) {
// throw was missing - reported by Eero Vaarnas
throw new CommunicationsException(
"Could not create a To header " + "for address:"
+ fromHeader.getAddress(), ex);
}
// User Agent Header
UserAgentHeader uaHeader = null;
ArrayList userAgentList = new ArrayList();
userAgentList.add(SIPConfig.getStackName());
try {
uaHeader = sipManCallback.headerFactory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -