Muestra de API de servicios web en Java
Este tema contiene métodos de API de servicios web de muestra en Java. El objetivo de este tema es ayudar a los desarrolles experimentados a comprender cómo utilizar la API de servicios web de GFI FaxMaker en aplicaciones Java. Las muestras pueden requerir modificaciones significativas para integrarse en las aplicaciones personalizadas.
Establecer detalles de usuario y servidor
/** Set the IP of the GFI FaxMaker server. */ private static final String IP = "10.44.3.200"; /** Set the GFI FaxMaker web service port. By default, this is 8555. */ private static final int PORT = 8555; /** Set the GFI FaxMaker Web Services URL. */ /** If an SSL certificate is configured use https:// rather than http:// */ private static final String URL = "http://" + IP + ":" + PORT + "/faxmaker/wsapi"; /** Web Service API user e-mail address as configured in GFI FaxMaker. */ /** User must be registered as a Web Service API user. */ private static final String USER_EMAIL = "wsapi_user@testdomain.local"; /** Active Directory users: Specify the user's Active Directory password. */ /** Non-Active Directory: Password generated by GFI FaxMaker in user screen. */ private static final String USER_PASSWORD = "abcdefghijklmnopqrstuvwxyz"; private static final ObjectFactory FACTORY = new ObjectFactory();
Envío de un fax
public static void main(String[] args) throws Exception
{
//create java web service object
FMSendReceive WSClient = new FMSendReceive(new URL(URL));
/** Create user using the Web Service API credentials specified above. **/
FMUser user = new FMUser();
user.setEmail(FACTORY.createFMUserEmail(USER_EMAIL));
user.setPassword(FACTORY.createFMUserPassword(USER_PASSWORD));
/** Populate the sender details **/
UserDetails senderDetails = new UserDetails();
// Specify the sender's first name
senderDetails.setFirstName( FACTORY.createUserDetailsFirstName("First Name") );
// Specify the sender's last name
senderDetails.setLastName( FACTORY.createUserDetailsLastName("Last Name") );
senderDetails.setEmail( FACTORY.createUserDetailsEmail(USER_EMAIL) );
/** Populate fax recipients **/
UserDetails recipient = new UserDetails();
// Specify recipient's first name
recipient.setFirstName( FACTORY.createUserDetailsFirstName("Recipient Name Here") );
// Specify recipient's last name
recipient.setLastName( FACTORY.createUserDetailsLastName("Last Name") );
// Specify recipient's company name
recipient.setCompany( FACTORY.createUserDetailsCompany("Company") );
// Specify recipient's department name
recipient.setDepartment( FACTORY.createUserDetailsDepartment("Department") );
// Specify recipient's email address
recipient.setEmail( FACTORY.createUserDetailsEmail("email@address.com") );
// Specify recipient's fax number
recipient.setFaxNumber( FACTORY.createUserDetailsFaxNumber("+1234567890") );
// Add recipient(s) to this array.
ArrayOfUserDetails recipients = new ArrayOfUserDetails();
recipients.getUserDetails().add(recipient);
/** Prepare fax message **/
MessageDetails message = new MessageDetails();
// Set the fax subject
message.setSubject(FACTORY.createMessageDetailsSubject("Subject Here"));
FileData messageBodyFileData = FACTORY.createFileData();
// Specify the file containing the message body
messageBodyFileData.setFilename(FACTORY.createFileDataFilename("message_body.txt"));
// Read message body data
byte[] bodydata = Files.readAllBytes("message_body.txt");
messageBodyFileData.setData(FACTORY.createFileDataData(bodydata) );
message.setMessageBodyFile(
FACTORY.createMessageDetailsMessageBodyFile(messageBodyFileData));
/** Prepare attachments **/
ArrayOfFileData attachments = FACTORY.createArrayOfFileData();
FileData file = FACTORY.createFileData();
//Specify attachment
file.setFilename( FACTORY.createFileDataFilename("Attachment_1.txt") );
//Read attachment data
byte[] data = Files.readAllBytes("Attachment_1.txt");
file.setData( FACTORY.createFileDataData(data) );
// Add attachment(s) to this array.
attachments.getFileData().add(file);
message.setAttachments( FACTORY.createMessageDetailsAttachments(attachments) );
/** Other message options **/
// Set fax priority.
message.setPriority(FaxPriority.NORMAL);
// Set fax resolution.
message.setResolution(FaxResolution.HIGH);
// Request that fax is sent over a particular line.
message.setUseFaxLine(FaxLineType.NONE);
/** Call the SendFax API method **/
Holder<FMResult> sendFaxResult = new Holder();
Holder<ArrayOfFaxJobID> faxJobIDs = new Holder();
WSClient.sendFax(
sender,
senderDetails,
recipients,
message,
sendFaxResult,
faxJobIDs
);
System.out.println("sendFax() result: " + sendFaxResult.value);
}
Supervisión de progreso de fax enviado
public static void main(String[] args) throws Exception
{
//create java web service object
FMSendReceive WSClient = new FMSendReceive(new URL(URL));
Holder<FMResult> result = new Holder();
Holder<FaxSendingStatus> faxStatus = new Holder();
try
{
// Run loop for each jobID
for (FaxJobID jobID : faxJobIds.value.getFaxJobID())
{
// Poll for status updates until fax is successfully sent or failed
do
{
// Get the status for submitted faxes
WSClient.getSendingFaxStatusUpdates(
user, jobID.getFaxUID(), result, faxStatus);
if (result.value == FMResult.SUCCESS)
{
System.out.println("Status: FaxID = " + jobID.getFaxUID() +
" current status: " + faxStatus.value.getStatus());
// Pause for 30 seconds before checking again for status.
Thread.sleep(30000);
}
else
System.err.println("Call to monitor fax with ID " +
jobID.getFaxUID() + " failed, result: " + result.value);
}
while (
result.value == FMResult.SUCCESS &&
( faxStatus.value.getStatus() = SendingStatus.SENT ||
faxStatus.value.getStatus() != SendingStatus.FAILED ));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
Descarga de faxes recibidos
public static void main(String[] args) throws Exception { //create java web service object FMSendReceive WSClient = new FMSendReceive(new URL(URL)); Holder<FMResult> GetFaxResult = new Holder<FMResult>(); Holder<ReceivedFaxDetails> FaxData = new Holder<ReceivedFaxDetails>(); //get the oldest fax from queue getNextFax(user,result,FaxData,GetFaxResult); if (result.value != FMResult.SUCCESS) System.out.println("Fax failed"); else { // Print received fax details System.out.println("Received Fax Details: "); System.out.println("UID: "+ FaxData.value.getFaxUID().toString()); System.out.println("Time: "+ FaxData.value.getWhenReceived().toString()); System.out.println("Pages: "+FaxData.value.getNosPages().toString()); System.out.println("Resolution: "+FaxData.value.getResolution().value()); System.out.println("Remote ID: "+FaxData.value.getRemoteID().getValue()); System.out.println("DTMF: "+FaxData.value.getDTMFDID().getValue()); // Save fax image to disk try { FileOutputStream fos = new FileOutputStream( new File("Faxes\\"+FaxData.value.getFilename().getValue())); BufferedOutputStream bos = new BufferedOutputStream(fos); bos.write(FaxData.value.getData().getValue()); bos.close(); } catch (FileNotFoundException e) { System.out.println("Exception: "+e.getMessage()); } catch (IOException e) { System.out.println("IOException: "+e.getMessage()); } //Delete downloaded fax. If not deleted, next call will get the same fax. FMResult fmresult = deleteFax(user,FaxData.value.getFaxUID()); System.out.println("Delete fax result: "+fmresult.toString()); } }
Descargar muestra de Java: GFI_WSAPI.java (Clic con botón derecho > Guardar como...)