Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: fe2a0f4cfcb444bb2d684f75eab7516e79473034 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.monitor;

import java.net.Socket;
import java.util.Iterator;
import java.util.Vector;

import org.eclipse.wst.wsi.internal.core.WSIException;
import org.eclipse.wst.wsi.internal.core.log.MessageEntry;
import org.eclipse.wst.wsi.internal.core.log.MimeParts;
import org.eclipse.wst.wsi.internal.core.monitor.config.Redirect;
import org.eclipse.wst.wsi.internal.core.util.Utils;

/**
 * A socket connection.
 *
 * @author Peter  Brittenham (peterbr@us.ibm.com)
 * @version 1.0.1
 */
public class SocketConnection extends Thread
{
  protected Monitor monitor;
  protected Redirect redirect;
  protected Socket inSocket;
  protected Socket outSocket;

  protected String httpProxyHost = null;
  protected int httpProxyPort;

  protected SocketHandler requestSocketHandler = null;
  protected SocketHandler responseSocketHandler = null;

  private Vector listeners = new Vector(); // SS

  /**
   * Create socket connection.
   * @param monitor  a Monitor object.
   * @param redirect a Redirect object.
   * @param inSocket in socket.
   */
  public SocketConnection(Monitor monitor, Redirect redirect, Socket inSocket)
  {
    this.monitor = monitor;
    this.redirect = redirect;
    this.inSocket = inSocket;

    // Start processing the connection
    start();
  }

  /**
   * Process the socket connection.
   */
  public void run()
  {
    // Get the HTTP proxy settings if there are any set (not used currently)
    loadHttpProxySettings();

    try
    {
      // Open the connection to the server
      this.outSocket = new Socket(redirect.getToHost(), redirect.getToPort());

      // Set the read timeout for the input socket
      this.inSocket.setSoTimeout(redirect.getReadTimeoutSeconds() * 1000);

      // Get the next conversation ID
      int conversationId = monitor.getNextConversationId();

      // Create the socket handlers which are used to send the data from
      // the client to the server
      requestSocketHandler =
        new SocketHandler(
          this,
          MessageEntry.TYPE_REQUEST,
          conversationId,
          redirect.getToHost(),
          this.inSocket,
          this.outSocket,
          redirect.getReadTimeoutSeconds());
      responseSocketHandler =
        new SocketHandler(
          this,
          MessageEntry.TYPE_RESPONSE,
          conversationId,
          redirect.getToHost(),
          this.outSocket,
          this.inSocket,
          redirect.getReadTimeoutSeconds());

      // Tell each socketHandler about the other one
      requestSocketHandler.setPairedSocketHandler(responseSocketHandler);
      responseSocketHandler.setPairedSocketHandler(requestSocketHandler);
    }

    catch (Exception e)
    {
      Monitor.staticPrintMessage(
        "error04",
        redirect.getToHost(),
        "Monitor cannot connect to a redirect host:");

      shutdown();

      try
      {
        this.inSocket.close();
      }
      catch (Throwable t)
      {
      }
    }
  }

  /**
   * Log message.
   * @param conversationID      the coversation id.
   * @param connectionType      the connection type.
   * @param timestamp           the timestamp.
   * @param senderHostAndPort   the sender host and port.
   * @param receiverHostAndPort the receiver host and port.
   * @param messageBuffer       the message.
   * @param bom                 the BOM.
   * @param encoding            the encoding.
   * @throws WSIException if  there is a problem logging message.
  */
  public void logMessage(
    int conversationID,
    String connectionType,
    String timestamp,
    String senderHostAndPort,
    String receiverHostAndPort,
    StringBuffer messageBuffer,
    int bom,
    String encoding)
    throws WSIException
  {
    MessageEntry messageEntry = null;

    // Create message entry
    messageEntry =
      createMessageEntry(
        conversationID,
        connectionType,
        timestamp,
        senderHostAndPort,
        receiverHostAndPort,
        messageBuffer.toString(),
        bom,
        encoding);

    // DEBUG:
    //debug("logMessage", " messageEntry: " + messageEntry);

    // Add entry to the queue
    monitor.getMessageEntryQueue().addMessageEntry(messageEntry);
  }

  /**
   * Create a log entry from all of the data that was gathered.
   */
  private MessageEntry createMessageEntry(
    int conversationID,
    String messageType,
    String timestamp,
    String senderHostAndPort,
    String receiverHostAndPort,
    String message,
    int bom,
    String encoding)
    throws WSIException
  {
    // Create log entry object
    MessageEntry messageEntry = monitor.getLog().createLogEntry();

    // Set input information in log entry
    // NOTE:  The ID is set just before the log entry is written to the log file in LogEntryQueue
    //logEntry.setId(id);
    messageEntry.setConversationId(String.valueOf(conversationID));
    messageEntry.setType(messageType);
    messageEntry.setTimestamp(timestamp);
    messageEntry.setSenderHostAndPort(senderHostAndPort);
    messageEntry.setReceiverHostAndPort(receiverHostAndPort);
    messageEntry.setEncoding(encoding);

    // Get HTTP headers from full message
    String httpHeaders = Utils.getHTTPHeaders(message);
    messageEntry.setHTTPHeaders(httpHeaders);
	
   if (Utils.isMultipartRelatedMessage(httpHeaders))
    {
    	MimeParts mimeParts = Utils.parseMultipartRelatedMessage(message, httpHeaders, encoding);
    	if (mimeParts == null)
    	{
    	  // problem creating Mimeparts -- treat it as simple SOAP message
    	  String content = Utils.getContent(message);
    	  messageEntry.setMessage(content);
    	  messageEntry.setMimeContent(false);
    	}
    	else
    	{
          messageEntry.setMimeParts(mimeParts);
          messageEntry.setMimeContent(true);
    	}
    }
    else
    {
      // Get the message content
      String content = Utils.getContent(message);
      messageEntry.setMessage(content);
      messageEntry.setMimeContent(false);
    }

    // Set the BOM, if there is one
    if (bom != 0)
      messageEntry.setBOM(bom);

    return messageEntry;
  }

  /**
   * Load HTTP proxy settings.
   */
  public void loadHttpProxySettings()
  {
    // Get the HTTP proxy host setting
    this.httpProxyHost = System.getProperty("http.proxyHost");
    if (this.httpProxyHost != null && httpProxyHost.equals(""))
      this.httpProxyHost = null;

    // If there was an HTTP proxy host setting, then get the port
    if (this.httpProxyHost != null)
    {
      String portString = System.getProperty("http.proxyPort");

      // Set default to 80
      if (portString == null || portString.equals(""))
        this.httpProxyPort = 80;
      else
        httpProxyPort = Integer.parseInt(portString);
    }
  }

  Monitor getMonitor()
  {
    return this.monitor;
  }

  synchronized void wakeUp()
  {
    fireConnectionClosed(); // SS
    notifyAll();
  }

  /**
   * Shutdown.
   */
  public void shutdown()
  {
    if (this.requestSocketHandler != null)
      this.requestSocketHandler.shutdown();
    if (this.responseSocketHandler != null)
      this.responseSocketHandler.shutdown();
  }

  // ==== SS start ====
  /**
   * Add connection event listener.
   * @param listener event listener.
   * @see #removeConnectionListener
   */
  public void addConnectionListener(ConnectionListener listener)
  {
    listeners.add(listener);
  }
  /**
   * Remove connection event listener.
   * @param listener event listener.
   * @see #addConnectionListener
   */
  public void removeConnectionListener(ConnectionListener listener)
  {
    listeners.remove(listener);
  }
  /**
   * Notify listeners on connection close.
   */
  private void fireConnectionClosed()
  {
    for (Iterator i = listeners.iterator(); i.hasNext();)
    {
      ConnectionListener listener = (ConnectionListener) i.next();
      listener.connectionClosed(this);
    }
  }
  // ==== SS end ====
}

Back to the top