1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.plugin.htmlunit;
20
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import net.sf.jameleon.SessionTag;
26 import net.sf.jameleon.function.ContextHelper;
27 import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitDelegate;
28 import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitHelper;
29 import net.sf.jameleon.plugin.htmlunit.util.TrustEverythingSSLProtocolSocketFactory;
30 import net.sf.jameleon.util.Configurator;
31 import net.sf.jameleon.util.StateStorer;
32
33 import org.apache.commons.httpclient.protocol.Protocol;
34
35 import com.gargoylesoftware.htmlunit.WebClient;
36 import com.gargoylesoftware.htmlunit.WebWindow;
37 import com.gargoylesoftware.htmlunit.html.HtmlForm;
38
39 /***
40 * A Session tag for the HtmlUnit plug-in.
41 *
42 * An example of its use might:
43 *
44 * <pre><source>
45 * <testcase xmlns="jelly:jameleon">
46 * <htmlunit-session baseUrl="http://www.google.com" beginSession="true">
47 * <htmlunit-validate
48 * functionId="Check that title is Google."
49 * title="Google"/>
50 * </htmlunit-session>
51 * </testcase>
52 * </source></pre>
53 *
54 * @jameleon.function name="htmlunit-session"
55 */
56 public class HtmlUnitSessionTag extends SessionTag implements HtmlUnitDelegate{
57
58 public static final String BASE_URL = "baseUrl";
59 public static final String BEGIN_AT = "beginAt";
60 protected WebClient session;
61 protected HtmlUnitHelper htmlHelper;
62 /***
63 * Enable or disable validity checking of SSL certificates.
64 * If "false", invalid SSL certs will be accepted. If not set, the default is "true".
65 * @jameleon.attribute contextName="htmlUnitEnableSslCertCheck"
66 */
67 protected Boolean enableSslCertCheck;
68 /***
69 * The url to use when starting the browser.
70 * This can be the entire URL including the path or not, but it must start with the
71 * protocol (e.g. http://some.domain.com)
72 * @jameleon.attribute contextName="baseUrl"
73 */
74 protected String baseUrl;
75 /***
76 * If set, this would be the path after the protocol and/or domain.
77 * (e.g. /some/path/to/a/file.html)
78 * @jameleon.attribute contextName="beginAt"
79 */
80 protected String beginAt;
81 /***
82 * The port to use to connect to https. Usually leaving it as the default should do.
83 * @jameleon.attribute contextName="htmlUnitEnableSslCertCheckPort" default="443"
84 */
85 protected Integer enableSslCertCheckPort;
86
87 public void setUpSession(){
88 htmlHelper = new HtmlUnitHelper(this);
89 session = new WebClient();
90 if (!isEnableSslCertCheck()) {
91 Protocol easyhttps = new Protocol("https", new TrustEverythingSSLProtocolSocketFactory(), getEnableSslCertCheckPort());
92 Protocol.registerProtocol("https", easyhttps);
93 }
94 }
95
96 public void tearDownSession(){
97 session = null;
98 }
99
100 /***
101 * Deregister this tag as a storable
102 */
103 protected void deregisterStorable(){
104 StateStorer.getInstance().removeStorable(this);
105 }
106
107 /***
108 * Gets the url to request
109 *
110 * @return The URL to use in startApplication.
111 */
112 protected String getRequestUrl(){
113 String url = baseUrl;
114 if (beginAt != null) {
115 url += beginAt;
116 }
117 return url;
118 }
119
120 /***
121 * Gets the current WebClient or the handle on the session
122 *
123 * @return the current browser.
124 */
125 public WebClient getSession(){
126 return session;
127 }
128
129 /***
130 * Return the port on which to register the enableSslCertCheck on.
131 */
132 public int getEnableSslCertCheckPort() {
133 return ContextHelper.getValueAsIntWithConfig(context, "htmlUnitEnableSslCertCheckPort", "htmlUnitEnableSslCertCheckPort", enableSslCertCheckPort.intValue());
134 }
135
136 /***
137 * Query whether SSL cert validity checking is enabled.
138 * @return - If true, an exception will be thrown when an invalid SSL cert is encountered.
139 * If false, invalid SSL certs will be accepted.
140 */
141 public boolean isEnableSslCertCheck() {
142 if (enableSslCertCheck == null) {
143 String enableS = Configurator.getInstance().getValue("htmlUnitEnableSslCertCheck", "true");
144 enableSslCertCheck = new Boolean(enableS);
145 }
146 return enableSslCertCheck.booleanValue();
147 }
148
149 /***
150 * Register this tag as a storable
151 */
152 protected void registerStorable(){
153 StateStorer.getInstance().addStorable(this);
154 }
155
156 /***
157 * Loads the URL given by <b>beginAt</b> and <b>baseUrl</b>.
158 */
159 public void startApplication(){
160 registerStorable();
161 try{
162 URL url = new URL(getRequestUrl());
163 session.getPage(url);
164 }catch (MalformedURLException mue){
165 throw new RuntimeException("The provide url: '"+getRequestUrl()+"' is invalid", mue);
166 }catch (IOException ioe){
167 throw new RuntimeException("Can not connect to '"+getRequestUrl()+"'.", ioe);
168 }finally{
169 deregisterStorable();
170 }
171 }
172
173 public void store(String fName, int event) throws IOException{
174 htmlHelper.store(fName);
175 }
176
177 /***
178 * Gets a handle on the current web window.
179 *
180 * @return WebWindow
181 */
182 public WebWindow getCurrentWebWindow(){
183 WebWindow currentWindow = null;
184 if (getSession() != null) {
185 currentWindow = getSession().getCurrentWindow();
186 }
187 return currentWindow;
188 }
189
190 /***
191 * Gets a handle on the WebClient instance used in this session
192 *
193 * @return WebClient
194 */
195 public WebClient getWebClient(){
196 return session;
197 }
198
199 /***
200 * Gets a handle on the WebClient instance used in this session
201 *
202 * @return WebClient
203 */
204 public HtmlForm getWorkingForm(){
205 return null;
206 }
207
208 }