View Javadoc
1   package us.codecraft.webmagic.proxy;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.net.URI;
5   import java.net.URISyntaxException;
6   import java.net.URLEncoder;
7   import java.nio.charset.StandardCharsets;
8   
9   import org.apache.commons.lang3.StringUtils;
10  
11  public class Proxy {
12  
13      private String scheme;
14  
15      private String host;
16  
17      private int port;
18  
19      private String username;
20  
21      private String password;
22  
23      public static Proxy create(final URI uri) {
24          Proxy proxy = new Proxy(uri.getHost(), uri.getPort(), uri.getScheme());
25          String userInfo = uri.getUserInfo();
26          if (userInfo != null) {
27              String[] up = userInfo.split(":");
28              if (up.length == 1) {
29                  proxy.username = up[0].isEmpty() ? null : up[0];
30              } else {
31                  proxy.username = up[0].isEmpty() ? null : up[0];
32                  proxy.password = up[1].isEmpty() ? null : up[1];
33              }
34          }
35          return proxy;
36      }
37  
38      public Proxy(String host, int port) {
39          this(host, port, null);
40      }
41  
42      public Proxy(String host, int port, String scheme) {
43          this.host = host;
44          this.port = port;
45          this.scheme = scheme;
46      }
47  
48      public Proxy(String host, int port, String username, String password) {
49          this.host = host;
50          this.port = port;
51          this.username = username;
52          this.password = password;
53      }
54  
55      public String getScheme() {
56          return scheme;
57      }
58  
59      public void setScheme(String scheme) {
60          this.scheme = scheme;
61      }
62  
63  	public String getHost() {
64          return host;
65      }
66  
67      public int getPort() {
68          return port;
69      }
70  
71      public String getUsername() {
72          return username;
73      }
74  
75      public String getPassword() {
76          return password;
77      }
78  
79      public URI toURI() {
80          final StringBuilder userInfoBuffer = new StringBuilder();
81          if (username != null) {
82              userInfoBuffer.append(urlencode(username));
83          }
84          if (password != null) {
85              userInfoBuffer.append(":").append(urlencode(password));
86          }
87          final String userInfo = StringUtils.defaultIfEmpty(userInfoBuffer.toString(), null);
88          URI uri;
89          try {
90              uri = new URI(scheme, userInfo, host, port, null, null, null);
91          } catch (URISyntaxException e) {
92              throw new IllegalArgumentException(e.getMessage(), e);
93          }
94          return uri;
95      }
96  
97      private String urlencode(String s) {
98          String enc = StandardCharsets.UTF_8.name();
99          try {
100             return URLEncoder.encode(s, enc);
101         } catch (UnsupportedEncodingException e) {
102             throw new IllegalArgumentException(e);
103         }
104     }
105 
106     @Override
107     public boolean equals(Object o) {
108         if (this == o) return true;
109         if (o == null || getClass() != o.getClass()) return false;
110 
111         Proxy proxy = (Proxy) o;
112 
113         if (port != proxy.port) return false;
114         if (host != null ? !host.equals(proxy.host) : proxy.host != null) return false;
115         if (scheme != null ? !scheme.equals(proxy.scheme) : proxy.scheme != null) return false;
116         if (username != null ? !username.equals(proxy.username) : proxy.username != null) return false;
117         return password != null ? password.equals(proxy.password) : proxy.password == null;
118     }
119 
120     @Override
121     public int hashCode() {
122         int result = host != null ? host.hashCode() : 0;
123         result = 31 * result + port;
124         result = 31 * result + (scheme != null ? scheme.hashCode() : 0);
125         result = 31 * result + (username != null ? username.hashCode() : 0);
126         result = 31 * result + (password != null ? password.hashCode() : 0);
127         return result;
128     }
129 
130     @Override
131     public String toString() {
132         return this.toURI().toString();
133     }
134 
135 }