1 /*------------------------------------------------------------------------------
2 Name: PublishFile.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Code for a client to publish files to xmlBlaster
6 Version: $Id: PublishFile.java 14846 2006-03-07 17:14:22Z ruff $
7 ------------------------------------------------------------------------------*/
8 package org.xmlBlaster.client.feeder;
9
10 import org.xmlBlaster.client.qos.ConnectQos;
11 import org.xmlBlaster.client.I_XmlBlasterAccess;
12 import org.xmlBlaster.client.key.PublishKey;
13 import org.xmlBlaster.client.qos.PublishQos;
14 import org.xmlBlaster.client.qos.PublishReturnQos;
15 import org.xmlBlaster.util.FileLocator;
16 import org.xmlBlaster.util.Global;
17 import org.xmlBlaster.util.XmlBlasterException;
18 import org.xmlBlaster.util.MsgUnit;
19
20 import java.util.logging.Logger;
21 import java.util.logging.Level;
22 import org.xmlBlaster.util.StopWatch;
23
24 import java.io.File;
25
26
27 /**
28 * Publish files to xmlBlaster.
29 * <br />
30 * Use this as a command line tool to publish files, images, etc. as messages to xmlBlaster.
31 * Invoke examples:<br />
32 * <pre>
33 * java org.xmlBlaster.client.feeder.PublishFile -c <content-file> -k <key-file> -q <qos-file> -m <mime-type>
34 * </pre>
35 * For other supported options type
36 * <pre>
37 * java org.xmlBlaster.client.feeder.PublishFile -?
38 * </pre>
39 */
40 public class PublishFile
41 {
42 private static final String ME = "PublishFile";
43 private I_XmlBlasterAccess senderConnection;
44 private Global glob;
45 private static Logger log = Logger.getLogger(PublishFile.class.getName());
46 private String loginName;
47 private String passwd;
48
49 /**
50 * Constructs the PublishFile object.
51 * <p />
52 * Tries to support you in guessing the missing command line parameters.
53 * <p />
54 * Start with parameter -? to get a usage description.<br />
55 * These command line parameters are not merged with xmlBlaster.property properties.
56 * <p />
57 * By default the classes in the java.io package always resolve relative pathnames
58 * against the current user directory. This directory is named by the system property user.dir,
59 * and is typically the directory in which the Java virtual machine was invoked
60 *
61 * @param args Command line arguments
62 */
63 public PublishFile(String[] args) throws XmlBlasterException
64 {
65 glob = new Global();
66 if (glob.init(args) != 0) {
67 usage();
68 System.err.println(ME + ": Bye");
69 System.exit(1);
70 }
71
72
73 loginName = glob.getProperty().get("loginName", ME);
74 passwd = glob.getProperty().get("passwd", "secret");
75
76 String contentMime = glob.getProperty().get("m", (String)null);
77 String contentMimeExtended = glob.getProperty().get("me", "1.0"); // optional
78
79 // if passing the stuff in files:
80 String contentFile = glob.getProperty().get("c", (String)null);
81 String keyFile = glob.getProperty().get("k", (String)null);
82 String qosFile = glob.getProperty().get("q", (String)null);
83
84 // if passing the text directly on the command line:
85 String xmlKeyGiven = glob.getProperty().get("xmlKey", (String)null);
86 String xmlQosGiven = glob.getProperty().get("xmlQos", (String)null);
87 String contentGiven = glob.getProperty().get("content", (String)null);
88
89 String body = null;
90 String exte = null;
91 if (contentFile != null) {
92 File contentHandle = new File(contentFile);
93 String name = contentHandle.getPath();
94 body = FileLocator.getBody(name); // filename without extension
95 exte = FileLocator.getExtension(name);
96 }
97
98 // Determine content ...
99 byte[] content = null;
100 if (contentFile != null) {
101 try { content = FileLocator.readFile(contentFile); }
102 catch (XmlBlasterException e) {
103 log.severe(e.toString());
104 }
105 }
106 if (content == null && contentGiven != null) {
107 content = contentGiven.getBytes();
108 }
109 if (content == null) {
110 content = new byte[0];
111 // allow empty contents
112 //log.panic(ME, "File content is missing, specify content as '-c <file>' or '-content <the content text>' (get help with -?)");
113 }
114
115 // Determine XmlKey ...
116 String xmlKey = null;
117 if (keyFile != null) {
118 try { xmlKey = FileLocator.readAsciiFile(keyFile); }
119 catch (XmlBlasterException e) {
120 log.severe(e.toString());
121 }
122 }
123 if (xmlKey == null) {
124 xmlKey = xmlKeyGiven;
125 }
126 if (contentMime == null && exte != null) {
127 contentMime = FileLocator.extensionToMime(exte, null);
128 }
129 if (xmlKey == null && body != null) { // The filename will be the key-oid ...
130 if (contentMime == null) {
131 log.severe("File MIME type is unknown, specify MIME type as '-m <MIME>', for example '-m \"image/gif\"' (get help with -?)");
132 System.exit(1);
133 }
134 PublishKey publishKey = new PublishKey(glob, body, contentMime, contentMimeExtended);
135 xmlKey = publishKey.toXml(); // default <key oid=......></key>
136 }
137 if (xmlKey == null) {
138 log.severe("XmlKey is missing, specify key as '-k <file>' or '-xmlKey <the XML key>' (get help with -?)");
139 System.exit(1);
140 }
141
142 // Determine XmlQoS ...
143 String xmlQos = null;
144 if (qosFile != null) {
145 try { xmlQos = FileLocator.readAsciiFile(qosFile); }
146 catch (XmlBlasterException e) {
147 log.severe(e.toString());
148 }
149 }
150 if (xmlQos == null) {
151 xmlQos = xmlQosGiven;
152 }
153 if (xmlQos == null) {
154 PublishQos publishQos = new PublishQos(glob);
155 xmlQos = publishQos.toXml(); // default qos = "<qos></qos>"
156 }
157
158 feed(xmlKey, content, xmlQos);
159 }
160
161
162 /**
163 * Open the connection, publish the message, close the connection.
164 */
165 public PublishFile(String loginName, String passwd, String xmlKey, byte[] content, String xmlQos)
166 {
167 this.loginName = loginName;
168 this.passwd = passwd;
169 feed(xmlKey, content, xmlQos);
170 }
171
172
173 /**
174 * open the connection, publish the message, close the connection
175 */
176 protected void feed(String xmlKey, byte[] content, String xmlQos)
177 {
178 setUp(); // login
179
180 publish(xmlKey, content, xmlQos); // publish message
181
182 tearDown(); // logout
183 }
184
185
186 /**
187 * Sets up the fixture.
188 * <p />
189 * Connect to xmlBlaster and login
190 */
191 protected void setUp()
192 {
193 try {
194 senderConnection = glob.getXmlBlasterAccess(); // Find orb
195 ConnectQos connectQos = new ConnectQos(glob, loginName, passwd);
196 senderConnection.connect(connectQos, null); // Login to xmlBlaster
197 }
198 catch (Exception e) {
199 log.severe(e.toString());
200 e.printStackTrace();
201 }
202 }
203
204
205 /**
206 * Logout from xmlBlaster
207 */
208 protected void tearDown()
209 {
210 senderConnection.disconnect(null);
211 }
212
213
214 /**
215 * Construct a message and publish it.
216 */
217 public void publish(String xmlKey, byte[] content, String qos)
218 {
219 if (log.isLoggable(Level.FINE)) log.fine("Publishing the message ...\nKEY:\n" + xmlKey + "\nCONTENT-LENGTH=" + content.length + "\nQOS:\n" + qos);
220
221 try {
222 MsgUnit msgUnit = new MsgUnit(glob, xmlKey, content, qos);
223 StopWatch stop = new StopWatch();
224 PublishReturnQos publish = senderConnection.publish(msgUnit);
225 log.info("Success: Publishing done: " + publish.toXml() + "\n" + stop.nice());
226 //log.info(ME, "Success: Publishing done, returned message oid=" + publish.getKeyOid() + stop.nice());
227 } catch(XmlBlasterException e) {
228 log.warning("XmlBlasterException: " + e.getMessage());
229 }
230 }
231
232
233 /**
234 * Command line usage.
235 */
236 private void usage()
237 {
238 System.out.println("----------------------------------------------------------");
239 System.out.println("java org.xmlBlaster.client.feeder.PublishFile <options>");
240 System.out.println("----------------------------------------------------------");
241 System.out.println("Options:");
242 System.out.println(" -? Print this message.");
243 System.out.println("");
244 System.out.println(" -loginName <LoginName> Your xmlBlaster login name.");
245 System.out.println(" -passwd <Password> Your xmlBlaster password.");
246 System.out.println("");
247 System.out.println(" -k <XmlKeyFile> The XmlKey for the content, or:");
248 System.out.println(" -xmlKey <XmlKey> The XML key on command line.");
249 System.out.println("");
250 System.out.println(" -c <contentFile> The content file you want to feed, or:");
251 System.out.println(" -content <content> The content on command line.");
252 System.out.println("");
253 System.out.println(" -q <XmlQosFile> The XmlQos for the message, or:");
254 System.out.println(" -xmlQos <XmlQos> The XML qos on command line.");
255 System.out.println("");
256 System.out.println(" These options only if you didn't specify -k or -xmlKey explicitly");
257 System.out.println(" -m <MIMEtype> The MIME type of the message.");
258 System.out.println(" -me <MIMEextendend> The extenden MIME type (for your own use).");
259 //I_XmlBlasterAccess.usage();
260 //log.usage();
261 System.out.println("----------------------------------------------------------");
262 System.out.println("Example:");
263 System.out.println("java org.xmlBlaster.client.feeder.PublishFile -c Hello.xml");
264 System.out.println(" The message will be named automatically 'Hello' and the MIME will be set to 'text/xml'");
265 System.out.println(" and the qos (quality of service) is set to default");
266 System.out.println("");
267 System.out.println("java org.xmlBlaster.client.feeder.PublishFile -content \"Hello World\" -xmlKey \"<key oid='number12' contentMime='text/plain'></key>\"");
268 System.out.println("");
269 System.out.println("java org.xmlBlaster.client.feeder.PublishFile -xmlKey \"<key oid='__cmd:sysprop/?trace=true'/>\"");
270 System.out.println("----------------------------------------------------------");
271 System.out.println("");
272 }
273
274
275 /**
276 * Invoke: java org.xmlBlaster.client.feeder.PublishFile -c <content-file> -k <key-file> -q <qos-file> -m <mime-type>
277 */
278 public static void main(String args[])
279 {
280 try {
281 new PublishFile(args);
282 } catch (Throwable e) {
283 e.printStackTrace();
284 System.err.println(PublishFile.ME + ": " + e.toString());
285 System.exit(1);
286 }
287 }
288 }
syntax highlighted by Code2HTML, v. 0.9.1