1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.url;
12
13 import java.net.MalformedURLException;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18
19
20
21 public class UrlParser {
22
23
24 private static final Logger logger = LoggerFactory.getLogger(UrlParser.class);
25
26
27 private String protocol;
28
29
30 private String host;
31
32
33 private int port = -1;
34
35
36 private String path;
37
38
39
40
41
42
43
44
45 public UrlParser(String url) throws MalformedURLException {
46 if (url == null || url.length() <= 0) {
47 throw new MalformedURLException("Empty URL");
48 }
49 int ppos = url.indexOf("://");
50
51
52 if (ppos >= 0) {
53 protocol = url.substring(0, ppos);
54 url = url.substring(ppos + 3);
55 }
56
57 String hostport;
58
59 ppos = url.indexOf('/');
60 if (ppos >= 0) {
61 hostport = url.substring(0, ppos);
62 path = url.substring(ppos + 1);
63 } else {
64 hostport = url;
65 }
66
67 ppos = hostport.indexOf(':');
68 if (ppos >= 0) {
69 host = hostport.substring(0, ppos);
70 String portString = hostport.substring(ppos + 1);
71 try {
72 this.port = Integer.parseInt(portString);
73 } catch (NumberFormatException e) {
74 logger.trace("", e);
75 throw new MalformedURLException("Invalid port " + portString);
76 }
77 } else {
78 host = hostport;
79 }
80 }
81
82
83
84
85
86
87 public String getProtocol() {
88 return protocol;
89 }
90
91
92
93
94
95
96 public void setProtocol(String protocol) {
97 this.protocol = protocol;
98 }
99
100
101
102
103
104
105 public String getHost() {
106 return host;
107 }
108
109
110
111
112
113
114 public void setHost(String host) {
115 this.host = host;
116 }
117
118
119
120
121
122
123 public int getPort() {
124 return port;
125 }
126
127
128
129
130
131
132 public void setPort(int port) {
133 this.port = port;
134 }
135
136
137
138
139
140
141 public String getPath() {
142 return path;
143 }
144
145
146
147
148
149
150 public void setPath(String path) {
151 this.path = path;
152 }
153
154 }