001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.scheduler.jobs;
034:
035: import java.io.IOException;
036:
037: import com.knowgate.debug.DebugFile;
038: import com.knowgate.dataobjs.DB;
039: import com.knowgate.scheduler.Atom;
040: import com.knowgate.scheduler.Job;
041: import com.knowgate.dataxslt.FastStreamReplacer;
042: import com.knowgate.dfs.FileSystem;
043:
044: /**
045: * <p>Publishes Files via FTP</p>
046: * @author Sergio Montoro Ten
047: * @version 1.0
048: */
049:
050: public class FTPPublisher extends Job {
051:
052: // A reference to the replacer class witch maps tags of the form {#Section.Field}
053: // to their corresponding database fields.
054: private FastStreamReplacer oReplacer;
055:
056: private FileSystem oFileSys;
057:
058: public FTPPublisher() {
059: oReplacer = new FastStreamReplacer();
060: oFileSys = new FileSystem();
061: }
062:
063: // ---------------------------------------------------------------------------
064:
065: /**
066: * <p>Process PageSet pointed by Atom and sends transformed document throught FTP</p>
067: * <p>Base workareas path is taken from "workareasput" property of hipergate.cnf<p>
068: * @param oAtm Atom holding a reference to PageSet instance to be dumped<br>
069: * Atom must have the following parameters set:<br>
070: * <table border=1 cellpadding=4>
071: * <tr><td>gu_workarea</td><td>GUID of WorkArea owner of document to be sent</td></tr>
072: * <tr><td>gu_pageset</td><td>GUID of PageSet to be sent</td></tr>
073: * <tr><td>nm_pageset</td><td>Name of PageSet document instance to be sent</td></tr>
074: * <tr><td>tx_nickname</td><td>FTP User Nickname for login</td></tr>
075: * <tr><td>tx_pwd</td><td>FTP User Password for login</td></tr>
076: * <tr><td>nm_server</td><td>FTP server name or IP address</td></tr>
077: * <tr><td>path</td><td>FTP target path</td></tr>
078: * <tr><td>bo_mkpath</td><td>"1" if path must be created at target server</td></tr>
079: * <tr><td>nm_file</td><td>Target file name for nm_pageset</td></tr>
080: * </table>
081: * @return String containing the final pos-processed document
082: * @throws IOException
083: */
084:
085: // ---------------------------------------------------------------------------
086: public void free() {
087: }
088:
089: public Object process(Atom oAtm) throws IOException {
090:
091: String sPathHTML; // Full Path to Document Template File
092: char cBuffer[]; // Internal Buffer for Document Template File Data
093: Object oReplaced; // Document Template File Data after FastStreamReplacer processing
094:
095: final String sSep = System.getProperty("file.separator"); // Alias for file.separator
096: final String Yes = "1";
097:
098: if (DebugFile.trace) {
099: DebugFile.writeln("Begin FTPPublisher.process([Job:"
100: + getStringNull(DB.gu_job, "") + ", Atom:"
101: + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
102: DebugFile.incIdent();
103: }
104:
105: // *************************************************
106: // Compose the full path to document template file
107:
108: // First get the storage base path from hipergate.cnf
109: sPathHTML = getProperty("workareasput");
110: if (!sPathHTML.endsWith(sSep))
111: sPathHTML += sSep;
112:
113: // Concatenate PageSet workarea guid and subpath to Mailwire application directory
114: sPathHTML += getParameter("gu_workarea") + sSep + "apps" + sSep
115: + "Mailwire" + sSep + "html" + sSep
116: + getParameter("gu_pageset") + sSep;
117:
118: // Concatenate PageSet Name
119: sPathHTML += getParameter("nm_pageset").replace(' ', '_')
120: + ".html";
121:
122: if (DebugFile.trace)
123: DebugFile.writeln("PathHTML = " + sPathHTML);
124:
125: // *************************************************
126: // Call FastStreamReplacer for {#Section.Field} tags
127:
128: oReplaced = oReplacer.replace(sPathHTML, oAtm.getItemMap());
129:
130: // **************************************
131: // Send throught final replaced file data
132:
133: oFileSys.user(getParameter("tx_nickname"));
134: oFileSys.password(getParameter("tx_pwd"));
135:
136: try {
137:
138: if (Yes.equals(getParameter("bo_path")))
139: oFileSys.mkdirs("ftp://" + getParameter("nm_server")
140: + "/" + getParameter("path"));
141:
142: oFileSys.copy("file://" + sPathHTML, "ftp://"
143: + getParameter("nm_server") + "/"
144: + getParameter("path") + "/"
145: + getParameter("nm_file"));
146:
147: } catch (java.lang.Exception e) {
148: throw new IOException(e.getMessage());
149: }
150:
151: // Decrement de count of atoms peding of processing at this job
152: iPendingAtoms--;
153:
154: if (DebugFile.trace) {
155: DebugFile.writeln("End FTPPublisher.process([Job:"
156: + getStringNull(DB.gu_job, "") + ", Atom:"
157: + String.valueOf(oAtm.getInt(DB.pg_atom)) + "])");
158: DebugFile.decIdent();
159: }
160:
161: return oReplaced;
162: } // process
163:
164: } // FTPPublisher
|