Search in sources :

Example 1 with HttpClient

use of org.apache.ofbiz.base.util.HttpClient 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 HttpClient

use of org.apache.ofbiz.base.util.HttpClient 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 HttpClient

use of org.apache.ofbiz.base.util.HttpClient 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 HttpClient

use of org.apache.ofbiz.base.util.HttpClient 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 HttpClient

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

the class WidgetMacroLibraryTests method testXmlMacroLibrary.

public void testXmlMacroLibrary() throws Exception {
    String screenxmlUrl = screenUrl.concat("Xml");
    HttpClient http = initHttpClient();
    http.setUrl(screenxmlUrl.concat(authentificationQuery));
    String screenOutString = http.post();
    assertNotNull("Response failed from ofbiz", screenOutString);
    assertEquals("Response contentType isn't good : " + http.getResponseContentType(), "text/xml;charset=UTF-8", http.getResponseContentType());
    // Test if a ftl macro error is present
    assertFalse("Xml Screen contains Macro on error : see " + screenxmlUrl + " for more detail", screenOutString.contains("FreeMarker template error:"));
}
Also used : HttpClient(org.apache.ofbiz.base.util.HttpClient)

Aggregations

HttpClient (org.apache.ofbiz.base.util.HttpClient)19 HttpClientException (org.apache.ofbiz.base.util.HttpClientException)10 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 GeneralException (org.apache.ofbiz.base.util.GeneralException)3 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TransformerException (javax.xml.transform.TransformerException)2 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)2 Document (org.w3c.dom.Document)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Locale (java.util.Locale)1 Map (java.util.Map)1 DispatchContext (org.apache.ofbiz.service.DispatchContext)1 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)1 Metadata (org.apache.tika.metadata.Metadata)1 ParseContext (org.apache.tika.parser.ParseContext)1