package smartbi.cas.client;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import smartbi.SmartbiException;
import smartbi.sdk.CASClientConnector;
import smartbi.util.CommonErrorCode;
import smartbi.util.StringUtil;
public class MyCASClientConnector extends CASClientConnector{
/**
*
*/
private String lt;
/**
*
*/
private String execute_id;
/**
*
*/
private String eventId;
public MyCASClientConnector(String smartbiUrl) {
super(smartbiUrl);
}
/** {@inheritDoc} */
@Override
public boolean open(String user, String password) {
try {
parseToken();
loginCas(user, password);
return loginSmartbi(user, password);
} catch (IOException e) {
throw new SmartbiException(CommonErrorCode.UNKOWN_ERROR, e);
}
}
private boolean loginSmartbi(String user, String password) throws IOException {
String url = smartbiUrl;
if (!smartbiUrl.endsWith("/")) {
url = url + "/";
}
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setInstanceFollowRedirects(false);
conn.getResponseCode();
String smartbiCookie = "";
try {
for (int i = 0; i < 100; i++) {
if ("Set-Cookie".equalsIgnoreCase(conn.getHeaderFieldKey(i))) {
String v = conn.getHeaderField(i);
smartbiCookie += " " + v.substring(0, v.indexOf(';') + 1);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
}
String location = conn.getHeaderField("Location");
conn.disconnect();
if (!location.startsWith("http")) {
cookie = smartbiCookie;
return super.open(user, password);
}
conn = (HttpURLConnection) new URL(location).openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.setInstanceFollowRedirects(false);
conn.getResponseCode();
location = conn.getHeaderField("Location");
conn.disconnect();
if (location == null) {
return false;
}
conn = (HttpURLConnection) new URL(location).openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Cookie", smartbiCookie);
conn.getResponseCode();
conn.disconnect();
cookie = smartbiCookie;
return super.open(user, password);
}
private void loginCas(String user, String password) throws IOException, MalformedURLException,
UnsupportedEncodingException {
HttpURLConnection conn = (HttpURLConnection) new URL(getCasLoginUrl()).openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream connOut = null;
try {
connOut = conn.getOutputStream();
String postData = "login_name=" + URLEncoder.encode(user, "UTF-8") + "&password="
+ URLEncoder.encode(password, "UTF-8") + "&execute_id=" + URLEncoder.encode(execute_id, "UTF-8") + "<="
+ URLEncoder.encode(lt, "UTF-8") + "&_eventId=" + URLEncoder.encode(eventId, "UTF-8");
connOut.write(postData.getBytes());
} finally {
if (connOut != null) {
connOut.flush();
connOut.close();
}
}
conn.getResponseCode();
try {
for (int i = 0; i < 100; i++) {
if ("Set-Cookie".equalsIgnoreCase(conn.getHeaderFieldKey(i))) {
String v = conn.getHeaderField(i);
cookie += " " + v.substring(0, v.indexOf(';') + 1);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
}
conn.disconnect();
}
private void parseToken() throws IOException {
URL url = new URL(getCasLoginUrl());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String html = StringUtil.readFromStream(conn.getInputStream(), "UTF-8");
int index = html.indexOf("name=\"lt\" value=\"");
int endIndex = -1;
if (index > -1) {
index += "name=\"lt\" value=\"".length();
endIndex = html.indexOf('"', index);
lt = html.substring(index, endIndex);
} else {
lt = "lt";
}
index = html.indexOf("name=\"execute_id\" value=\"");
index += "name=\"execute_id\" value=\"".length();
endIndex = html.indexOf('"', index);
execute_id = html.substring(index, endIndex);
index = html.indexOf("name=\"_eventId\" value=\"");
index += "name=\"_eventId\" value=\"".length();
endIndex = html.indexOf('"', index);
eventId = html.substring(index, endIndex);
for (int i = 0; i < 100; i++) {
if ("Set-Cookie".equalsIgnoreCase(conn.getHeaderFieldKey(i))) {
cookie = conn.getHeaderField(i);
break;
}
}
if (cookie != null) {
cookie = cookie.substring(0, cookie.indexOf(';') + 1);
} else {
cookie = "";
}
conn.disconnect();
}
} |