1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.http; |
---|
16 | |
---|
17 | import java.io.File; |
---|
18 | import java.util.Collection; |
---|
19 | import java.util.LinkedList; |
---|
20 | import java.util.List; |
---|
21 | |
---|
22 | import javax.xml.bind.JAXBContext; |
---|
23 | import javax.xml.bind.JAXBElement; |
---|
24 | import javax.xml.bind.JAXBException; |
---|
25 | import javax.xml.bind.Unmarshaller; |
---|
26 | import javax.xml.transform.stream.StreamSource; |
---|
27 | |
---|
28 | import org.xml.sax.SAXException; |
---|
29 | |
---|
30 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
31 | import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange; |
---|
32 | import de.ugoe.cs.autoquest.httpmonitor.exchange.Session; |
---|
33 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
34 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
35 | |
---|
36 | /** |
---|
37 | * <p> |
---|
38 | * Parser for HTTP Monitor logs. Uses JAXB for parsing and is therefore quite simple. For each |
---|
39 | * exchange in the log, it creates an appropriate event. |
---|
40 | * </p> |
---|
41 | * |
---|
42 | * @author Patrick Harms |
---|
43 | */ |
---|
44 | public class HTTPLogParser { |
---|
45 | |
---|
46 | /** |
---|
47 | * <p> |
---|
48 | * the event sequences parsed by this parser |
---|
49 | * </p> |
---|
50 | */ |
---|
51 | private Collection<List<Event>> sequences = new LinkedList<List<Event>>(); |
---|
52 | |
---|
53 | /** |
---|
54 | * <p> |
---|
55 | * Parses a log file written by the HTTPMonitor and creates a collection of event sequences. |
---|
56 | * </p> |
---|
57 | * |
---|
58 | * @param filename |
---|
59 | * name and path of the log file |
---|
60 | * |
---|
61 | * @throws SAXException in the case, the file could not be parsed |
---|
62 | */ |
---|
63 | public void parseFile(String filename) throws JAXBException { |
---|
64 | if (filename == null) { |
---|
65 | throw new IllegalArgumentException("filename must not be null"); |
---|
66 | } |
---|
67 | |
---|
68 | parseFile(new File(filename)); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * <p> |
---|
73 | * Parses a log file written by the HTTPMonitor and creates a collection of event sequences. |
---|
74 | * </p> |
---|
75 | * |
---|
76 | * @param file |
---|
77 | * file to be parsed |
---|
78 | * |
---|
79 | * @throws SAXException in the case, the file could not be parsed |
---|
80 | */ |
---|
81 | public void parseFile(File file) throws JAXBException { |
---|
82 | if (file == null) { |
---|
83 | throw new IllegalArgumentException("file must not be null"); |
---|
84 | } |
---|
85 | |
---|
86 | JAXBContext jc = JAXBContext.newInstance(Session.class.getPackage().getName()); |
---|
87 | |
---|
88 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
---|
89 | StreamSource source = new StreamSource(file); |
---|
90 | |
---|
91 | @SuppressWarnings("unchecked") |
---|
92 | JAXBElement<Session> sessionObj = (JAXBElement<Session>) unmarshaller.unmarshal(source); |
---|
93 | |
---|
94 | Session session = sessionObj.getValue(); |
---|
95 | |
---|
96 | if ((session.getHttpExchange() != null) && (session.getHttpExchange().size() > 0)) { |
---|
97 | List<Event> sequence = new LinkedList<Event>(); |
---|
98 | for (HttpExchange exchange : session.getHttpExchange()) { |
---|
99 | sequence.add(new Event(new HTTPEventType(exchange), |
---|
100 | new HTTPTarget(exchange.getReceiver()))); |
---|
101 | } |
---|
102 | sequences.add(sequence); |
---|
103 | } |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * <p> |
---|
109 | * returns the sequences parsed by this parser |
---|
110 | * </p> |
---|
111 | * |
---|
112 | * @return as described |
---|
113 | */ |
---|
114 | public Collection<List<Event>> getSequences() { |
---|
115 | return sequences; |
---|
116 | } |
---|
117 | } |
---|