1 package com.freemindcafe.apache.commons;
2 import java.io.IOException;
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
6 import org.
apache.commons.httpclient.HttpClient;
7 import org.
apache.commons.httpclient.NameValuePair;
8 import org.
apache.commons.httpclient.methods.PostMethod;
12 import org.junit.Test;
18 private static final Logger logger = Logger.getLogger (
HttpsClientTest.class);
23 BasicConfigurator.configure();
24 System.setProperty(
"javax.net.ssl.trustStore",
"/home/eip/conf/systemProperties/localhostkeystore");
25 final HttpClient client =
new HttpClient();
27 final PostMethod post =
new PostMethod(
"https://nikhil-vm.emeter.com:9443/em-cas/login");
28 String username =
"abc";
30 post.setRequestBody(
new NameValuePair[] {
31 new NameValuePair(
"username", username),
32 new NameValuePair(
"password",
"xyz") });
35 logger.info(
"Attempt to get TGT for user [" + username +
"]");
38 Level oldLevel = Logger.getLogger(
"httpclient.wire.content").getLevel();
39 Logger.getLogger(
"httpclient.wire.content").setLevel(Level.INFO);
41 client.executeMethod(post);
43 Logger.getLogger(
"httpclient.wire.content").setLevel(oldLevel);
45 final String response = post.getResponseBodyAsString();
47 switch (post.getStatusCode()) {
49 final Matcher matcher = Pattern.compile(
".*action=\".*/(.*?)\".*").matcher(response);
51 if (matcher.matches()) {
52 logger.info(
"result - " + matcher.group(1));
55 logger.error(
"Successful ticket granting request, but no ticket found!");
56 logger.error(
"Response (1k): "
57 + response.substring(0, Math.min(1024, response.length())));
60 logger.error(
"Invalid response code (" + post.getStatusCode()
61 +
") from CAS server!");
62 logger.error(
"Response (1k): "
63 + response.substring(0, Math.min(1024, response.length())));
67 }
catch (
final IOException e) {
68 logger.error(
"Exception while trying to get TGT from CAS server for USER - " + username, e);
70 post.releaseConnection();
72 logger.info(
"result - " +
"null");