1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.util;
18
19 import java.io.Serializable;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22
23 import javax.servlet.http.HttpServletRequest;
24
25
26
27
28
29
30 public class LinkBuilder implements Serializable {
31
32
33 private static final long serialVersionUID = 1L;
34
35
36 private String protocol;
37
38
39 private String host;
40
41
42 private Integer port;
43
44
45 private String file;
46
47
48
49
50 public LinkBuilder() {
51 this.clear();
52 }
53
54
55
56
57 public void clear() {
58 this.protocol = null;
59 this.host = null;
60 this.port = null;
61 this.file = null;
62 }
63
64
65
66
67
68
69 public String getProtocol() {
70 return protocol;
71 }
72
73
74
75
76
77
78
79
80
81 public void setProtocol(final String protocol) {
82 if (protocol == null) {
83 throw new NullPointerException("No protocol");
84 }
85 this.protocol = protocol;
86 }
87
88
89
90
91
92
93 public String getHost() {
94 return host;
95 }
96
97
98
99
100
101
102
103
104
105 public void setHost(final String host) {
106 if (host == null) {
107 throw new NullPointerException("No host");
108 }
109 this.host = host;
110 }
111
112
113
114
115
116
117 public int getPort() {
118 return port;
119 }
120
121
122
123
124
125
126
127
128
129
130 public void setPort(final int port) {
131 this.port = port;
132 }
133
134
135
136
137
138
139 public String getFile() {
140 return file;
141 }
142
143
144
145
146
147
148
149 public void setFile(final String file) {
150 this.file = file;
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public String toLink(final HttpServletRequest request)
167 throws MalformedURLException {
168 final URL requestURL = new URL(request.getRequestURL().toString());
169 final String newProtocol = getNewProtocol(requestURL.getProtocol());
170 final String newHost = getNewHost(requestURL.getHost());
171 final int newPort = getNewPort(requestURL.getPort());
172 final String newFile = getNewFile(requestURL.getFile());
173 final URL newURL = correctDefaultPort(new URL(newProtocol, newHost,
174 newPort, newFile));
175 if (isRelativeLink(requestURL, newURL)) {
176 return newURL.getFile();
177 } else {
178 return newURL.toExternalForm();
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191 private URL correctDefaultPort(URL url) throws MalformedURLException {
192 if (url.getPort() == url.getDefaultPort()) {
193 final URL correctedURL = new URL(url.getProtocol(), url.getHost(),
194 -1, url.getFile());
195 return correctedURL;
196 }
197 return url;
198 }
199
200
201
202
203
204
205
206
207
208
209
210 private boolean isRelativeLink(final URL url1, final URL url2) {
211 if (!url1.getProtocol().equals(url2.getProtocol())) {
212 return false;
213 }
214 if (!url1.getHost().equals(url2.getHost())) {
215 return false;
216 }
217 if (url1.getPort() != url2.getPort()) {
218 return false;
219 }
220 return true;
221 }
222
223
224
225
226
227
228
229
230 private String getNewProtocol(final String requestProtocol) {
231 if (this.protocol == null) {
232 return requestProtocol;
233 } else {
234 return this.protocol;
235 }
236 }
237
238
239
240
241
242
243
244
245 private String getNewHost(final String requestHost) {
246 if (this.host == null) {
247 return requestHost;
248 } else {
249 return this.host;
250 }
251 }
252
253
254
255
256
257
258
259
260 private int getNewPort(final int requestPort) {
261 if (this.port == null) {
262 return requestPort;
263 } else {
264 return this.port;
265 }
266 }
267
268
269
270
271
272
273
274
275 private String getNewFile(final String currentFile) {
276 if (this.file == null) {
277 return currentFile;
278 } else {
279 return this.file;
280 }
281 }
282
283
284
285
286
287
288
289
290 public LinkBuilder protocol(final String protocol) {
291 this.setProtocol(protocol);
292 return this;
293 }
294
295
296
297
298
299
300
301
302 public LinkBuilder host(final String host) {
303 this.setHost(host);
304 return this;
305 }
306
307
308
309
310
311
312
313
314 public LinkBuilder port(final int port) {
315 this.setPort(port);
316 return this;
317 }
318
319
320
321
322
323
324
325
326 public LinkBuilder file(final String file) {
327 this.setFile(file);
328 return this;
329 }
330
331
332
333
334 @Override
335 public String toString() {
336 final StringBuilder builder = new StringBuilder();
337 builder.append(this);
338 builder.append(" [host=");
339 builder.append(host);
340 builder.append(",protocol=");
341 builder.append(protocol);
342 builder.append(",port=");
343 builder.append(port);
344 builder.append(",file=");
345 builder.append(file);
346 builder.append("]");
347 return builder.toString();
348 }
349
350 }