Java Web Service API sample
This topic contains sample Web Services API methods in Java. The scope of this topic is to help experienced developers understand how to use the GFI FaxMaker Web Services API in Java applications. Samples may require extensive modifications to integrate within custom applications.
Set server and user details
/** Set the IP of the GFI FaxMaker serverThe machine where GFI FaxMaker is installed.. */ 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 URLThe Uniform Resource Locator is the address of a web page on the world wide web.. */ /** If an SSLSecure Sockets Layer 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();
Sending a 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); }
Monitoring submitted fax progress
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(); } }
Downloading received faxes
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("DTMFDual-tone multi-frequency signalling: "+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()); } }
Download Java sample: GFI_WSAPI.java (Right click > Save as...)