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