001: /**
002: * $Id: NetWareFile.java,v 1.17 2006/05/17 11:57:38 bustamam 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.java2;
014:
015: import java.util.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.io.*;
018: import java.text.SimpleDateFormat;
019:
020: import com.sun.portal.netfile.shared.*;
021:
022: /*
023: * getFTPDir by Sushma Chakkirala Jun 14th 2000
024: * Subclassed to use common methods from FtpFile by Ali Baqri
025: * Modified for Java2 by Suresh Yellamaraju
026: */
027:
028: class NetWareFile extends FtpFile {
029:
030: private final int MAX_COLUMNS = 8;
031: private Locale clocale;
032:
033: public NetWareFile(String szMachineEncoding, Locale cloc) {
034: super (szMachineEncoding, cloc);
035: }
036:
037: public String[] getFTPDir(String username, String password,
038: String machname, String VMSname, String dirS,
039: NetFileResource nfrUsrResBundle) throws NetFileException {
040:
041: writeDebug("Expanding Netware directory", null);
042:
043: int txt = 0;
044: StringBuffer buftxt = new StringBuffer(400);
045: String textouting = "";
046: String col_type = "";
047: String col_junk1 = "";
048: String col_size = "";
049: String col_month = "";
050: String col_day = "";
051: String col_time = "";
052: String type = "";
053: String date = "";
054: FullFtpClient ffc = null;
055: String error6 = "";
056: try {
057:
058: writeDebug("VMS= " + VMSname + ",Dir=" + dirS
059: + " ,machine=" + machname + ",user name="
060: + username + ", encoding=" + szEncoding, null);
061:
062: ffc = initialiseFtpClient(username, password, machname,
063: VMSname, dirS);
064:
065: writeDebug("Fetching listing for \n" + ffc.pwd(), null);
066:
067: // Do a 'dir' using FTP command LIST
068: BufferedReader theList = new BufferedReader(
069: new InputStreamReader(ffc.list(), this .szEncoding));
070: String theLine;
071: while ((theLine = theList.readLine()) != null) {
072: buftxt.append(theLine).append("\n");
073: }
074: textouting = buftxt.toString();
075:
076: writeDebug("The Netware File Listing obtained is \n"
077: + textouting, null);
078:
079: theList.close();
080: ffc.closeServer();
081:
082: } catch (Exception ex) {
083: writeDebug(
084: "Exception in getting Netware file listing from "
085: + machname, ex);
086:
087: if (ex instanceof NetFileException) {
088: throw (NetFileException) ex;
089: } else {
090: throw new NetFileException(
091: NetFileException.NETFILE_GENERROR_CODE,
092: //"Error occured in fetching listing from " + VMSname+dirS + " of " + machname);
093: nfrUsrResBundle
094: .getString("nwf.1", new Object[] {
095: VMSname, dirS, machname }));
096: }
097:
098: }
099:
100: if (textouting.indexOf("Permission denied") > 0) {
101: throw new NetFileException(
102: NetFileException.NETFILE_GENERROR_CODE,
103: //error6 + " in fetching listing from " + VMSname+dirS + " of " + machname);
104: nfrUsrResBundle.getString("nwf.2", new Object[] {
105: error6, VMSname, dirS, machname }));
106: }
107:
108: ArrayList cache = new ArrayList();
109: cache.clear();
110: StringTokenizer pars = new StringTokenizer(textouting, "\n");
111: /** Output looks like this. parse it.
112: * ftp> dir
113: * 200 PORT command okay.
114: * 150 Opening data connection for (192.18.178.58,36872).
115: * total 0
116: * - [RWCEAFMS] supervisor 890 May 07 22:33 vol$log.err
117: * - [RWCEAFMS] supervisor 607 May 07 22:33 tts$log.err
118: * - [RWCEAFMS] supervisor 65536 May 12 19:32 backout.tts
119: * d [RWCEAFMS] supervisor 512 Jun 13 12:37 login
120: * d [RWCEAFMS] icebox81 512 May 02 22:40 ndps
121: * 226 Transfer complete.
122: * 1687 bytes received in 0.01 seconds (163.70 Kbytes/s)
123: * ftp>
124: **/
125:
126: String tmp = "";
127: while (pars.hasMoreElements()) {
128: tmp = (String) pars.nextElement();
129: /* grep for the 'total' word in the 'ls' output */
130: /**
131: * Bug 4521436
132: *
133: * while (tmp.startsWith(nfrUsrResBundle.getString("total"))) {
134: * while (pars.hasMoreElements()) {
135: * tmp = (String)pars.nextElement();
136: * cache.addElement(tmp);
137: * }
138: * }
139: *
140: */
141: if (pars.countTokens() == 0) {
142: return new String[] {};
143: }
144: while (tmp.startsWith(nfrUsrResBundle.getString("total"))) {
145: while (pars.hasMoreTokens()) {
146: tmp = pars.nextToken();
147: cache.add(tmp);
148: }
149: }
150: // end of code change for Bug 4521436
151: }
152:
153: Object[] rowdata = new Object[300];
154: int i_number_of_files = cache.size();
155: if (i_number_of_files == 0) {
156: return new String[] {};
157: }
158:
159: String[] sa_file_listing = new String[i_number_of_files * 4];
160:
161: writeDebug("Netware file listing size = " + i_number_of_files,
162: null);
163:
164: int i_index = 0;
165: if (cache.size() > 0) {
166: for (int row = 0; row < cache.size(); row++) {
167: String str_cache = (String) cache.get(row);
168: StringTokenizer parsnew = new StringTokenizer(
169: str_cache, " ");
170: String col_name = "";
171: int parscntnew = parsnew.countTokens();
172: for (int j = 0; j < parscntnew; j++) {
173: rowdata[j] = parsnew.nextToken();
174: }
175:
176: /* Format of ftpd from Sun Solaris
177: * drwxrwxrwt 8 sys sys 2717 May
178: * 7 17:11 tmp
179: * Format of ftpd from Novell Netware
180: * d [RWCEAFMS] admin 512 May 03
181: **/
182: /*
183: * - [RWCEAFMS] supervisor 890 May 07 22:33 vol$log.err
184: * - [RWCEAFMS] supervisor 607 May 07 22:33 tts$log.err
185: */
186: col_type = rowdata[0].toString();
187: col_junk1 = rowdata[1].toString();
188: col_size = rowdata[3].toString();
189: col_month = rowdata[4].toString();
190: col_day = rowdata[5].toString();
191: col_time = rowdata[6].toString();
192:
193: // This is for the spaced files(directories)
194: if (parscntnew > MAX_COLUMNS - 1) {
195: for (int l = MAX_COLUMNS - 1; l < parscntnew; l++) {
196: col_name = col_name + rowdata[l].toString()
197: + " ";
198: }
199: } else {
200: col_name = col_name
201: + rowdata[MAX_COLUMNS - 1].toString();
202: }
203: col_name = col_name.trim();
204:
205: if ((col_name.equals(".")) || (col_name.equals(".."))) {
206: } else {
207: type = col_type.substring(0, 1);
208: date = getModifiedDateFormat(col_month, col_day,
209: col_time, nfrUsrResBundle
210: .getString("nwf.DateFormat"));
211: sa_file_listing[i_index++] = type;
212: sa_file_listing[i_index++] = col_name;
213: sa_file_listing[i_index++] = col_size;
214: sa_file_listing[i_index++] = date;
215: writeDebug("Type = " + type, null);
216: writeDebug("Name = " + col_name, null);
217: writeDebug("Size = " + col_size, null);
218: writeDebug("Date = " + date, null);
219: txt++;
220: }
221: }
222: }
223: writeDebug("Returning from expanding Netware directory", null);
224: return sa_file_listing;
225: }
226:
227: private String getModifiedDateFormat(String month, String date,
228: String time, String datef) {
229: GregorianCalendar calendar = new GregorianCalendar(TimeZone
230: .getDefault());
231: calendar.set(Calendar.MONTH, getMonthCode(month));
232: calendar.set(Calendar.DATE, Integer.parseInt(date));
233: if (time.indexOf(":") != -1) {
234: int colonIndex = time.indexOf(":");
235: String hourStr = time.substring(0, colonIndex);
236: String minuteStr = time.substring(colonIndex + 1).trim();
237: calendar.set(Calendar.HOUR_OF_DAY, Integer
238: .parseInt(hourStr));
239: calendar.set(Calendar.MINUTE, Integer.parseInt(minuteStr));
240: } else {
241: calendar.set(Calendar.YEAR, Integer.parseInt(time.trim()));
242: }
243: Date d = calendar.getTime();
244: SimpleDateFormat sdf = new SimpleDateFormat(datef, clocale);
245: sdf.setCalendar(new GregorianCalendar(TimeZone.getDefault()));
246: return sdf.format(d);
247: }
248:
249: /* Checks if the machine type is Unix FTP or NetWare Ftp */
250: boolean getFTPType(String machname) {
251: FullFtpClient fc;
252: String ftpResponse;
253: String novellResponse = "NetWare";
254: boolean novellServer = false;
255:
256: writeDebug("Detecting if " + machname
257: + "Netware FTP or other FTP server", null);
258: try {
259: ffc = new FullFtpClient(machname, szEncoding);
260: ftpResponse = ffc.getResponseString();
261: ffc.closeServer();
262:
263: /* Verify if the server is a Unix FTP server
264: * or a Novell FTP Server
265: */
266: if (ftpResponse.indexOf(novellResponse) != -1) {
267: novellServer = true;
268: } else {
269: novellServer = false;
270: }
271: } catch (Exception e) {
272: writeDebug("getFTPType: Exception: ", e);
273: }
274: return novellServer;
275: }
276: }
|