Source Code Cross Referenced for WinFile.java in  » Portal » Open-Portal » com » sun » portal » netfile » servlet » java1 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.netfile.servlet.java1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * $Id: WinFile.java,v 1.45 2005/09/21 11:04:47 dg154973 Exp $
003:         * Copyright 2002 Sun Microsystems, Inc. All
004:         * rights reserved. Use of this product is subject
005:         * to license terms. Federal Acquisitions:
006:         * Commercial Software -- Government Users
007:         * Subject to Standard License Terms and
008:         * Conditions.
009:         *
010:         * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011:         * are trademarks or registered trademarks of Sun Microsystems,
012:         * Inc. in the United States and other countries.
013:         */package com.sun.portal.netfile.servlet.java1;
014:
015:        import java.util.*;
016:        import com.sun.portal.log.common.PortalLogger;
017:        import java.util.logging.*;
018:        import java.io.*;
019:        import java.net.*;
020:        import java.text.SimpleDateFormat;
021:
022:        import com.iplanet.sso.SSOToken;
023:
024:        import jcifs.smb.*;
025:
026:        /*
027:         *  WinFile:
028:         *          Uses the jCIFS (java Common Internet File System) to access window's
029:         *          share. CIFS is the de-facto standard file sharing protocol on the
030:         *          Microsoft Windows platform and jCIFS is the implementation of
031:         *          CIFS solutions using the Java Programming language.
032:         */
033:
034:        class WinFile implements  XFileInterface {
035:
036:            private static Logger logger = PortalLogger
037:                    .getLogger(WinFile.class);
038:            private NetFileLogManager logManager = null;
039:            private String machineEncoding;
040:            private String machineEncodingOriginal;
041:            private SSOToken ssoToken = null;
042:
043:            private int directoriesTraversed; //Keep track of number of directories scanned during single instance of a search
044:            private String winsServerIpAddress = "";
045:            private int smbPort;
046:            private String smbProtocolName;
047:            ArrayList searchResults = null;
048:
049:            public WinFile(NetFileLogManager logMgr, String machineEncoding,
050:                    SSOToken token) throws NetFileException {
051:
052:                this .logManager = logMgr;
053:                machineEncodingOriginal = machineEncoding;
054:                this .ssoToken = token;
055:                directoriesTraversed = 0;
056:                FileOption filopt = new FileOption(logManager, machineEncoding,
057:                        this .ssoToken);
058:                this .machineEncoding = getSmbNameForEncoding(machineEncoding);
059:                String[] pathinfo = filopt.grepPlatinfo();
060:
061:                winsServerIpAddress = pathinfo[0];
062:                smbPort = 139;
063:                smbProtocolName = "smb";
064:
065:                //  Set the WINS server's IP address for address resolution
066:                //        logger.info("WinFile: setting WINS server's IP to-" + winsServerIpAddress);
067:                Object[] params0 = { winsServerIpAddress };
068:                logger.log(Level.INFO, "PSSRNF_CSPNSJ1205", params0);
069:                jcifs.Config.setProperty("wins", winsServerIpAddress);
070:
071:                //  Set the encoding to be used when accessing multibyte characters.
072:                //        logger.info("WinFile: setting machineEncoding to-" + machineEncoding);
073:                Object[] params1 = { machineEncoding };
074:                logger.log(Level.INFO, "PSSRNF_CSPNSJ1206", params1);
075:                jcifs.Config.setProperty("jcifs.netbios.encoding",
076:                        machineEncoding);
077:
078:            }
079:
080:            /*
081:             *  Constructs the java.net.URL object using:
082:             *      protocolName, machineName, protocolPort,directory and SmbHandler
083:             */
084:            private URL constructURL(String machine, String dir,
085:                    SmbHandler handler) throws MalformedURLException {
086:                return new URL(smbProtocolName, machine, smbPort, dir, handler);
087:            }
088:
089:            /*
090:             * Constructs the java.net.URL object using a string form of
091:             * protocolName and domain/workgroup name
092:             */
093:            private URL constructURL(String domain, SmbHandler handler)
094:                    throws MalformedURLException {
095:                return new URL(smbProtocolName, domain, smbPort, "", handler);
096:            }
097:
098:            private String handleDirectoryNames(String dir) {
099:                /*
100:                 *  Handle any "\\" prefixes in directory names.
101:                 *  smb protocol used by jCIFS doesn't handle urls that have
102:                 *  "\\" in the url. So replace them with "/".
103:                 */
104:                String returnDir = dir;
105:                if (dir.indexOf("\\") != -1) {
106:                    returnDir = dir.replace('\\', '/');
107:                }
108:                /*
109:                 *  After replacing, if the directory names starts with "//", replace
110:                 *  them with "/".
111:                 *  Though the operations can be performed normally (doesn't  throw
112:                 *  any malformed URL exception),  want the url to be of proper syntax.
113:                 */
114:                if (returnDir.startsWith("//")) {
115:                    returnDir = returnDir.substring(1);
116:                }
117:                return returnDir;
118:            }
119:
120:            //-------------------------------------------------
121:            // See if we can determine whether this is a WIN or NT machine
122:            //-------------------------------------------------
123:            public String[] verifyHostType(String domain, String server,
124:                    String userName, String password) throws NetFileException {
125:                /*
126:                 *  If the user has NOT entered domain name, then
127:                 *  the machine type is assumed to be "WIN", else
128:                 *  the machine type is assumed to be "NT"
129:                 */
130:                if (domain != null) {
131:                    SmbHandler handler = new SmbHandler(userName, password,
132:                            domain);
133:                    domain = domain.trim();
134:                    if (domain.equals("")) {
135:                        return new String[] { "WIN", "" };
136:                    } else {
137:                        try {
138:                            jcifs.Config
139:                                    .setProperty(
140:                                            "jcifs.smb.client.disablePlainTextPasswords",
141:                                            "false");
142:
143:                            URL hostUrl = constructURL(domain, handler);
144:                            SmbFile file = (SmbFile) hostUrl.openConnection();
145:                            Object obj = file.list();
146:                            if (obj != null) {
147:                                /*
148:                                 String[] hosts = (String[])obj;
149:                                 for(int i = 0; i < hosts.length; i++) {
150:                                 if(hosts[i].equalsIgnoreCase(server)) {
151:                                 return new String[] {"NT",domain};
152:                                 }
153:                                 }
154:                                 throw new NetFileException(NetFileException.NETFILE_SERVER_NOTIN_DOMAIN, "");
155:                                 */
156:                                return new String[] { "NT", domain };
157:                            } else {
158:                                throw new NetFileException(
159:                                        NetFileException.NETFILE_INVALID_DOMAIN,
160:                                        "");
161:                            }
162:                        } catch (SmbException smbe) {
163:                            //                        logger.log(Level.SEVERE, "SmbException in determining if domain/server name exists", smbe);
164:                            logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1207");
165:                            throw new NetFileException(
166:                                    NetFileException.NETFILE_INVALID_DOMAIN, "");
167:                        } catch (IOException ioe) {
168:                            //                        logger.log(Level.SEVERE, "Exception in determining if domain/server name exists", ioe);
169:                            logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1208");
170:                            throw new NetFileException();
171:                        }
172:                    }
173:                } else {
174:                    return new String[] { "WIN", "" };
175:                }
176:            }
177:
178:            //-------------------------------------------------
179:            // Get the list of shares for the given system
180:            //-------------------------------------------------
181:            public String getShares(String username, String password,
182:                    String machine, String domain, NetFileResource nfRes)
183:                    throws Exception, NetFileException {
184:
185:                String shares = "";
186:                boolean accessible = false;
187:                try {
188:                    SmbHandler handler = new SmbHandler(username, password,
189:                            domain);
190:                    URL sharesUrl = constructURL(machine, "/", handler);
191:                    SmbFile file = (SmbFile) sharesUrl.openConnection();
192:
193:                    SmbFile[] files = file.listFiles();
194:
195:                    String shareName = "";
196:                    for (int i = 0; i < files.length; i++) {
197:                        shareName = files[i].getName();
198:                        //Don't consider shareNames that end with "$/"
199:                        if (!shareName.endsWith("$/")) {
200:                            //remove "/" at the end of share.
201:                            shareName = shareName.substring(0, shareName
202:                                    .lastIndexOf("/"));
203:                            shares += shareName + "\n";
204:                        } else {
205:                            /*
206:                             *  Check if the user has the permission to access $ share.
207:                             */
208:                            shareName = shareName.substring(0, shareName
209:                                    .length() - 1);
210:                            accessible = isShareAccessible(username, password,
211:                                    machine, shareName, domain, nfRes);
212:                            if (accessible)
213:                                shares += shareName + "\n";
214:                        }
215:                    }
216:
217:                    return shares;
218:                } catch (Exception ex) {
219:                    //                logger.log(Level.SEVERE, "Exception getting shares",ex);
220:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1209");
221:
222:                    if (ex instanceof  SmbException) {
223:                        SmbException se = (SmbException) ex;
224:                        throw new Exception(getErrorString(se.getNtStatus(),
225:                                nfRes));
226:                    } else {
227:                        return "ERROR:" + ex.getMessage();
228:                    }
229:                }
230:            }
231:
232:            //-------------------------------------------------
233:            // Generate a directory listing for a pc machine
234:            //-------------------------------------------------
235:            public String[] getDirectory(String username, String password,
236:                    String machine, String share, String directory,
237:                    String domain, NetFileResource nfRes)
238:                    throws NetFileException {
239:
240:                ArrayList fileListings = new ArrayList();
241:                try {
242:                    SmbHandler handler = new SmbHandler(username, password,
243:                            domain);
244:                    directory = handleDirectoryNames(directory);
245:                    URL listingUrl = constructURL(machine, "/" + share
246:                            + directory + "/", handler);
247:                    //  dir has "/" characters initially. But still the creation of url works.
248:                    SmbFile file = (SmbFile) listingUrl.openConnection();
249:
250:                    //  Get the listing of files.
251:                    SmbFile[] files = file.listFiles();
252:                    for (int i = 0; i < files.length; i++) {
253:                        extractFileInformation(files[i], fileListings);
254:                    }
255:
256:                    int fileListingSize = fileListings.size();
257:                    String[] totalListings = new String[fileListingSize];
258:                    for (int i = 0; i < fileListingSize; i++) {
259:                        totalListings[i] = (String) fileListings.get(i);
260:                    }
261:
262:                    return totalListings;
263:
264:                } catch (Exception e) {
265:                    //                logger.log(Level.SEVERE, "Exception in getting the file listing",e);
266:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1210");
267:
268:                    if (e instanceof  SmbException) {
269:                        SmbException se = (SmbException) e;
270:                        throw new NetFileException(getErrorString(se
271:                                .getNtStatus(), nfRes));
272:                    } else {
273:                        throw new NetFileException(
274:                                NetFileException.KEY_IDENTIFIER_PREFIX
275:                                        + "unable_to_get_listing_error");
276:                    }
277:                } finally {
278:                    fileListings = null;
279:                }
280:            }
281:
282:            private void extractFileInformation(SmbFile file,
283:                    ArrayList fileListings) throws Exception {
284:
285:                String fileType = "";
286:                String fileLastModified = "";
287:                String fileName = "";
288:
289:                /*
290:                 * Following the old convention of setting file type.
291:                 * Old convention means dir listing as returned by smbclient
292:                 *      D - dir
293:                 *      A - files.
294:                 *      AR - read-only files.
295:                 *      AH - hidden files.
296:                 * Only 2 types returned to client
297:                 *      D - dir
298:                 *      - - files(including read-only files).
299:                 *
300:                 * Check if the file is hidden first,
301:                 * if yes, then don't return file information.
302:                 */
303:                if (file.isHidden()) {
304:                    return;
305:                } else if (file.isDirectory()) {
306:                    fileType = "D";
307:                    String name = file.getName();
308:                    //  Remove trailing "/" from directory name.
309:                    fileName = name.substring(0, name.lastIndexOf("/"));
310:                } else if (file.isFile()) {
311:                    fileType = "-";
312:                    fileName = file.getName();
313:                } else {
314:                    //            logger.log(Level.SEVERE, "WinFile: file "  + file.getName() + "is of Unknown type.");
315:                    Object[] params6 = { file.getName(), "is of Unknown type." };
316:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1211", params6);
317:                    return;
318:                }
319:
320:                Date d = new Date(file.lastModified());
321:                SimpleDateFormat sdf = new SimpleDateFormat(
322:                        "MMM dd yy, hh:mm a");
323:                sdf.setCalendar(new GregorianCalendar(TimeZone.getDefault()));
324:                fileLastModified = sdf.format(d);
325:
326:                fileListings.add(fileType);
327:                fileListings.add(fileName);
328:                fileListings.add(new Long(file.length()).toString());
329:                fileListings.add(fileLastModified);
330:            }
331:
332:            //-------------------------------------------------
333:            // Make a local copy of the requested file.
334:            // Send back the local filename to the calling prog.
335:            //-------------------------------------------------
336:            public String getFile(String username, String password,
337:                    String machine, String share, String file,
338:                    String directory, String domain, String localTempDir,
339:                    NetFileResource nfRes) throws NetFileException {
340:
341:                try {
342:                    Long randomNo = new Long(System.currentTimeMillis());
343:                    if (file.startsWith("\"")) {
344:                        file = file.substring(1, file.length());
345:                    }
346:                    String localTempFileName = localTempDir + "/"
347:                            + randomNo.toString() + username.toUpperCase()
348:                            + file;
349:
350:                    SmbHandler handler = new SmbHandler(username, password,
351:                            domain);
352:                    directory = handleDirectoryNames(directory);
353:                    URL fromUrl = constructURL(machine, "/" + share + directory
354:                            + "/" + file, handler);
355:                    SmbFile from = (SmbFile) fromUrl.openConnection();
356:
357:                    SmbFileInputStream in = new SmbFileInputStream(from);
358:                    FileOutputStream out = new FileOutputStream(
359:                            localTempFileName);
360:
361:                    byte[] b = new byte[8192];
362:                    int n;
363:                    while ((n = in.read(b)) >= 0) {
364:                        out.write(b, 0, n);
365:                    }
366:
367:                    in.close();
368:                    out.close();
369:
370:                    return localTempFileName;
371:
372:                } catch (Exception ex) {
373:                    //                logger.log(Level.SEVERE, "Exception in obtaining the file " + file,ex);
374:                    Object[] params7 = { file, ex };
375:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1212", params7);
376:                    if (ex instanceof  SmbException) {
377:                        SmbException se = (SmbException) ex;
378:                        return "ERROR:"
379:                                + getErrorString(se.getNtStatus(), nfRes);
380:                    } else {
381:                        return "ERROR:" + nfRes.getString("error21");
382:                    }
383:                }
384:            }
385:
386:            /*
387:             *  Get the Input stream for the source File. This method eliminates the
388:             *  need to create temporary files on the portal server side.
389:             */
390:            public InputStream getInputStream(String username, String password,
391:                    String domain, String machine, String share,
392:                    String directory, String file, NetFileResource nfRes)
393:                    throws NetFileException {
394:                try {
395:                    SmbHandler handler = new SmbHandler(username, password,
396:                            domain);
397:                    directory = handleDirectoryNames(directory);
398:                    URL fromUrl = constructURL(machine, "/" + share + directory
399:                            + "/" + file, handler);
400:                    //                logger.log(Level.SEVERE, "WinFile: getWinInputStream(), fromUrl--" + fromUrl.toString());
401:                    Object[] params8 = { fromUrl.toString() };
402:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1213", params8);
403:                    SmbFile from = (SmbFile) fromUrl.openConnection();
404:
405:                    return (new SmbFileInputStream(from));
406:
407:                } catch (Exception ex) {
408:                    //                logger.log(Level.SEVERE, "Exception in obtaining the InputStream for file " + file,ex);
409:                    Object[] params9 = { file, ex };
410:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1214", params9);
411:
412:                    if (ex instanceof  SmbException) {
413:                        SmbException se = (SmbException) ex;
414:                        throw new NetFileException(getErrorString(se
415:                                .getNtStatus(), nfRes));
416:                    } else {
417:                        throw new NetFileException(
418:                                NetFileException.KEY_IDENTIFIER_PREFIX
419:                                        + nfRes.getString("error21"));
420:                    }
421:                }
422:            }
423:
424:            /*
425:             *  Get the Output Stream for the target file. This method does not use
426:             *  the temporary files which were created earlier.
427:             */
428:            public OutputStream getOutputStream(String username,
429:                    String password, String domain, String machine,
430:                    String share, String directory, String file,
431:                    NetFileResource nfRes) throws NetFileException {
432:                return this .getOutputStream(username, password, domain,
433:                        machine, share, directory, file, nfRes, false);
434:            }
435:
436:            public OutputStream getOutputStream(String username,
437:                    String password, String domain, String machine,
438:                    String share, String directory, String file,
439:                    NetFileResource nfRes, boolean append)
440:                    throws NetFileException {
441:
442:                try {
443:                    SmbHandler handler = new SmbHandler(username, password,
444:                            domain);
445:                    directory = handleDirectoryNames(directory);
446:                    URL toUrl = constructURL(machine, "/" + share + directory
447:                            + "/" + file, handler);
448:                    SmbFile to = (SmbFile) toUrl.openConnection();
449:                    if (append) {
450:                        return (new SmbFileOutputStream(to, true));
451:                    } else {
452:                        return (new SmbFileOutputStream(to));
453:                    }
454:                } catch (Exception e) {
455:                    //                logger.log(Level.SEVERE, "Exception getting OutputStream for  file-" + file,e);
456:                    Object[] params10 = { file, e };
457:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1215", params10);
458:
459:                    if (e instanceof  SmbException) {
460:                        SmbException se = (SmbException) e;
461:                        throw new NetFileException(getErrorString(se
462:                                .getNtStatus(), nfRes));
463:                    } else {
464:                        throw new NetFileException(
465:                                NetFileException.KEY_IDENTIFIER_PREFIX
466:                                        + nfRes.getString("error21"));
467:                    }
468:                }
469:            }
470:
471:            public String[] doSearch(String username, String password,
472:                    String machine, String VMSnam, String pattern,
473:                    String directory, String domain, int maxsrchdir,
474:                    NetFileResource nfRes) throws NetFileException {
475:
476:                searchResults = new ArrayList();
477:
478:                search(username, password, machine, VMSnam, pattern, directory,
479:                        domain, maxsrchdir, nfRes);
480:                if (directoriesTraversed > maxsrchdir) {
481:                    String[] searchStr = new String[searchResults.size() + 1];
482:                    searchStr[0] = "EXCEED:" + nfRes.getString("maxSearch");
483:                    for (int i = 1; i < searchStr.length; i++) {
484:                        searchStr[i] = (String) searchResults.get(i - 1);
485:                    }
486:                    return searchStr;
487:                }
488:
489:                if (searchResults.size() == 0) {
490:                    return new String[] { "        " };
491:                } else {
492:                    String[] finalSearchResults = new String[searchResults
493:                            .size()];
494:                    for (int i = 0; i < finalSearchResults.length; ++i) {
495:                        finalSearchResults[i] = (String) searchResults.get(i);
496:                    }
497:                    return finalSearchResults;
498:                }
499:            }
500:
501:            void search(String username, String password, String machine,
502:                    String VMSnam, String pattern, String directory,
503:                    String domain, int maxsrchdir, NetFileResource nfRes)
504:                    throws NetFileException {
505:
506:                try {
507:                    ++directoriesTraversed;
508:                    //            logger.info(this + ":Directory number being searched=" + directoriesTraversed);
509:                    Object[] params11 = { ":Directory number being searched=",
510:                            new Integer(directoriesTraversed) };
511:                    logger.log(Level.INFO, "PSSRNF_CSPNSJ1216", params11);
512:                    if (directoriesTraversed > maxsrchdir) {
513:                        return;
514:                    }
515:
516:                    String[] fileListing = getDirectory(username, password,
517:                            machine, VMSnam, directory, domain, nfRes);
518:
519:                    for (int i = 0; ((i < fileListing.length) && (!(directoriesTraversed > maxsrchdir))); i = i + 4) {
520:                        if (fileListing[i].startsWith("ERROR:")) {
521:                            throw new NetFileException(new String[] {
522:                                    NetFileException.KEY_IDENTIFIER_PREFIX
523:                                            + "error13",
524:                                    NetFileException.KEY_IDENTIFIER_PREFIX
525:                                            + "textseperator",
526:                                    VMSnam + directory + "/" });
527:                        }
528:                        if (fileListing[i].equalsIgnoreCase("d")) {
529:                            String errorString = "";
530:                            String newDirectory = directory + "/"
531:                                    + fileListing[i + 1];
532:                            try {
533:                                search(username, password, machine, VMSnam,
534:                                        pattern, newDirectory, domain,
535:                                        maxsrchdir, nfRes);
536:                            } catch (NetFileException e) {
537:                                errorString = "(" + e.getMessage(nfRes) + ")";
538:                            }
539:                            if (fileListing[i + 1].indexOf(pattern) > -1) {
540:                                searchResults.add("/" + VMSnam + newDirectory
541:                                        + "/" + errorString);
542:                            }
543:                        } else {
544:                            if (fileListing[i + 1].indexOf(pattern) > -1) {
545:                                searchResults.add("/" + VMSnam + directory
546:                                        + "/" + fileListing[i + 1]);
547:                            }
548:                        }
549:                    }
550:                } catch (NetFileException e) {
551:                    throw e;
552:                } catch (Exception e) {
553:                    throw new NetFileException(
554:                            new String[] {
555:                                    NetFileException.KEY_IDENTIFIER_PREFIX
556:                                            + "error13",
557:                                    NetFileException.KEY_IDENTIFIER_PREFIX
558:                                            + "textseperator",
559:                                    VMSnam + directory + "/" });
560:                }
561:            }
562:
563:            public String doCreateDirectory(String machine, String share,
564:                    String username, String password, String domain,
565:                    String parentDir, String dirToCreate, NetFileResource nfRes)
566:                    throws NetFileException {
567:
568:                try {
569:                    SmbHandler handler = new SmbHandler(username, password,
570:                            domain);
571:                    parentDir = handleDirectoryNames(parentDir);
572:                    URL mkdirUrl = constructURL(machine, "/" + share
573:                            + parentDir + "/" + dirToCreate, handler);
574:                    SmbFile dir = (SmbFile) mkdirUrl.openConnection();
575:
576:                    dir.mkdir();
577:
578:                    return nfRes.getString("warning52");
579:                } catch (Exception e) {
580:                    //                logger.log(Level.SEVERE, "Problem in creating directory "+dirToCreate+" on machine "+machine, e);
581:                    Object[] params12 = { dirToCreate, " on machine ", machine,
582:                            e };
583:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1217", params12);
584:
585:                    if (e instanceof  SmbException) {
586:                        SmbException se = (SmbException) e;
587:                        throw new NetFileException(getErrorString(se
588:                                .getNtStatus(), nfRes));
589:                    } else {
590:                        throw new NetFileException(
591:                                NetFileException.KEY_IDENTIFIER_PREFIX
592:                                        + "could_not_create_directory");
593:                    }
594:                }
595:            }
596:
597:            public String doDeleteFile(String username, String password,
598:                    String machine, String share, String file,
599:                    String directory, String domain, NetFileResource nfRes)
600:                    throws NetFileException {
601:
602:                try {
603:                    SmbHandler handler = new SmbHandler(username, password,
604:                            domain);
605:                    directory = handleDirectoryNames(directory);
606:                    URL deleteUrl = constructURL(machine, "/" + share
607:                            + directory + "/" + file, handler);
608:                    SmbFile deleteFile = (SmbFile) deleteUrl.openConnection();
609:
610:                    deleteFile.delete();
611:
612:                } catch (Exception e) {
613:                    //                logger.log(Level.SEVERE, "Problem in deleting file "+file+" on machine "+machine, e);
614:                    Object[] params13 = { file, " on machine ", machine, e };
615:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1218", params13);
616:
617:                    if (e instanceof  SmbException) {
618:                        SmbException se = (SmbException) e;
619:                        return "ERROR:"
620:                                + getErrorString(se.getNtStatus(), nfRes);
621:                    } else {
622:                        return "ERROR:"
623:                                + nfRes
624:                                        .getString("error_in_connection_to_server");
625:                    }
626:                }
627:                return nfRes.getString("info5");
628:            }
629:
630:            public void doRenameFile(String username, String password,
631:                    String machine, String domain, String share,
632:                    String directory, String oldFileName, String newFileName,
633:                    NetFileResource nfRes) throws NetFileException {
634:
635:                try {
636:                    SmbHandler handler = new SmbHandler(username, password,
637:                            domain);
638:                    directory = handleDirectoryNames(directory);
639:                    URL fromUrl = constructURL(machine, "/" + share + directory
640:                            + "/" + oldFileName, handler);
641:                    SmbFile from = (SmbFile) fromUrl.openConnection();
642:
643:                    URL toUrl = constructURL(machine, "/" + share + directory
644:                            + "/" + newFileName, handler);
645:                    SmbFile to = (SmbFile) toUrl.openConnection();
646:
647:                    from.renameTo(to);
648:
649:                } catch (SmbException se) {
650:                    throw new NetFileException(getErrorString(se.getNtStatus(),
651:                            nfRes));
652:                } catch (Exception e) {
653:                    throw new NetFileException(nfRes.getString("renameError"));
654:                }
655:            }
656:
657:            private String getSmbNameForEncoding(String encoding) {
658:                FileOption file_option = new FileOption(logManager, encoding,
659:                        this .ssoToken);
660:                return file_option.getPlatformLocalisedString(encoding);
661:            }
662:
663:            public String getErrorString(int errorCode, NetFileResource nfRes) {
664:                /*
665:                 *  This method handles hundreds of error codes that may be returned
666:                 *  by a CIFS server. Instead of having an Exception for each, representation
667:                 *  all of these exceptions are in SmbException class. The SmbException
668:                 *  class has errorClass and errorCode members to allow catch routines
669:                 *  to examine these values and potentially act on them accordingly.
670:                 */
671:                String result = "";
672:                switch (errorCode) {
673:                // start of dos status code
674:                case 0x00010001:
675:                case 0x00010002:
676:                    result += nfRes.getString("ERRDOS_badfunc");
677:                    break;
678:                case 0x00020001:
679:                    result += nfRes.getString("ERRDOS_badfile");
680:                    break;
681:                case 0x00020002:
682:                    result += nfRes.getString("ERRDOS_badpswd");
683:                    break;
684:                case 0x00030001:
685:                    result += nfRes.getString("ERRDOS_badfile");
686:                    break;
687:                case 0x00030002:
688:                    result += nfRes.getString("ERRDOS_rsrv");
689:                    break;
690:                case 0x00040002:
691:                    result += nfRes.getString("ERRDOS_noright");
692:                    break;
693:                case 0x00050001:
694:                    result += nfRes.getString("ERRDOS_noaccess");
695:                    break;
696:                case 0x00050002:
697:                    result += nfRes.getString("ERRDOS_badtid");
698:                    break;
699:                case 0x00060001:
700:                    result += nfRes.getString("ERRDOS_badfid");
701:                    break;
702:                case 0x00060002:
703:                    result += nfRes.getString("ERRDOS_badnetname");
704:                    break;
705:                case 0x00080001:
706:                    result += nfRes.getString("ERRDOS_nospace");
707:                    break;
708:                case 0x00130003:
709:                    result += nfRes.getString("ERRDOS_wrtptd");
710:                    break;
711:                case 0x00150003:
712:                    result += nfRes.getString("ERRDOS_notready");
713:                    break;
714:                case 0x001f0001:
715:                    result += nfRes.getString("ERRDOS_baddev");
716:                    break;
717:                case 0x001f0003:
718:                    result += nfRes.getString("ERRDOS_baddev");
719:                    break;
720:                case 0x00200001:
721:                    result += nfRes.getString("ERRDOS_badshare");
722:                    break;
723:                case 0x00200003:
724:                    result += nfRes.getString("ERRDOS_badshare");
725:                    break;
726:                case 0x00210003:
727:                    result += nfRes.getString("ERRDOS_lckfl");
728:                    break;
729:                case 0x00270003:
730:                    result += nfRes.getString("ERRDOS_dskful");
731:                    break;
732:                case 0x00340001:
733:                    result += nfRes.getString("ERRDOS_dupname");
734:                    break;
735:                case 0x00430001:
736:                    result += nfRes.getString("ERRDOS_badnetname");
737:                    break;
738:                case 0x00470001:
739:
740:                    result += nfRes.getString("ERRDOS_nomoreconn");
741:                    break;
742:                case 0x00500001:
743:                    result += nfRes.getString("ERRDOS_filexists");
744:                    break;
745:                case 0x00570001:
746:                    result += nfRes.getString("ERRDOS_badparm");
747:                    break;
748:                case 0x005a0002:
749:                    result += nfRes.getString("ERRDOS_manyuid");
750:                    break;
751:                case 0x006d0001:
752:                    result += nfRes.getString("ERRDOS_brokenpipe");
753:                    break;
754:                case 0x007b0001:
755:                    result += nfRes.getString("ERRDOS_invname");
756:                    break;
757:                case 0x00910001:
758:                    result += nfRes.getString("ERRDOS_notempty");
759:                    break;
760:                case 0x00b70001:
761:                    result += nfRes.getString("ERRDOS_filexists");
762:                    break;
763:                case 0x00e70001:
764:                    result += nfRes.getString("ERRDOS_pipebusy");
765:                    break;
766:                case 0x00e80001:
767:                    result += nfRes.getString("ERRDOS_nodata");
768:                    break;
769:                case 0x00e90001:
770:                    result += nfRes.getString("ERRDOS_noproc");
771:                    break;
772:                case 0x00ea0001:
773:                    result += nfRes.getString("ERRDOS_moredata");
774:                    break;
775:                case 0x08bf0002:
776:                    result += nfRes.getString("ERRDOS_accexp");
777:                    break;
778:                case 0x08c00002:
779:                    result += nfRes.getString("ERRDOS_badusr");
780:                    break;
781:                case 0x08c10002:
782:                    result += nfRes.getString("ERRDOS_usrnotal");
783:                    break;
784:                case 0x08c20002:
785:                    result += nfRes.getString("ERRDOS_pswdexp");
786:                    break;
787:                // end of dos status code                    
788:                //start of nt status codes
789:                case SmbException.NT_STATUS_OK:
790:                    result += nfRes.getString("NT_STATUS_OK");
791:                    break;
792:                case SmbException.NT_STATUS_UNSUCCESSFUL:
793:                    result += nfRes.getString("NT_STATUS_UNSUCCESSFUL");
794:                    break;
795:                case SmbException.NT_STATUS_NOT_IMPLEMENTED:
796:                    result += nfRes.getString("NT_STATUS_NOT_IMPLEMENTED");
797:                    break;
798:                case SmbException.NT_STATUS_INVALID_INFO_CLASS:
799:                    result += nfRes.getString("NT_STATUS_INVALID_INFO_CLASS");
800:                    break;
801:                case SmbException.NT_STATUS_ACCESS_VIOLATION:
802:                    result += nfRes.getString("NT_STATUS_ACCESS_VIOLATION");
803:                    break;
804:                case SmbException.NT_STATUS_INVALID_HANDLE:
805:                    result += nfRes.getString("NT_STATUS_INVALID_HANDLE");
806:                    break;
807:                case SmbException.NT_STATUS_NO_SUCH_DEVICE:
808:                    result += nfRes.getString("NT_STATUS_NO_SUCH_DEVICE");
809:                    break;
810:                case SmbException.NT_STATUS_NO_SUCH_FILE:
811:                    result += nfRes.getString("NT_STATUS_NO_SUCH_FILE");
812:                    break;
813:                case SmbException.NT_STATUS_ACCESS_DENIED:
814:                    result += nfRes.getString("NT_STATUS_ACCESS_DENIED");
815:                    break;
816:                case SmbException.NT_STATUS_OBJECT_NAME_INVALID:
817:                    result += nfRes.getString("NT_STATUS_OBJECT_NAME_INVALID");
818:                    break;
819:                case SmbException.NT_STATUS_OBJECT_NAME_NOT_FOUND:
820:                    result += nfRes
821:                            .getString("NT_STATUS_OBJECT_NAME_NOT_FOUND");
822:                    break;
823:                case SmbException.NT_STATUS_OBJECT_NAME_COLLISION:
824:                    result += nfRes
825:                            .getString("NT_STATUS_OBJECT_NAME_COLLISION");
826:                    break;
827:                case SmbException.NT_STATUS_PORT_DISCONNECTED:
828:                    result += nfRes.getString("NT_STATUS_PORT_DISCONNECTED");
829:                    break;
830:                case SmbException.NT_STATUS_OBJECT_PATH_NOT_FOUND:
831:                    result += nfRes
832:                            .getString("NT_STATUS_OBJECT_PATH_NOT_FOUND");
833:                    break;
834:                case SmbException.NT_STATUS_OBJECT_PATH_SYNTAX_BAD:
835:                    result += nfRes
836:                            .getString("NT_STATUS_OBJECT_PATH_SYNTAX_BAD");
837:                    break;
838:                case SmbException.NT_STATUS_SHARING_VIOLATION:
839:                    result += nfRes.getString("NT_STATUS_SHARING_VIOLATION");
840:                    break;
841:                case SmbException.NT_STATUS_DELETE_PENDING:
842:                    result += nfRes.getString("NT_STATUS_DELETE_PENDING");
843:                    break;
844:                case SmbException.NT_STATUS_NO_SUCH_USER:
845:                    result += nfRes.getString("NT_STATUS_NO_SUCH_USER");
846:                    break;
847:                case SmbException.NT_STATUS_WRONG_PASSWORD:
848:                    result += nfRes.getString("NT_STATUS_WRONG_PASSWORD");
849:                    break;
850:                case SmbException.NT_STATUS_LOGON_FAILURE:
851:                    result += nfRes.getString("NT_STATUS_LOGON_FAILURE");
852:                    break;
853:                case SmbException.NT_STATUS_ACCOUNT_RESTRICTION:
854:                    result += nfRes.getString("NT_STATUS_ACCOUNT_RESTRICTION");
855:                    break;
856:                case SmbException.NT_STATUS_INVALID_LOGON_HOURS:
857:                    result += nfRes.getString("NT_STATUS_INVALID_LOGON_HOURS");
858:                    break;
859:                case SmbException.NT_STATUS_INVALID_WORKSTATION:
860:                    result += nfRes.getString("NT_STATUS_INVALID_WORKSTATION");
861:                    break;
862:                case SmbException.NT_STATUS_PASSWORD_EXPIRED:
863:                    result += nfRes.getString("NT_STATUS_PASSWORD_EXPIRED");
864:                    break;
865:                case SmbException.NT_STATUS_ACCOUNT_DISABLED:
866:                    result += nfRes
867:                            .getString("SmbException.NT_STATUS_ACCOUNT_DISABLED");
868:                    break;
869:                case SmbException.NT_STATUS_INSTANCE_NOT_AVAILABLE:
870:                    result += nfRes
871:                            .getString("NT_STATUS_INSTANCE_NOT_AVAILABLE");
872:                    break;
873:                case SmbException.NT_STATUS_PIPE_NOT_AVAILABLE:
874:                    result += nfRes.getString("NT_STATUS_PIPE_NOT_AVAILABLE");
875:                    break;
876:                case SmbException.NT_STATUS_INVALID_PIPE_STATE:
877:                    result += nfRes.getString("NT_STATUS_INVALID_PIPE_STATE");
878:                    break;
879:                case SmbException.NT_STATUS_PIPE_BUSY:
880:                    result += nfRes.getString("NT_STATUS_PIPE_BUSY");
881:                    break;
882:                case SmbException.NT_STATUS_PIPE_DISCONNECTED:
883:                    result += nfRes.getString("NT_STATUS_PIPE_DISCONNECTED");
884:                    break;
885:                case SmbException.NT_STATUS_PIPE_CLOSING:
886:                    result += nfRes.getString("NT_STATUS_PIPE_CLOSING");
887:                    break;
888:                case SmbException.NT_STATUS_PIPE_LISTENING:
889:                    result += nfRes.getString("NT_STATUS_PIPE_LISTENING");
890:                    break;
891:                case SmbException.NT_STATUS_FILE_IS_A_DIRECTORY:
892:                    result += nfRes.getString("NT_STATUS_FILE_IS_A_DIRECTORY");
893:                    break;
894:                case SmbException.NT_STATUS_NETWORK_NAME_DELETED:
895:                    result += nfRes.getString("NT_STATUS_NETWORK_NAME_DELETED");
896:                    break;
897:                case SmbException.NT_STATUS_BAD_NETWORK_NAME:
898:                    result += nfRes.getString("NT_STATUS_BAD_NETWORK_NAME");
899:                    break;
900:                case SmbException.NT_STATUS_NOT_A_DIRECTORY:
901:                    result += nfRes.getString("NT_STATUS_NOT_A_DIRECTORY");
902:                    break;
903:                case SmbException.NT_STATUS_CANNOT_DELETE:
904:                    result += nfRes.getString("NT_STATUS_CANNOT_DELETE");
905:                    break;
906:                case SmbException.NT_STATUS_PIPE_BROKEN:
907:                    result += nfRes.getString("NT_STATUS_PIPE_BROKEN");
908:                    break;
909:                case SmbException.NT_STATUS_LOGON_TYPE_NOT_GRANTED:
910:                    result += nfRes
911:                            .getString("SmbException.NT_STATUS_LOGON_TYPE_NOT_GRANTED");
912:                    break;
913:                case SmbException.NT_STATUS_TRUSTED_DOMAIN_FAILURE:
914:                    result += nfRes
915:                            .getString("NT_STATUS_TRUSTED_DOMAIN_FAILURE");
916:                    break;
917:                case SmbException.NT_STATUS_ACCOUNT_LOCKED_OUT:
918:                    result += nfRes
919:                            .getString("SmbException.NT_STATUS_ACCOUNT_LOCKED_OUT");
920:                    break;
921:                case SmbException.NT_STATUS_PATH_NOT_COVERED:
922:                    result += nfRes.getString("NT_STATUS_PATH_NOT_COVERED");
923:                    break;
924:                //end of nt status codes                    
925:                default:
926:                    result += "unknown error code: " + errorCode;
927:                }
928:                return result;
929:            }
930:
931:            public String doDeleteDirectory(String username, String password,
932:                    String machine, String share, String directory,
933:                    String domain, NetFileResource nfRes)
934:                    throws NetFileException {
935:                throw new NetFileException(
936:                        "Delete directory operation is not supported");
937:            }
938:
939:            /*
940:             *  Verifies for the given share, if it is accessible. For this, check whether
941:             *  the folders under a given share is accessible.
942:             */
943:            public boolean isShareAccessible(String username, String password,
944:                    String machine, String share, String domain,
945:                    NetFileResource nfRes) {
946:                boolean returnValue = false;
947:                try {
948:                    SmbHandler handler = new SmbHandler(username, password,
949:                            domain);
950:                    URL listingUrl = constructURL(machine, "/" + share + "/",
951:                            handler);
952:                    SmbFile file = (SmbFile) listingUrl.openConnection();
953:
954:                    SmbFile[] files = file.listFiles();
955:                    if (files != null)
956:                        returnValue = true;
957:                    else
958:                        returnValue = false;
959:                } catch (SmbException se) {
960:                    //            logger.log(Level.SEVERE, "WinFile: share - "  + share + " not accessible to user - " + username + "." +
961:                    logger.log(Level.SEVERE, "WinFile: share - " + share
962:                            + " not accessible to user - " + username + "."
963:                            + getErrorString(se.getNtStatus(), nfRes));
964:                    returnValue = false;
965:                } catch (Exception e) {
966:                    //            logger.log(Level.SEVERE, "WinFile: exception when checking for share accessibility," + e.toString());
967:                    Object[] params15 = { e.toString() };
968:                    logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1220", params15);
969:                    returnValue = false;
970:                }
971:                return returnValue;
972:            }
973:
974:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.