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