KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Field JavaDoc;
41
42 import org.apache.commons.jelly.JellyTagException;
43 import org.apache.commons.jelly.XMLOutput;
44
45 import com.gargoylesoftware.htmlunit.BrowserVersion;
46 import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
47 import com.gargoylesoftware.htmlunit.WebClient;
48
49 /**
50  * Jelly tag "webClient".
51  *
52  * @version $Revision: 100 $
53  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54  */

55 public class WebClientTag extends HtmlUnitTagSupport {
56     private WebClient webClient_;
57     private String JavaDoc userId_;
58     private String JavaDoc password_;
59     private String JavaDoc browserVersionName_;
60
61     /**
62      * Create an instance
63      */

64     public WebClientTag() {
65     }
66
67
68     /**
69      * Process the tag
70      *
71      * @param xmlOutput to write output
72      * @throws JellyTagException when any error occurs
73      */

74     public void doTag(final XMLOutput xmlOutput) throws JellyTagException {
75         final BrowserVersion browserVersion = getBrowserVersion();
76         if( browserVersion == null ) {
77             webClient_ = new WebClient();
78         }
79         else {
80             webClient_ = new WebClient(browserVersion);
81         }
82         if( userId_ != null || password_ != null ) {
83             if( userId_ == null || password_ == null ) {
84                 throw new JellyTagException("userid and password must either both be set or neither set");
85             }
86             ((DefaultCredentialsProvider) webClient_.getCredentialsProvider()).addCredentials( userId_, password_ );
87         }
88
89         final String JavaDoc varName = getVarValueOrNull();
90         if( varName != null ) {
91             getContext().setVariable(varName, webClient_);
92         }
93         invokeBody(xmlOutput);
94     }
95
96
97     /**
98      * Callback from Jelly to set the value of the browserVersion attribute.
99      * @param browserVersion The new value.
100      */

101     public void setBrowserVersion( final String JavaDoc browserVersion ) {
102         browserVersionName_ = browserVersion;
103     }
104
105
106     private BrowserVersion getBrowserVersion() {
107         if( browserVersionName_ == null ) {
108             return null;
109         }
110
111         try {
112             final Field JavaDoc field = BrowserVersion.class.getDeclaredField(browserVersionName_);
113             return (BrowserVersion)field.get(null);
114         }
115         catch( final NoSuchFieldException JavaDoc e ) {
116             return null;
117         }
118         catch( final IllegalAccessException JavaDoc e ) {
119             // All the legitimate constants are accessible so this exception
120
// would mean that something else has been specified.
121
return null;
122         }
123     }
124
125     /**
126      * Return the WebClient created by this tag.
127      * @return The web client
128      */

129     public WebClient getWebClient() {
130         return webClient_;
131     }
132
133
134     /**
135      * Set the userid attribute
136      * @param userid the new value
137      */

138     public void setUserid( final String JavaDoc userid ) {
139         userId_ = userid;
140     }
141
142
143     /**
144      * Set the password attribute
145      * @param password the new value
146      */

147     public void setPassword( final String JavaDoc password ) {
148         password_ = password;
149     }
150 }
151
Popular Tags