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.eventcore; |
---|
16 | |
---|
17 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
18 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
19 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
20 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpRequest; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * the event type for HTTP events. Holds a reference to the HTTP exchange recorded by the HTTP |
---|
25 | * monitor. |
---|
26 | * </p> |
---|
27 | * |
---|
28 | * @author Patrick Harms |
---|
29 | */ |
---|
30 | public class HTTPEventType implements IEventType { |
---|
31 | |
---|
32 | /** */ |
---|
33 | private static final long serialVersionUID = 1L; |
---|
34 | |
---|
35 | /** |
---|
36 | * <p> |
---|
37 | * the HTTP exchange recorded by the HTTP monitor |
---|
38 | * </p> |
---|
39 | */ |
---|
40 | private HttpExchange exchange; |
---|
41 | |
---|
42 | /** |
---|
43 | * <p> |
---|
44 | * the name of the event type as required by AutoQUEST's framework |
---|
45 | * </p> |
---|
46 | */ |
---|
47 | private String name; |
---|
48 | |
---|
49 | /** |
---|
50 | * <p> |
---|
51 | * initializes this event with the represented HTTP exchange |
---|
52 | * </p> |
---|
53 | * |
---|
54 | * @param exchange the HTTP exchange recorded by the HTTP monitor |
---|
55 | */ |
---|
56 | public HTTPEventType(HttpExchange exchange) { |
---|
57 | if (exchange == null) { |
---|
58 | throw new IllegalArgumentException("exchange must not be null"); |
---|
59 | } |
---|
60 | |
---|
61 | this.exchange = exchange; |
---|
62 | |
---|
63 | StringBuffer nameBuffer = new StringBuffer("HTTPEvent"); |
---|
64 | |
---|
65 | boolean somethingAdded = false; |
---|
66 | |
---|
67 | if ((this.exchange.getRequest() != null) && |
---|
68 | (this.exchange.getRequest().getMethod() != null)) |
---|
69 | { |
---|
70 | nameBuffer.append("("); |
---|
71 | nameBuffer.append(this.exchange.getRequest().getMethod()); |
---|
72 | somethingAdded = true; |
---|
73 | } |
---|
74 | |
---|
75 | String senderStr = HTTPUtils.toString(this.exchange.getSender()); |
---|
76 | String receiverStr = HTTPUtils.toString(this.exchange.getReceiver()); |
---|
77 | |
---|
78 | if ((senderStr != null) && (receiverStr != null)) { |
---|
79 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
80 | nameBuffer.append(senderStr); |
---|
81 | nameBuffer.append(" --> "); |
---|
82 | nameBuffer.append(receiverStr); |
---|
83 | somethingAdded = true; |
---|
84 | } |
---|
85 | else if (senderStr != null) { |
---|
86 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
87 | nameBuffer.append(senderStr); |
---|
88 | somethingAdded = true; |
---|
89 | } |
---|
90 | else if (receiverStr != null) { |
---|
91 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
92 | nameBuffer.append(receiverStr); |
---|
93 | somethingAdded = true; |
---|
94 | } |
---|
95 | |
---|
96 | if (somethingAdded) { |
---|
97 | nameBuffer.append(")"); |
---|
98 | } |
---|
99 | |
---|
100 | this.name = nameBuffer.toString(); |
---|
101 | } |
---|
102 | |
---|
103 | /* (non-Javadoc) |
---|
104 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName() |
---|
105 | */ |
---|
106 | @Override |
---|
107 | public String getName() { |
---|
108 | return name; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * @return the HTTP exchange recorded by the HTTP monitor |
---|
113 | */ |
---|
114 | public HttpExchange getExchange() { |
---|
115 | return exchange; |
---|
116 | } |
---|
117 | |
---|
118 | /* (non-Javadoc) |
---|
119 | * @see java.lang.Object#toString() |
---|
120 | */ |
---|
121 | @Override |
---|
122 | public String toString() { |
---|
123 | return name; |
---|
124 | } |
---|
125 | |
---|
126 | /* (non-Javadoc) |
---|
127 | * @see java.lang.Object#equals(java.lang.Object) |
---|
128 | */ |
---|
129 | @Override |
---|
130 | public boolean equals(Object obj) { |
---|
131 | if (this == obj) { |
---|
132 | return true; |
---|
133 | } |
---|
134 | |
---|
135 | if (obj instanceof HTTPEventType) { |
---|
136 | HTTPEventType other = (HTTPEventType) obj; |
---|
137 | |
---|
138 | if (!other.getClass().isAssignableFrom(this.getClass())) { |
---|
139 | return false; |
---|
140 | } |
---|
141 | |
---|
142 | if (exchange == null) { |
---|
143 | return other.exchange == null; |
---|
144 | } |
---|
145 | else if (other.exchange == null) { |
---|
146 | return false; |
---|
147 | } |
---|
148 | |
---|
149 | HttpRequest request1 = exchange.getRequest(); |
---|
150 | HttpRequest request2 = other.exchange.getRequest(); |
---|
151 | |
---|
152 | // do not compare the sender, as this may change |
---|
153 | return (HTTPUtils.equals(exchange.getReceiver(), other.exchange.getReceiver()) && |
---|
154 | HTTPUtils.equals(request1.getMethod(), request2.getMethod()) && |
---|
155 | HTTPUtils.equals(request1.getProtocol(), request2.getProtocol()) && |
---|
156 | HTTPUtils.equals(request1.getUrl(), request2.getUrl())); |
---|
157 | } |
---|
158 | else { |
---|
159 | return false; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /* (non-Javadoc) |
---|
164 | * @see java.lang.Object#hashCode() |
---|
165 | */ |
---|
166 | @Override |
---|
167 | public int hashCode() { |
---|
168 | if (exchange != null) { |
---|
169 | return |
---|
170 | exchange.getRequest().getMethod().hashCode() + |
---|
171 | exchange.getRequest().getProtocol().hashCode() + |
---|
172 | exchange.getRequest().getUrl().hashCode(); |
---|
173 | } |
---|
174 | else { |
---|
175 | return 0; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | } |
---|