1/*
2* Conditions Of Use
3*
4* This software was developed by employees of the National Institute of
5* Standards and Technology (NIST), an agency of the Federal Government.
6* Pursuant to title 15 Untied States Code Section 105, works of NIST
7* employees are not subject to copyright protection in the United States
8* and are considered to be in the public domain.  As a result, a formal
9* license is not needed to use the software.
10*
11* This software is provided by NIST as a service and is expressly
12* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15* AND DATA ACCURACY.  NIST does not warrant or make any representations
16* regarding the use of the software or the results thereof, including but
17* not limited to the correctness, accuracy, reliability or usefulness of
18* the software.
19*
20* Permission to use this software is contingent upon your acceptance
21* of the terms of this agreement
22*
23* .
24*
25*/
26/*******************************************************************************
27 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
28 *******************************************************************************/
29
30package gov.nist.javax.sip;
31
32import gov.nist.javax.sip.stack.*;
33import gov.nist.javax.sip.message.*;
34import javax.sip.*;
35
36/**
37 * Implements all the support classes that are necessary for the nist-sip stack
38 * on which the jain-sip stack has been based. This is a mapping class to map
39 * from the NIST-SIP abstractions to the JAIN abstractions. (i.e. It is the glue
40 * code that ties the NIST-SIP event model and the JAIN-SIP event model
41 * together. When a SIP Request or SIP Response is read from the corresponding
42 * messageChannel, the NIST-SIP stack calls the SIPStackMessageFactory
43 * implementation that has been registered with it to process the request.)
44 *
45 * @version 1.2 $Revision: 1.14 $ $Date: 2009/07/29 20:38:17 $
46 *
47 * @author M. Ranganathan <br/>
48 *
49 *
50 */
51class NistSipMessageFactoryImpl implements StackMessageFactory {
52
53    private SipStackImpl sipStack;
54
55    /**
56     * Construct a new SIP Server Request.
57     *
58     * @param sipRequest
59     *            is the SIPRequest from which the SIPServerRequest is to be
60     *            constructed.
61     * @param messageChannel
62     *            is the MessageChannel abstraction for this SIPServerRequest.
63     */
64    public ServerRequestInterface newSIPServerRequest(SIPRequest sipRequest,
65            MessageChannel messageChannel) {
66
67        if (messageChannel == null || sipRequest == null) {
68            throw new IllegalArgumentException("Null Arg!");
69        }
70
71        SipStackImpl theStack = (SipStackImpl) messageChannel.getSIPStack();
72        DialogFilter retval = new DialogFilter(
73                theStack);
74        if (messageChannel instanceof SIPTransaction) {
75            // If the transaction has already been created
76            // then set the transaction channel.
77            retval.transactionChannel = (SIPTransaction) messageChannel;
78        }
79        retval.listeningPoint = messageChannel.getMessageProcessor()
80                .getListeningPoint();
81        if (retval.listeningPoint == null)
82            return null;
83        if (sipStack.isLoggingEnabled())
84            sipStack.getStackLogger().logDebug(
85                    "Returning request interface for "
86                            + sipRequest.getFirstLine() + " " + retval
87                            + " messageChannel = " + messageChannel);
88        return retval;
89    }
90
91    /**
92     * Generate a new server response for the stack.
93     *
94     * @param sipResponse
95     *            is the SIPRequest from which the SIPServerRequest is to be
96     *            constructed.
97     * @param messageChannel
98     *            is the MessageChannel abstraction for this SIPServerResponse
99     */
100    public ServerResponseInterface newSIPServerResponse(
101            SIPResponse sipResponse, MessageChannel messageChannel) {
102        SIPTransactionStack theStack = (SIPTransactionStack) messageChannel
103                .getSIPStack();
104        // Tr is null if a transaction is not mapped.
105        SIPTransaction tr = (SIPTransaction) ((SIPTransactionStack) theStack)
106                .findTransaction(sipResponse, false);
107        if (sipStack.isLoggingEnabled())
108            sipStack.getStackLogger().logDebug(
109                    "Found Transaction " + tr + " for " + sipResponse);
110
111        if (tr != null) {
112            // Prune unhealthy responses early if handling statefully.
113            // If the state has not yet been assigned then this is a
114            // spurious response. This was moved up from the transaction
115            // layer for efficiency.
116            if (tr.getState() == null) {
117                if (sipStack.isLoggingEnabled())
118                    sipStack.getStackLogger().logDebug(
119                            "Dropping response - null transaction state");
120                return null;
121                // Ignore 1xx
122            } else if (TransactionState.COMPLETED == tr.getState()
123                    && sipResponse.getStatusCode() / 100 == 1) {
124                if (sipStack.isLoggingEnabled())
125                    sipStack.getStackLogger().logDebug(
126                            "Dropping response - late arriving "
127                                    + sipResponse.getStatusCode());
128                return null;
129            }
130        }
131
132        DialogFilter retval = new DialogFilter(
133                sipStack);
134
135        retval.transactionChannel = tr;
136
137        retval.listeningPoint = messageChannel.getMessageProcessor()
138                .getListeningPoint();
139        return retval;
140    }
141
142    public NistSipMessageFactoryImpl(SipStackImpl sipStackImpl) {
143        this.sipStack = sipStackImpl;
144
145    }
146
147}