View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.http;
18  
19  import org.apache.commons.httpclient.HttpClient;
20  import org.apache.commons.jelly.JellyTagException;
21  import org.apache.commons.jelly.TagSupport;
22  import org.apache.commons.jelly.XMLOutput;
23  
24  /***
25   * A http session. This is the container for data shared across requests
26   *
27   * @author dion
28   */
29  public class SessionTag extends TagSupport {
30  
31      /*** default host for requests */
32      private String _host;
33      /*** default port for requests */
34      private String _port;
35      /*** Proxy details for requests */
36      private Proxy _proxy = new Proxy();
37      /*** whether the default is for secure comms */
38      private boolean _secure;
39      /*** the browser identifier */
40      private String _userAgent;
41      /*** strict compliance */
42      private boolean _strictMode = false;
43  
44      /*** http client used to store state and execute requests */
45      private HttpClient _httpClient;
46  
47      /***
48       * Creates a new instance of SessionTag
49       */
50      public SessionTag() {
51      }
52  
53      /***
54       * Process the tag
55       *
56       * @param xmlOutput to write output
57       * @throws Exception when any error occurs
58       */
59      public void doTag(XMLOutput xmlOutput) throws JellyTagException {
60          if (_httpClient == null)
61          {
62              _httpClient = new HttpClient();
63          }
64          
65          if (isProxyAvailable()) {
66              _httpClient.getHostConfiguration().setProxy(getProxyHost(), getProxyPort());
67          }
68  
69          invokeBody(xmlOutput);
70      }
71  
72      /***
73       * Getter for property httpClient.
74       *
75       * @return Value of property httpClient.
76       */
77      public HttpClient getHttpClient() {
78          return _httpClient;
79      }
80  
81      /***
82       * Setter for property httpClient.
83       *
84       * @param httpClient New value of property httpClient.
85       */
86      public void setHttpClient(HttpClient httpClient) {
87          _httpClient = httpClient;
88      }
89      /***
90       * Tests whether the {@link #getProxy() proxy} is ready for use
91       *
92       * @return true if the {@link #getProxy() proxy} is configured for use
93       */
94      public boolean isProxyAvailable() {
95          return getProxy() != null && getProxy().getHost() != null
96              && getProxy().getPort() != Proxy.PORT_UNSPECIFIED;
97      }
98  
99      /***
100      * Helper method for proxy host property
101      *
102      * @return the {@link #getProxy() proxy's} host property
103      */
104     public String getProxyHost() {
105         return getProxy().getHost();
106     }
107 
108     /***
109      * Helper method for proxy <code>host</code> property
110      *
111      * @param host the {@link #getProxy() proxy's} host property
112      */
113     public void setProxyHost(String host) {
114         getProxy().setHost(host);
115     }
116 
117     /***
118      * Helper method for proxy <code>port</code> property
119      *
120      * @return the {@link #getProxy() proxy's} port property
121      */
122     public int getProxyPort() {
123         return getProxy().getPort();
124     }
125 
126     /***
127      * Helper method for proxy <code>port</code> property
128      *
129      * @param port the {@link #getProxy() proxy's} port property
130      */
131     public void setProxyPort(int port) {
132         getProxy().setPort(port);
133     }
134 
135     /***
136      * Getter for property host.
137      *
138      * @return Value of property host.
139      */
140     public String getHost() {
141         return _host;
142     }
143 
144     /***
145      * Setter for property host.
146      *
147      * @param host New value of property host.
148      */
149     public void setHost(String host) {
150         _host = host;
151     }
152 
153     /*** Getter for property port.
154      * @return Value of property port.
155      */
156     public String getPort() {
157         return _port;
158     }
159 
160     /*** Setter for property port.
161      * @param port New value of property port.
162      */
163     public void setPort(String port) {
164         _port = port;
165     }
166 
167     /***
168      * Getter for property proxy.
169      *
170      * @return Value of property proxy.
171      */
172     public Proxy getProxy() {
173         return _proxy;
174     }
175 
176     /***
177      * Setter for property proxy.
178      *
179      * @param proxy New value of property proxy.
180      */
181     public void setProxy(Proxy proxy) {
182         _proxy = proxy;
183     }
184 
185     /***
186      * Getter for property secure.
187      *
188      * @return Value of property secure.
189      */
190     public boolean isSecure() {
191         return _secure;
192     }
193 
194     /***
195      * Setter for property secure.
196      *
197      * @param secure New value of property secure.
198      */
199     public void setSecure(boolean secure) {
200         _secure = secure;
201     }
202 
203     /*** Getter for property userAgent.
204      * @return Value of property userAgent.
205      */
206     public String getUserAgent() {
207         return _userAgent;
208     }
209 
210     /*** Setter for property userAgent.
211      * @param userAgent New value of property userAgent.
212      */
213     public void setUserAgent(String userAgent) {
214         _userAgent = userAgent;
215     }
216 
217     /*** Getter for property strictMode.
218      * @return Value of property strictMode.
219      */
220     public boolean isStrictMode() {
221         return _strictMode;
222     }
223 
224     /*** Setter for property strictMode.
225      * @param strictMode New value of property strictMode.
226      */
227     public void setStrictMode(boolean strictMode) {
228         _strictMode = strictMode;
229     }
230 
231 }