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
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 int 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 = -1;
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 public void setPort(final int port) {
130 if (port < 0) {
131 throw new IllegalArgumentException("Invalid port number :" + port);
132 }
133 this.port = port;
134 }
135
136
137
138
139
140
141 public String getFile() {
142 return file;
143 }
144
145
146
147
148
149
150
151 public void setFile(final String file) {
152 this.file = file;
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public String toLink(final HttpServletRequest request)
169 throws MalformedURLException {
170 final URL requestURL = new URL(request.getRequestURL().toString());
171 final URL newURL = new URL(getNewProtocol(requestURL.getProtocol()),
172 getNewHost(requestURL.getHost()), getNewPort(requestURL
173 .getPort()), getNewFile(requestURL.getFile()));
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
191 private boolean isRelativeLink(final URL url1, final URL url2) {
192 if (!url1.getProtocol().equals(url2.getProtocol())) {
193 return false;
194 }
195 if (!url1.getHost().equals(url2.getHost())) {
196 return false;
197 }
198 if (url1.getPort() != url2.getPort()) {
199 return false;
200 }
201 return true;
202 }
203
204
205
206
207
208
209
210
211 private String getNewProtocol(final String requestProtocol) {
212 if (this.protocol == null) {
213 return requestProtocol;
214 } else {
215 return this.protocol;
216 }
217 }
218
219
220
221
222
223
224
225
226 private String getNewHost(final String requestHost) {
227 if (this.host == null) {
228 return requestHost;
229 } else {
230 return this.host;
231 }
232 }
233
234
235
236
237
238
239
240
241 private int getNewPort(final int requestPort) {
242 if (this.port < 0) {
243 return requestPort;
244 } else {
245 return this.port;
246 }
247 }
248
249
250
251
252
253
254
255
256 private String getNewFile(final String currentFile) {
257 if (this.file == null) {
258 return currentFile;
259 } else {
260 return this.file;
261 }
262 }
263
264
265
266
267
268
269
270
271 public LinkBuilder protocol(final String protocol) {
272 this.setProtocol(protocol);
273 return this;
274 }
275
276
277
278
279
280
281
282
283 public LinkBuilder host(final String host) {
284 this.setHost(host);
285 return this;
286 }
287
288
289
290
291
292
293
294
295 public LinkBuilder port(final int port) {
296 this.setPort(port);
297 return this;
298 }
299
300
301
302
303
304
305
306
307 public LinkBuilder file(final String file) {
308 this.setFile(file);
309 return this;
310 }
311
312
313
314
315 @Override
316 public String toString() {
317 final StringBuilder builder = new StringBuilder();
318 builder.append(this);
319 builder.append(" [host=");
320 builder.append(host);
321 builder.append(",protocol=");
322 builder.append(protocol);
323 builder.append(",port=");
324 builder.append(port);
325 builder.append(",file=");
326 builder.append(file);
327 builder.append("]");
328 return builder.toString();
329 }
330
331 }