KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > http > HttpClientTransport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.http;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InterruptedIOException JavaDoc;
23 import java.net.URI JavaDoc;
24
25 import org.apache.activemq.transport.FutureResponse;
26 import org.apache.activemq.transport.util.TextWireFormat;
27 import org.apache.activemq.util.ByteArrayInputStream;
28 import org.apache.activemq.util.IOExceptionSupport;
29 import org.apache.activemq.util.IdGenerator;
30 import org.apache.activemq.util.ServiceStopper;
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.HttpMethod;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.commons.httpclient.methods.GetMethod;
35 import org.apache.commons.httpclient.methods.HeadMethod;
36 import org.apache.commons.httpclient.methods.PostMethod;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * A HTTP {@link org.apache.activemq.transport.TransportChannel} which uses the <a
42  * HREF="http://jakarta.apache.org/commons/httpclient/">commons-httpclient</a>
43  * library
44  *
45  * @version $Revision$
46  */

47 public class HttpClientTransport extends HttpTransportSupport {
48     private static final Log log = LogFactory.getLog(HttpClientTransport.class);
49     public static final int MAX_CLIENT_TIMEOUT = 30000;
50
51     private static final IdGenerator clientIdGenerator = new IdGenerator();
52
53     private HttpClient sendHttpClient;
54     private HttpClient receiveHttpClient;
55     
56     private final String JavaDoc clientID = clientIdGenerator.generateId();
57     private boolean trace;
58     
59     public HttpClientTransport(TextWireFormat wireFormat, URI JavaDoc remoteUrl) {
60         super(wireFormat, remoteUrl);
61     }
62
63     public FutureResponse asyncRequest(Object JavaDoc command) throws IOException JavaDoc {
64         return null;
65     }
66
67     public void oneway(Object JavaDoc command) throws IOException JavaDoc {
68         
69         if( isStopped() ) {
70             throw new IOException JavaDoc("stopped.");
71         }
72         
73         PostMethod httpMethod = new PostMethod(getRemoteUrl().toString());
74         configureMethod(httpMethod);
75         String JavaDoc data = getTextWireFormat().marshalText(command);
76         byte[] bytes = data.getBytes("UTF-8");
77         httpMethod.setRequestBody(new ByteArrayInputStream(bytes));
78         
79         try {
80             
81             HttpClient client = getSendHttpClient();
82             client.setTimeout(MAX_CLIENT_TIMEOUT);
83             int answer = client.executeMethod(httpMethod);
84             if (answer != HttpStatus.SC_OK) {
85                 throw new IOException JavaDoc("Failed to post command: " + command + " as response was: " + answer);
86             }
87                         
88 // checkSession(httpMethod);
89
} catch (IOException JavaDoc e) {
90             throw IOExceptionSupport.create("Could not post command: " + command + " due to: " + e, e);
91         } finally {
92             httpMethod.getResponseBody();
93             httpMethod.releaseConnection();
94         }
95     }
96
97     public Object JavaDoc request(Object JavaDoc command) throws IOException JavaDoc {
98         return null;
99     }
100
101     public void run() {
102         
103         log.trace("HTTP GET consumer thread starting: " + this);
104         HttpClient httpClient = getReceiveHttpClient();
105         URI JavaDoc remoteUrl = getRemoteUrl();
106                 
107         while ( !isStopped() && !isStopping() ) {
108
109             GetMethod httpMethod = new GetMethod(remoteUrl.toString());
110             configureMethod(httpMethod);
111
112             try {
113                 int answer = httpClient.executeMethod(httpMethod);
114                 if (answer != HttpStatus.SC_OK) {
115                     if (answer == HttpStatus.SC_REQUEST_TIMEOUT) {
116                         log.debug("GET timed out");
117                         try {
118                             Thread.sleep(1000);
119                         } catch (InterruptedException JavaDoc e) {
120                             onException(new InterruptedIOException JavaDoc());
121                             break;
122                         }
123                     }
124                     else {
125                         onException(new IOException JavaDoc("Failed to perform GET on: " + remoteUrl + " as response was: " + answer));
126                         break;
127                     }
128                 }
129                 else {
130 // checkSession(httpMethod);
131
DataInputStream JavaDoc stream = new DataInputStream JavaDoc(httpMethod.getResponseBodyAsStream());
132                     Object JavaDoc command = (Object JavaDoc) getTextWireFormat().unmarshal(stream);
133                     if (command == null) {
134                         log.warn("Received null command from url: " + remoteUrl);
135                     } else {
136                         doConsume(command);
137                     }
138                 }
139             }
140             catch (IOException JavaDoc e) {
141                 onException(IOExceptionSupport.create("Failed to perform GET on: " + remoteUrl+" Reason: "+e.getMessage(),e));
142                 break;
143             } finally {
144                 httpMethod.getResponseBody();
145                 httpMethod.releaseConnection();
146             }
147         }
148     }
149
150     // Properties
151
// -------------------------------------------------------------------------
152
public HttpClient getSendHttpClient() {
153         if (sendHttpClient == null) {
154             sendHttpClient = createHttpClient();
155         }
156         return sendHttpClient;
157     }
158
159     public void setSendHttpClient(HttpClient sendHttpClient) {
160         this.sendHttpClient = sendHttpClient;
161     }
162
163     public HttpClient getReceiveHttpClient() {
164         if (receiveHttpClient == null) {
165             receiveHttpClient = createHttpClient();
166         }
167         return receiveHttpClient;
168     }
169
170     public void setReceiveHttpClient(HttpClient receiveHttpClient) {
171         this.receiveHttpClient = receiveHttpClient;
172     }
173
174     // Implementation methods
175
// -------------------------------------------------------------------------
176
protected void doStart() throws Exception JavaDoc {
177         
178         log.trace("HTTP GET consumer thread starting: " + this);
179         HttpClient httpClient = getReceiveHttpClient();
180         URI JavaDoc remoteUrl = getRemoteUrl();
181                 
182         HeadMethod httpMethod = new HeadMethod(remoteUrl.toString());
183         configureMethod(httpMethod);
184
185         int answer = httpClient.executeMethod(httpMethod);
186         if (answer != HttpStatus.SC_OK) {
187             throw new IOException JavaDoc("Failed to perform GET on: " + remoteUrl + " as response was: " + answer);
188         }
189         
190         super.doStart();
191     }
192     
193     protected void doStop(ServiceStopper stopper) throws Exception JavaDoc {
194     }
195
196     protected HttpClient createHttpClient() {
197         HttpClient client = new HttpClient();
198         if (getProxyHost() != null) {
199             client.getHostConfiguration().setProxy(getProxyHost(), getProxyPort());
200         }
201         return client;
202     }
203
204     protected void configureMethod(HttpMethod method) {
205         method.setRequestHeader("clientID", clientID);
206     }
207
208     public boolean isTrace() {
209         return trace;
210     }
211
212     public void setTrace(boolean trace) {
213         this.trace = trace;
214     }
215
216 // protected void checkSession(HttpMethod client) {
217
// Header header = client.getRequestHeader("Set-Cookie");
218
// if (header != null) {
219
// String set_cookie = header.getValue();
220
//
221
// if (set_cookie != null && set_cookie.startsWith("JSESSIONID=")) {
222
// String[] bits = set_cookie.split("[=;]");
223
// sessionID = bits[1];
224
// }
225
// }
226
// }
227

228 }
229
Popular Tags