package de.viathinksoft.utils.http;
import java.io.IOException;
import java.util.Random;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* This factory produces a HttpUtil instance with faked user agents.
* The instance is only created once.
* @author Daniel Marschall
*/
public class SecretHttpFactory {
static HttpUtils instance;
public static HttpUtils getInstance() {
return instance;
}
public static String getUserAgent
() {
return userAgent;
}
private SecretHttpFactory() {
}
private static String getRandomUserAgent
() {
try {
// Newest version here: http://www.user-agents.org/allagents.xml
// File file = new File(
// "src/de/viathinksoft/utils/http/allagents.xml");
// Document doc = db.parse(file);
Document doc = db.
parse(SecretHttpFactory.
class.
getResourceAsStream("allagents.xml"));
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.
getElementsByTagName("user-agent");
if (nodeLst.getLength() == 0) {
userAgent = null;
} else {
int s = random.nextInt(nodeLst.getLength());
Node fstNode = nodeLst.
item(s
);
if (fstNode.
getNodeType() ==
Node.
ELEMENT_NODE) {
.getElementsByTagName("String");
NodeList fstNm = fstNmElmnt.
getChildNodes();
userAgent =
((Node) fstNm.
item(0)).
getNodeValue();
}
}
userAgent = null;
userAgent = null;
e.printStackTrace();
userAgent = null;
}
return userAgent;
}
static {
userAgent = getRandomUserAgent();
if (userAgent == null) {
instance = new HttpUtils();
} else {
instance = new HttpUtils(userAgent);
}
}
}