KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > PCShare


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb;
18
19 /**
20  * PC share class.
21  * <p>
22  * The PC share class holds the details of a network share, including the required username and
23  * password access control.
24  */

25 public final class PCShare
26 {
27
28     // Domain name
29

30     private String JavaDoc m_domain = null;
31
32     // Node name string.
33

34     private String JavaDoc m_nodename = null;
35
36     // Remote share name string.
37

38     private String JavaDoc m_shrname = null;
39
40     // User name access control string.
41

42     private String JavaDoc m_username = null;
43
44     // Password access control string.
45

46     private String JavaDoc m_password = null;
47
48     // Remote path, relative to the share.
49

50     private String JavaDoc m_path = null;
51
52     // File name string.
53

54     private String JavaDoc m_fname = null;
55
56     // Primary and secondary protocols to try connection on
57

58     private int m_primaryProto = Protocol.UseDefault;
59     private int m_secondaryProto = Protocol.UseDefault;
60
61     // Extended security negotiation flags
62

63     private int m_extendedSecFlags;
64     
65     /**
66      * Construct an empty PCShare object.
67      */

68     public PCShare()
69     {
70     }
71
72     /**
73      * Construct a PCShare using the supplied UNC path.
74      *
75      * @param netpath Network path of the remote server, in UNC format ie. \\node\\share.
76      * @exception InvalidUNCPathException If the network path is invalid.
77      */

78     public PCShare(String JavaDoc netpath) throws InvalidUNCPathException
79     {
80         setNetworkPath(netpath);
81
82         // If the user name has not been set, use the guest account
83

84         if (m_username == null)
85             setUserName("GUEST");
86     }
87
88     /**
89      * Construct a PCShare using the specified remote server and access control details.
90      *
91      * @param nname Node name of the remote server.
92      * @param shr Share name on the remote server.
93      * @param uname User name used to access the remote share.
94      * @param pwd Password used to access the remote share.
95      */

96     public PCShare(String JavaDoc nname, String JavaDoc shr, String JavaDoc uname, String JavaDoc pwd)
97     {
98         setNodeName(nname);
99         setShareName(shr);
100         setUserName(uname);
101         setPassword(pwd);
102     }
103
104     /**
105      * Build a share relative path using the supplied working directory and file name.
106      *
107      * @param workdir Working directory string, relative to the root of the share.
108      * @param fname File name string.
109      * @return Share relative path string.
110      */

111     public static String JavaDoc makePath(String JavaDoc workdir, String JavaDoc fname)
112     {
113
114         // Create a string buffer to build the share relative path
115

116         StringBuffer JavaDoc pathStr = new StringBuffer JavaDoc();
117
118         // Make sure there is a leading '\' on the path string
119

120         if (!workdir.startsWith("\\"))
121             pathStr.append("\\");
122         pathStr.append(workdir);
123
124         // Make sure the path ends with '\'
125

126         if (pathStr.charAt(pathStr.length() - 1) != '\\')
127             pathStr.append("\\");
128
129         // Add the file name to the path string
130

131         pathStr.append(fname);
132
133         // Return share relative the path string
134

135         return pathStr.toString();
136     }
137
138     /**
139      * Return the domain for the share.
140      *
141      * @return java.lang.String
142      */

143     public final String JavaDoc getDomain()
144     {
145         return m_domain;
146     }
147
148     /**
149      * Determine if extended security flags have been set
150      *
151      * @return boolean
152      */

153     public final boolean hasExtendedSecurityFlags()
154     {
155         return m_extendedSecFlags != 0 ? true : false;
156     }
157     
158     /**
159      * Return the extended security flags
160      *
161      * @return int
162      */

163     public final int getExtendedSecurityFlags()
164     {
165         return m_extendedSecFlags;
166     }
167     
168     /**
169      * Get the remote file name string.
170      *
171      * @return Remote file name string.
172      */

173     public final String JavaDoc getFileName()
174     {
175         return m_fname;
176     }
177
178     /**
179      * Return the full UNC path for this PC share object.
180      *
181      * @return Path string of the remote share/path/file in UNC format, ie. \\node\share\path\file.
182      */

183     public final String JavaDoc getNetworkPath()
184     {
185
186         // Create a string buffer to build up the full network path
187

188         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(128);
189
190         // Add the node name and share name
191

192         strBuf.append("\\\\");
193         strBuf.append(getNodeName());
194         strBuf.append("\\");
195         strBuf.append(getShareName());
196
197         // Add the path, if there is one
198

199         if (getPath() != null && getPath().length() > 0)
200         {
201             if (getPath().charAt(0) != '\\')
202             {
203                 strBuf.append("\\");
204             }
205             strBuf.append(getPath());
206         }
207
208         // Add the file name if there is one
209

210         if (getFileName() != null && getFileName().length() > 0)
211         {
212             if (strBuf.charAt(strBuf.length() - 1) != '\\')
213             {
214                 strBuf.append("\\");
215             }
216             strBuf.append(getFileName());
217         }
218
219         // Return the network path
220

221         return strBuf.toString();
222     }
223
224     /**
225      * Get the remote node name string.
226      *
227      * @return Node name string.
228      */

229     public final String JavaDoc getNodeName()
230     {
231         return m_nodename;
232     }
233
234     /**
235      * Get the remote password required to access the remote share.
236      *
237      * @return Remote password string.
238      */

239     public final String JavaDoc getPassword()
240     {
241         return m_password;
242     }
243
244     /**
245      * Get the share relative path string.
246      *
247      * @return Share relative path string.
248      */

249     public final String JavaDoc getPath()
250     {
251         return m_path != null ? m_path : "\\";
252     }
253
254     /**
255      * Return the share relative path for this PC share object.
256      *
257      * @return Path string of the remote share/path/file relative to the share, ie. \path\file.
258      */

259     public final String JavaDoc getRelativePath()
260     {
261
262         // Create a string buffer to build up the full network path
263

264         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(128);
265
266         // Add the path, if there is one
267

268         if (getPath().length() > 0)
269         {
270             if (getPath().charAt(0) != '\\')
271             {
272                 strBuf.append("\\");
273             }
274             strBuf.append(getPath());
275         }
276
277         // Add the file name if there is one
278

279         if (getFileName().length() > 0)
280         {
281             if (strBuf.charAt(strBuf.length() - 1) != '\\')
282             {
283                 strBuf.append("\\");
284             }
285             strBuf.append(getFileName());
286         }
287
288         // Return the network path
289

290         return strBuf.toString();
291     }
292
293     /**
294      * Get the remote share name string.
295      *
296      * @return Remote share name string.
297      */

298
299     public final String JavaDoc getShareName()
300     {
301         return m_shrname;
302     }
303
304     /**
305      * Get the remote user name string.
306      *
307      * @return Remote user name string required to access the remote share.
308      */

309
310     public final String JavaDoc getUserName()
311     {
312         return m_username != null ? m_username : "";
313     }
314
315     /**
316      * Get the primary protocol to connect with
317      *
318      * @return int
319      */

320     public final int getPrimaryProtocol()
321     {
322         return m_primaryProto;
323     }
324
325     /**
326      * Get the secondary protocol to connect with
327      *
328      * @return int
329      */

330     public final int getSecondaryProtocol()
331     {
332         return m_secondaryProto;
333     }
334
335     /**
336      * Determine if the share has a domain specified.
337      *
338      * @return boolean
339      */

340     public final boolean hasDomain()
341     {
342         return m_domain == null ? false : true;
343     }
344
345     /**
346      * Set the domain to be used during the session setup.
347      *
348      * @param domain java.lang.String
349      */

350     public final void setDomain(String JavaDoc domain)
351     {
352         m_domain = domain;
353         if (m_domain != null)
354             m_domain = m_domain.toUpperCase();
355     }
356
357     /**
358      * Set the remote file name string.
359      *
360      * @param fn Remote file name string.
361      */

362
363     public final void setFileName(String JavaDoc fn)
364     {
365         m_fname = fn;
366     }
367
368     /**
369      * Set the PC share from the supplied UNC path string.
370      *
371      * @param netpath UNC format remote file path.
372      */

373
374     public final void setNetworkPath(String JavaDoc netpath) throws InvalidUNCPathException
375     {
376
377         // Take a copy of the network path
378

379         StringBuffer JavaDoc path = new StringBuffer JavaDoc(netpath);
380         for (int i = 0; i < path.length(); i++)
381         {
382
383             // Convert forward slashes to back slashes
384

385             if (path.charAt(i) == '/')
386                 path.setCharAt(i, '\\');
387         }
388         String JavaDoc npath = path.toString();
389
390         // UNC path starts with '\\'
391

392         if (!npath.startsWith("\\\\") || npath.length() < 5)
393             throw new InvalidUNCPathException(npath);
394
395         // Extract the node name from the network path
396

397         int pos = 2;
398         int endpos = npath.indexOf("\\", pos);
399
400         if (endpos == -1)
401             throw new InvalidUNCPathException(npath);
402
403         setNodeName(npath.substring(pos, endpos));
404         pos = endpos + 1;
405
406         // Extract the share name from the network path
407

408         endpos = npath.indexOf("\\", pos);
409
410         if (endpos == -1)
411         {
412
413             // Share name is the last part of the UNC path
414

415             setShareName(npath.substring(pos));
416
417             // Set the root path and clear the file name
418

419             setPath("\\");
420             setFileName("");
421         }
422         else
423         {
424             setShareName(npath.substring(pos, endpos));
425
426             pos = endpos + 1;
427
428             // Extract the share relative path from the network path
429

430             endpos = npath.lastIndexOf("\\");
431
432             if (endpos != -1 && endpos > pos)
433             {
434
435                 // Set the share relative path, and update the current position index
436

437                 setPath(npath.substring(pos, endpos));
438
439                 // File name is the rest of the UNC path
440

441                 setFileName(npath.substring(endpos + 1));
442             }
443             else
444             {
445
446                 // Set the share relative path to the root path
447

448                 setPath("\\");
449
450                 // Set the file name string
451

452                 if (npath.length() > pos)
453                     setFileName(npath.substring(pos));
454                 else
455                     setFileName("");
456             }
457         }
458
459         // Check if the share name contains embedded access control
460

461         pos = m_shrname.indexOf("%");
462         if (pos != -1)
463         {
464
465             // Find the end of the user name
466

467             endpos = m_shrname.indexOf(":", pos);
468             if (endpos != -1)
469             {
470
471                 // Extract the user name and password strings
472

473                 setUserName(m_shrname.substring(pos + 1, endpos));
474                 setPassword(m_shrname.substring(endpos + 1));
475             }
476             else
477             {
478
479                 // Extract the user name string
480

481                 setUserName(m_shrname.substring(pos + 1));
482             }
483
484             // Reset the share name string, to remove the access control
485

486             setShareName(m_shrname.substring(0, pos));
487         }
488
489         // Check if the path has been set, if not then use the root path
490

491         if (m_path == null || m_path.length() == 0)
492             m_path = "\\";
493     }
494
495     /**
496      * Set the extended security negotiation flags
497      *
498      * @param extFlags int
499      */

500     public final void setExtendedSecurityFlags(int extFlags)
501     {
502         m_extendedSecFlags = extFlags;
503     }
504     
505     /**
506      * Set the remote node name string.
507      *
508      * @param nname Remote node name string.
509      */

510
511     public final void setNodeName(String JavaDoc nname)
512     {
513         m_nodename = nname;
514     }
515
516     /**
517      * Set the remote password string.
518      *
519      * @param pwd Remote password string, required to access the remote share.
520      */

521
522     public final void setPassword(String JavaDoc pwd)
523     {
524         m_password = pwd;
525     }
526
527     /**
528      * Set the share relative path string.
529      *
530      * @param pth Share relative path string.
531      */

532
533     public final void setPath(String JavaDoc pth)
534     {
535         m_path = pth;
536     }
537
538     /**
539      * Set the remote share name string.
540      *
541      * @param shr Remote share name string.
542      */

543
544     public final void setShareName(String JavaDoc shr)
545     {
546         m_shrname = shr;
547     }
548
549     /**
550      * Set the remote user name string.
551      *
552      * @param uname Remote user name string.
553      */

554
555     public final void setUserName(String JavaDoc uname)
556     {
557         m_username = uname;
558     }
559
560     /**
561      * Set the primary and secondary protocol order that is used to connect to the remote host.
562      *
563      * @param pri int
564      * @param sec int
565      */

566     public final void setProtocolOrder(int pri, int sec)
567     {
568         m_primaryProto = pri;
569         m_secondaryProto = sec;
570     }
571
572     /**
573      * Return the PCShare object as a string
574      *
575      * @return PCShare string.
576      */

577
578     public final String JavaDoc toString()
579     {
580         return getNetworkPath();
581     }
582 }
Popular Tags