Search in sources :

Example 6 with HttpClientException

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

the class HttpEngine method runSync.

/**
 * @see org.apache.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.apache.ofbiz.service.ModelService, java.util.Map)
 */
@Override
public Map<String, Object> runSync(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
    DispatchContext dctx = dispatcher.getLocalContext(localName);
    String xmlContext = null;
    try {
        if (Debug.verboseOn()) {
            Debug.logVerbose("Serializing Context --> " + context, module);
        }
        xmlContext = XmlSerializer.serialize(context);
    } catch (Exception e) {
        throw new GenericServiceException("Cannot serialize context.", e);
    }
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("serviceName", modelService.invoke);
    if (xmlContext != null) {
        parameters.put("serviceContext", xmlContext);
    }
    HttpClient http = new HttpClient(this.getLocation(modelService), parameters);
    String postResult = null;
    try {
        postResult = http.post();
    } catch (HttpClientException e) {
        throw new GenericServiceException("Problems invoking HTTP request", e);
    }
    Map<String, Object> result = null;
    try {
        Object res = XmlSerializer.deserialize(postResult, dctx.getDelegator());
        if (res instanceof Map<?, ?>) {
            result = UtilGenerics.checkMap(res);
        } else {
            throw new GenericServiceException("Result not an instance of Map.");
        }
    } catch (Exception e) {
        throw new GenericServiceException("Problems deserializing result.", e);
    }
    return result;
}
Also used : DispatchContext(org.apache.ofbiz.service.DispatchContext) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HashMap(java.util.HashMap) HttpClient(org.apache.ofbiz.base.util.HttpClient) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) HashMap(java.util.HashMap) Map(java.util.Map) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) IOException(java.io.IOException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 7 with HttpClientException

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

the class HttpViewHandler method render.

public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
    if (request == null)
        throw new ViewHandlerException("Null HttpServletRequest object");
    if (UtilValidate.isEmpty(page))
        throw new ViewHandlerException("Null or empty source");
    if (Debug.infoOn())
        Debug.logInfo("Retreiving HTTP resource at: " + page, module);
    try {
        HttpClient httpClient = new HttpClient(page);
        String pageText = httpClient.get();
        // TODO: parse page and remove harmful tags like <HTML>, <HEAD>, <BASE>, etc - look into the OpenSymphony piece for an example
        response.getWriter().print(pageText);
    } catch (IOException e) {
        throw new ViewHandlerException("IO Error in view", e);
    } catch (HttpClientException e) {
        throw new ViewHandlerException(e.getNonNestedMessage(), e.getNested());
    }
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HttpClient(org.apache.ofbiz.base.util.HttpClient) IOException(java.io.IOException)

Example 8 with HttpClientException

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

the class ValueLinkServices method assignWorkingKey.

// change working key service
public static Map<String, Object> assignWorkingKey(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Properties props = getProperties(context);
    Locale locale = (Locale) context.get("locale");
    // get an api instance
    ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
    vl.reload();
    // place holder
    byte[] mwk = null;
    // see if we passed in the DES hex string
    String desHexString = (String) context.get("desHexString");
    if (UtilValidate.isEmpty(desHexString)) {
        mwk = vl.generateMwk();
    } else {
        mwk = vl.generateMwk(StringUtil.fromHexString(desHexString));
    }
    // encrypt the mwk
    String mwkHex = StringUtil.toHexString(vl.encryptViaKek(mwk));
    // build the request
    Map<String, Object> request = vl.getInitialRequestMap(context);
    request.put("Interface", "Encrypt");
    request.put("EncryptKey", mwkHex);
    request.put("EncryptID", Long.valueOf(vl.getWorkingKeyIndex().longValue() + 1));
    // send the request
    Map<String, Object> response = null;
    try {
        response = vl.send(request);
    } catch (HttpClientException e) {
        Debug.logError(e, "Problem communicating with VL");
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkCannotUpdateWorkingKey", locale));
    }
    Debug.logInfo("Response : " + response, module);
    // on success update the database / reload the cached api
    String responseCode = (String) response.get("responsecode");
    if (!"00".equals(responseCode)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkTransactionFailed", UtilMisc.toMap("responseCode", responseCode), locale));
    }
    GenericValue vlKeys = GenericValue.create(vl.getGenericValue());
    vlKeys.set("lastWorkingKey", vlKeys.get("workingKey"));
    vlKeys.set("workingKey", StringUtil.toHexString(mwk));
    vlKeys.set("workingKeyIndex", request.get("EncryptID"));
    vlKeys.set("lastModifiedDate", UtilDateTime.nowTimestamp());
    vlKeys.set("lastModifiedByUserLogin", userLogin != null ? userLogin.get("userLoginId") : null);
    try {
        vlKeys.store();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to store updated keys; the keys were changed with ValueLink : " + vlKeys, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkCannotStoreWorkingKey", locale));
    }
    vl.reload();
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) EntityUtilProperties(org.apache.ofbiz.entity.util.EntityUtilProperties) Properties(java.util.Properties)

Example 9 with HttpClientException

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

the class ValueLinkServices method refund.

public static Map<String, Object> refund(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    Properties props = getProperties(context);
    String cardNumber = (String) context.get("cardNumber");
    String pin = (String) context.get("pin");
    String currency = (String) context.get("currency");
    String orderId = (String) context.get("orderId");
    String partyId = (String) context.get("partyId");
    BigDecimal amount = (BigDecimal) context.get("amount");
    Locale locale = (Locale) context.get("locale");
    // override interface for void/rollback
    String iFace = (String) context.get("Interface");
    // get an api instance
    ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
    Map<String, Object> request = vl.getInitialRequestMap(context);
    request.put("Interface", iFace != null ? iFace : "Refund");
    request.put("CardNo", cardNumber);
    request.put("PIN", vl.encryptPin(pin));
    request.put("Amount", vl.getAmount(amount));
    request.put("LocalCurr", vl.getCurrency(currency));
    // user defined field #1
    if (UtilValidate.isNotEmpty(orderId)) {
        request.put("User1", orderId);
    }
    // user defined field #2
    if (UtilValidate.isNotEmpty(partyId)) {
        request.put("User2", partyId);
    }
    // set the timeout reversal
    setTimeoutReversal(dctx, context, request);
    // send the request
    Map<String, Object> response = null;
    try {
        response = vl.send(request);
    } catch (HttpClientException e) {
        Debug.logError(e, "Problem communicating with VL");
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkUnableToRefundGiftCard", locale));
    }
    String responseCode = (String) response.get("responsecode");
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("processResult", "00".equals(responseCode));
    result.put("responseCode", responseCode);
    result.put("authCode", response.get("authcode"));
    result.put("previousAmount", vl.getAmount((String) response.get("prevbal")));
    result.put("amount", vl.getAmount((String) response.get("newbal")));
    result.put("expireDate", response.get("expiredate"));
    result.put("cardClass", response.get("cardclass"));
    result.put("referenceNum", response.get("traceno"));
    Debug.logInfo("Refund Result : " + result, module);
    return result;
}
Also used : Locale(java.util.Locale) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) Delegator(org.apache.ofbiz.entity.Delegator) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) EntityUtilProperties(org.apache.ofbiz.entity.util.EntityUtilProperties) Properties(java.util.Properties) BigDecimal(java.math.BigDecimal)

Example 10 with HttpClientException

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

the class ValueLinkServices method linkPhysicalCard.

public static Map<String, Object> linkPhysicalCard(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    Properties props = getProperties(context);
    String virtualCard = (String) context.get("virtualCard");
    String virtualPin = (String) context.get("virtualPin");
    String physicalCard = (String) context.get("physicalCard");
    String physicalPin = (String) context.get("physicalPin");
    String partyId = (String) context.get("partyId");
    Locale locale = (Locale) context.get("locale");
    // get an api instance
    ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
    Map<String, Object> request = vl.getInitialRequestMap(context);
    request.put("Interface", "Link");
    request.put("VCardNo", virtualCard);
    request.put("VPIN", vl.encryptPin(virtualPin));
    request.put("PCardNo", physicalCard);
    request.put("PPIN", vl.encryptPin(physicalPin));
    // user defined field #2
    if (UtilValidate.isNotEmpty(partyId)) {
        request.put("User2", partyId);
    }
    // send the request
    Map<String, Object> response = null;
    try {
        response = vl.send(request);
    } catch (HttpClientException e) {
        Debug.logError(e, "Problem communicating with VL");
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkUnableToLinkGiftCard", locale));
    }
    String responseCode = (String) response.get("responsecode");
    Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "AccountingValueLinkGiftCardActivated", locale));
    result.put("processResult", "00".equals(responseCode));
    result.put("responseCode", responseCode);
    result.put("authCode", response.get("authcode"));
    result.put("amount", vl.getAmount((String) response.get("newbal")));
    result.put("expireDate", response.get("expiredate"));
    result.put("cardClass", response.get("cardclass"));
    result.put("referenceNum", response.get("traceno"));
    Debug.logInfo("Link Result : " + result, module);
    return result;
}
Also used : Locale(java.util.Locale) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) Delegator(org.apache.ofbiz.entity.Delegator) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) EntityUtilProperties(org.apache.ofbiz.entity.util.EntityUtilProperties) Properties(java.util.Properties)

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