/** * Creates and sends a SUBSCRIBE request to the subscription <tt>Address</tt>/Request URI of a * specific <tt>Subscription</tt> in order to request receiving event notifications and adds the * specified <tt>Subscription</tt> to the list of subscriptions managed by this instance. The * added <tt>Subscription</tt> may later receive notifications to process the <tt>Request</tt>s * and/or <tt>Response</tt>s which constitute the signaling session associated with it. If the * attempt to create and send the SUBSCRIBE request fails, the specified <tt>Subscription</tt> is * not added to the list of subscriptions managed by this instance. * * @param subscription a <tt>Subscription</tt> which specifies the properties of the SUBSCRIBE * request to be created and sent, to be added to the list of subscriptions managed by this * instance * @throws OperationFailedException if we fail constructing or sending the subscription request. */ public void subscribe(Subscription subscription) throws OperationFailedException { Dialog dialog = subscription.getDialog(); if ((dialog != null) && DialogState.TERMINATED.equals(dialog.getState())) dialog = null; // create the subscription ClientTransaction subscribeTransaction = null; try { subscribeTransaction = (dialog == null) ? createSubscription(subscription, subscriptionDuration) : createSubscription(subscription, dialog, subscriptionDuration); } catch (OperationFailedException ex) { ProtocolProviderServiceSipImpl.throwOperationFailedException( "Failed to create the subscription", OperationFailedException.INTERNAL_ERROR, ex, logger); } // we register the contact to find him when the OK will arrive CallIdHeader callIdHeader = (CallIdHeader) subscribeTransaction.getRequest().getHeader(CallIdHeader.NAME); String callId = callIdHeader.getCallId(); addSubscription(callId, subscription); // send the message try { if (dialog == null) subscribeTransaction.sendRequest(); else dialog.sendRequest(subscribeTransaction); } catch (SipException ex) { // this contact will never been accepted or rejected removeSubscription(callId, subscription); ProtocolProviderServiceSipImpl.throwOperationFailedException( "Failed to send the subscription", OperationFailedException.NETWORK_FAILURE, ex, logger); } }
/** Test whether new ClientTransactions are properly created. */ public void testGetNewClientTransaction() { try { Request invite = createTiInviteRequest(null, null, null); ClientTransaction tran = null; try { tran = tiSipProvider.getNewClientTransaction(invite); } catch (TransactionUnavailableException exc) { exc.printStackTrace(); fail( "A TransactionUnavailableException was thrown while trying to " + "create a new client transaction"); } assertNotNull( "A null ClientTransaction was returned by SipProvider." + "getNewClientTransaction().", tran); String tranBranch = tran.getBranchId(); String reqBranch = ((ViaHeader) invite.getHeader(ViaHeader.NAME)).getBranch(); assertEquals( "The newly created transaction did not have the same " + "branch id as the request that created it", tranBranch, reqBranch); assertNotNull( "The newly created transaction returned a null Dialog. " + "Please check the docs on Transaction.getDialog()", tran.getDialog()); assertNotNull( "The transaction's getRequest() method returned a null Request ", tran.getRequest()); assertEquals( "The transaction's getRequest() method returned a Request " + "that did not match the one that we used to create it!", tran.getRequest(), invite); } catch (Throwable exc) { exc.printStackTrace(); fail(exc.getClass().getName() + ": " + exc.getMessage()); } assertTrue(new Exception().getStackTrace()[0].toString(), true); }
/* * Begin Third-Party Call Control. */ public void initiateCall() throws IOException { try { try { busyTreatment = new TreatmentManager("busy.au", 0); } catch (IOException e) { Logger.println("Invalid busy treatment: " + e.getMessage()); } Logger.writeFile("Call " + cp + ": Begin SIP third party call"); setState(CallState.INVITED); InetSocketAddress isa = callHandler.getReceiveAddress(); if (isa == null) { throw new IOException("can't get receiver socket!"); } // send INVITE to the CallParticipant clientTransaction = sipUtil.sendInvite(cp, isa); if (clientTransaction == null) { Logger.error("Error placing call: " + cp); setState(CallState.ENDED, "Reason='Error placing call'"); throw new IOException("Error placing call: " + cp); } CallIdHeader callIdHeader = (CallIdHeader) clientTransaction.getRequest().getHeader(CallIdHeader.NAME); sipCallId = callIdHeader.getCallId(); sipServerCallback = SipServer.getSipServerCallback(); sipServerCallback.addSipListener(sipCallId, this); } catch (java.text.ParseException e) { Logger.println("Call " + cp + " Error placing call " + cp + ": " + e.getMessage()); setState(CallState.ENDED, "Reason='Error placing call " + cp + " " + e.getMessage() + "'"); throw new IOException("Error placing call " + cp + " " + e.getMessage()); } catch (InvalidArgumentException e) { Logger.println("Call " + cp + " Error placing call " + cp + ": " + e.getMessage()); setState(CallState.ENDED, "Reason='Error placing call " + cp + " " + e.getMessage() + "'"); throw new IOException("Error placing call " + cp + " " + e.getMessage()); } catch (SipException e) { Logger.println("Call " + cp + " Error placing call " + cp + ": " + e.getMessage()); setState(CallState.ENDED, "Reason='Error placing call " + cp + " " + e.getMessage() + "'"); throw new IOException("Error placing call " + cp + " " + e.getMessage()); } }
public void processResponse(ResponseEvent responseEvent) { ClientTransaction ct = responseEvent.getClientTransaction(); Response response = responseEvent.getResponse(); ServerTransaction st = (ServerTransaction) ct.getApplicationData(); try { Response otherResponse = messageFactory.createResponse(response.getStatusCode(), st.getRequest()); if (response.getStatusCode() == 200 && ct.getRequest().getMethod().equals("INVITE")) { Address address = addressFactory.createAddress("B2BUA <sip:" + myAddress + ":" + myPort + ">"); ContactHeader contactHeader = headerFactory.createContactHeader(address); response.addHeader(contactHeader); ToHeader toHeader = (ToHeader) otherResponse.getHeader(ToHeader.NAME); if (toHeader.getTag() == null) toHeader.setTag(new Long(counter.getAndIncrement()).toString()); otherResponse.addHeader(contactHeader); } st.sendResponse(otherResponse); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Implements {@link MethodProcessor#processResponse(ResponseEvent)}. Handles only responses to * SUBSCRIBE requests because they are the only requests concerning event package subscribers (and * the only requests sent by them, for that matter) and if the processing of a given response * requires event package-specific handling, delivers the response to the matching * <tt>Subscription</tt> instance. Examples of such event package-specific handling include * letting the respective <tt>Subscription</tt> handle the success or failure in the establishment * of a subscription. * * @param responseEvent a <tt>ResponseEvent</tt> specifying the SIP <tt>Response</tt> to be * processed * @return <tt>true</tt> if the SIP <tt>Response</tt> specified by <tt>responseEvent</tt> was * processed; otherwise, <tt>false</tt> */ @Override public boolean processResponse(ResponseEvent responseEvent) { Response response = responseEvent.getResponse(); CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME); if (cseqHeader == null) { logger.error("An incoming response did not contain a CSeq header"); return false; } if (!Request.SUBSCRIBE.equals(cseqHeader.getMethod())) return false; ClientTransaction clientTransaction = responseEvent.getClientTransaction(); Request request = clientTransaction.getRequest(); /* * Don't handle responses to requests not coming from this event * package. */ if (request != null) { EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME); if ((eventHeader == null) || !eventPackage.equalsIgnoreCase(eventHeader.getEventType())) return false; } // Find the subscription. CallIdHeader callIdHeader = (CallIdHeader) response.getHeader(CallIdHeader.NAME); String callId = callIdHeader.getCallId(); Subscription subscription = getSubscription(callId); // if it's the response to an unsubscribe message, we just ignore it // whatever the response is however if we need to handle a // challenge, we do it ExpiresHeader expHeader = response.getExpires(); int statusCode = response.getStatusCode(); SipProvider sourceProvider = (SipProvider) responseEvent.getSource(); if (((expHeader != null) && (expHeader.getExpires() == 0)) || (subscription == null)) // this handle the unsubscription // case where we removed the contact // from subscribedContacts { boolean processed = false; if ((statusCode == Response.UNAUTHORIZED) || (statusCode == Response.PROXY_AUTHENTICATION_REQUIRED)) { try { processAuthenticationChallenge(clientTransaction, response, sourceProvider); processed = true; } catch (OperationFailedException e) { logger.error("can't handle the challenge", e); } } else if ((statusCode != Response.OK) && (statusCode != Response.ACCEPTED)) processed = true; // any other cases (200/202) will imply a NOTIFY, so we will // handle the end of a subscription there return processed; } if ((statusCode >= Response.OK) && (statusCode < Response.MULTIPLE_CHOICES)) { // OK (200/202) if ((statusCode == Response.OK) || (statusCode == Response.ACCEPTED)) { if (expHeader == null) { // not conform to rfc3265 logger.error("no Expires header in this response"); return false; } SubscriptionRefreshTask refreshTask = new SubscriptionRefreshTask(subscription); subscription.setTimerTask(refreshTask); int refreshDelay = expHeader.getExpires(); // try to keep a margin if the refresh delay allows it if (refreshDelay >= (2 * refreshMargin)) refreshDelay -= refreshMargin; timer.schedule(refreshTask, refreshDelay * 1000); // do it to remember the dialog in case of a polling // subscription (which means no call to finalizeSubscription) subscription.setDialog(clientTransaction.getDialog()); subscription.processSuccessResponse(responseEvent, statusCode); } } else if ((statusCode >= Response.MULTIPLE_CHOICES) && (statusCode < Response.BAD_REQUEST)) { if (logger.isInfoEnabled()) logger.info( "Response to subscribe to " + subscription.getAddress() + ": " + response.getReasonPhrase()); } else if (statusCode >= Response.BAD_REQUEST) { // if the response is a 423 response, just re-send the request // with a valid expires value if (statusCode == Response.INTERVAL_TOO_BRIEF) { MinExpiresHeader min = (MinExpiresHeader) response.getHeader(MinExpiresHeader.NAME); if (min == null) { logger.error("no minimal expires value in this 423 " + "response"); return false; } ExpiresHeader exp = request.getExpires(); try { exp.setExpires(min.getExpires()); } catch (InvalidArgumentException e) { logger.error("can't set the new expires value", e); return false; } ClientTransaction transac = null; try { transac = protocolProvider.getDefaultJainSipProvider().getNewClientTransaction(request); } catch (TransactionUnavailableException e) { logger.error("can't create the client transaction", e); return false; } try { transac.sendRequest(); } catch (SipException e) { logger.error("can't send the new request", e); return false; } return true; // UNAUTHORIZED (401/407) } else if ((statusCode == Response.UNAUTHORIZED) || (statusCode == Response.PROXY_AUTHENTICATION_REQUIRED)) { try { processAuthenticationChallenge(clientTransaction, response, sourceProvider); } catch (OperationFailedException e) { logger.error("can't handle the challenge", e); removeSubscription(callId, subscription); subscription.processFailureResponse(responseEvent, statusCode); } // 408 480 486 600 603 : non definitive reject // others: definitive reject (or not implemented) } else { if (logger.isDebugEnabled()) logger.debug("error received from the network:\n" + response); removeSubscription(callId, subscription); subscription.processFailureResponse(responseEvent, statusCode); } } return true; }