⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhlservices.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * *  Copyright (c) 2003 The Open For Business Project - www.ofbiz.org * *  Permission is hereby granted, free of charge, to any person obtaining a *  copy of this software and associated documentation files (the "Software"), *  to deal in the Software without restriction, including without limitation *  the rights to use, copy, modify, merge, publish, distribute, sublicense, *  and/or sell copies of the Software, and to permit persons to whom the *  Software is furnished to do so, subject to the following conditions: * *  The above copyright notice and this permission notice shall be included *  in all copies or substantial portions of the Software. * *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *  THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.shipment.thirdparty.dhl;import java.io.FileOutputStream;import java.io.IOException;import java.io.StringWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Locale;import java.util.Map;import javax.xml.parsers.ParserConfigurationException;import org.ofbiz.base.util.Base64;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.HttpClient;import org.ofbiz.base.util.HttpClientException;import org.ofbiz.base.util.StringUtil;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.service.ModelService;import org.ofbiz.content.content.ContentWorker;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.service.GenericServiceException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.SAXException;/** * DHL ShipmentServices *  * <p>Implementation of DHL US domestic shipment interface using DHL ShipIT XML APi.</p> * * Shipment services not supported in DHL ShipIT 1.1 * <ul> * <li>Multiple Piece shipping (Shipment must not have more than one * ShipmentPackage)</li> * <li>Dynamic editing of previously submitted shipment (void first then * resubmit instead)</li> * <li>Label size 4"x6"</li> * <li>Out of origin shipping</li> * </ul> *  * TODO: International *  * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres </a> * @author <a href="mailto:sichen@hotmail.com">Si Chen </a> * @author <a href="mailto:timchen_sh@hotmail.com">Tim Chen </a> */public class DhlServices {    public final static String module = DhlServices.class.getName();    public final static String shipmentPropertiesFile = "shipment.properties";    public final static String DHL_WEIGHT_UOM_ID = "WT_lb"; // weight Uom used by DHL    /**     * Opens a URL to DHL and makes a request.     *      * @param dhlService     *            Name of the DHL service to invoke     * @param xmlString     *            XML message to send     * @return XML string response from DHL     * @throws DhlConnectException     */    public static String sendDhlRequest(String xmlString)            throws DhlConnectException {        String conStr = UtilProperties.getPropertyValue(shipmentPropertiesFile,                "shipment.dhl.connect.url");        if (conStr == null) {            throw new DhlConnectException(                    "Incomplete connection URL; check your DHL configuration");        }        // xmlString should contain the auth document at the beginning        // all documents require an <?xml version="1.0"?> header        if (xmlString == null) {            throw new DhlConnectException("XML message cannot be null");        }        // prepare the connect string        conStr = conStr.trim();        String timeOutStr = UtilProperties.getPropertyValue(                shipmentPropertiesFile, "shipment.dhl.connect.timeout", "60");        int timeout = 60;        try {            timeout = Integer.parseInt(timeOutStr);        } catch (NumberFormatException e) {            Debug.logError(e, "Unable to set timeout to " + timeOutStr                    + " using default " + timeout);        }        if (Debug.verboseOn()) {            Debug.logVerbose("DHL Connect URL : " + conStr, module);            Debug.logVerbose("DHL XML String : " + xmlString, module);        }        HttpClient http = new HttpClient(conStr);        http.setTimeout(timeout * 1000);        String response = null;        try {            response = http.post(xmlString);        } catch (HttpClientException e) {            Debug.logError(e, "Problem connecting with DHL server", module);            throw new DhlConnectException("URL Connection problem", e);        }        if (response == null) {            throw new DhlConnectException("Received a null response");        }        if (Debug.verboseOn()) {            Debug.logVerbose("DHL Response : " + response, module);        }        return response;    }    /*     * Service to obtain a rate estimate from DHL for a shipment. Notes: Only one package per shipment currently supported by DHL ShipIT.     * If this service returns a null shippingEstimateAmount, then the shipment has not been processed     */    public static Map dhlRateEstimate(DispatchContext dctx, Map context) {        GenericDelegator delegator = dctx.getDelegator();        LocalDispatcher dispatcher = dctx.getDispatcher();        Locale locale = (Locale) context.get("locale");        // some of these can be refactored        String upsRateInquireMode = (String) context.get("upsRateInquireMode");        String carrierRoleTypeId = (String) context.get("carrierRoleTypeId");        String carrierPartyId = (String) context.get("carrierPartyId");        String shipmentMethodTypeId = (String) context.get("shipmentMethodTypeId");        String shippingContactMechId = (String) context.get("shippingContactMechId");        List shippableItemInfo = (List) context.get("shippableItemInfo");        Double shippableTotal = (Double) context.get("shippableTotal");        Double shippableQuantity = (Double) context.get("shippableQuantity");        Double shippableWeight = (Double) context.get("shippableWeight");                if (shipmentMethodTypeId.equals("NO_SHIPPING")) {            Map result = ServiceUtil.returnSuccess();            result.put("shippingEstimateAmount", null);            return result;        }        // translate shipmentMethodTypeId to DHL service code        String dhlShipmentDetailCode = null;        try {            GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", UtilMisc.toMap("shipmentMethodTypeId", shipmentMethodTypeId,                    "partyId", carrierPartyId, "roleTypeId", "CARRIER"));            if (carrierShipmentMethod == null) {                return ServiceUtil.returnError("No CarrierShipmentMethod entry for carrier " + carrierPartyId + ", shipmentMethodTypeId " + shipmentMethodTypeId);            }            dhlShipmentDetailCode = carrierShipmentMethod.getString("carrierServiceCode");        } catch (GenericEntityException e) {            Debug.logError(e, "Failed to get rate estimate: " + e.getMessage(), module);        }        // shipping credentials (configured in properties)        String userid = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.dhl.access.userid");        String password = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.dhl.access.password");        String shippingKey = UtilProperties.getPropertyValue("shipment", "shipment.dhl.access.shippingKey");        String accountNbr = UtilProperties.getPropertyValue("shipment", "shipment.dhl.access.accountNbr");        if ((shippingKey == null) || (accountNbr == null) || (shippingKey.length() == 0) || (accountNbr.length() == 0)) {            return ServiceUtil.returnError("DHL Shipping Credentials are not configured. (check shipment.dhl.access)");        }        // obtain the ship-to address        GenericValue shipToAddress = null;        if (shippingContactMechId != null) {            try {                shipToAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId));                if (shipToAddress == null) {                    return ServiceUtil.returnError("Unable to determine ship-to address");                }            }            catch (GenericEntityException e) {                Debug.logError(e, module);            }        }        if ((shippableWeight == null) || (shippableWeight.doubleValue() <= 0.0)) {            String tmpValue = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.default.weight.value");            if (tmpValue != null) {                try {                    shippableWeight = new Double(tmpValue);                } catch (Exception e) {                    return ServiceUtil.returnError("Cannot get DHL Estimate: Default shippable weight not configured (shipment.default.weight.value)");                }            }        }        // TODO: if a weight UOM is passed in, use convertUom service to convert it here        if (shippableWeight.doubleValue() < 1.0) {            Debug.logWarning("DHL Estimate: Weight is less than 1 lb, submitting DHL minimum of 1 lb for estimate.", module);            shippableWeight = new Double(1.0);        }        if ((dhlShipmentDetailCode.equals("G") && shippableWeight.doubleValue() > 999) || (shippableWeight.doubleValue() > 150)) {            return ServiceUtil.returnError("Cannot get DHL Estimate: Shippable weight cannot be greater than 999 lbs for ground or 150 lbs for all other services.");        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -