Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Conditions Of Use
   *
   * This software was developed by employees of the National Institute of
   * Standards and Technology (NIST), and others.
   * This software is has been contributed to the public domain.
   * As a result, a formal license is not needed to use the software.
   *
   * This software is provided "AS IS."
  * NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
  * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
  * AND DATA ACCURACY.  NIST does not warrant or make any representations
  * regarding the use of the software or the results thereof, including but
  * not limited to the correctness, accuracy, reliability or usefulness of
  * the software.
  *
  *
  */
 package test.unit.gov.nist.javax.sip.stack.challenge;
 
 
 
 import  test.tck.msgflow.callflows.ProtocolObjects;

This class is a UAC template.

Author(s):
M. Ranganathan
 
 
 public class Shootme implements SipListener {
 
     private ProtocolObjects protocolObjects;
 
     // To run on two machines change these to suit.
     public static final String myAddress = "127.0.0.1";
 
     public static final int myPort = 5070;
 
     private static Logger logger = Logger.getLogger(Shootme.class);
 
     static {
         if (.getAllAppenders().equals(NullEnumeration.getInstance())) {
             .addAppender(new ConsoleAppender(new SimpleLayout()));
         }
     }
 
     private Dialog dialog;
 
     private boolean challengedchallengedBye;
 
     class ApplicationData {
         protected int ackCount;
     }
 
     public Shootme(ProtocolObjects protocolObjects) {
         this. = protocolObjects;
     }
 
     public void processRequest(RequestEvent requestEvent) {
         Request request = requestEvent.getRequest();
         ServerTransaction serverTransactionId = requestEvent
                 .getServerTransaction();
 
         .info("\n\nRequest " + request.getMethod() + " received at "
                 + ..getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         if (request.getMethod().equals(.)) {
             processInvite(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processAck(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processBye(requestEventserverTransactionId);
         }
 
    }

    
Process the invite request.
    public void processInvite(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        .info("Got an INVITE  " + request);
        try {
            // JvB: first time, challenge with 401 response (without proper
            // headers)
            if (!) {
                 = true;
                ChallengeTest.assertNull(requestEvent.getServerTransaction());
                Response challenge = .
                        .createResponse(401, request);
                ToHeader toHeader = (ToHeaderchallenge
                        .getHeader(.);
                toHeader.setTag("challenge");
                sipProvider.sendResponse(challenge); // dont create ST
                return;
            }
            .info("shootme: got an Invite sending OK");
            // logger.info("shootme: " + request);
            Response response = ..createResponse(
                    180, request);
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            toHeader.setTag("4321");
            Address address = .
                    .createAddress("Shootme <sip:" +  + ":" + 
                            + ">");
            ContactHeader contactHeader = .
                    .createContactHeader(address);
            response.addHeader(contactHeader);
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                st = sipProvider.getNewServerTransaction(request);
                .info("Server transaction created!" + request);
                .info("Dialog = " + st.getDialog());
                if (st.getDialog().getApplicationData() == null) {
                    st.getDialog().setApplicationData(new ApplicationData());
                }
            } else {
                // If Server transaction is not null, then
                // this is a re-invite.
                .info("This is a RE INVITE ");
                ChallengeTest.assertSame("Dialog mismatch "st.getDialog(),
                        this.);
            }
            // Thread.sleep(5000);
            .info("got a server tranasaction " + st);
            byte[] content = request.getRawContent();
            if (content != null) {
                .info(" content = " + new String(content));
                ContentTypeHeader contentTypeHeader = .
                        .createContentTypeHeader("application""sdp");
                .info("response = " + response);
                response.setContent(contentcontentTypeHeader);
            }
             = st.getDialog();
            if ( != null) {
                .info("Dialog " + );
                .info("Dialog state " + .getState());
            }
            st.sendResponse(response);
            response = ..createResponse(200,
                    request);
            toHeader = (ToHeaderresponse.getHeader(.);
            toHeader.setTag("4321");
            // Application is supposed to set.
            response.addHeader(contactHeader);
            st.sendResponse(response);
            .info("TxState after sendResponse = " + st.getState());
        } catch (Exception ex) {
            String s = "unexpected exception";
            .error(sex);
            ChallengeTest.fail(s);
        }
    }

    
Process the ACK request.
    private void processAck(RequestEvent rServerTransaction tid) {
        try {
            .info("Got an ACK!");
        } catch (Exception ex) {
            .error("unexpected exception"ex);
            ChallengeTest.fail("unexpected exception");
        }
    }

    
Process the bye request.
    public void processBye(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        // SipProvider sipProvider = (SipProvider) requestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            // JvB: first time, challenge with 401 response (without proper
            // headers)
            if (!) {
                 = true;
                .info("Got a BYE! Challenging...");
                Response challenge = .
                        .createResponse(401, request);
                serverTransactionId.sendResponse(challenge);
                return;
            }
            .info("shootme:  got a bye sending OK.");
            Response response = ..createResponse(
                    200, request);
            if (serverTransactionId != null) {
                serverTransactionId.sendResponse(response);
                .info("Dialog State is "
                        + serverTransactionId.getDialog().getState());
            } else {
                .info("null server tx.");
                // sipProvider.sendResponse(response);
            }
        } catch (Exception ex) {
            String s = "Unexpected exception";
            .error(sex);
            ChallengeTest.fail(s);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        .info("Response received with client transaction id " + tid
                + ":\n" + response);
        try {
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                ChallengeTest.assertNotNull(
                        "INVITE 200 response should match a transaction"tid);
                Dialog dialog = tid.getDialog();
                CSeqHeader cseq = (CSeqHeaderresponse
                        .getHeader(.);
                Request request = dialog.createAck(cseq.getSeqNumber());
                dialog.sendAck(request);
            }
            if (tid != null) {
                Dialog dialog = tid.getDialog();
                .info("Dalog State = " + dialog.getState());
            }
        } catch (Exception ex) {
            String s = "Unexpected exception";
            .error(sex);
            ChallengeTest.fail(s);
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        Transaction transaction;
        if (timeoutEvent.isServerTransaction()) {
            transaction = timeoutEvent.getServerTransaction();
        } else {
            transaction = timeoutEvent.getClientTransaction();
        }
        .info("state = " + transaction.getState());
        .info("dialog = " + transaction.getDialog());
        .info("dialogState = " + transaction.getDialog().getState());
        .info("Transaction Time out");
    }
    public SipProvider createSipProvider() throws Exception {
        ListeningPoint lp = ..createListeningPoint(
                .);
        SipProvider sipProvider = .
                .createSipProvider(lp);
        return sipProvider;
    }
    public static void main(String args[]) throws Exception {
        .addAppender(new ConsoleAppender(new SimpleLayout()));
        ProtocolObjects protocolObjects = new ProtocolObjects("shootme",
                "gov.nist""udp"true,false);
        Shootme shootme = new Shootme(protocolObjects);
        shootme.createSipProvider().addSipListener(shootme);
    }
    public void checkState() {
        ChallengeTest.assertTrue(true);
    }
    /*
     * (non-Javadoc)
     *
     * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
     */
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("An IO Exception was detected : "
                + exceptionEvent.getHost());
    }
    /*
     * (non-Javadoc)
     *
     * @seejavax.sip.SipListener#processTransactionTerminated(javax.sip.
     * TransactionTerminatedEvent)
     */
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Tx terminated event ");
    }
    /*
     * (non-Javadoc)
     *
     * @see
     * javax.sip.SipListener#processDialogTerminated(javax.sip.DialogTerminatedEvent
     * )
     */
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("Dialog terminated event detected ");
    }
New to GrepCode? Check out our FAQ X