import com.sun.xml.bind.api.JAXBRIContext;
import com.sun.xml.ws.api.message.Headers;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author dan.ware
*/
public class SendMailing extends HttpServlet {
/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// For more info on consuming Web Services;
// http://netbeans.org/kb/docs/websvc/client.html
//
// WEB SERVICE URL:
// https://www.cfhdocmail.com/TestAPI/DMWS.asmx?wsdl
//
// TEST WEBSITE URL:
// https://www.cfhdocmail.com/test/
//
// NOTES
// - Live URLs are the same as above but replace "Test" with "Live"
// - Remember to add a user to your account with "web service" permission.
// - The test/live web services share the same namespace so that only the
// reference will need changing when you go to live.
//
try { // Call Web Service Operation
https.cfhdocmail_com.liveapi.DMWS service = new https.cfhdocmail_com.liveapi.DMWS();
https.cfhdocmail_com.liveapi.DMWSSoap port = service.getDMWSSoap();
// Ensure the SOAP messages are logged in the GlassFish output panel
com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump = true;
// Define docmail login ID (for use in the SOAP header)
java.lang.String sUsr = "JavaTest";
java.lang.String sPwd = "javatest";
// Add the SOAP header with the login credentials using JAXB
com.sun.xml.ws.developer.WSBindingProvider bp = (com.sun.xml.ws.developer.WSBindingProvider)port;
bp.setOutboundHeaders(Headers.create((JAXBRIContext)JAXBContext.newInstance(CustomSoapHeader.class), new CustomSoapHeader(sUsr, sPwd)));
// Create a mailing object and set the options
https.cfhdocmail_com.liveapi.Mailing oMailing = new https.cfhdocmail_com.liveapi.Mailing();
oMailing.setMailingName("JAVA TEST");
oMailing.setMailingDescription("Created From: DMWS_Main_Servlet");
oMailing.setIsBlackAndWhite(true);
oMailing.setIsDuplex(false);
oMailing.setAddressNameFormat("T"); // "Title Firstname Surname" format
oMailing.setDespatchCode("2"); // 2nd class post
// Add mail pack and address list from stored ones in docmail account
// (can be administered from the website)
oMailing.setMailPackName("Sample Mail Pack");
oMailing.setMailingListName("Sample Mailing List");
// TEMPLATES AND ADDRESSES
// There are many other ways of adding the documents/address lists
// such as by URL, populating instances of Templates/Addresses classes
// (defined by the WSDL) or uploading the binary (BASE64).
//
// View the userguide for more info;
// http://www.cfhdocmail.com/downloads/WebServiceHelp.pdf
// Output a display to the calling HTML page
out.println("");
out.println("
FAILED:
"+oResult.getFailureMessage()+"
Order GUID (unique ID): "+oResult.getMailingGUID()+"
"); // NOTE: Set the uuid of the mailing record for further actions/calls oMailing.setGUID(oResult.getMailingGUID()); // Request a proof to approve https.cfhdocmail_com.liveapi.Proof oProof = port.getProof(oMailing.getGUID(), true); // Wait for the proof to be ready int iSecondCount = 0; while ((iSecondCount < 180) && (oProof.isSuccess() == true) && (oProof.isProofReady() == false)) { Thread.sleep(1000); // NOTE: Proof file can be obtained from oProof.getProofFileData() // if you pass a "true" value to the second argument of getProof() oProof = port.getProof(oMailing.getGUID(), false); iSecondCount ++; } // Display the result of the proof request if ((oProof.isProofReady() == false) || (oProof.isSuccess() == false)) { out.println("Proofing failed:
Proof not returned within 3 minutes
Order ref: "+oProof.getOrderRef()+"
"); out.println("Mailing cost: £"+oProof.getTotalCost()+" (includes £"+oProof.getVAT()+" VAT)
"); // Submit the order (effectively approving the proof) https.cfhdocmail_com.liveapi.PlaceOrderResult oConfirmOrderResult = port.placeOrder(oMailing.getGUID(), ""); if (oConfirmOrderResult.isSuccess() == false) { out.println("Place order failed:
"+oConfirmOrderResult.getFailureMessage()+"
Order has been successfully submitted
"+oConfirmOrderResult.getBalanceMessage()+"
Exception:
"+ex.getMessage()+"
Stack trace:
"+ex.getStackTrace()+"