001: /**
002: * $Id: FtpFile.java,v 1.37 2005/11/30 11:26:33 ss150821 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.text.SimpleDateFormat;
020:
021: class FtpFile {
022:
023: protected NetFileLogManager logMgr = null;
024: private NetFileResource nfRes = null;
025: private static Logger logger = PortalLogger
026: .getLogger(FtpFile.class);
027: protected String s_machine_encoding;
028: protected FullFtpClient ffc = null;
029: protected FullFtpClient ffcOne = null;
030: protected int i_number_of_directories_traversed; //Keep track of number of directories scanned during single instance of a search
031: protected ArrayList alSearchResults = new ArrayList();
032:
033: FtpFile(NetFileLogManager log_Mgr, String s_machine_encoding) {
034: this .logMgr = log_Mgr;
035: this .i_number_of_directories_traversed = 0;
036: this .s_machine_encoding = s_machine_encoding;
037: }
038:
039: String[] ftpDir(String usernam, String passwrd, String machnam,
040: String VMSnam, String pattern, String dir_nam,
041: int maxsrchdir, NetFileResource nfr_user_locale_i18n_bucket)
042: throws NetFileException {
043: this .nfRes = nfr_user_locale_i18n_bucket;
044: this .ffc = initialiseFtpClient(usernam, passwrd, machnam,
045: VMSnam, dir_nam);
046:
047: search(usernam, passwrd, machnam, VMSnam, pattern, dir_nam,
048: maxsrchdir, nfr_user_locale_i18n_bucket);
049:
050: //Close FTP connection after Search is completed.
051: try {
052: if (ffc != null) {
053: ffc.quit();
054: }
055: } catch (IOException ioe) {
056: // logger.log(Level.SEVERE, "FtpFile, search - ", ioe);
057: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1055");
058: }
059:
060: if (i_number_of_directories_traversed > maxsrchdir) {
061: /*
062: * When maximum search directories has been exceeded, send the msg
063: * to the client along with the search results found till that point.
064: */
065: String[] searchResults = new String[alSearchResults.size() + 1];
066: searchResults[0] = "EXCEED:"
067: + nfr_user_locale_i18n_bucket
068: .getString("maxSearch");
069: for (int j = 1; j < searchResults.length; j++) {
070: searchResults[j] = (String) alSearchResults.get(j - 1);
071: }
072: return searchResults;
073: }
074:
075: if (alSearchResults.size() == 0) {
076: alSearchResults = null;
077: return new String[] { " " };
078: } else {
079: String[] sa_results = new String[alSearchResults.size()];
080: for (int j = 0; j < sa_results.length; ++j) {
081: sa_results[j] = (String) alSearchResults.get(j);
082: }
083: alSearchResults = null;
084: return sa_results;
085: }
086: }
087:
088: void search(String usernam, String passwrd, String machnam,
089: String VMSnam, String pattern, String dir_nam,
090: int maxsrchdir, NetFileResource nfr_user_locale_i18n_bucket)
091: throws NetFileException {
092:
093: try {
094: ++i_number_of_directories_traversed;
095: // logger.info(this+":Directory number being searched="+i_number_of_directories_traversed);
096: Object[] params1 = { ":Directory number being searched=",
097: new Integer(i_number_of_directories_traversed) };
098: logger.log(Level.INFO, "PSSRNF_CSPNSJ1056", params1);
099: if (i_number_of_directories_traversed > maxsrchdir) {
100: return;
101: }
102:
103: String[] sa_file_listing = getFTPDir(usernam, passwrd,
104: machnam, VMSnam, dir_nam,
105: nfr_user_locale_i18n_bucket, false);
106:
107: if ((sa_file_listing.length <= 0)
108: || (sa_file_listing[0] == null)) {
109: return;
110: }
111:
112: for (int i = 0; ((i < sa_file_listing.length) && (!(i_number_of_directories_traversed > maxsrchdir))); i = i + 4) {
113: if (sa_file_listing[i] == null) {
114: break;
115: }
116: if (sa_file_listing[i].equalsIgnoreCase("d")) {
117: String s_error = "";
118: String s_directory = dir_nam + "/"
119: + sa_file_listing[i + 1];
120: try {
121: search(usernam, passwrd, machnam, VMSnam,
122: pattern, s_directory, maxsrchdir,
123: nfr_user_locale_i18n_bucket);
124: } catch (NetFileException e) {
125: s_error = "("
126: + e
127: .getMessage(nfr_user_locale_i18n_bucket)
128: + ")";
129: if (i_number_of_directories_traversed < maxsrchdir)
130: continue;
131: else
132: throw e;
133: } catch (Exception e) {
134: // logger.log(Level.SEVERE, "Exception in searching directory "+s_directory+" in "+VMSnam,e);
135: Object[] params2 = { s_directory, " in ",
136: VMSnam, e };
137: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1057",
138: params2);
139: s_error = "("
140: + nfr_user_locale_i18n_bucket
141: .getString("error13") + ")";
142: }
143: if (sa_file_listing[i + 1].indexOf(pattern) > -1) {
144: alSearchResults.add("/" + VMSnam + s_directory
145: + "/" + s_error);
146: }
147: } else {
148: if (sa_file_listing[i + 1].indexOf(pattern) > -1) {
149: alSearchResults.add("/" + VMSnam + dir_nam
150: + "/" + sa_file_listing[i + 1]);
151: }
152: }
153: }
154: } catch (NetFileException e) {
155: if (i_number_of_directories_traversed < maxsrchdir)
156: ;
157: else
158: throw e;
159: } catch (Exception e) {
160: // logger.log(Level.SEVERE, "Exception in searching "+VMSnam+dir_nam+"/",e);
161: Object[] params3 = { VMSnam, dir_nam, "/", e };
162: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1058", params3);
163: throw new NetFileException(new String[] {
164: NetFileException.KEY_IDENTIFIER_PREFIX + "error13",
165: NetFileException.KEY_IDENTIFIER_PREFIX
166: + "textseperator", VMSnam + dir_nam + "/" });
167: }
168: }
169:
170: InputStream getInputFTPStream(String usernam, String passwrd,
171: String machnam, String VMSnam, String mainfilenam,
172: String dir_nam) throws NetFileException {
173:
174: Exception e = null;
175: InputStream ftpin = null;
176:
177: try {
178: ffc = initialiseFtpClient(usernam, passwrd, machnam,
179: VMSnam, dir_nam);
180: ftpin = ffc.get(mainfilenam);
181:
182: } catch (Exception ex) {
183: e = ex;
184: }
185: performFinalProcessing(e, "Exceptione obtaining the FTP file",
186: ffc);
187: return ftpin;
188: }
189:
190: OutputStream getOutputFTPStream(String usernam, String passwrd,
191: String machnam, String VMSnam, String remotefil_arg,
192: String dir_nam) throws NetFileException {
193: return this .getOutputFTPStream(usernam, passwrd, machnam,
194: VMSnam, remotefil_arg, dir_nam, false);
195: }
196:
197: OutputStream getOutputFTPStream(String usernam, String passwrd,
198: String machnam, String VMSnam, String remotefil_arg,
199: String dir_nam, boolean append) throws NetFileException {
200:
201: Exception e = null;
202: OutputStream ftpout = null;
203:
204: try {
205: ffc = initialiseFtpClient(usernam, passwrd, machnam,
206: VMSnam, dir_nam);
207: if (append) {
208: ftpout = ffc.append(remotefil_arg);
209: } else {
210: ftpout = ffc.put(remotefil_arg);
211: }
212: } catch (Exception ex) {
213: e = ex;
214: performFinalProcessing(e, "Could upload file", ffc);
215: }
216: return ftpout;
217: }
218:
219: void closeFtpFile() {
220: try {
221: if (this .ffc != null) {
222: this .ffc.quit();
223: }
224: } catch (IOException ioe) {
225: }
226: try {
227: if (this .ffcOne != null) {
228: this .ffcOne.quit();
229: }
230: } catch (IOException ioe) {
231: } finally {
232: ffc = null;
233: ffcOne = null;
234: }
235: }
236:
237: String getFTPFile(String usernam, String passwrd, String machnam,
238: String VMSnam, String mainfilenam, String dir_nam,
239: String tmpdir, NetFileResource nfr_user_locale_i18n_bucket,
240: String usersession) throws NetFileException {
241:
242: String mknodfilename = "";
243: String txtouting = "";
244: Exception e = null;
245: FullFtpClient ffc = null;
246: ByteArrayOutputStream buftxt = new ByteArrayOutputStream();
247: byte[] buffer = new byte[8 * 1024];
248:
249: try {
250: int c;
251: InputStream ftpout = null;
252: BufferedOutputStream bos = null;
253:
254: String tempdir = tmpdir;
255: Long random = new Long(System.currentTimeMillis());
256: mknodfilename = tempdir + "/" + random.toString()
257: + usernam.toUpperCase() + mainfilenam;
258: File newfil = new File(mknodfilename);
259: if (newfil.exists()) {
260: newfil.delete();
261: }
262: bos = new BufferedOutputStream(new FileOutputStream(newfil));
263: ffc = initialiseFtpClient(usernam, passwrd, machnam,
264: VMSnam, dir_nam);
265: ftpout = ffc.get(mainfilenam);
266:
267: while ((c = ftpout.read(buffer)) > -1) {
268: bos.write(buffer, 0, c);
269: buftxt.write(buffer, 0, c);
270: }
271: ftpout.close();
272: bos.flush();
273: bos.close();
274: ffc.closeServer();
275:
276: txtouting = buftxt.toString();
277: if (txtouting.startsWith("ERROR:")
278: || txtouting.indexOf(nfr_user_locale_i18n_bucket
279: .getString("permissiondenied")) >= 0
280: || txtouting.indexOf(nfr_user_locale_i18n_bucket
281: .getString("nosuchfile")) >= 0) {
282: FileOption fil4 = new FileOption(logMgr,
283: s_machine_encoding);
284: fil4.doError(nfr_user_locale_i18n_bucket
285: .getString("error6"));
286: return "ERROR:"
287: + nfr_user_locale_i18n_bucket
288: .getString("error6");
289: }
290: } catch (Exception ex) {
291: e = ex;
292: } finally {
293: try {
294: if (buftxt != null) {
295: buftxt.close();
296: }
297: } catch (IOException ioe) {
298: }
299: }
300: performFinalProcessing(e, "Exceptione obtaining the FTP file",
301: ffc);
302: return mknodfilename;
303: }
304:
305: //-------------------------------------------------
306: // Delete the local file onto the target system.
307: //-------------------------------------------------
308: String delFTPFile(String username, String password, String machine,
309: String share, String file_to_delete, String directory,
310: NetFileResource nfr_user_locale_i18n_bucket)
311: throws NetFileException {
312: FullFtpClient ffc = null;
313: Exception e = null;
314: try {
315: if (this .ffc == null) {
316: ffc = initialiseFtpClient(username, password, machine,
317: share, directory);
318: this .ffc = ffc;
319: } else {
320: ffc = this .ffc;
321: }
322: ffc.delete(file_to_delete);
323: } catch (Exception ex) {
324: e = ex;
325: }
326: performFinalProcessing(e, "Could not delete file", ffc);
327: return nfr_user_locale_i18n_bucket.getString("info5");
328: }
329:
330: String reEncodeString(String string) throws Exception {
331: return new String(string.getBytes(), s_machine_encoding);
332: }
333:
334: String[] getFTPDir(String username, String password,
335: String machname, String VMSname, String dirS,
336: NetFileResource nfr_user_locale_i18n_bucket)
337: throws NetFileException {
338: return getFTPDir(username, password, machname, VMSname, dirS,
339: nfr_user_locale_i18n_bucket, true);
340: }
341:
342: private String[] getFTPDir(String username, String password,
343: String machname, String VMSname, String dirS,
344: NetFileResource nfr_user_locale_i18n_bucket,
345: boolean closeConn) throws NetFileException {
346: FullFtpClient full_ftp_client = null;
347: Exception e = null;
348: NetFileException nfe = null;
349: String[] file_listing = new String[] {};
350:
351: ArrayList names_list = null;
352: ArrayList full_listing = null;
353: try {
354: if (this .ffc == null) {
355: full_ftp_client = initialiseFtpClient(username,
356: password, machname, VMSname, dirS);
357: } else {
358: full_ftp_client = this .ffc;
359: full_ftp_client.cd(VMSname + dirS);
360: }
361: names_list = getNamesList(full_ftp_client);
362: full_listing = getList(full_ftp_client, false);
363: String[][] listing = processListings(names_list,
364: full_listing);
365: int number_of_files = names_list.size();
366: if (number_of_files == 0) {
367: /*
368: * In case of windows FTP server, it returns 0 for empty folders.
369: * So instead of returning null string, return string of len = 1,
370: * so that there will not be any exception thrown when accessed.
371: */
372: file_listing = new String[1];
373: } else {
374: file_listing = new String[number_of_files * 4];
375: }
376: int j = 0;
377:
378: for (int i = 0; i < number_of_files; ++i) {
379: if (listing[i][1].equals("..")
380: || listing[i][1].startsWith(".")) {
381: continue;
382: } else {
383: file_listing[j++] = listing[i][0];
384: file_listing[j++] = listing[i][1];
385: file_listing[j++] = listing[i][3];
386: file_listing[j++] = listing[i][2];
387: }
388: }
389: } catch (NetFileException ex) {
390: nfe = ex;
391: } catch (Exception ex) {
392: e = ex;
393: } finally {
394: names_list = null;
395: full_listing = null;
396: if (closeConn) {
397: try {
398: full_ftp_client.quit();
399: } catch (Exception ge) {
400: }
401: performFinalProcessingForGetFTPDir(nfe, e,
402: "Could not obtain FTP listing", full_ftp_client);
403: }
404: }
405: return file_listing;
406: }
407:
408: private ArrayList getNamesList(FullFtpClient full_ftp_client)
409: throws Exception {
410: return getList(full_ftp_client, true);
411: }
412:
413: private ArrayList getList(FullFtpClient full_ftp_client,
414: boolean names_only) throws Exception {
415: if (full_ftp_client == null) {
416: throw new IllegalArgumentException("null full ftp client");
417: }
418: sun.net.TelnetInputStream list = null;
419: ArrayList v_list = new ArrayList();
420: if (names_only) {
421: list = full_ftp_client.nlist();
422: } else {
423: list = full_ftp_client.list(true);
424: }
425:
426: java.io.InputStreamReader ipsr_list = new java.io.InputStreamReader(
427: list, s_machine_encoding);
428: java.io.BufferedReader br_list = new java.io.BufferedReader(
429: ipsr_list);
430: String s_input_line = null;
431: while (true) {
432: s_input_line = br_list.readLine();
433: if (s_input_line == null)
434: break;
435: v_list.add(s_input_line);
436: }
437: br_list.close();
438: ipsr_list.close();
439: list.close();
440: s_input_line = null;
441: br_list = null;
442: ipsr_list = null;
443: list = null;
444: return v_list;
445: }
446:
447: private String[][] processListings(ArrayList v_file_names,
448: ArrayList v_full_listing) throws Exception {
449:
450: int listing_size = v_full_listing.size();
451: if (listing_size <= 0)
452: return new String[][] {};
453:
454: String last_entry = (String) (v_full_listing
455: .get(listing_size - 1));
456:
457: boolean is_windows = !(last_entry.startsWith("-")
458: || last_entry.startsWith("d")
459: || last_entry.startsWith("l")
460: || last_entry.startsWith("d")
461: || last_entry.startsWith("b")
462: || last_entry.startsWith("c")
463: || last_entry.startsWith("p")
464: || last_entry.startsWith("s") || last_entry
465: .startsWith("D"));
466:
467: String[][] listing = null;
468: if (is_windows)
469: listing = processMSDirectoryListingType(v_file_names,
470: v_full_listing);
471: else
472: listing = processUnixDirectoryListingType(v_file_names,
473: v_full_listing);
474:
475: return listing;
476: }
477:
478: private void mergeListing(ArrayList fileListing,
479: ArrayList fullListing) {
480: /*
481: * NetFile assumes that both file listing and full file listing are same.
482: * But in cases when they are unequal ( say when the FTP server returns
483: * different listings for file names and full file listing) then it throws
484: * exception to the user without giving him the listings.
485: *
486: * This method handles the cases when the fileListing and full listing
487: * are of different sizes.
488: *
489: * Assumption: fileListing and fullListing have the files arranged in
490: * the same order except with few special files that are listed
491: * only in the fullListing and not in fileListing.
492: */
493: int fileIndex, listingIndex;
494: String fileName, fullListingRow;
495: for (fileIndex = 0, listingIndex = 0; fileIndex < fileListing
496: .size(); fileIndex++, listingIndex++) {
497: fileName = (String) fileListing.get(fileIndex);
498: fullListingRow = (String) fullListing.get(listingIndex);
499: if (fullListingRow.indexOf(fileName) != -1) {
500: continue;
501: } else {
502: do {
503: fullListing.remove(listingIndex);
504: fullListingRow = (String) fullListing
505: .get(listingIndex);
506: } while (fullListingRow.indexOf(fileName) == -1);
507: fileIndex--;
508: listingIndex--;
509: }
510: }
511: }
512:
513: private String getModifiedDateFormat(String modifiedTime) {
514: Date d;
515: if (modifiedTime.indexOf('-') != -1) {
516: SimpleDateFormat format = new SimpleDateFormat(
517: "MM-dd-yy hh:mma");
518: format.setCalendar(new GregorianCalendar(TimeZone
519: .getDefault()));
520: try {
521: d = format.parse(modifiedTime);
522: } catch (Exception e) {
523: // logger.log(Level.SEVERE, "Exception: ", e);
524: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1059");
525: return null;
526: }
527: } else {
528: GregorianCalendar calendar = new GregorianCalendar(TimeZone
529: .getDefault());
530: String strArray[] = new String[3];
531: StringTokenizer stok = new StringTokenizer(modifiedTime,
532: " ");
533: int tokenCount = stok.countTokens();
534: for (int i = 0; i < tokenCount; i++) {
535: strArray[i] = stok.nextToken();
536: }
537: calendar.set(Calendar.MONTH, getMonthCode(strArray[0]));
538: calendar.set(Calendar.DATE, Integer.parseInt(strArray[1]));
539: String dateStr = strArray[2];
540: if (dateStr.indexOf(":") != -1) {
541: int colonIndex = dateStr.indexOf(":");
542: String hourStr = dateStr.substring(0, colonIndex);
543: String minuteStr = dateStr.substring(colonIndex + 1)
544: .trim();
545: calendar.set(Calendar.HOUR_OF_DAY, Integer
546: .parseInt(hourStr));
547: calendar.set(Calendar.MINUTE, Integer
548: .parseInt(minuteStr));
549: } else {
550: calendar.set(Calendar.YEAR, Integer.parseInt(dateStr
551: .trim()));
552: }
553: d = calendar.getTime();
554: }
555:
556: SimpleDateFormat sdf = new SimpleDateFormat(
557: "MMM dd yy, hh:mm a");
558: sdf.setCalendar(new GregorianCalendar(TimeZone.getDefault()));
559: return sdf.format(d);
560: }
561:
562: public int getMonthCode(String month) {
563: int monthCode = -1;
564: if (month.equalsIgnoreCase("jan")) {
565: monthCode = Calendar.JANUARY;
566: } else if (month.equalsIgnoreCase("feb")) {
567: monthCode = Calendar.FEBRUARY;
568: } else if (month.equalsIgnoreCase("mar")) {
569: monthCode = Calendar.MARCH;
570: } else if (month.equalsIgnoreCase("apr")) {
571: monthCode = Calendar.APRIL;
572: } else if (month.equalsIgnoreCase("may")) {
573: monthCode = Calendar.MAY;
574: } else if (month.equalsIgnoreCase("jun")) {
575: monthCode = Calendar.JUNE;
576: } else if (month.equalsIgnoreCase("jul")) {
577: monthCode = Calendar.JULY;
578: } else if (month.equalsIgnoreCase("aug")) {
579: monthCode = Calendar.AUGUST;
580: } else if (month.equalsIgnoreCase("sep")) {
581: monthCode = Calendar.SEPTEMBER;
582: } else if (month.equalsIgnoreCase("oct")) {
583: monthCode = Calendar.OCTOBER;
584: } else if (month.equalsIgnoreCase("nov")) {
585: monthCode = Calendar.NOVEMBER;
586: } else if (month.equalsIgnoreCase("dec")) {
587: monthCode = Calendar.DECEMBER;
588: }
589: return monthCode;
590: }
591:
592: private String[][] processUnixDirectoryListingType(
593: ArrayList v_file_names, ArrayList v_full_listing)
594: throws Exception {
595: if (v_file_names.size() != v_full_listing.size())
596: mergeListing(v_file_names, v_full_listing);
597:
598: int number_of_files = v_file_names.size();
599: int full_listing_length = v_full_listing.size();
600:
601: String[][] listing = new String[number_of_files][4];
602: String file_name = null;
603: String full_listing_row = null;
604:
605: for (int i = number_of_files - 1, j = full_listing_length - 1; i >= 0; --i, --j) {
606:
607: listing[i][1] = file_name = (String) (v_file_names.get(i));
608: full_listing_row = (String) (v_full_listing.get(j));
609: // logger.info(full_listing_row);
610: logger.info("PSSRNF_CSPNSJ1060");
611: /*
612: *Following lines mimick the legacy logic of previous getFTPDir()
613: *However they are patently wrong. Consider the case of links for
614: *example.
615: */
616: if (full_listing_row.charAt(0) == 'd')
617: listing[i][0] = "d";
618: else
619: listing[i][0] = full_listing_row.charAt(0) + "";
620: int k = -1;
621: if (listing[i][0].equals("t"))
622: continue;//Netware hosts adds total 0 along with filelist
623: //so skipping "total 0" from file listing
624: if (listing[i][0].equals("l")) {
625: k = full_listing_row.indexOf((file_name + " ->")) - 1;
626: /*
627: *In case of links, we are getting the
628: *<from> -> <to>
629: *to show up. Hence we are getting the full name
630: *from output of "ls -la"
631: */
632: listing[i][1] = file_name = full_listing_row.substring(
633: k, full_listing_row.length());
634: } else
635: k = full_listing_row.lastIndexOf(file_name) - 1;
636: for (; k >= 0; --k)
637: if ((full_listing_row.charAt(k) != ' '))
638: break;
639: int end_of_date = k + 1;
640: //Start of time/year
641: for (; k >= 0; --k)
642: if (full_listing_row.charAt(k) == ' ')
643: break;
644: //End of day
645: for (; k >= 0; --k)
646: if ((full_listing_row.charAt(k) != ' '))
647: break;
648: //Start of day
649: for (; k >= 0; --k)
650: if (full_listing_row.charAt(k) == ' ')
651: break;
652: //End of month
653: for (; k >= 0; --k)
654: if ((full_listing_row.charAt(k) != ' '))
655: break;
656: //Start of month
657: for (; k >= 0; --k)
658: if (full_listing_row.charAt(k) == ' ')
659: break;
660: listing[i][2] = getModifiedDateFormat(full_listing_row
661: .substring(k + 1, end_of_date));
662: //End of length
663: for (; k >= 0; --k)
664: if ((full_listing_row.charAt(k) != ' '))
665: break;
666: int end_of_length = k + 1;
667: //Start of length
668: for (; k >= 0; --k)
669: if (full_listing_row.charAt(k) == ' ')
670: break;
671: listing[i][3] = full_listing_row.substring(k + 1,
672: end_of_length);
673: }
674:
675: return listing;
676: }
677:
678: private String[][] processMSDirectoryListingType(
679: ArrayList v_file_names, ArrayList v_full_listing)
680: throws Exception {
681: int number_of_files = v_file_names.size();
682: String[][] listing = new String[number_of_files][4];
683: String full_listing_row = null;
684:
685: for (int i = number_of_files - 1; i >= 0; --i) {
686: listing[i][1] = (String) (v_file_names.get(i));
687: full_listing_row = (String) (v_full_listing.get(i));
688: if (full_listing_row.indexOf("<DIR>") > 0) {
689: listing[i][3] = "0";
690: listing[i][0] = "d";
691: } else
692: listing[i][0] = "-";
693: int j = 0;
694: for (; j < full_listing_row.length(); ++j)
695: if (full_listing_row.charAt(j) == ' ')
696: break;
697: for (; j < full_listing_row.length(); ++j)
698: if ((full_listing_row.charAt(j) != ' '))
699: break;
700:
701: for (; j < full_listing_row.length(); ++j)
702: if (full_listing_row.charAt(j) == ' ')
703: break;
704: listing[i][2] = getModifiedDateFormat(full_listing_row
705: .substring(0, j));
706: if (listing[i][3] == null) {
707: int start_of_length = -1;
708: for (; j < full_listing_row.length(); ++j)
709: if ((full_listing_row.charAt(j) != ' '))
710: break;
711: start_of_length = j;
712:
713: for (; j < full_listing_row.length(); ++j)
714: if (full_listing_row.charAt(j) == ' ')
715: break;
716:
717: listing[i][3] = full_listing_row.substring(
718: start_of_length, j);
719: }
720: }
721: return listing;
722: }
723:
724: void rename(String username, String password, String machine,
725: String share, String directory, String oldFileName,
726: String newFileName) throws NetFileException {
727: FullFtpClient ffc = null;
728: Exception e = null;
729: try {
730: ffc = initialiseFtpClient(username, password, machine,
731: share, directory);
732: ffc.rename(oldFileName, newFileName);
733: } catch (Exception ex) {
734: e = ex;
735: performFinalProcessing(e, "Could not rename file", ffc);
736: } finally {
737: try {
738: if (ffc != null)
739: ffc.closeServer();
740: } catch (Exception exception) {
741: }
742: }
743: }
744:
745: private void performFinalProcessing(Exception ex,
746: String message_to_debug, FullFtpClient ffc)
747: throws NetFileException {
748: if (ex != null) {
749: // logger.log(Level.SEVERE, message_to_debug,ex);
750: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1061");
751: if (ex instanceof FileNotFoundException) {
752: throw new NetFileException(
753: NetFileException.KEY_IDENTIFIER_PREFIX
754: + "no_file_or_permission");
755: }
756: try {
757: if (ffc != null) {
758: ffc.quit();
759: }
760: } catch (Exception e) {
761: }
762:
763: throw new NetFileException(
764: NetFileException.KEY_IDENTIFIER_PREFIX
765: + "noop_no_reason");
766: }
767: }
768:
769: private void performFinalProcessingForGetFTPDir(
770: NetFileException nfe, Exception ex,
771: String message_to_debug, FullFtpClient ffc)
772: throws NetFileException {
773:
774: try {
775: if (ffc != null) {
776: ffc.quit();
777: }
778: } catch (Exception e) {
779: }
780:
781: if (nfe != null)
782: throw nfe;
783:
784: if (ex != null) {
785: // logger.log(Level.SEVERE, message_to_debug,ex);
786: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1062");
787: if (ex instanceof FileNotFoundException) {
788: throw new NetFileException(
789: NetFileException.KEY_IDENTIFIER_PREFIX
790: + "no_file_or_permission");
791: }
792: throw new NetFileException(
793: NetFileException.KEY_IDENTIFIER_PREFIX
794: + "noop_no_reason");
795: }
796: }
797:
798: private FullFtpClient initialiseFtpClient(String username,
799: String password, String machine, String share,
800: String directory) throws NetFileException {
801: FullFtpClient ffc = null;
802: try {
803: ffc = new FullFtpClient(machine, s_machine_encoding);
804: ffc.setMachineToAccess(machine);
805: } catch (Exception ioe) {
806: // logger.log(Level.SEVERE, "Failed to Bind on Port 21 to host: " + machine,ioe);
807: Object[] params8 = { machine, ioe };
808: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1063", params8);
809: throw new NetFileException(
810: NetFileException.KEY_IDENTIFIER_PREFIX + "error19");
811: }
812: try {
813: ffc.login(username, password);
814: } catch (Exception e) {
815: // logger.log(Level.SEVERE, "Login Error to " + machine,e);
816: Object[] params9 = { machine, e };
817: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1064", params9);
818: throw new NetFileException(
819: NetFileException.KEY_IDENTIFIER_PREFIX + "error12");
820: }
821: try {
822: ffc.cd(share + directory);
823: } catch (Exception ioe) {
824: // logger.log(Level.SEVERE, "Cannot change to directory " + share+directory,ioe);
825: Object[] params10 = { share, directory, ioe };
826: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1065", params10);
827: throw new NetFileException(
828: NetFileException.KEY_IDENTIFIER_PREFIX + "error20");
829: }
830: try {
831: ffc.binary();
832: } catch (Exception ioe) {
833: // logger.log(Level.SEVERE, "Cannot change mode to binary",ioe);
834: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1066");
835: throw new NetFileException(
836: NetFileException.KEY_IDENTIFIER_PREFIX
837: + "error20_2");
838: }
839: return ffc;
840: }
841:
842: }
|