1 package net.sourceforge.argval.utils;
2
3
4 import java.net.URI;
5 import java.net.URISyntaxException;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10
11
12 public class UriUtil {
13
14 private transient static Logger logger = LoggerFactory.getLogger(UriUtil.class);
15
16
17 public static URI createUri(final URI newUri, final URI defaultUri) {
18
19
20
21
22
23 if (newUri == null) {
24 return defaultUri;
25 }
26
27 String scheme = StringUtil.polish(newUri.getScheme(), defaultUri.getScheme());
28 String userInfo = StringUtil.polish(newUri.getUserInfo(), defaultUri.getUserInfo());
29 String hostname = StringUtil.polish(newUri.getHost(), defaultUri.getHost());
30 int port = (newUri.getPort() > 0) ? newUri.getPort() : defaultUri.getPort();
31 String pathStr = StringUtil.polish(newUri.getPath(), defaultUri.getPath());
32 String queryStr = StringUtil.polish(newUri.getQuery(), defaultUri.getQuery());
33 String fragment = StringUtil.polish(newUri.getFragment(), defaultUri.getFragment());
34
35
36
37
38
39
40
41
42 URI serviceUri = null;
43 try {
44 serviceUri = new URI(scheme, userInfo, hostname, port, pathStr,
45 queryStr, fragment);
46 }
47 catch (URISyntaxException urise) {
48 logger.warn("Returning default URI '" + defaultUri + "', because of problem while constructing new URI.", urise);
49 return defaultUri;
50 }
51
52 return serviceUri;
53 }
54
55 }