Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/UrlBuilder.java


1   /*
2    * UrlBuilder.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  import com.flexstor.common.io.FileAccessProtocol;
14  
15  /**
16   * Builds an URL string.
17   */
18  public class UrlBuilder
19  {
20     /** @deprecated use FileAccessProtocol.FILE_ID instead. */
21     public final static int FILE_URL = FileAccessProtocol.FILE_ID;
22     
23     /** @deprecated use FileAccessProtocol.HTTP_ID instead. */
24     public final static int HTTP_URL = FileAccessProtocol.HTTP_ID;
25     
26     /** @deprecated use FileAccessProtocol.NFS_ID instead. */
27     public final static int NFS_URL  = FileAccessProtocol.NFS_ID;
28     
29     /** @deprecated use FileAccessProtocol.FTP_ID instead. */
30     public final static int FTP_URL  = FileAccessProtocol.FTP_ID;
31  
32     private UrlBuilder ( )
33     {
34     }
35  
36     /**
37      * Constructs an URL string.
38      * Please note that this method will automatically try to resolve the server name to
39      * a DNS name.  For example "hoth" would be resolver to "hoth.flexstor.com".
40      *
41      * @param sProtocol the URL protocol to be used.
42      * @param sServer   the server name
43      * @param sLocation the location of the file.
44      * @param sFile     the file name.
45      * @return the String URL.
46      */
47     public static String buildURLString ( String sProtocol, String sServer, String sLocation, String sFile )
48     {
49        // Build the beginning of the URL.
50        String sURL = sProtocol + "://" + resolveServerName(sServer);
51        
52        // Only HTTP URLs have a port number.
53        if ( sProtocol.equalsIgnoreCase( FileAccessProtocol.HTTP ) )
54           sURL += ":" + resolvePortNumber(sServer);
55  
56        // Make sure that don't get a double slash if there is no location.
57        if ( !sLocation.equals("") )
58           sURL += "/" + stripSlashes(sLocation);
59        
60        return sURL + "/" + stripSlashes(sFile);
61     }
62     
63     /**
64      * Constructs an URL string.
65      * Please note that this method will automatically try to resolve the server name to
66      * a DNS name.  For example "hoth" would be resolver to "hoth.flexstor.com".
67      *
68      * @param nProtocol the URL protocol to be used.
69      * @param sServer   the server name
70      * @param sLocation the location of the file.
71      * @param sFile     the file name.
72      * @return the String URL.
73      */
74     public static String buildURLString ( int nProtocol, String sServer, String sLocation, String sFile )
75     {
76        // Build the beginning of the URL.
77        String sURL = resolveProtocol(nProtocol) + "://" + resolveServerName(sServer);
78        
79        // Only HTTP URLs have a port number.
80        if ( nProtocol == FileAccessProtocol.HTTP_ID )
81           sURL += ":" + resolvePortNumber(sServer);
82  
83        // Make sure that don't get a double slash if there is no location.
84        if ( !sLocation.equals("") )
85           sURL += "/" + stripSlashes(sLocation);
86        
87        return sURL + "/" + stripSlashes(sFile);
88     }
89     
90     private static String resolveServerName ( String sServer )
91     {
92        return ServerList.getDNSName ( sServer );
93     }
94        
95     private static String resolvePortNumber ( String sServer )
96     {
97        return ServerList.getPort ( sServer );
98     }
99     
100    private static String resolveProtocol ( int nProtocol )
101    {
102       String sProtocolName = FileAccessProtocol.getProtocolName( nProtocol );
103       
104       if ( sProtocolName.equals( FileAccessProtocol.UNKNOWN ) )
105          return "";
106          
107       return sProtocolName;
108    }
109 
110    private static String stripSlashes ( String sString )
111    {
112       if (sString.length() == 0)
113          return sString;
114 
115        // Remove all leading slashes.
116        while ( (sString.substring ( 0, 1 )).equals("/") )
117            sString = sString.substring ( 1 );
118 
119        // Remove all trailing slashes.
120        while ( (sString.substring ( sString.length()-1)).equals("/") )
121            sString = sString.substring ( 0, sString.length()-1 );
122 
123        return sString;
124    }
125 }