KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > smb > SmbFileName


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.smb;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileType;
21 import org.apache.commons.vfs.provider.GenericFileName;
22
23 /**
24  * An SMB URI. Adds a share name to the generic URI.
25  *
26  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27  */

28 public class SmbFileName
29     extends GenericFileName
30 {
31     private static final int DEFAULT_PORT = 139;
32
33     private final String JavaDoc share;
34     private final String JavaDoc domain;
35     private String JavaDoc uriWithoutAuth;
36
37     protected SmbFileName(
38         final String JavaDoc scheme,
39         final String JavaDoc hostName,
40         final int port,
41         final String JavaDoc userName,
42         final String JavaDoc password,
43         final String JavaDoc domain,
44         final String JavaDoc share,
45         final String JavaDoc path,
46         final FileType type)
47     {
48         super(scheme, hostName, port, DEFAULT_PORT, userName, password, path, type);
49         this.share = share;
50         this.domain = domain;
51     }
52
53     /**
54      * Returns the share name.
55      */

56     public String JavaDoc getShare()
57     {
58         return share;
59     }
60
61     /**
62      * Builds the root URI for this file name.
63      */

64     protected void appendRootUri(final StringBuffer JavaDoc buffer)
65     {
66         super.appendRootUri(buffer);
67         if(share!=null && !share.equals("")) {
68           if (buffer.charAt(buffer.length() - 1) != '/')
69             buffer.append('/');
70           buffer.append(share);
71         }
72     }
73
74     /**
75      * put domain before username if both are set
76      */

77     protected void appendCredentials(StringBuffer JavaDoc buffer)
78     {
79         if (getDomain() != null && getDomain().length() != 0 &&
80             getUserName() != null && getUserName().length() != 0)
81         {
82             buffer.append(getDomain());
83             buffer.append("\\");
84         }
85         super.appendCredentials(buffer);
86     }
87
88     /**
89      * Factory method for creating name instances.
90      */

91     public FileName createName(final String JavaDoc path, FileType type)
92     {
93         return new SmbFileName(
94             getScheme(),
95             getHostName(),
96             getPort(),
97             getUserName(),
98             getPassword(),
99             domain,
100             share,
101             path,
102             type);
103     }
104
105     /**
106      * Construct the path suitable for SmbFile when used with NtlmPasswordAuthentication
107      */

108     public String JavaDoc getUriWithoutAuth() throws FileSystemException
109     {
110         if (uriWithoutAuth != null)
111         {
112             return uriWithoutAuth;
113         }
114
115         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(120);
116         sb.append(getScheme());
117         sb.append("://");
118         sb.append(getHostName());
119         sb.append("/");
120         sb.append(getShare());
121         if(!getPathDecoded().equals("/"))
122           sb.append(getPathDecoded());
123         uriWithoutAuth = sb.toString();
124         return uriWithoutAuth;
125     }
126
127     /**
128      * returns the domain name
129      */

130     public String JavaDoc getDomain()
131     {
132         return domain;
133     }
134 }
135
Popular Tags