Source Code Cross Referenced for SniffMessage.java in  » 6.0-JDK-Modules » Java-Advanced-Imaging » tools » sniffer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » Java Advanced Imaging » tools.sniffer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package tools.sniffer;
002:
003:        import java.util.*;
004:        import java.text.*;
005:        import gov.nist.javax.sip.parser.*;
006:        import gov.nist.javax.sip.message.*;
007:
008:        /** 
009:         * A parser for Sniff files. This  is the main
010:         * workhorse that reads a Ethereal Sniff file and
011:         * converts it to a fromat that can be used by the trace viewer application
012:         * Acknowledgement:
013:         * This code was contributed by Tim Bardzil <bardzil@colorado.edu>.
014:         * This code was completed as part of a class project in TLEN 5843
015:         * Singaling Protocols, taught by Professor Douglas C. Sicker, Ph.D. at
016:         * the University of Colorado, Boulder.
017:         *
018:         *@author Tim Bardzil <bardzil@colorado.edu> (original)
019:         *@author M. Ranganathan  (ported to 1.2)
020:         *Jeff Adams submitted a patch for this file.
021:         *
022:         */
023:
024:        public class SniffMessage implements  ParseExceptionListener {
025:            String time;
026:            String sourceIP;
027:            String destIP;
028:            SIPMessage sipMessage;
029:
030:            public SniffMessage() {
031:            }
032:
033:            public SniffMessage(ArrayList sniffMsgList) throws ParseException {
034:                getTime(sniffMsgList);
035:                getIPAddresses(sniffMsgList);
036:                getSipMessage(sniffMsgList);
037:            }
038:
039:            private void getTime(ArrayList sniffMsgList) {
040:                Iterator i = sniffMsgList.iterator();
041:                //Date d = new Date(System.currentTimeMillis());
042:                while (i.hasNext()) {
043:                    String line = (String) i.next();
044:                    if (line.startsWith("Arrival Time")) {
045:                        time = line.substring(line.indexOf(":") + 1).trim();
046:                        time = time.substring(0, time.length() - 6);
047:                        SimpleDateFormat formatter = new SimpleDateFormat(
048:                                "MMM dd',' yyyy hh:mm:ss.SSS");
049:                        ParsePosition pos = new ParsePosition(0);
050:                        Date d = formatter.parse(time, pos);
051:                        time = String.valueOf(d.getTime());
052:                        break;
053:                    }
054:                }
055:            }
056:
057:            private void getIPAddresses(ArrayList sniffMsgList) {
058:                Iterator i = sniffMsgList.iterator();
059:                while (i.hasNext()) {
060:                    String line = (String) i.next();
061:                    if (line.startsWith("Internet Protocol")) {
062:                        StringTokenizer st = new StringTokenizer(line, ",");
063:                        while (st.hasMoreTokens()) {
064:                            String temp = st.nextToken().trim();
065:                            if (temp.startsWith("Src Addr:")) {
066:                                StringTokenizer st2 = new StringTokenizer(temp,
067:                                        ":");
068:                                st2.nextToken(); //skip Src Addr:
069:                                sourceIP = st2.nextToken().trim();
070:                            }
071:                            if (temp.startsWith("Dst Addr:")) {
072:                                StringTokenizer st2 = new StringTokenizer(temp,
073:                                        ":");
074:                                st2.nextToken(); //skip Src Addr:
075:                                destIP = st2.nextToken().trim();
076:                            }
077:                        }
078:                        break;
079:                    }
080:                }
081:            }
082:
083:            private int indexOfSDP(ArrayList sniffMsgList) {
084:                Iterator i = sniffMsgList.iterator();
085:                while (i.hasNext()) {
086:                    String line = (String) i.next();
087:                    if (line.startsWith("Session Description Protocol")) {
088:                        return sniffMsgList.indexOf(line);
089:                    }
090:                }
091:                return sniffMsgList.size();
092:            }
093:
094:            private int indexOfSIP(ArrayList sniffMsgList) {
095:                Iterator i = sniffMsgList.iterator();
096:                while (i.hasNext()) {
097:                    String line = (String) i.next();
098:                    if (line.startsWith("Session Initiation Protocol")) {
099:                        return sniffMsgList.indexOf(line);
100:                    }
101:                }
102:                return sniffMsgList.size();
103:            }
104:
105:            private void getSipMessage(ArrayList sniffMsgList)
106:                    throws ParseException {
107:                int sipIndex = indexOfSIP(sniffMsgList);
108:                int sdpIndex = indexOfSDP(sniffMsgList);
109:                String msgBuffer = new String();
110:
111:                //get SIP message
112:                for (int i = sipIndex + 1; i < sdpIndex; i++) {
113:                    String line = (String) sniffMsgList.get(i);
114:                    if (line.startsWith("Request-Line")
115:                            || line.startsWith("Status-Line")) {
116:                        msgBuffer = msgBuffer
117:                                + line.substring(line.indexOf(":") + 1).trim()
118:                                + "\r\n";
119:                    } else if (line.startsWith("Message Header")) {
120:                        //do nothing
121:                    } else if (line.startsWith("Message body")) {
122:                        //do nothing (start of SDP)
123:                    } else {
124:                        msgBuffer = msgBuffer + line.trim() + "\r\n";
125:                    }
126:                }
127:
128:                msgBuffer = msgBuffer + "\r\n";
129:
130:                //get SDP if it exsits
131:                for (int j = sdpIndex; j < sniffMsgList.size(); j++) {
132:                    String line = (String) sniffMsgList.get(j);
133:                    if (line.indexOf("(") > 0 && line.indexOf(")") > 0) {
134:                        msgBuffer = msgBuffer
135:                                + line.charAt(line.indexOf(":") - 2) + "="
136:                                + line.substring(line.indexOf(":") + 1).trim()
137:                                + "\r\n";
138:                    }
139:                }
140:
141:                //parse SIP message
142:                StringMsgParser parser = new StringMsgParser();
143:                parser.setParseExceptionListener(new SniffMessage());
144:                sipMessage = parser.parseSIPMessage(msgBuffer);
145:            }
146:
147:            public String getCallID() {
148:
149:                return sipMessage.getCallId().getCallId();
150:            }
151:
152:            public String toXML() {
153:                String xmlMessage = new String();
154:
155:                String message = sipMessage.encode();
156:                String statusMessage = "";
157:                String firstLine = new String();
158:                if (sipMessage.getClass().isInstance(new SIPRequest())) {
159:                    SIPRequest sipReq = (SIPRequest) sipMessage;
160:                    firstLine = sipReq.getRequestLine().encode().trim();
161:                } else if (sipMessage.getClass().isInstance(new SIPResponse())) {
162:                    SIPResponse sipRes = (SIPResponse) sipMessage;
163:                    firstLine = sipRes.getStatusLine().encode().trim();
164:                }
165:                xmlMessage += "<message from=\"" + sourceIP + "\" to=\""
166:                        + destIP + "\" time=\"" + time + "\" isSender=\""
167:                        + true + "\" callId=\""
168:                        + sipMessage.getCallId().getCallId()
169:                        + "\" statusMessage=\"" + statusMessage
170:                        + "\" transactionId=\"" + sipMessage.getTransactionId()
171:                        + "\" firstLine=\"" + firstLine + "\">\n";
172:                xmlMessage += "<![CDATA[";
173:                xmlMessage += message;
174:                xmlMessage += "]]>\n";
175:                xmlMessage += "</message>\n";
176:
177:                return xmlMessage;
178:            }
179:
180:            public void handleException(ParseException ex,
181:                    SIPMessage sipMessage, Class headerClass,
182:                    String headerText, String messageText)
183:                    throws ParseException {
184:                System.out.println("Error line = " + headerText);
185:
186:                try {
187:                    if (headerClass.equals(Class
188:                            .forName("gov.nist.javax.sip.header.From"))
189:                            || headerClass.equals(Class
190:                                    .forName("gov.nist.javax.sip.header.To"))
191:                            || headerClass
192:                                    .equals(Class
193:                                            .forName("gov.nist.javax.sip.header.ViaList"))
194:                            || headerClass.equals(Class
195:                                    .forName("gov.nist.javax.sip.header.CSeq"))
196:                            || headerClass
197:                                    .equals(Class
198:                                            .forName("gov.nist.javax.sip.header.CallId")))
199:                        throw ex;
200:                } catch (ClassNotFoundException e) {
201:                    System.out
202:                            .println("could not find class -- internal error");
203:                    e.printStackTrace();
204:                    System.exit(0);
205:                }
206:
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.