KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > jelly > GetPageTag


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.jelly;
39
40 import java.io.IOException JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.List JavaDoc;
46
47 import org.apache.commons.jelly.JellyContext;
48 import org.apache.commons.jelly.JellyTagException;
49 import org.apache.commons.jelly.XMLOutput;
50
51 import com.gargoylesoftware.htmlunit.Page;
52 import com.gargoylesoftware.htmlunit.SubmitMethod;
53 import com.gargoylesoftware.htmlunit.WebClient;
54 import com.gargoylesoftware.htmlunit.WebRequestSettings;
55
56 /**
57  * Jelly tag to load a page from a server.
58  *
59  * @version $Revision: 100 $
60  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
61  * @author <a HREF="mailto:cse@dynabean.com">Christian Sell</a>
62  */

63 public class GetPageTag extends HtmlUnitTagSupport {
64     private String JavaDoc url_ = null;
65     private List JavaDoc parameters_ = null;
66     private String JavaDoc method_ = "get";
67     private String JavaDoc webClientName_;
68
69     /**
70      * Create an instance
71      */

72     public GetPageTag() {
73     }
74
75
76     /**
77      * Process the tag
78      * @param xmlOutput The xml output
79      * @throws JellyTagException If a problem occurs
80      */

81     public void doTag(final XMLOutput xmlOutput) throws JellyTagException {
82         invokeBody(xmlOutput);
83
84         final JellyContext jellyContext = getContext();
85
86         final WebClient webClient;
87         if( webClientName_ != null ) {
88             webClient = (WebClient)jellyContext.getVariable(webClientName_);
89             if( webClient == null ) {
90                 throw new JellyTagException("No webclient found for name ["+webClientName_+"]");
91             }
92         }
93         else {
94             webClient = getWebClient();
95         }
96
97         final Page page;
98         try {
99             final WebRequestSettings settings = new WebRequestSettings(getUrl(), getSubmitMethod());
100             settings.setRequestParameters(getParameters());
101             page = webClient.getPage(settings);
102             jellyContext.setVariable( getVarValueOrDie(), page );
103         }
104         catch( final IOException JavaDoc e ) {
105             throw new JellyTagException(e);
106         }
107     }
108
109
110     /**
111      * Callback from Jelly to set the value of the url attribute.
112      * @param url The new value.
113      */

114     public void setUri( final String JavaDoc url ) {
115         url_ = url;
116     }
117
118
119     /**
120      * Return the value of the url attribute or throw an exception if it hasn't been set.
121      * @return The URL
122      * @throws JellyTagException If the attribute hasn't been set or the url is malformed.
123      */

124     public URL JavaDoc getUrl() throws JellyTagException {
125         if( url_ == null ) {
126             throw new JellyTagException("url attribute is mandatory");
127         }
128
129         try {
130             return new URL JavaDoc(url_);
131         }
132         catch( final MalformedURLException JavaDoc e ) {
133             throw new JellyTagException("url attribute is malformed: "+url_);
134         }
135     }
136
137
138     /**
139      * Add a parameter to the request.
140      * @param parameter the new parameter
141      */

142     public synchronized void addParameter( final String JavaDoc parameter ) {
143         if( parameters_ == null ) {
144             parameters_ = new ArrayList JavaDoc();
145         }
146         parameters_.add(parameter);
147     }
148
149
150     /**
151      * Return the list of parameters.
152      * @return The list of parameters
153      */

154     public synchronized List JavaDoc getParameters() {
155         if( parameters_ == null ) {
156             return Collections.EMPTY_LIST;
157         }
158         else {
159             return parameters_;
160         }
161     }
162
163
164     /**
165      * Callback from Jelly to set the value of the method attribute.
166      * @param method The new value.
167      */

168     public void setMethod( final String JavaDoc method ) {
169         method_ = method;
170     }
171
172
173     /**
174      * Callback from Jelly to set the value of the method attribute.
175      * @param webClientName The new value.
176      */

177     public void setWebclient( final String JavaDoc webClientName ) {
178         webClientName_ = webClientName;
179     }
180
181
182     /**
183      * Return the submit method to be used when retrieving the page.
184      * @return The submit method
185      * @throws JellyTagException If the submit method could not be determined.
186      */

187     public SubmitMethod getSubmitMethod() throws JellyTagException {
188         try {
189             return SubmitMethod.getInstance(method_);
190         }
191         catch( final IllegalArgumentException JavaDoc e ) {
192             // Provide a nicer error message
193
throw new JellyTagException("Value of method attribute is not a valid submit method: "+method_);
194         }
195     }
196 }
197
Popular Tags