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