Tuesday, 24 June 2008

Hylafax client program

package com.prma.faxmanager;

import gnu.hylafax.HylaFAXClient;
import gnu.hylafax.HylaFAXClientProtocol;
import gnu.hylafax.Job;
import gnu.inet.ftp.FtpClientProtocol;
import gnu.inet.ftp.ServerResponseException;

import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class Test {

HylaFAXClient c = new HylaFAXClient();
String sendTime;
Job job;

public static void main(String[] args) throws Exception {
new Test().doSendHylaFax("192.168.0.26", "c://invitatioon_1.pdf",
"root");

}

public void doSendHylaFax(String destination, String filename, String user)
throws Exception, IOException, IOException, ServerResponseException {

String host = "192.168.0.26"; // -h
String port = "4559";
String from = "root";
String notify = "done";
String notifyaddr = "anil.goud@cellarch.com"; // -f
int resolution = 196; // -l
// 98,
// -m
// 196
Hashtable pageSizes = Job.pagesizes;
Dimension pagesize = (Dimension) pageSizes.get("a4"); // -s
String killtime = "000259"; // -k
int maxdials = 12; // -T
int maxtries = 3; // -t
int priority = 127; // -P
String pagechop = "default";
int chopthreshold = 3;
Vector documents = new Vector();
boolean verbose = false;
// boolean from_is_set = false;
HylaFAXClient c = new HylaFAXClient();
c.setDebug(verbose); // enable debug messages
c.open(host, Integer.parseInt(port));

c.user(from);
c.pass("3rata$$3");
c.mode(FtpClientProtocol.MODE_ZLIB);
c.type(FtpClientProtocol.TYPE_IMAGE);
try {

c.noop(); // for the heck of it
c.tzone(HylaFAXClientProtocol.TZONE_LOCAL);
// schlep file up to server
FileInputStream file = new FileInputStream(filename);
String remote_filename = c.putTemporary(file);
documents.addElement(remote_filename);
job = CreateJob(); // start a new job
// set job properties
job.setFromUser(from);
job.setNotifyAddress(notifyaddr);
job.setKilltime(killtime);
job.setMaximumDials(maxdials);
job.setMaximumTries(maxtries);
job.setPriority(priority);
job.setDialstring(destination);
job.setVerticalResolution(resolution);
job.setPageDimension(pagesize);
job.setNotifyType(notify);
job.setChopThreshold(chopthreshold);

// add documents to the job
for (int i = 0; i < documents.size(); i++) {
String document = (String) documents.elementAt(i);
job.addDocument(document);
}

c.submit(job); // submit the job to the scheduler

} catch (Exception e) {
e.printStackTrace();
} finally {
// disconnect from the server
try {
c.quit();
} catch (Exception e) {
// quit failed, not much we can do now
e.printStackTrace();
}
}
}

public Job getJob(long faxid) throws ServerResponseException, IOException {
return (Job) c.getJob(faxid);
}

public String getStatus(Job job) {
try {
return job.getStatus();
} catch (Exception e) {
}
return "not available";
}

public Job CreateJob() throws ServerResponseException, IOException {
return c.createJob();
}

public String getSendtime(Job job) {
try {
sendTime = job.getSendtime();
} catch (Exception e) {
sendTime = "0";
}
if (sendTime.equals("0")) {
return "now";
} else {
return sendTime.substring(4, 6) + "-" + sendTime.substring(6, 8)
+ "-" + sendTime.substring(0, 4) + " "
+ sendTime.substring(8, 10) + ":"
+ sendTime.substring(10, 12);
}
}

/* void sedFax() throws Exception {
FaxManager faxManager = new FaxManager();
faxManager.initHylafax("192.168.0.15", "root", "3rata$$3");
// faxManager.
faxManager.open();
File file = new File("/home/eanilkumar/Desktop/"
+ "Manning.iText.in.Action.Dec.2006.pdf");
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
byte[] data = new byte[(int) (fc.size())]; // fc.size returns the size
// of the file which backs
// the channel
ByteBuffer bb = ByteBuffer.wrap(data);
fc.read(bb);
byte[] document = data;

faxManager.sendFax("usetr", " ", document, "now", "here only");

faxManager.close();
}*/



public Enumeration getAlNonComplteFaxes() throws FileNotFoundException,
IOException, ServerResponseException {

// Get status of the sendq and the doneq
Vector list = new Vector();

// Get the list of faxes that have not completed yet.
list.addAll(c.getList("sendq"));

// Get the list of faxes that have completed but not been cleaned
// up by faxcron or similar.
list.addAll(c.getList("doneq"));

// Print out each faxes status, 1 per line.
Enumeration faxes = list.elements();
while (faxes.hasMoreElements()) {
System.out.println(faxes.nextElement());

}

return faxes;

}

}

No comments:

Post a Comment