Advertisement
lonestar101x

TrackWebServiceClient

Apr 14th, 2014
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.64 KB | None | 0 0
  1. import com.fedex.track.stub.*;
  2. import java.util.*;
  3. import javax.activation.DataHandler;
  4.  
  5. /**
  6.  * Demo of using the Track service with Axis
  7.  * to track a shipment.
  8.  * <p>
  9.  * com.fedex.track.stub is generated via WSDL2Java, like this:<br>
  10.  * <pre>
  11.  * java org.apache.axis.wsdl.WSDL2Java -w -p com.fedex.track.stub http://www.fedex.com/...../TrackService?wsdl
  12.  * </pre>
  13.  *
  14.  * This sample code has been tested with JDK 5 and Apache Axis 1.4
  15.  *
  16.  * Credentials:
  17.  
  18.  *
  19.  *
  20.  *
  21.  */
  22. public class TrackWebServiceClient {
  23.     //
  24.     public static void main(String[] args) throws Exception {  
  25.  
  26.         //
  27.             //System.setProperty("TrackingNumber", "9241990100130250264585");
  28.         TrackRequest request = new TrackRequest();
  29.  
  30.         request.setClientDetail(createClientDetail());
  31.         request.setWebAuthenticationDetail(createWebAuthenticationDetail());
  32.         //
  33.         TransactionDetail transactionDetail = new TransactionDetail();
  34.         transactionDetail.setCustomerTransactionId("java sample - Tracking Request"); //This is a reference field for the customer.  Any value can be used and will be provided in the response.
  35.         request.setTransactionDetail(transactionDetail);
  36.  
  37.         //
  38.         VersionId versionId = new VersionId("trck",8, 0, 0);
  39.         request.setVersion(versionId);
  40.         //
  41.         TrackSelectionDetail selectionDetail=new TrackSelectionDetail();
  42.         TrackPackageIdentifier packageIdentifier=new TrackPackageIdentifier();
  43.         packageIdentifier.setType(TrackIdentifierType.TRACKING_NUMBER_OR_DOORTAG);
  44.         //packageIdentifier.setValue("9241990100130250264585"); // tracking number
  45.         packageIdentifier.setValue("584007099279");
  46.         //packageIdentifier.setValue(getSystemProperty("TrackingNumber"));
  47.         selectionDetail.setPackageIdentifier(packageIdentifier);
  48.         request.setSelectionDetails(new TrackSelectionDetail[] {selectionDetail});
  49.         TrackRequestProcessingOptionType processingOption=TrackRequestProcessingOptionType.INCLUDE_DETAILED_SCANS;
  50.         request.setProcessingOptions(new TrackRequestProcessingOptionType[]{processingOption});
  51.  
  52.         //
  53.         try {
  54.             // Initializing the service
  55.             TrackServiceLocator service;
  56.             TrackPortType port;
  57.             //
  58.             service = new TrackServiceLocator();
  59.             updateEndPoint(service);
  60.             port = service.getTrackServicePort();
  61.             //
  62.             TrackReply reply = port.track(request); // This is the call to the web service passing in a request object and returning a reply object
  63.             //
  64.             if (isResponseOk(reply.getHighestSeverity())) // check if the call was successful
  65.             {
  66.                 System.out.println("--Track Reply--");
  67.                 if(printNotifications(reply.getNotifications())){
  68.                     printCompletedTrackDetail(reply.getCompletedTrackDetails());
  69.                 }
  70.                 System.out.println("--Track Reply--");
  71.             }
  72. //                        else
  73. //                        {
  74. //                            System.out.println("FAIL");
  75. //                        }
  76.            
  77.         } catch (Exception e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81.    
  82.     private static void printCompletedTrackDetail(CompletedTrackDetail[] ctd){
  83.         for (int i=0; i< ctd.length; i++) { // package detail information
  84.             boolean cont=true;
  85.             System.out.println("--Completed Tracking Detail--");
  86.             if(ctd[i].getNotifications()!=null){
  87.                 System.out.println("  Completed Track Detail Notifications--");
  88.                 cont=printNotifications(ctd[i].getNotifications());
  89.                 System.out.println("  Completed Track Detail Notifications--");
  90.             }
  91.             if(cont){
  92.                 print("DuplicateWayBill", ctd[i].getDuplicateWaybill());
  93.                 print("Track Details Count", ctd[i].getTrackDetailsCount());
  94.                 if(ctd[i].getMoreData()){
  95.                     System.out.println("  Additional package data not yet retrieved");
  96.                     if(ctd[i].getPagingToken()!=null){
  97.                         print("  Paging Token", ctd[i].getPagingToken());
  98.                     }
  99.                 }
  100.                 printTrackDetail(ctd[i].getTrackDetails());            
  101.             }
  102.             System.out.println("--Completed Tracking Detail--");
  103.             System.out.println();
  104.         }
  105.     }
  106.  
  107.     private static void printTrackDetail(TrackDetail[] td){
  108.         for (int i=0; i< td.length; i++) {
  109.             boolean cont=true;
  110.             System.out.println("--Track Details--");
  111.             if(td[i].getNotification()!=null){
  112.                 System.out.println("  Track Detail Notification-- lo");
  113.                 cont=printNotifications(td[i].getNotification());
  114.                 System.out.println("  Track Detail Notification--");
  115.             }
  116.             if(cont){
  117.                 print("Tracking Number", td[i].getTrackingNumber());
  118.                 print("Carrier code", td[i].getCarrierCode());
  119.                 if(td[i].getService()!=null){
  120.                     if(td[i].getService().getType()!=null &&
  121.                             td[i].getService().getDescription()!=null){
  122.                         print("Service", td[i].getService().getType());
  123.                         print("Description", td[i].getService().getDescription());
  124.                     }
  125.                 }
  126.                 if(td[i].getOtherIdentifiers()!=null){
  127.                     System.out.println("--Track Package Identifer--");
  128.                     printTrackOtherIdentifierDetail(td[i].getOtherIdentifiers());
  129.                     System.out.println("--Track Package Identifer--");
  130.                 }
  131.                 if(td[i].getStatusDetail()!=null){
  132.                     System.out.println("--Status Details--");
  133.                     printStatusDetail(td[i].getStatusDetail());
  134.                     System.out.println("--Status Details--");
  135.                 }
  136.                 if(td[i].getOriginLocationAddress()!=null){
  137.                     System.out.println("--Origin Location--");
  138.                     print(td[i].getOriginLocationAddress());
  139.                     System.out.println("--Origin Location--");
  140.                 }
  141.                 if(td[i].getDestinationAddress()!=null){
  142.                     System.out.println("--Destination Location--");
  143.                     printDestinationInformation(td[i]);
  144.                     System.out.println("--Destination Location--");
  145.                 }
  146.                 if(td[i].getActualDeliveryAddress()!=null){
  147.                     System.out.println("--Delivery Address--");
  148.                     print(td[i].getActualDeliveryAddress());
  149.                     System.out.println("--Delivery Address--");
  150.                 }
  151.                 if(td[i].getActualDeliveryTimestamp()!=null){
  152.                     System.out.println("Delivery Date");
  153.                     print(td[i].getActualDeliveryTimestamp());
  154.                 }
  155.                 if(td[i].getAppointmentDeliveryTimestamp()!=null){
  156.                     System.out.println("Appointment Date");
  157.                     print(td[i].getAppointmentDeliveryTimestamp());
  158.                 }
  159.                 if(td[i].getCommitmentTimestamp()!=null){
  160.                     System.out.println("Commitment Date");
  161.                     print(td[i].getCommitmentTimestamp());
  162.                 }
  163.                 if(td[i].getDeliveryAttempts().shortValue()>0){
  164.                     System.out.println("--Delivery Information--");
  165.                     printDeliveryInformation(td[i]);
  166.                     System.out.println("--Delivery Information--");
  167.                 }
  168.                 if(td[i].getCustomerExceptionRequests()!=null){
  169.                     System.out.println("--Customer Exception Information--");
  170.                     printCustomerExceptionRequests(td[i].getCustomerExceptionRequests());
  171.                     System.out.println("--Customer Exception Information--");
  172.                 }
  173.                 if(td[i].getCharges()!=null){
  174.                     System.out.println("--Charges--");
  175.                     printCharges(td[i].getCharges());
  176.                     System.out.println("--Charges--");
  177.                 }
  178.                 if(td[i].getEvents()!=null){
  179.                     System.out.println("--Tracking Events--");
  180.                     printTrackEvents(td[i].getEvents());
  181.                     System.out.println("--Tracking Events--");
  182.                 }
  183.                 System.out.println("--Track Details--");
  184.                 System.out.println();
  185.             }
  186.         }
  187.     }
  188.  
  189.     private static void printCustomerExceptionRequests(CustomerExceptionRequestDetail[] exceptions){
  190.         if(exceptions!=null){
  191.             for(int i=0; i<exceptions.length; i++){
  192.                 CustomerExceptionRequestDetail exception=exceptions[i];
  193.                 print("Exception Id", exception.getId());
  194.                 print("Excpetion Status Code", exception.getStatusCode());
  195.                 print("Excpetion Status Description", exception.getStatusDescription());
  196.                 if(exception.getCreateTime()!=null){
  197.                     System.out.println("  Customer Exception Date--");
  198.                     print(exception.getCreateTime());
  199.                     System.out.println("  Customer Exception Date--");
  200.                 }
  201.             }
  202.         }
  203.     }
  204.     private static void printTrackEvents(TrackEvent[] events){
  205.         if(events!=null){
  206.             for(int i=0; i<events.length;i++){
  207.                 TrackEvent event=events[i];
  208.                 print("Event no. ", i);
  209.                 print(event.getTimestamp());
  210.                 if(event.getEventType()!=null){
  211.                     print("Type", event.getEventType());
  212.                 }
  213.                 print("Station Id", event.getStationId());
  214.                 print("Exception Code", event.getStatusExceptionCode());
  215.                 print("", event.getStatusExceptionDescription());
  216.                 print("Description", event.getEventDescription());
  217.                 if(event.getAddress()!=null){
  218.                     System.out.println("  Event Address--");
  219.                     print(event.getAddress());
  220.                     System.out.println("  Event Address--");
  221.                 }
  222.                 System.out.println();
  223.             }
  224.         }
  225.     }
  226.     private static void printStatusDetail(TrackStatusDetail tsd){
  227.         if(tsd!=null){
  228.             print(tsd.getCreationTime());
  229.             print("Code", tsd.getCode());
  230.             if(tsd.getLocation()!=null){
  231.                 System.out.println("--Location Address Detail--");
  232.                 print(tsd.getLocation());
  233.                 System.out.println("--Location Address Detail--");
  234.             }
  235.             if(tsd.getAncillaryDetails()!=null){
  236.                 System.out.println("--Ancillary Details--");
  237.                 printAncillaryDetails(tsd.getAncillaryDetails());
  238.                 System.out.println("--Ancillary Details--");
  239.             }
  240.         }
  241.     }
  242.     private static void printAncillaryDetails(TrackStatusAncillaryDetail[] details){
  243.         if(details!=null){
  244.             for(int i=0; i<details.length;i++){
  245.                 if(details[i]!=null){
  246.                     if(details[i].getReason()!=null && details[i].getReasonDescription()!=null){
  247.                         print(details[i].getReason(), details[i].getReasonDescription());
  248.                     }
  249.                 }
  250.             }
  251.         }
  252.     }
  253.     private static void printDestinationInformation(TrackDetail td){
  254.         if(td.getDestinationAddress()!=null){
  255.             print(td.getDestinationAddress());
  256.         }
  257.         print("Destination Type", td.getDestinationLocationType());
  258.         print("Service Area", td.getDestinationServiceArea());
  259.         print("Service Area Description", td.getDestinationServiceAreaDescription());
  260.         print("Station Id", td.getDestinationStationId());
  261.         print("Destination Timezone Offset", td.getDestinationLocationTimeZoneOffset());
  262.     }
  263.     private static void printDeliveryOptionEligibility(DeliveryOptionEligibilityDetail[] options){
  264.         for(int i=0; i<options.length; i++){
  265.             DeliveryOptionEligibilityDetail option = options[i];
  266.             if(option!=null){
  267.                 print(option.getOption(), option.getEligibility());
  268.             }
  269.         }
  270.     }
  271.     private static void printDeliveryInformation(TrackDetail td){
  272.         System.out.println("Delivery attempts: " + td.getDeliveryAttempts());
  273.         print("Delivery Location", td.getDeliveryLocationDescription());
  274.         print("Delivery Signature", td.getDeliverySignatureName());
  275.         if(td.getDeliveryOptionEligibilityDetails()!=null){
  276.             System.out.println("Delivery Options");
  277.             printDeliveryOptionEligibility(td.getDeliveryOptionEligibilityDetails());
  278.         }
  279.     }
  280.     private static void printTrackOtherIdentifierDetail(TrackOtherIdentifierDetail[] id){
  281.         if(id!=null){
  282.             for(int i=0; i<id.length; i++){
  283.                 if(id[i].getPackageIdentifier()!=null){
  284.                     print(id[i].getPackageIdentifier().getType(),
  285.                             id[i].getPackageIdentifier().getValue());
  286.                 }
  287.             }
  288.         }
  289.     }
  290.  
  291.     private static void printTime(Calendar calendar){
  292.         if(calendar!=null){
  293.             int month = calendar.get(Calendar.MONTH)+1;
  294.             int day = calendar.get(Calendar.DAY_OF_MONTH);
  295.             int year = calendar.get(Calendar.YEAR);
  296.             String date = new String(year + "-" + month + "-" + day);
  297.             print("Date", date);
  298.             printDOW(calendar);
  299.         }
  300.     }
  301.     private static void printAddress(Address address){
  302.         print("__________________________________");
  303.         if(address.getStreetLines()!=null){
  304.             String[] streetLines=address.getStreetLines();
  305.             for(int i=0;i<streetLines.length;i++){
  306.                 if(streetLines[i]!=null){
  307.                     print("Street", streetLines[i]);
  308.                            
  309.                 }
  310.             }
  311.         }      
  312.         print("City", address.getCity());
  313.         print("State or Province Code", address.getStateOrProvinceCode());
  314.         print("Postal Code", address.getPostalCode());
  315.         print("Country Code", address.getCountryCode());
  316.         if(address.getResidential()!=null){
  317.             if(address.getResidential()){
  318.                 print("Address Type","Residential");
  319.             }else{
  320.                 print("Address Type", "Commercial");
  321.             }
  322.         }
  323.         print("__________________________________");
  324.     }
  325.     private static void printDOW(Calendar calendar){
  326.         if(calendar!=null){
  327.             String day;
  328.             switch(calendar.get(Calendar.DAY_OF_WEEK)){
  329.                 case 1: day="Sunday";
  330.                 break;
  331.                 case 2: day="Monday";
  332.                 break;
  333.                 case 3: day="Tuesday";
  334.                 break;
  335.                 case 4: day="Wedensday";
  336.                 break;
  337.                 case 5: day="Thursday";
  338.                 break;
  339.                 case 6: day="Friday";
  340.                 break;
  341.                 case 7: day="Saturday";
  342.                 break;
  343.                 default: day="Invalid Date";
  344.                 break;
  345.             }
  346.             print("Day of Week", day);         
  347.         }
  348.     }
  349.     private static boolean isResponseOk(NotificationSeverityType notificationSeverityType) {
  350.         if (notificationSeverityType == null) {
  351.             return false;
  352.         }
  353.         if (notificationSeverityType.equals(NotificationSeverityType.WARNING) ||
  354.             notificationSeverityType.equals(NotificationSeverityType.NOTE)    ||
  355.             notificationSeverityType.equals(NotificationSeverityType.SUCCESS)) {
  356.             return true;
  357.         }
  358.         return false;
  359.     }
  360.    
  361.  
  362.     private static ClientDetail createClientDetail() {
  363.         ClientDetail clientDetail = new ClientDetail();
  364.         //String accountNumber = System.getProperty("accountNumber");
  365.         String accountNumber = "my_account_number";
  366.         String meterNumber = "my_meter_number";
  367.         //String meterNumber = System.getProperty("meterNumber");
  368.        
  369.         //
  370.         // See if the accountNumber and meterNumber properties are set,
  371.         // if set use those values, otherwise default them to "XXX"
  372.         //
  373.         if (accountNumber == null) {
  374.             accountNumber = "XXX"; // Replace "XXX" with clients account number
  375.         }
  376.         if (meterNumber == null) {
  377.             meterNumber = "XXX"; // Replace "XXX" with clients meter number
  378.         }
  379.         clientDetail.setAccountNumber(accountNumber);
  380.         clientDetail.setMeterNumber(meterNumber);
  381.         return clientDetail;
  382.     }
  383.    
  384.     private static WebAuthenticationDetail createWebAuthenticationDetail() {
  385.         WebAuthenticationCredential wac = new WebAuthenticationCredential();
  386.         //String key = System.getProperty("key");
  387.         String key ="my_key";
  388.         //String password = System.getProperty("password");
  389.         String password ="my_password";
  390.        
  391.         //
  392.         // See if the key and password properties are set,
  393.         // if set use those values, otherwise default them to "XXX"
  394.         //
  395.         if (key == null) {
  396.             key = "XXX"; // Replace "XXX" with clients key
  397.         }
  398.         if (password == null) {
  399.             password = "XXX"; // Replace "XXX" with clients password
  400.         }
  401.         wac.setKey(key);
  402.         wac.setPassword(password);
  403.         return new WebAuthenticationDetail(wac);
  404.     }
  405.    
  406.     private static void printCharges(TrackChargeDetail[] charges){
  407.         if(charges!=null){
  408.             for(int i=0; i<charges.length; i++){
  409.                 print("Charge Type", charges[i].getType());
  410.                 printMoney(charges[i].getChargeAmount());
  411.             }
  412.         }
  413.     }
  414.     private static void printMoney(Money money){
  415.         if(money!=null){
  416.             String currency = money.getCurrency();
  417.             String amount = money.getAmount().toString();
  418.             print("Charge", currency + " " + amount);
  419.         }
  420.     }
  421.    
  422.     private static boolean printNotifications(Object n) {
  423.         boolean cont=true;
  424.         if(n!=null){
  425.             Notification[] notifications=null;
  426.             Notification notification=null;
  427.             if(n instanceof Notification[]){
  428.                 notifications=(Notification[])n;
  429.                 if (notifications == null || notifications.length == 0) {
  430.                     System.out.println("  No notifications returned");
  431.                 }
  432.                 for (int i=0; i < notifications.length; i++){
  433.                     printNotification(notifications[i]);
  434.                     if(!success(notifications[i])){cont=false;}
  435.                 }
  436.             }else if(n instanceof Notification){
  437.                 notification=(Notification)n;
  438.                 printNotification(notification);
  439.                 if(!success(notification)){cont=false;}
  440.             }
  441.  
  442.         }
  443.         return cont;
  444.     }
  445.     private static void printNotification(Notification notification){
  446.         if (notification == null) {
  447.             System.out.println("null");
  448.         }
  449.         NotificationSeverityType nst = notification.getSeverity();
  450.  
  451.         print("  Severity", (nst == null ? "null" : nst.getValue()));
  452.         print("  Code", notification.getCode());
  453.         print("  Message", notification.getMessage());
  454.         print("  Source", notification.getSource());
  455.     }
  456.    
  457.     private static boolean success(Notification notification){
  458.         Boolean cont = true;
  459.         if(notification!=null){
  460.             if(notification.getSeverity()==NotificationSeverityType.FAILURE ||
  461.                     notification.getSeverity()==NotificationSeverityType.ERROR){
  462.                 cont=false;
  463.             }
  464.         }
  465.        
  466.         return cont;
  467.     }
  468.    
  469.     private static void updateEndPoint(TrackServiceLocator serviceLocator) {
  470.         String endPoint = System.getProperty("endPoint");
  471.         if (endPoint != null) {
  472.             serviceLocator.setTrackServicePortEndpointAddress(endPoint);
  473.         }
  474.     }
  475.  
  476.     private static void print(Object k, Object v) {
  477.         if (k == null || v == null) {
  478.             return;
  479.         }
  480.         String key;
  481.         String value;
  482.         if(k instanceof String){
  483.             key=(String)k;
  484.         }else{
  485.             key=k.toString();
  486.         }
  487.         if(v instanceof String){
  488.             value=(String)v;
  489.         }else{
  490.             value=v.toString();
  491.         }
  492.         System.out.println("  " + key + ": " + value);
  493.     }
  494.    
  495.     private static void print(Object o){
  496.         if(o!=null){
  497.             if(o instanceof String){
  498.                 System.out.println((String)o);
  499.             }else if(o instanceof Address){
  500.                 printAddress((Address)o);
  501.             }else if(o instanceof Calendar){
  502.                 printTime((Calendar)o);
  503.             }else{
  504.                 System.out.println(o.toString());
  505.             }
  506.            
  507.         }
  508.     }
  509.    
  510.     private static void printWeight(String msg, Weight weight) {
  511.         if (msg == null || weight == null) {
  512.             return;
  513.         }
  514.         System.out.println(msg + ": " + weight.getValue() + " " + weight.getUnits());
  515.     }
  516.  
  517.     private static String getSystemProperty(String property){
  518.         String returnProperty = System.getProperty(property);
  519.         if (returnProperty == null){
  520.             return "XXX";
  521.         }
  522.         return returnProperty;
  523.     }
  524.  
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement