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

Last change on this file since 1923 was 1923, checked in by sherbold, 9 years ago
  • added convenience function to remove all non-SOAP events from a sequence
  • modified HTTP log parser to turn of the SOAP logging facility. This removes a lot of junk messages, but it is possible we overlook something in the future. In case of problems, the logger should be turned on again.
File size: 9.3 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.ByteArrayInputStream;
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.FileNotFoundException;
21import java.io.IOException;
22import java.io.InputStream;
23import java.util.Collection;
24import java.util.LinkedList;
25import java.util.List;
26import java.util.Properties;
27import java.util.logging.Level;
28import java.util.logging.Logger;
29
30import javax.xml.bind.JAXBContext;
31import javax.xml.bind.JAXBElement;
32import javax.xml.bind.JAXBException;
33import javax.xml.bind.Unmarshaller;
34import javax.xml.soap.MessageFactory;
35import javax.xml.soap.SOAPMessage;
36import javax.xml.transform.stream.StreamSource;
37
38import org.xml.sax.SAXException;
39
40import de.ugoe.cs.autoquest.eventcore.Event;
41import de.ugoe.cs.autoquest.eventcore.IEventType;
42import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType;
43import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget;
44import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
45import de.ugoe.cs.autoquest.plugin.http.logdata.Content;
46import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
47import de.ugoe.cs.autoquest.plugin.http.logdata.Session;
48import de.ugoe.cs.util.console.Console;
49
50/**
51 * <p>
52 * Parser for HTTP Monitor logs. Uses JAXB for parsing and is therefore quite simple. For each
53 * exchange in the log, it creates an appropriate event. It differes between default HTTP events
54 * and SOAP events.
55 * </p>
56 *
57 * @author Patrick Harms
58 */
59public class HTTPLogParser {
60
61    /**
62     * <p>
63     * the event sequences parsed by this parser
64     * </p>
65     */
66    private Collection<List<Event>> sequences = new LinkedList<List<Event>>();
67   
68    /**
69     * <p>
70     * the message factory used for parsing SOAP messages
71     * </p>
72     */
73    private MessageFactory soapMessageFactory;
74   
75    /**
76     * <p>
77     * properties used to map the path of a service to its logical name
78     * </p>
79     */
80    private Properties urlNameMap = null;
81   
82    /**
83     * <p>
84     * Constructor. Creates a new HTTPLogParser without a urlNameMap.
85     * </p>
86     */
87    public HTTPLogParser() {
88        Logger.getLogger("com.sun.xml.internal.messaging.saaj").setLevel(Level.OFF);
89    }
90   
91    /**
92     * <p>
93     * Constructor. Creates a new HTTPLogParser with a urlNameMap
94     * </p>
95     *
96     * @param urlNameMapFile properties file with the path to logical name map
97     * @throws IOException thrown if there is an error load in the service name map
98     */
99    public HTTPLogParser(File urlNameMapFile) throws IOException {
100        this(new FileInputStream(urlNameMapFile));
101    }
102   
103    /**
104     * <p>
105     * Constructor. Creates a new HTTPLogParser with a urlNameMap
106     * </p>
107     *
108     * @param urlNameMapFile properties file with the path to logical name map
109     * @throws IOException thrown if there is an error load in the service name map
110     */
111    public HTTPLogParser(InputStream urlNameMapStream) throws IOException {
112        this();
113        urlNameMap = new Properties();
114        urlNameMap.load(urlNameMapStream);
115    }
116   
117    /**
118     * <p>
119     * Constructor. Creates a new HTTPLogParser with a urlNameMap.
120     * </p>
121     *
122     * @param urlNameMapFile properties that include the logical name map
123     */
124    public HTTPLogParser(Properties urlNameMapFile) {
125        this();
126        urlNameMap = urlNameMapFile;
127    }
128
129    /**
130     * <p>
131     * Parses a log file written by the HTTPMonitor and creates a collection of event sequences.
132     * </p>
133     *
134     * @param filename
135     *            name and path of the log file
136     *
137     * @throws SAXException in the case, the file could not be parsed
138     */
139    public void parseFile(String filename) throws JAXBException {
140        if (filename == null) {
141            throw new IllegalArgumentException("filename must not be null");
142        }
143
144        parseFile(new File(filename));
145    }
146
147    /**
148     * <p>
149     * Parses a log file written by the HTTPMonitor and creates a collection of event sequences.
150     * </p>
151     *
152     * @param file
153     *            file to be parsed
154     *
155     * @throws SAXException in the case, the file could not be parsed
156     */
157    public void parseFile(File file) throws JAXBException {
158        if (file == null) {
159            throw new IllegalArgumentException("file must not be null");
160        }
161       
162        try {
163            parseFile(new FileInputStream(file));
164        }
165        catch (FileNotFoundException e) {
166            Console.printerr("Error parsing file + " + file.getName());
167            Console.logException(e);
168            return;
169        }
170    }
171   
172    /**
173     * <p>
174     * Parses a log file written by the HTTPMonitor and creates a collection of event sequences.
175     * </p>
176     *
177     * @param stream
178     *            file to be parsed
179     *
180     * @throws SAXException in the case, the file could not be parsed
181     */
182    public void parseFile(InputStream stream) throws JAXBException {
183        if (stream == null) {
184            throw new IllegalArgumentException("stream must not be null");
185        }
186       
187        JAXBContext jc = JAXBContext.newInstance(Session.class.getPackage().getName());
188       
189        Unmarshaller unmarshaller = jc.createUnmarshaller();
190        StreamSource source = new StreamSource(stream);
191       
192        @SuppressWarnings("unchecked")
193        JAXBElement<Session> sessionObj = (JAXBElement<Session>) unmarshaller.unmarshal(source);
194       
195        Session session = sessionObj.getValue();
196
197        if ((session.getHttpExchange() != null) && (session.getHttpExchange().size() > 0)) {
198            List<Event> sequence = new LinkedList<Event>();
199            for (HttpExchange exchange : session.getHttpExchange()) {
200                sequence.add(new Event(createEvent(exchange),
201                                       new HTTPTarget(exchange.getReceiver())));
202            }
203            sequences.add(sequence);
204        }
205       
206    }
207
208    /**
209     * <p>
210     * instantiates the appropriate event type. If it encounters a SOAP exchange, a SOAP event type
211     * is instantiated. Otherwise a normal HTTP event type is created.
212     * </p>
213     *
214     * @param exchange the exchange for which an event type is to be created
215     *
216     * @return as described
217     */
218    private IEventType createEvent(HttpExchange exchange) {
219        Content requestContent =
220            exchange.getRequest() != null ? exchange.getRequest().getContent() : null;
221       
222        SOAPMessage soapRequest = getSOAPMessage(requestContent);
223       
224        Content responseContent =
225            exchange.getResponse() != null ? exchange.getResponse().getContent() : null;
226           
227        SOAPMessage soapResponse = getSOAPMessage(responseContent);
228       
229        if (soapRequest != null) {
230            return new SOAPEventType(exchange, soapRequest, soapResponse, urlNameMap);
231        }
232        else {
233            return new HTTPEventType(exchange);
234        }
235    }
236
237    /**
238     * <p>
239     * convenience method to convert the content of an HTTP request or response into a SOAP message
240     * </p>
241     *
242     * @param content the content to be converted into a SOAP message
243     *
244     * @return the SOAP message contained in the content or null if either the content is null or
245     *         the content does not contain a SOAP message
246     */
247    private SOAPMessage getSOAPMessage(Content content) {
248        if ((content != null) && (content.getData() != null)) {
249            try {
250                if (soapMessageFactory == null) {
251                    soapMessageFactory = MessageFactory.newInstance();
252                }
253           
254                String encoding = content.getEncoding();
255                if (encoding == null) {
256                    encoding = "UTF-8";
257                }
258               
259                InputStream in = new ByteArrayInputStream(content.getData().getBytes(encoding));
260                SOAPMessage message = soapMessageFactory.createMessage(null, in);
261               
262                // try to access something to see, if everything worked fine.
263                message.getSOAPHeader();
264                message.getSOAPBody();
265               
266                return message;
267            }
268            catch (Exception e) {
269                if (content.getData().toLowerCase().indexOf("envelope") > 0) {
270                    Console.traceln(Level.WARNING, "HTTP message seems to be a SOAP message but " +
271                                    "it could not be parsed as such: " + e);
272                    //Console.logException(e);
273                }
274            }
275        }
276       
277        return null;
278    }
279
280    /**
281     * <p>
282     * returns the sequences parsed by this parser
283     * </p>
284     *
285     * @return as described
286     */
287    public Collection<List<Event>> getSequences() {
288        return sequences;
289    }
290}
Note: See TracBrowser for help on using the repository browser.