📄 dhlservices.java
字号:
String weight = (new Integer((int) shippableWeight.longValue())).toString(); // create AccessRequest XML doc using FreeMarker template String templateName = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.template.dhl.rate.estimate"); if ((templateName == null) || (templateName.trim().length() == 0)) { return ServiceUtil.returnError("Cannot get DHL Estimate: DHL Rate template not configured (shipment.template.dhl.rate.estimate"); } StringWriter outWriter = new StringWriter(); Map inContext = new HashMap(); inContext.put("action", "RateEstimate"); inContext.put("userid", userid); inContext.put("password", password); inContext.put("accountNbr", accountNbr); inContext.put("shippingKey", shippingKey); inContext.put("shipDate", UtilDateTime.nowTimestamp()); inContext.put("dhlShipmentDetailCode", dhlShipmentDetailCode); inContext.put("weight", weight); inContext.put("state", shipToAddress.getString("stateProvinceGeoId")); inContext.put("postalCode", shipToAddress.getString("postalCode")); try { Map tmpResult = ContentWorker.renderContentAsText(delegator, templateName, outWriter, inContext, null, locale, "text/plain"); } catch (Exception e) { Debug.logError(e, "Cannot get DHL Estimate: Failed to render DHL XML Request.", module); return ServiceUtil.returnError("Cannot get DHL Estimate: Failed to render DHL XML Request."); } String requestString = outWriter.toString(); if (Debug.verboseOn()) { Debug.logVerbose(requestString, module); } // send the request String rateResponseString = null; try { rateResponseString = sendDhlRequest(requestString); if (Debug.verboseOn()) { Debug.logVerbose(rateResponseString, module); } } catch (DhlConnectException e) { String uceErrMsg = "Error sending DHL request for DHL Service Rate: " + e.toString(); Debug.logError(e, uceErrMsg, module); return ServiceUtil.returnError(uceErrMsg); } Document rateResponseDocument = null; try { rateResponseDocument = UtilXml.readXmlDocument(rateResponseString, false); return handleDhlRateResponse(rateResponseDocument); } catch (SAXException e2) { String excErrMsg = "Error parsing the RatingServiceResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } catch (ParserConfigurationException e2) { String excErrMsg = "Error parsing the RatingServiceResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } catch (IOException e2) { String excErrMsg = "Error parsing the RatingServiceResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } } /* * Parses an XML document from DHL to get the rate estimate */ public static Map handleDhlRateResponse(Document rateResponseDocument) { List errorList = new LinkedList(); Map dhlRateCodeMap = new HashMap(); // process RateResponse Element rateResponseElement = rateResponseDocument.getDocumentElement(); DhlServices.handleErrors(rateResponseElement, errorList); if (UtilValidate.isNotEmpty(errorList)) { return ServiceUtil.returnError(errorList); } // handle Response element info Element responseElement = UtilXml.firstChildElement( rateResponseElement, "Shipment"); Element responseResultElement = UtilXml.firstChildElement( responseElement, "Result"); Element responseEstimateDetailElement = UtilXml.firstChildElement( responseElement, "EstimateDetail"); DhlServices.handleErrors(responseElement, errorList); if (UtilValidate.isNotEmpty(errorList)) { return ServiceUtil.returnError(errorList); } String responseStatusCode = UtilXml.childElementValue( responseResultElement, "Code"); String responseStatusDescription = UtilXml.childElementValue( responseResultElement, "Desc"); String dateGenerated = UtilXml.childElementValue( responseEstimateDetailElement, "DateGenerated"); Element responseServiceLevelCommitmentElement = UtilXml .firstChildElement(responseEstimateDetailElement, "ServiceLevelCommitment"); String responseServiceLevelCommitmentDescription = UtilXml .childElementValue(responseServiceLevelCommitmentElement, "Desc"); Element responseRateEstimateElement = UtilXml.firstChildElement( responseEstimateDetailElement, "RateEstimate"); String responseTotalChargeEstimate = UtilXml.childElementValue( responseRateEstimateElement, "TotalChargeEstimate"); Element responseChargesElement = UtilXml.firstChildElement( responseRateEstimateElement, "Charges"); List chargeNodeList = UtilXml.childElementList(responseChargesElement, "Charge"); List chargeList = new ArrayList(); if (chargeNodeList != null && chargeNodeList.size() > 0) { for (int i = 0; chargeNodeList.size() > i; i++) { Map charge = new HashMap(); Element responseChargeElement = (Element) chargeNodeList.get(i); Element responseChargeTypeElement = UtilXml.firstChildElement( responseChargeElement, "Type"); String responseChargeTypeCode = UtilXml.childElementValue( responseChargeTypeElement, "Code"); String responseChargeTypeDesc = UtilXml.childElementValue( responseChargeTypeElement, "Desc"); String responseChargeValue = UtilXml.childElementValue( responseChargeElement, "Value"); charge.put("chargeTypeCode", responseChargeTypeCode); charge.put("chargeTypeDesc", responseChargeTypeDesc); charge.put("chargeValue", responseChargeValue); chargeList.add(charge); } } Double shippingEstimateAmount = new Double(responseTotalChargeEstimate); dhlRateCodeMap.put("dateGenerated", dateGenerated); dhlRateCodeMap.put("serviceLevelCommitment", responseServiceLevelCommitmentDescription); dhlRateCodeMap.put("totalChargeEstimate", responseTotalChargeEstimate); dhlRateCodeMap.put("chargeList", chargeList); Map result = ServiceUtil.returnSuccess(); result.put("shippingEstimateAmount", shippingEstimateAmount); result.put("dhlRateCodeMap", dhlRateCodeMap); return result; } /* * Register a DHL account for shipping by obtaining the DHL shipping key */ public static Map dhlRegisterInquire(DispatchContext dctx, Map context) { Map result = new HashMap(); String postalCode = (String) context.get("postalCode"); String accountNbr = UtilProperties.getPropertyValue("shipment", "shipment.dhl.access.accountNbr"); if (accountNbr == null) { return ServiceUtil .returnError("accountNbr not found for Register Account."); } // create AccessRequest XML doc Document requestDocument = createAccessRequestDocument(); String requestString = null; Element requesElement = requestDocument.getDocumentElement(); Element registerRequestElement = UtilXml.addChildElement(requesElement, "Register", requestDocument); registerRequestElement.setAttribute("version", "1.0"); registerRequestElement.setAttribute("action", "ShippingKey"); UtilXml.addChildElementValue(registerRequestElement, "AccountNbr", accountNbr, requestDocument); UtilXml.addChildElementValue(registerRequestElement, "PostalCode", postalCode, requestDocument); try { requestString = UtilXml.writeXmlDocument(requestDocument); Debug.log("AccessRequest XML Document:" + requestString); } catch (IOException e) { String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString(); Debug.logError(e, ioeErrMsg, module); return ServiceUtil.returnError(ioeErrMsg); } // send the request String registerResponseString = null; try { registerResponseString = sendDhlRequest(requestString); Debug.log("DHL request for DHL Register Account:" + registerResponseString); } catch (DhlConnectException e) { String uceErrMsg = "Error sending DHL request for DHL Register Account: " + e.toString(); Debug.logError(e, uceErrMsg, module); return ServiceUtil.returnError(uceErrMsg); } Document registerResponseDocument = null; try { registerResponseDocument = UtilXml.readXmlDocument( registerResponseString, false); result = handleDhlRegisterResponse(registerResponseDocument); Debug.log("DHL response for DHL Register Account:" + registerResponseString); } catch (SAXException e2) { String excErrMsg = "Error parsing the RegisterAccountServiceSelectionResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } catch (ParserConfigurationException e2) { String excErrMsg = "Error parsing the RegisterAccountServiceSelectionResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } catch (IOException e2) { String excErrMsg = "Error parsing the RegisterAccountServiceSelectionResponse: " + e2.toString(); Debug.logError(e2, excErrMsg, module); return ServiceUtil.returnError(excErrMsg); } return result; } /* * Parse response from DHL registration request to get shipping key */ public static Map handleDhlRegisterResponse( Document registerResponseDocument) { List errorList = new LinkedList(); // process RegisterResponse Element registerResponseElement = registerResponseDocument
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -