Subversion Repositories javautils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
package de.viathinksoft.utils.http;
2
 
3
import java.io.IOException;
4
import java.util.Random;
5
 
6
import javax.xml.parsers.DocumentBuilder;
7
import javax.xml.parsers.DocumentBuilderFactory;
8
import javax.xml.parsers.ParserConfigurationException;
9
 
10
import org.w3c.dom.Document;
11
import org.w3c.dom.Element;
12
import org.w3c.dom.Node;
13
import org.w3c.dom.NodeList;
14
import org.xml.sax.SAXException;
15
 
16
/**
17
 * This factory produces a HttpUtil instance with faked user agents.
18
 * The instance is only created once.
19
 * @author Daniel Marschall
20
 */
21
 
22
public class SecretHttpFactory {
23
 
24
        static HttpUtils instance;
25
        static String userAgent;
26
 
27
        public static HttpUtils getInstance() {
28
                return instance;
29
        }
30
 
31
        public static String getUserAgent() {
32
                return userAgent;
33
        }
34
 
35
        private SecretHttpFactory() {
36
        }
37
 
38
        private static String getRandomUserAgent() {
39
                String userAgent = null;
40
 
41
                try {
42
                        // Newest version here: http://www.user-agents.org/allagents.xml
43
 
44
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
45
                        DocumentBuilder db = dbf.newDocumentBuilder();
46
 
47
//                      File file = new File(
48
//                      "src/de/viathinksoft/utils/http/allagents.xml");
49
//                      Document doc = db.parse(file);
50
                        Document doc = db.parse(SecretHttpFactory.class.getResourceAsStream("allagents.xml"));
51
                        doc.getDocumentElement().normalize();
52
                        NodeList nodeLst = doc.getElementsByTagName("user-agent");
53
 
54
                        if (nodeLst.getLength() == 0) {
55
                                userAgent = null;
56
                        } else {
57
                                Random random = new Random();
58
                                int s = random.nextInt(nodeLst.getLength());
59
 
60
                                Node fstNode = nodeLst.item(s);
61
 
62
                                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
63
 
64
                                        Element fstElmnt = (Element) fstNode;
65
 
66
                                        NodeList fstNmElmntLst = fstElmnt
67
                                                        .getElementsByTagName("String");
68
                                        Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
69
                                        NodeList fstNm = fstNmElmnt.getChildNodes();
70
                                        userAgent = ((Node) fstNm.item(0)).getNodeValue();
71
                                }
72
                        }
73
                } catch (ParserConfigurationException e) {
74
                        userAgent = null;
75
                } catch (SAXException e) {
76
                        userAgent = null;
77
                } catch (IOException e) {
78
                        e.printStackTrace();
79
                        userAgent = null;
80
                }
81
 
82
                return userAgent;
83
        }
84
 
85
        static {
86
                userAgent = getRandomUserAgent();
87
                if (userAgent == null) {
88
                        instance = new HttpUtils();
89
                } else {
90
                        instance = new HttpUtils(userAgent);
91
                }
92
        }
93
 
94
}