KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormLoginDemo


1 /*
2  * ====================================================================
3  *
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements. See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ====================================================================
19  *
20  * This software consists of voluntary contributions made by many
21  * individuals on behalf of the Apache Software Foundation. For more
22  * information on the Apache Software Foundation, please see
23  * <http://www.apache.org/>.
24  *
25  * [Additional notices, if required by prior licensing conditions]
26  *
27  */

28
29 import org.apache.commons.httpclient.*;
30 import org.apache.commons.httpclient.cookie.CookiePolicy;
31 import org.apache.commons.httpclient.cookie.CookieSpec;
32 import org.apache.commons.httpclient.methods.*;
33
34 /**
35  * <p>
36  * A example that demonstrates how HttpClient APIs can be used to perform
37  * form-based logon.
38  * </p>
39  *
40  * @author Oleg Kalnichevski
41  *
42  */

43 public class FormLoginDemo
44 {
45     static final String JavaDoc LOGON_SITE = "developer.java.sun.com";
46     static final int LOGON_PORT = 80;
47
48     public FormLoginDemo() {
49         super();
50     }
51
52     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
53
54         HttpClient client = new HttpClient();
55         client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
56         client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
57         // 'developer.java.sun.com' has cookie compliance problems
58
// Their session cookie's domain attribute is in violation of the RFC2109
59
// We have to resort to using compatibility cookie policy
60

61         GetMethod authget = new GetMethod("/servlet/SessionServlet");
62
63         client.executeMethod(authget);
64         System.out.println("Login form get: " + authget.getStatusLine().toString());
65         // release any connection resources used by the method
66
authget.releaseConnection();
67         // See if we got any cookies
68
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
69         Cookie[] initcookies = cookiespec.match(
70             LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
71         System.out.println("Initial set of cookies:");
72         if (initcookies.length == 0) {
73             System.out.println("None");
74         } else {
75             for (int i = 0; i < initcookies.length; i++) {
76                 System.out.println("- " + initcookies[i].toString());
77             }
78         }
79         
80         PostMethod authpost = new PostMethod("/servlet/SessionServlet");
81         // Prepare login parameters
82
NameValuePair action = new NameValuePair("action", "login");
83         NameValuePair url = new NameValuePair("url", "/index.html");
84         NameValuePair userid = new NameValuePair("UserId", "userid");
85         NameValuePair password = new NameValuePair("Password", "password");
86         authpost.setRequestBody(
87           new NameValuePair[] {action, url, userid, password});
88         
89         client.executeMethod(authpost);
90         System.out.println("Login form post: " + authpost.getStatusLine().toString());
91         // release any connection resources used by the method
92
authpost.releaseConnection();
93         // See if we got any cookies
94
// The only way of telling whether logon succeeded is
95
// by finding a session cookie
96
Cookie[] logoncookies = cookiespec.match(
97             LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
98         System.out.println("Logon cookies:");
99         if (logoncookies.length == 0) {
100             System.out.println("None");
101         } else {
102             for (int i = 0; i < logoncookies.length; i++) {
103                 System.out.println("- " + logoncookies[i].toString());
104             }
105         }
106         // Usually a successful form-based login results in a redicrect to
107
// another url
108
int statuscode = authpost.getStatusCode();
109         if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
110             (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
111             (statuscode == HttpStatus.SC_SEE_OTHER) ||
112             (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
113             Header header = authpost.getResponseHeader("location");
114             if (header != null) {
115                 String JavaDoc newuri = header.getValue();
116                 if ((newuri == null) || (newuri.equals(""))) {
117                     newuri = "/";
118                 }
119                 System.out.println("Redirect target: " + newuri);
120                 GetMethod redirect = new GetMethod(newuri);
121
122                 client.executeMethod(redirect);
123                 System.out.println("Redirect: " + redirect.getStatusLine().toString());
124                 // release any connection resources used by the method
125
redirect.releaseConnection();
126             } else {
127                 System.out.println("Invalid redirect");
128                 System.exit(1);
129             }
130         }
131     }
132 }
133
Popular Tags