Skip to main content
summaryrefslogtreecommitdiffstats
blob: a1d169ca2cc438ba6f8470f8895db5c89a712f8d (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
/*******************************************************************************
 * 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.config.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.wst.wsi.internal.core.WSIConstants;
import org.eclipse.wst.wsi.internal.core.monitor.config.Comment;
import org.eclipse.wst.wsi.internal.core.monitor.config.Redirect;
import org.eclipse.wst.wsi.internal.core.xml.XMLUtils;

/**
 *  The implementation for monitor config Redirect element.
 * 
 * @version 1.0.1
 * @author Peter Brittenham (peterbr@us.ibm.com)
 */
public class RedirectImpl implements Redirect
{
  /**
   * Comment.
   */
  protected Comment comment;

  /**
   * Listen port.
   */
  protected int listenPort;

  /**
   * Host.
   */
  protected String host;

  /**
   * To protocol.
   */
  protected String toProtocol;

  /**
   * To port.
   */
  protected int toPort;

  /**
   * To host.
   */
  protected String toHost;

  /**
   * Maximum connections.
   */
  protected int maxConnections;

  /**
   * Read timeout seconds.
   */
  protected int readTimeoutSeconds;

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#getComment()
   */
  public Comment getComment()
  {
    return this.comment;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#setComment(comment)
   */
  public void setComment(Comment comment)
  {
    this.comment = comment;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#getListenPort()
   */
  public int getListenPort()
  {
    return this.listenPort;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#setListenPort(int)
   */
  public void setListenPort(int listenPort)
  {
    this.listenPort = listenPort;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#getHost()
   */
  public String getHost()
  {
    return this.host;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#setHost(String)
   */
  public void setHost(String host)
  {
    this.host = host;

    // Parse location to get protocol, host and port
    URL url;
    try
    {
      url = new URL(host);
      this.toProtocol = url.getProtocol();
      this.toHost = url.getHost();
      this.toPort = url.getPort();
      if (this.toPort == -1)
      {
        if (this.toProtocol.equalsIgnoreCase("http"))
          this.toPort = 80;
        else
          this.toPort = 80;
      }
    }

    catch (MalformedURLException mue)
    {
      throw new IllegalArgumentException(
        "The "
          + WSIConstants.ELEM_SCHEME_AND_HOSTPORT
          + " option contains an invalid value: "
          + host);
    }

    if ((url.getPath() != null && !url.getPath().equals(""))
      || (url.getQuery() != null && !url.getQuery().equals("")))
      throw new IllegalArgumentException(
        "The "
          + WSIConstants.ELEM_SCHEME_AND_HOSTPORT
          + " option must contain only a scheme, host and port: "
          + host);
  }

  /**
   * Get to port.
   * @see org.eclipse.wst.wsi.monitor.config.Redirect#getToPort()
   */
  public int getToPort()
  {
    return this.toPort;
  }

  /**
   * Get to host.
   * @see org.eclipse.wst.wsi.monitor.config.Redirect#getToHost()
   */
  public String getToHost()
  {
    return this.toHost;
  }

  /**
   * Get to protocol.
   * @see org.eclipse.wst.wsi.monitor.config.Redirect#getToProtocol()
   */
  public String getToProtocol()
  {
    return this.toProtocol;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#getMaxConnections()
   */
  public int getMaxConnections()
  {
    return this.maxConnections;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#setMaxConnections(int)
   */
  public void setMaxConnections(int maxConnections)
  {
    this.maxConnections = maxConnections;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#getReadTimeoutSeconds()
   */
  public int getReadTimeoutSeconds()
  {
    return this.readTimeoutSeconds;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.Redirect#setReadTimeoutSeconds(int)
   */
  public void setReadTimeoutSeconds(int readTimeoutSeconds)
  {
    this.readTimeoutSeconds = readTimeoutSeconds;
  }

  /**
   * Get string representation of this object.
   */
  public String toString()
  {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    pw.println("    comment ................... " + this.comment);
    pw.println("    listenPort ................ " + this.listenPort);
    pw.println("    host ...................... " + cutHost(this.host));
    pw.println("    maxConnections ............ " + this.maxConnections);
    pw.println("    readTimeoutSeconds ........ " + this.readTimeoutSeconds);

    return sw.toString();
  }

  /**
   * Formats host name according to the specification (path is ommited).
   * @param host
   * @return
   */
  private String cutHost(String host)
  {
    try
    {
      URL url = new URL(host);
      String port = url.getPort() > -1 ? String.valueOf(url.getPort()) : "80";
      // REMOVED: This code only works on 1.4
      // String.valueOf(url.getDefaultPort());
      return url.getProtocol() + "://" + url.getHost() + ":" + port;
    }
    catch (Exception e)
    {
      return host;
    }
  }

  /* (non-Javadoc)
   * @see org.wsi.test.document.DocumentElement#toXMLString(String)
   */
  public String toXMLString(String namespaceName)
  {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    String nsName = namespaceName;
    if ((!nsName.equals("") && (!nsName.endsWith(":"))))
      nsName += ":";

    // Start element
    pw.println("        <" + nsName + WSIConstants.ELEM_REDIRECT + ">");

    // Comment
    if (this.comment != null)
    {
      pw.println(this.comment.toXMLString(nsName));
    }

    // listenPort
    pw.print("          <" + nsName + WSIConstants.ELEM_LISTEN_PORT + ">");
    pw.print(getListenPort());
    pw.println("</" + nsName + WSIConstants.ELEM_LISTEN_PORT + ">");

    // schemeAndHostPort
    pw.print(
      "          <" + nsName + WSIConstants.ELEM_SCHEME_AND_HOSTPORT + ">");
    pw.print(XMLUtils.xmlEscapedString(getHost()));
    pw.println("</" + nsName + WSIConstants.ELEM_SCHEME_AND_HOSTPORT + ">");

    // maxConnections
    pw.print("          <" + nsName + WSIConstants.ELEM_MAX_CONNECTIONS + ">");
    pw.print(getMaxConnections());
    pw.println("</" + nsName + WSIConstants.ELEM_MAX_CONNECTIONS + ">");

    // readTimeoutSeconds
    pw.print(
      "          <" + nsName + WSIConstants.ELEM_READ_TIMEOUT_SECONDS + ">");
    pw.print(getReadTimeoutSeconds());
    pw.println("</" + nsName + WSIConstants.ELEM_READ_TIMEOUT_SECONDS + ">");

    // End Element
    pw.println("        </" + nsName + WSIConstants.ELEM_REDIRECT + ">");

    return sw.toString();
  }

}

Back to the top