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.stack;
31
32import gov.nist.javax.sip.LogRecord;
33
34/**
35 * This class stores a message along with some other informations
36 * Used to log messages.
37 *
38 *@version 1.2 $Revision: 1.9 $ $Date: 2009/07/17 18:58:13 $
39 *
40 * @author M. Ranganathan   <br/>
41 * @author Marc Bednarek  <br/>
42 *
43 *
44 */
45class MessageLog implements LogRecord {
46
47    private String message;
48
49    private String source;
50
51    private String destination;
52
53    private long timeStamp;
54
55    private boolean isSender;
56
57    private String firstLine;
58
59    private String tid;
60
61    private String callId;
62
63    private long timeStampHeaderValue;
64
65    /* (non-Javadoc)
66     * @see gov.nist.javax.sip.stack.LogRecord#equals(java.lang.Object)
67     */
68    public boolean equals(Object other) {
69        if (!(other instanceof MessageLog)) {
70            return false;
71        } else {
72            MessageLog otherLog = (MessageLog) other;
73            return otherLog.message.equals(message)
74                && otherLog.timeStamp == timeStamp;
75        }
76    }
77
78    /**
79     * Constructor
80     */
81
82    public MessageLog(
83        String message,
84        String source,
85        String destination,
86        String timeStamp,
87        boolean isSender,
88        String firstLine,
89        String tid,
90        String callId,
91        long timeStampHeaderValue) {
92        if (message == null || message.equals(""))
93            throw new IllegalArgumentException("null msg");
94        this.message = message;
95        this.source = source;
96        this.destination = destination;
97        try {
98            long ts = Long.parseLong(timeStamp);
99            if (ts < 0)
100                throw new IllegalArgumentException("Bad time stamp ");
101            this.timeStamp = ts;
102        } catch (NumberFormatException ex) {
103            throw new IllegalArgumentException(
104                "Bad number format " + timeStamp);
105        }
106        this.isSender = isSender;
107        this.firstLine = firstLine;
108        this.tid = tid;
109        this.callId = callId;
110        this.timeStampHeaderValue = timeStampHeaderValue;
111    }
112
113
114
115    public MessageLog(
116        String message,
117        String source,
118        String destination,
119        long timeStamp,
120        boolean isSender,
121        String firstLine,
122        String tid,
123        String callId,
124        long timestampVal) {
125        if (message == null || message.equals(""))
126            throw new IllegalArgumentException("null msg");
127        this.message = message;
128        this.source = source;
129        this.destination = destination;
130        if (timeStamp < 0)
131            throw new IllegalArgumentException("negative ts");
132        this.timeStamp = timeStamp;
133        this.isSender = isSender;
134        this.firstLine = firstLine;
135        this.tid = tid;
136        this.callId = callId;
137        this.timeStampHeaderValue = timestampVal;
138    }
139
140
141    /* (non-Javadoc)
142     * @see gov.nist.javax.sip.stack.LogRecord#toString()
143     */
144
145    public String toString() {
146        String log;
147
148
149            log =
150                "<message\nfrom=\""
151                    + source
152                    + "\" \nto=\""
153                    + destination
154                    + "\" \ntime=\""
155                    + timeStamp
156                    + "\"" +
157                    (this.timeStampHeaderValue != 0 ? "\ntimeStamp = \"" + timeStampHeaderValue + "\"": "")
158                    +"\nisSender=\""
159                    + isSender
160                    + "\" \ntransactionId=\""
161                    + tid
162                    + "\" \ncallId=\""
163                    + callId
164                    + "\" \nfirstLine=\""
165                    + firstLine.trim() + "\"" +
166                    " \n>\n";
167            log += "<![CDATA[";
168            log += message;
169            log += "]]>\n";
170            log += "</message>\n";
171
172        return log;
173    }
174}
175