/***************************************************************************
* Copyright 2001-2008 The VietSpider All rights reserved. *
**************************************************************************/
package org.vietspider.browser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.message.BasicNameValuePair;
import org.vietspider.chars.CharsDecoder;
import org.vietspider.chars.URLUtils;
import org.vietspider.html.HTMLNode;
import org.vietspider.html.Name;
import org.vietspider.html.parser.HTMLParser2;
import org.vietspider.html.parser.NodeImpl;
import org.vietspider.html.util.HTMLParserDetector;
import org.vietspider.net.client.HttpHandlers;
import org.vietspider.net.client.HttpMethodHandler;
import org.vietspider.net.client.HttpResponseReader;
import org.vietspider.net.client.WebClient;
import org.vietspider.token.attribute.Attributes;
/**
* Author : Nhu Dinh Thuan
* nhudinhthuan@yahoo.com
* Jul 24, 2008
*/
public class VirtualFormPostBak {
public final static String TYPE_ATTR = "type";
public final static String PASSWORD = "password";
public final static String ID_ATTR = "id";
public final static String NAME_ATTR = "name";
public final static String VALUE = "value";
public final static String ACTION = "action";
public final static int ERROR = -1;
public final static int LOGIN = 0;
public final static int SUCCESSFULL = 1;
private HashMap<String, String> maps;
private WebClient webClient;
private HttpMethodHandler httpMethod;
private String charset= "utf-8";
private String message = null;
public VirtualFormPostBak(WebClient webClient) {
maps = new HashMap<String, String>();
this.webClient = webClient;
this.httpMethod = HttpHandlers.getInstance().createMethod(webClient);
}
public byte[] get(String referer, String url) throws Exception{
HttpResponse response = httpMethod.execute(url, referer);
if(response == null) {
message = "Not response!";
return null;
}
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
switch (statusCode) {
case HttpStatus.SC_NOT_FOUND:
case HttpStatus.SC_NO_CONTENT:
case HttpStatus.SC_BAD_REQUEST:
case HttpStatus.SC_REQUEST_TIMEOUT:
case HttpStatus.SC_NOT_ACCEPTABLE:
case HttpStatus.SC_SERVICE_UNAVAILABLE:
case 999:{
throw new Exception(url + " " + statusLine.getReasonPhrase());
}
default:
break;
}
HttpResponseReader responseReader = HttpHandlers.getInstance().createReader();
byte [] data = responseReader.readBody(response);
return data;
}
public int post(String referer, String url) throws Exception {
byte[] data = get(referer, url);
return post(referer, url, data);
}
public int post(String referer, String url, byte[] data) throws Exception {
if(data == null) {
message = "Not html data!";
return ERROR;
}
if(charset == null) {
HTMLParserDetector detector = new HTMLParserDetector();
charset = detector.detectCharset(data);
}
char [] chars = CharsDecoder.decode(charset, data, 0, data.length);
return post(referer, url, chars);
}
public int post(String referer, String url, char[] chars) throws Exception {
List<NodeImpl> tokens = new HTMLParser2().createTokens(chars);
if(tokens == null) {
message = "Can't parse tokens!";
return ERROR;
}
for(int i = 0; i < tokens.size(); i++) {
NodeImpl node = tokens.get(i);
if(!node.isNode(Name.INPUT)) continue;
String value = getAttribute(node, TYPE_ATTR);
if(value == null) continue;
if(value.equalsIgnoreCase(PASSWORD)) return LOGIN;
}
int i = 0;
for(;i < tokens.size(); i++) {
NodeImpl node = tokens.get(i);
if(node.isNode(Name.FORM)) break;
}
HTMLNode form = null;
List<HTMLNode> inputs = new ArrayList<HTMLNode>();
for(; i < tokens.size(); i++) {
NodeImpl node = tokens.get(i);
if(node.isNode(Name.FORM)) {
if(node.isOpen()) {
form = node;
} else {
break;
}
} else if(node.isNode(Name.INPUT)
|| node.isNode(Name.TEXTAREA)
|| node.isNode(Name.SELECT)) {
inputs.add(node);
}
}
if(form == null || inputs.size() < 1) {
message = "form not found";
return ERROR;
}
String address = getAttribute(form, ACTION);
if(address == null) {
message = "post address not found!";
return ERROR;
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
for(i = 0 ; i < inputs.size(); i++) {
Attributes attrs = inputs.get(i).getAttributes();
String name = getAttribute(attrs, NAME_ATTR);
if(name == null) getAttribute(attrs, ID_ATTR);
if(name == null) continue;
String type = getAttribute(attrs, TYPE_ATTR);
if(type == null) type = "text";
String value = maps.get(name);
if(value == null) {
value = getAttribute(attrs, VALUE);
}
if(value == null) value = "";
params.add(new BasicNameValuePair(name, value.trim()));
}
URLUtils urlUtils = new URLUtils();
address = urlUtils.createURL(url, address).trim();
address = urlUtils.getCanonical(address);
// System.out.println(" chuan bi login rui " + address);
referer = url.toString();
HttpHost httpHost = webClient.createHttpHost(address);
// HttpResponse response2 =
httpMethod.execute(httpHost, webClient.createFormPostMethod(address, referer, params));
// System.out.println(" chuan bi log roi "+ address);
// HttpResponseReader reader = new HttpResponseReader();
// java.io.File file;
// org.vietspider.common.io.DataWriter writer = new org.vietspider.common.io.DataWriter();
// byte bytes[] = reader.readBody(response2);
// file = new java.io.File("D:\\Temp\\tmp\\", "login.html");
// writer.save(file, bytes);
return SUCCESSFULL;
}
public String getAttribute(HTMLNode node, String name) {
Attributes attrs = node.getAttributes();
int idx = attrs.indexOf(name);
if(idx < 0) return null;
return attrs.get(idx).getValue();
}
public String getAttribute(Attributes attrs, String name) {
int idx = attrs.indexOf(name);
if(idx < 0) return null;
return attrs.get(idx).getValue();
}
public HashMap<String, String> getData() { return maps; }
public void resetData() { maps.clear(); }
public void putData(String key, String value) { maps.put(key, value); }
public String getCharset() { return charset; }
public void setCharset(String charset) { this.charset = charset; }
public String getMessage() { return message; }
}