KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpInvoker


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

17 package org.apache.servicemix.components.http;
18
19 import org.apache.commons.httpclient.HostConfiguration;
20 import org.apache.commons.httpclient.HttpClient;
21 import org.apache.commons.httpclient.HttpStatus;
22 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
23 import org.apache.commons.httpclient.URI;
24 import org.apache.commons.httpclient.methods.PostMethod;
25 import org.apache.servicemix.MessageExchangeListener;
26 import org.apache.servicemix.components.util.TransformComponentSupport;
27
28 import javax.jbi.JBIException;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessagingException;
31 import javax.jbi.messaging.NormalizedMessage;
32
33 /**
34  * Performs HTTP client invocations on a remote HTTP site.
35  *
36  * @version $Revision: 426415 $
37  */

38 public class HttpInvoker extends TransformComponentSupport implements MessageExchangeListener {
39
40     protected HttpClientMarshaler marshaler = new HttpClientMarshaler();
41     protected MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
42     protected HttpClient httpClient = new HttpClient(connectionManager);
43     protected HostConfiguration hostConfiguration = new HostConfiguration();
44     protected String JavaDoc url;
45     protected boolean defaultInOut = true;
46
47     public void stop() throws JBIException {
48         super.stop();
49         connectionManager.shutdown();
50     }
51     
52     public HttpInvoker() {
53         // By default, do not forward anything
54
setCopyAttachments(false);
55         setCopyProperties(false);
56     }
57
58     protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
59         PostMethod method = new PostMethod(url);
60         try {
61             marshaler.fromNMS(method, exchange, in);
62             if (url != null) {
63                 hostConfiguration.setHost(new URI(url, false));
64             }
65             int response = httpClient.executeMethod(hostConfiguration, method);
66
67             if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) {
68                 throw new InvalidStatusResponseException(response);
69             }
70
71             // now lets grab the output and set it on the out message
72
if (defaultInOut) {
73                 marshaler.toNMS(out, method);
74             }
75             return defaultInOut;
76         }
77         catch (Exception JavaDoc e) {
78             throw new MessagingException("Error executing http request", e);
79         }
80         finally {
81             method.releaseConnection();
82         }
83     }
84
85     public HttpClient getHttpClient() {
86         return httpClient;
87     }
88
89     public void setHttpClient(HttpClient httpClient) {
90         this.httpClient = httpClient;
91     }
92
93     public String JavaDoc getUrl() {
94         return url;
95     }
96
97     public void setUrl(String JavaDoc url) {
98         this.url = url;
99     }
100
101     public boolean isDefaultInOut() {
102         return defaultInOut;
103     }
104
105     public void setDefaultInOut(boolean defaultInOut) {
106         this.defaultInOut = defaultInOut;
107     }
108
109     public HttpClientMarshaler getMarshaler() {
110         return marshaler;
111     }
112
113     public void setMarshaler(HttpClientMarshaler marshaler) {
114         this.marshaler = marshaler;
115     }
116 }
117
Popular Tags