View Javadoc

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      /** The logging instance. */
14      private transient static Logger logger = LoggerFactory.getLogger(UriUtil.class);
15  
16  
17      public static URI createUri(final URI newUri, final URI defaultUri) {
18          // FIXME [20071210 tv]: Remove circular dependency.
19  //        ArgumentValidation argVal = new ArgumentValidationImpl();
20  //        argVal.isValidWhenNotNull("defaultUri", defaultUri);
21  //        if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException();
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  //        System.out.println("port: " + newUri.getPort() + "  " + defaultUri.getPort());
37  //        System.out.println("new: " + newUri);
38  //        System.out.println("  path " + newUri.getPath().length());
39  //        System.out.println("new: " + newUri);
40  //        System.out.println("new: " + newUri);
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  }