inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.core / src / net / sourceforge / phpdt / monitor / core / internal / Request.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
12
13 import java.util.Date;
14 import java.util.Properties;
15
16 import net.sourceforge.phpdt.monitor.core.IProtocolAdapter;
17 import net.sourceforge.phpdt.monitor.core.IRequest;
18 /**
19  * A single TCP/IP request/response pair.
20  */
21 public class Request implements IRequest {
22         protected Date date;
23         protected long responseTime = -1;
24         protected int localPort;
25         protected String remoteHost;
26         protected int remotePort;
27         protected byte[] request;
28         protected byte[] response;
29         
30         protected String label;
31         protected IProtocolAdapter type;
32
33         protected Properties properties;
34
35         /**
36          * RequestResponse constructor comment.
37          */
38         public Request(IProtocolAdapter type, int localPort, String remoteHost, int remotePort) {
39                 super();
40                 this.type = type;
41                 this.localPort = localPort;
42                 this.remoteHost = remoteHost;
43                 this.remotePort = remotePort;
44                 date = new Date();
45                 properties = new Properties();
46                 MonitorManager.getInstance().addRequest(this);
47         }
48         
49         public IProtocolAdapter getType() {
50                 return type;
51         }
52
53         /**
54          * Add to the request.
55          *
56          * @param addRequest byte[]
57          */
58         public void addToRequest(byte[] addRequest) {
59                 if (addRequest == null || addRequest.length == 0)
60                         return;
61         
62                 if (request == null || request.length == 0) {
63                         setRequest(addRequest);
64                         return;
65                 }
66         
67                 int size = request.length + addRequest.length;
68                 byte[] b = new byte[size];
69                 System.arraycopy(request, 0, b, 0, request.length);
70                 System.arraycopy(addRequest, 0, b, request.length, addRequest.length);
71                 request = b;
72         }
73
74         /**
75          * Add to the response.
76          *
77          * @param addResponse byte[]
78          */
79         public void addToResponse(byte[] addResponse) {
80                 if (addResponse == null || addResponse.length == 0)
81                         return;
82         
83                 if (response == null || response.length == 0) {
84                         setResponse(addResponse);
85                         return;
86                 }
87         
88                 int size = response.length + addResponse.length;
89                 byte[] b = new byte[size];
90                 System.arraycopy(response, 0, b, 0, response.length);
91                 System.arraycopy(addResponse, 0, b, response.length, addResponse.length);
92                 response = b;
93         }
94
95         /**
96          * Return the date/time of this request.
97          *
98          * @return java.util.Date
99          */
100         public Date getDate() {
101                 return date;
102         }
103
104         /**
105          * Returns the local port.
106          *
107          * @return int
108          */
109         public int getLocalPort() {
110                 return localPort;
111         }
112
113         /**
114          * Returns the remote host.
115          *
116          * @return java.lang.String
117          */
118         public String getRemoteHost() {
119                 return remoteHost;
120         }
121
122         /**
123          * Returns the remote port.
124          *
125          * @return int
126          */
127         public int getRemotePort() {
128                 return remotePort;
129         }
130
131         /**
132          * Returns the request as a byte array.
133          *
134          * @return byte[]
135          */
136         public byte[] getRequest(byte type2) {
137                 return request;
138         }
139
140         /**
141          * Returns the response as a byte array.
142          *
143          * @return byte[]
144          */
145         public byte[] getResponse(byte type2) {
146                 return response;
147         }
148
149         /**
150          * Returns the response time in milliseconds.
151          *
152          * @return long
153          */
154         public long getResponseTime() {
155                 return responseTime;
156         }
157
158         /**
159          * Returns the title, if one exists.
160          *
161          * @return java.lang.String
162          */
163         public String getLabel() {
164                 if (label == null)
165                         return getRemoteHost() + ":" + getRemotePort();
166                 else
167                         return label;
168         }
169
170         /**
171          * Set the request.
172          *
173          * @param request byte[]
174          */
175         protected void setRequest(byte[] request) {
176                 if (request == null || request.length == 0)
177                         return;
178         
179                 this.request = request;
180         
181                 MonitorManager.getInstance().requestChanged(this);
182         }
183
184         /**
185          * Set the response.
186          *
187          * @param response byte[]
188          */
189         protected void setResponse(byte[] response) {
190                 if (response == null || response.length == 0)
191                         return;
192         
193                 this.response = response;
194         
195                 responseTime = System.currentTimeMillis() - date.getTime();
196         
197                 MonitorManager.getInstance().requestChanged(this);
198         }
199
200         /**
201          * Sets the title.
202          *
203          * @param s java.lang.String
204          */
205         public void setLabel(String s) {
206                 // property can only be set once
207                 if (label != null)
208                         return;
209         
210                 label = s;
211                 MonitorManager.getInstance().requestChanged(this);
212         }
213         
214         /**
215          * 
216          */
217         public void addProperty(String key, Object value) {
218                 try {
219                         if (properties.containsKey(key))
220                                 properties.remove(key);
221                         properties.put(key, value);
222                 } catch (Exception e) {
223                         Trace.trace(Trace.SEVERE, "Could not add property", e);
224                 }
225         }
226         
227         /**
228          * 
229          */
230         public String getStringProperty(String key) {
231                 try {
232                         return (String) properties.get(key);
233                 } catch (Exception e) {
234                         return "";
235                 }
236         }
237         
238         /**
239          * 
240          */
241         public Integer getIntegerProperty(String key) {
242                 try {
243                         return (Integer) properties.get(key);
244                 } catch (Exception e) {
245                         return null;
246                 }
247         }
248         
249         /**
250          * 
251          */
252         public Object getObjectProperty(String key) {
253                 try {
254                         return properties.get(key);
255                 } catch (Exception e) {
256                         return null;
257                 }
258         }
259         
260         public void fireChangedEvent() {
261                 MonitorManager.getInstance().requestChanged(this);
262         }
263 }