KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpConnector


1 /*
2  * $Id: HttpConnector.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.providers.tcp.TcpConnector;
16 import org.mule.umo.UMOComponent;
17 import org.mule.umo.endpoint.UMOEndpoint;
18 import org.mule.umo.provider.UMOConnector;
19 import org.mule.umo.provider.UMOMessageReceiver;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * <code>HttpConnector</code> provides a way of receiving and sending http requests
27  * and responses. The UMOConnector itself handles dispatching http requests. The
28  * <code>HttpMessageReceiver</code> handles the receiving requests and processing
29  * of headers This endpoint recognises the following properties - <p/>
30  * <ul>
31  * <li>hostname - The hostname to send and receive http requests</li>
32  * <li>port - The port to listen on. The industry standard is 80 and if this propert
33  * is not set it will default to 80</li>
34  * <li>proxyHostname - If you access the web through a proxy, this holds the server
35  * address</li>
36  * <li>proxyPort - The port the proxy is configured on</li>
37  * <li>proxyUsername - If the proxy requires authentication supply a username</li>
38  * <li>proxyPassword - If the proxy requires authentication supply a password</li>
39  * </ul>
40  *
41  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
42  * @version $Revision: 3798 $
43  */

44
45 public class HttpConnector extends TcpConnector
46 {
47
48     /**
49      * Event property to pass back the status for the response
50      */

51     public static final String JavaDoc HTTP_STATUS_PROPERTY = "http.status";
52     public static final String JavaDoc HTTP_VERSION_PROPERTY = "http.version";
53     public static final String JavaDoc HTTP_CUSTOM_HEADERS_MAP_PROPERTY = "http.custom.headers";
54     public static final String JavaDoc HTTP_METHOD_PROPERTY = "http.method";
55     public static final String JavaDoc HTTP_REQUEST_PROPERTY = "http.request";
56     public static final String JavaDoc HTTP_PARAMS = "http.params";
57     public static final String JavaDoc HTTP_GET_BODY_PARAM_PROPERTY = "http.get.body.param";
58     public static final String JavaDoc DEFAULT_HTTP_GET_BODY_PARAM_PROPERTY = "body";
59     public static final String JavaDoc HTTP_POST_BODY_PARAM_PROPERTY = "http.post.body.param";
60
61     public static final String JavaDoc HTTP_COOKIE_SPEC_PROPERTY = "cookieSpec";
62     public static final String JavaDoc HTTP_COOKIES_PROPERTY = "cookies";
63     public static final String JavaDoc HTTP_ENABLE_COOKIES_PROPERTY = "enableCookies";
64
65     public static final String JavaDoc COOKIE_SPEC_NETSCAPE = "netscape";
66     public static final String JavaDoc COOKIE_SPEC_RFC2109 = "rcf2109";
67
68     private String JavaDoc proxyHostname = null;
69
70     private int proxyPort = HttpConstants.DEFAULT_HTTP_PORT;
71
72     private String JavaDoc proxyUsername = null;
73
74     private String JavaDoc proxyPassword = null;
75
76     private String JavaDoc cookieSpec;
77
78     private boolean enableCookies = false;
79
80     /**
81      * @see UMOConnector#registerListener(UMOComponent, UMOEndpoint)
82      */

83     public UMOMessageReceiver registerListener(UMOComponent component, UMOEndpoint endpoint) throws Exception JavaDoc
84     {
85         if (endpoint != null)
86         {
87             Map JavaDoc endpointProperties = endpoint.getProperties();
88             if (endpointProperties != null)
89             {
90                 // normalize properties for HTTP
91
Map JavaDoc newProperties = new HashMap JavaDoc(endpointProperties.size());
92                 for (Iterator JavaDoc entries = endpointProperties.entrySet().iterator(); entries.hasNext();)
93                 {
94                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
95                     Object JavaDoc key = entry.getKey();
96                     Object JavaDoc normalizedKey = HttpConstants.ALL_HEADER_NAMES.get(key);
97                     if (normalizedKey != null)
98                     {
99                         // normalized property exists
100
key = normalizedKey;
101                     }
102                     newProperties.put(key, entry.getValue());
103                 }
104                 // set normalized properties back on the endpoint
105
endpoint.setProperties(newProperties);
106             }
107         }
108         // proceed as usual
109
return super.registerListener(component, endpoint);
110     }
111
112     /**
113      * The method determines the key used to store the receiver against.
114      *
115      * @param component the component for which the endpoint is being registered
116      * @param endpoint the endpoint being registered for the component
117      * @return the key to store the newly created receiver against
118      */

119     protected Object JavaDoc getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
120     {
121         String JavaDoc key = endpoint.getEndpointURI().toString();
122         int i = key.indexOf('?');
123         if (i > -1)
124         {
125             key = key.substring(0, i);
126         }
127         return key;
128     }
129
130     /**
131      * @see org.mule.umo.provider.UMOConnector#getProtocol()
132      */

133     public String JavaDoc getProtocol()
134     {
135         return "http";
136     }
137
138     /**
139      * @return
140      */

141     public String JavaDoc getProxyHostname()
142     {
143         return proxyHostname;
144     }
145
146     /**
147      * @return
148      */

149     public String JavaDoc getProxyPassword()
150     {
151         return proxyPassword;
152     }
153
154     /**
155      * @return
156      */

157     public int getProxyPort()
158     {
159         return proxyPort;
160     }
161
162     /**
163      * @return
164      */

165     public String JavaDoc getProxyUsername()
166     {
167         return proxyUsername;
168     }
169
170     /**
171      * @param host
172      */

173     public void setProxyHostname(String JavaDoc host)
174     {
175         proxyHostname = host;
176     }
177
178     /**
179      * @param string
180      */

181     public void setProxyPassword(String JavaDoc string)
182     {
183         proxyPassword = string;
184     }
185
186     /**
187      * @param port
188      */

189     public void setProxyPort(int port)
190     {
191         proxyPort = port;
192     }
193
194     /**
195      * @param string
196      */

197     public void setProxyUsername(String JavaDoc string)
198     {
199         proxyUsername = string;
200     }
201
202     public Map JavaDoc getReceivers()
203     {
204         return this.receivers;
205     }
206
207     public String JavaDoc getCookieSpec()
208     {
209         return cookieSpec;
210     }
211
212     public void setCookieSpec(String JavaDoc cookieSpec)
213     {
214         if (!(cookieSpec.equalsIgnoreCase(COOKIE_SPEC_NETSCAPE) && cookieSpec.equalsIgnoreCase(COOKIE_SPEC_RFC2109)))
215         {
216             throw new IllegalArgumentException JavaDoc(new Message(Messages.PROPERTY_X_HAS_INVALID_VALUE_X,
217                 "cookieSpec", cookieSpec).toString());
218         }
219         this.cookieSpec = cookieSpec;
220     }
221
222     public boolean isEnableCookies()
223     {
224         return enableCookies;
225     }
226
227     public void setEnableCookies(boolean enableCookies)
228     {
229         this.enableCookies = enableCookies;
230     }
231 }
232
Popular Tags