KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > ConnectMethod


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import com.maverick.ssl.SSLTransportFactory;
27
28 /**
29  * Implementation of an {@link HttpMethod} that is specified to <i>SSL-Explorer</i>
30  * and used for SSL-Tunnnels.
31  *
32  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
33  */

34 public class ConnectMethod extends HttpMethod {
35
36     String JavaDoc hostname;
37     int port;
38     boolean targetIsSecure;
39     HttpMethod toExecute;
40
41     // #ifdef DEBUG
42
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ConnectMethod.class);
43
44     // #endif
45

46     public ConnectMethod(String JavaDoc hostname, int port, boolean targetIsSecure, HttpMethod toExecute) {
47         super("CONNECT", hostname + ":" + port); //$NON-NLS-1$ //$NON-NLS-2$
48
this.hostname = hostname;
49         this.port = port;
50         this.targetIsSecure = targetIsSecure;
51         this.toExecute = toExecute;
52     }
53
54     public ConnectMethod(String JavaDoc hostname, int port, boolean targetIsSecure) {
55         this(hostname, port, targetIsSecure, null);
56     }
57
58     public String JavaDoc getVersion() {
59         return "1.0"; //$NON-NLS-1$
60
}
61
62     public String JavaDoc getURI() {
63         return hostname + ":" + port; //$NON-NLS-1$
64
}
65
66     public void processRequest(HttpRequest request) {
67
68         request.setHeaderField("Proxy-Connection", "Keep-Alive"); //$NON-NLS-1$ //$NON-NLS-2$
69

70     }
71
72     public HttpResponse execute(HttpRequest request, HttpConnection con) throws IOException JavaDoc {
73
74         HttpResponse response = super.execute(request, con);
75
76         // #ifdef DEBUG
77
log.info(MessageFormat.format(Messages.getString("ConnectMethod.httpConnect"), new Object JavaDoc[] { hostname, new Integer JavaDoc(port), new Integer JavaDoc(response.getStatus()), response.getReason() })); //$NON-NLS-1$
78

79         for (Enumeration JavaDoc e = response.getHeaderFieldNames(); e.hasMoreElements();) {
80             String JavaDoc name = (String JavaDoc) e.nextElement();
81             log.info(name + ": " + response.getHeaderField(name)); //$NON-NLS-1$
82
}
83         // #endif
84

85         if (response.getStatus() == 200) {
86
87             if (targetIsSecure)
88                 ((SocketWithLayeredTransport) response.getConnection().getSocket()).pushTransport(SSLTransportFactory.newInstance());
89
90         }
91
92         return response;
93     }
94
95 }
96
Popular Tags