source: trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/HTTPLogParser.java @ 1367

Last change on this file since 1367 was 1367, checked in by pharms, 10 years ago

Initial import.

File size: 3.4 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.http;
16
17import java.io.File;
18import java.util.Collection;
19import java.util.LinkedList;
20import java.util.List;
21
22import javax.xml.bind.JAXBContext;
23import javax.xml.bind.JAXBElement;
24import javax.xml.bind.JAXBException;
25import javax.xml.bind.Unmarshaller;
26import javax.xml.transform.stream.StreamSource;
27
28import org.xml.sax.SAXException;
29
30import de.ugoe.cs.autoquest.eventcore.Event;
31import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange;
32import de.ugoe.cs.autoquest.httpmonitor.exchange.Session;
33import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType;
34import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget;
35
36/**
37 * <p>
38 * TODO comment
39 * </p>
40 *
41 * @author Patrick Harms
42 */
43public class HTTPLogParser {
44
45    /**
46     * <p>
47     * the event sequences parsed by this parser
48     * </p>
49     */
50    private Collection<List<Event>> sequences = new LinkedList<List<Event>>();
51
52    /**
53     * <p>
54     * Parses a log file written by the HTTPMonitor and creates a collection of event sequences.
55     * </p>
56     *
57     * @param filename
58     *            name and path of the log file
59     *
60     * @throws SAXException in the case, the file could not be parsed
61     */
62    public void parseFile(String filename) throws JAXBException {
63        if (filename == null) {
64            throw new IllegalArgumentException("filename must not be null");
65        }
66
67        parseFile(new File(filename));
68    }
69
70    /**
71     * <p>
72     * Parses a log file written by the HTTPMonitor and creates a collection of event sequences.
73     * </p>
74     *
75     * @param file
76     *            file to be parsed
77     *
78     * @throws SAXException in the case, the file could not be parsed
79     */
80    public void parseFile(File file) throws JAXBException {
81        if (file == null) {
82            throw new IllegalArgumentException("file must not be null");
83        }
84       
85        JAXBContext jc = JAXBContext.newInstance(Session.class.getPackage().getName());
86       
87        Unmarshaller unmarshaller = jc.createUnmarshaller();
88        StreamSource source = new StreamSource(file);
89       
90        @SuppressWarnings("unchecked")
91        JAXBElement<Session> sessionObj = (JAXBElement<Session>) unmarshaller.unmarshal(source);
92       
93        Session session = sessionObj.getValue();
94
95        if ((session.getHttpExchange() != null) && (session.getHttpExchange().size() > 0)) {
96            List<Event> sequence = new LinkedList<Event>();
97            for (HttpExchange exchange : session.getHttpExchange()) {
98                sequence.add(new Event(new HTTPEventType(exchange),
99                                       new HTTPTarget(exchange.getReceiver())));
100            }
101            sequences.add(sequence);
102        }
103       
104    }
105
106    /**
107     *
108     */
109    public Collection<List<Event>> getSequences() {
110        return sequences;
111    }
112}
Note: See TracBrowser for help on using the repository browser.