Search in sources :

Example 1 with HttpClientException

use of org.apache.ofbiz.base.util.HttpClientException in project ofbiz-framework by apache.

the class FedexConnectException method sendFedexRequest.

/**
 * Opens a URL to Fedex and makes a request.
 *
 * @param xmlString XML message to send
 * @param delegator the delegator
 * @param shipmentGatewayConfigId the shipmentGatewayConfigId
 * @param resource resource file name
 * @return XML string response from FedEx
 * @throws FedexConnectException
 */
public static String sendFedexRequest(String xmlString, Delegator delegator, String shipmentGatewayConfigId, String resource, Locale locale) throws FedexConnectException {
    String url = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectUrl", resource, "shipment.fedex.connect.url");
    if (UtilValidate.isEmpty(url)) {
        throw new FedexConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexConnectUrlIncomplete", locale));
    }
    // all documents require an <?xml version="1.0" encoding="UTF-8" ?> header
    if (!xmlString.matches("^(?s)<\\?xml\\s+version=\"1\\.0\"\\s+encoding=\"UTF-8\"\\s*\\?>.*")) {
        throw new FedexConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexXmlHeaderMalformed", locale));
    }
    // prepare the connect string
    url = url.trim();
    String timeOutStr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectTimeout", resource, "shipment.fedex.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("Fedex Connect URL : " + url, module);
        Debug.logVerbose("Fedex XML String : " + xmlString, module);
    }
    HttpClient http = new HttpClient(url);
    http.setTimeout(timeout * 1000);
    String response = null;
    try {
        response = http.post(xmlString);
    } catch (HttpClientException e) {
        Debug.logError(e, "Problem connecting to Fedex server", module);
        throw new FedexConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexConnectUrlProblem", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    if (response == null) {
        throw new FedexConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexReceivedNullResponse", locale));
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("Fedex Response : " + response, module);
    }
    return response;
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 2 with HttpClientException

use of org.apache.ofbiz.base.util.HttpClientException in project ofbiz-framework by apache.

the class UpsConnectException method sendUpsRequest.

/**
 * Opens a URL to UPS and makes a request.
 * @param upsService Name of the UPS service to invoke
 * @param xmlString XML message to send
 * @return XML string response from UPS
 * @throws UpsConnectException
 */
public static String sendUpsRequest(String upsService, String xmlString, String shipmentGatewayConfigId, String resource, Delegator delegator, Locale locale) throws UpsConnectException {
    // need a ups service to call
    if (upsService == null) {
        throw new UpsConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsServiceNameCannotBeNull", locale));
    }
    // all documents require an <?xml version="1.0"?> header
    if (xmlString == null) {
        throw new UpsConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsXmlMessageCannotBeNull", locale));
    }
    String conStr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectUrl", resource, "shipment.ups.connect.url");
    if (UtilValidate.isEmpty(conStr)) {
        throw new UpsConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsIncompleteConnectionURL", locale));
    }
    // prepare the connect string
    conStr = conStr.trim();
    if (!conStr.endsWith("/")) {
        conStr = conStr + "/";
    }
    conStr = conStr + upsService;
    String timeOutStr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectTimeout", resource, "shipment.ups.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);
    }
    HttpClient http = new HttpClient(conStr);
    http.setTimeout(timeout * 1000);
    http.setAllowUntrusted(true);
    String response = null;
    try {
        response = http.post(xmlString);
    } catch (HttpClientException e) {
        Debug.logError(e, "Problem connecting with UPS server [" + conStr + "]", module);
        throw new UpsConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsURLConnectionProblem", UtilMisc.toMap("exception", e), locale));
    }
    if (response == null) {
        throw new UpsConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsReceivedNullResponse", locale));
    }
    if (Debug.verboseOn())
        Debug.logVerbose("UPS Response : " + response, module);
    return response;
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 3 with HttpClientException

use of org.apache.ofbiz.base.util.HttpClientException in project ofbiz-framework by apache.

the class UspsRequestException method sendUspsRequest.

private static Document sendUspsRequest(String requestType, Document requestDocument, Delegator delegator, String shipmentGatewayConfigId, String resource, Locale locale) throws UspsRequestException {
    String conUrl = null;
    List<String> labelRequestTypes = UtilMisc.toList("PriorityMailIntl", "PriorityMailIntlCertify");
    if (labelRequestTypes.contains(requestType)) {
        conUrl = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectUrlLabels", resource, "shipment.usps.connect.url.labels");
    } else {
        conUrl = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectUrl", resource, "shipment.usps.connect.url");
    }
    if (UtilValidate.isEmpty(conUrl)) {
        throw new UspsRequestException(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsConnectUrlIncomplete", locale));
    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        UtilXml.writeXmlDocument(requestDocument, os, "UTF-8", true, false, 0);
    } catch (TransformerException e) {
        throw new UspsRequestException(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsSerializingError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    String xmlString = new String(os.toByteArray(), UtilIO.getUtf8());
    Debug.logInfo("USPS XML request string: " + xmlString, module);
    String timeOutStr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectTimeout", resource, "shipment.usps.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);
    }
    HttpClient http = new HttpClient(conUrl);
    http.setTimeout(timeout * 1000);
    http.setParameter("API", requestType);
    http.setParameter("XML", xmlString);
    String responseString = null;
    try {
        responseString = http.get();
    } catch (HttpClientException e) {
        throw new UspsRequestException(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsConnectionProblem", UtilMisc.toMap("errorString", e), locale));
    }
    Debug.logInfo("USPS response: " + responseString, module);
    if (UtilValidate.isEmpty(responseString)) {
        return null;
    }
    Document responseDocument = null;
    try {
        responseDocument = UtilXml.readXmlDocument(responseString, false);
    } catch (Exception e) {
        throw new UspsRequestException(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsResponseError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    // If a top-level error document is returned, throw exception
    // Other request-level errors should be handled by the caller
    Element responseElement = responseDocument.getDocumentElement();
    if ("Error".equals(responseElement.getNodeName())) {
        throw new UspsRequestException(UtilXml.childElementValue(responseElement, "Description"));
    }
    return responseDocument;
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HttpClient(org.apache.ofbiz.base.util.HttpClient) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException)

Example 4 with HttpClientException

use of org.apache.ofbiz.base.util.HttpClientException in project ofbiz-framework by apache.

the class DhlConnectException method sendDhlRequest.

/**
 * Opens a URL to DHL and makes a request.
 *
 * @param xmlString Name of the DHL service to invoke
 * @param delegator the delegator
 * @param shipmentGatewayConfigId the shipment gateway config id
 * @param resource the resource file (i.e. shipment.properties)
 * @param locale locale in use
 * @return XML string response from DHL
 * @throws DhlConnectException
 */
public static String sendDhlRequest(String xmlString, Delegator delegator, String shipmentGatewayConfigId, String resource, Locale locale) throws DhlConnectException {
    String conStr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectUrl", resource, "shipment.dhl.connect.url");
    if (conStr.isEmpty()) {
        throw new DhlConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlConnectUrlIncomplete", locale));
    }
    // all documents require an <?xml version="1.0"?> header
    if (xmlString == null) {
        throw new DhlConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlXmlCannotBeNull", locale));
    }
    // prepare the connect string
    conStr = conStr.trim();
    String timeOutStr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "connectTimeout", resource, "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(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlConnectUrlProblem", UtilMisc.toMap("errorString", e), locale), e);
    }
    if (response == null) {
        throw new DhlConnectException(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlReceivedNullResponse", locale));
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("DHL Response : " + response, module);
    }
    return response;
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 5 with HttpClientException

use of org.apache.ofbiz.base.util.HttpClientException in project ofbiz-framework by apache.

the class EmailServices method sendMailFromUrl.

/**
 * JavaMail Service that gets body content from a URL
 *@param ctx The DispatchContext that this service is operating in
 *@param rcontext Map containing the input parameters
 *@return Map with the result of the service, the output parameters
 */
public static Map<String, Object> sendMailFromUrl(DispatchContext ctx, Map<String, ? extends Object> rcontext) {
    // pretty simple, get the content and then call the sendMail method below
    Map<String, Object> sendMailContext = UtilMisc.makeMapWritable(rcontext);
    String bodyUrl = (String) sendMailContext.remove("bodyUrl");
    Map<String, Object> bodyUrlParameters = UtilGenerics.checkMap(sendMailContext.remove("bodyUrlParameters"));
    Locale locale = (Locale) rcontext.get("locale");
    LocalDispatcher dispatcher = ctx.getDispatcher();
    URL url = null;
    try {
        url = new URL(bodyUrl);
    } catch (MalformedURLException e) {
        Debug.logWarning(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendMalformedUrl", UtilMisc.toMap("bodyUrl", bodyUrl, "errorString", e.toString()), locale));
    }
    HttpClient httpClient = new HttpClient(url, bodyUrlParameters);
    String body = null;
    try {
        body = httpClient.post();
    } catch (HttpClientException e) {
        Debug.logWarning(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendGettingError", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    sendMailContext.put("body", body);
    Map<String, Object> sendMailResult;
    try {
        sendMailResult = dispatcher.runSync("sendMail", sendMailContext);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    // just return the same result; it contains all necessary information
    return sendMailResult;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) MalformedURLException(java.net.MalformedURLException) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HttpClient(org.apache.ofbiz.base.util.HttpClient) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) URL(java.net.URL)

Aggregations

HttpClientException (org.apache.ofbiz.base.util.HttpClientException)20 Locale (java.util.Locale)11 HttpClient (org.apache.ofbiz.base.util.HttpClient)10 Properties (java.util.Properties)9 UtilProperties (org.apache.ofbiz.base.util.UtilProperties)9 Delegator (org.apache.ofbiz.entity.Delegator)9 EntityUtilProperties (org.apache.ofbiz.entity.util.EntityUtilProperties)9 IOException (java.io.IOException)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)3 GeneralException (org.apache.ofbiz.base.util.GeneralException)3 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)3 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)3 Document (org.w3c.dom.Document)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TransformerException (javax.xml.transform.TransformerException)2 Element (org.w3c.dom.Element)2 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1