Search in sources :

Example 16 with HttpClient

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

the class WidgetMacroLibraryTests method testFopMacroLibrary.

public void testFopMacroLibrary() throws Exception {
    String screentextUrl = screenUrl.concat("Fop");
    HttpClient http = initHttpClient();
    http.setUrl(screentextUrl.concat(authentificationQuery));
    // FIXME need to check if the stream is an application-pdf that don't contains ftl stack trace
    InputStream screenInputStream = (InputStream) http.postStream();
    assertNotNull("Response failed from ofbiz", screenInputStream);
    assertEquals("Response contentType isn't good : " + http.getResponseContentType(), "application/pdf;charset=UTF-8", http.getResponseContentType());
    String screenOutString = "";
    try {
        BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE);
        Metadata metadata = new Metadata();
        new PDFParser().parse(screenInputStream, handler, metadata, new ParseContext());
        screenOutString = handler.toString();
    } finally {
        screenInputStream.close();
    }
    // Test if a ftl macro error is present
    assertFalse("Fop Screen contains Macro on error : see " + screentextUrl + " for more detail", screenOutString.contains("FreeMarker template error:"));
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) InputStream(java.io.InputStream) HttpClient(org.apache.ofbiz.base.util.HttpClient) PDFParser(org.apache.tika.parser.pdf.PDFParser) Metadata(org.apache.tika.metadata.Metadata) ParseContext(org.apache.tika.parser.ParseContext)

Example 17 with HttpClient

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

the class RitaApi method send.

public RitaApi send() throws IOException, GeneralException {
    if (host == null || port == 0) {
        throw new GeneralException("TCP transaction not supported without valid host/port configuration");
    }
    if (mode == MODE_IN) {
        String stream = this.toString() + "..\r\n";
        Debug.logInfo("Sending - \n" + stream, module);
        String urlString = "http://" + host + ":" + port;
        HttpClient http = new HttpClient(urlString);
        http.setDebug(true);
        Map<String, String> docMap = new HashMap<>();
        String resp = null;
        try {
            resp = http.post(stream);
        } catch (HttpClientException e) {
            Debug.logError(e, module);
            throw new IOException(e.getMessage());
        }
        String[] lines = resp.split("\n");
        for (int i = 0; i < lines.length; i++) {
            Debug.logInfo(lines[i], module);
            if (!".".equals(lines[i].trim())) {
                String[] lineSplit = lines[i].trim().split(" ", 2);
                if (lineSplit != null && lineSplit.length == 2) {
                    docMap.put(lineSplit[0], lineSplit[1]);
                } else {
                    Debug.logWarning("Line split error - " + lines[i], module);
                }
            } else {
                break;
            }
        }
        RitaApi out = new RitaApi(docMap);
        return out;
    }
    throw new IllegalStateException("Cannot send output object");
}
Also used : GeneralException(org.apache.ofbiz.base.util.GeneralException) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HashMap(java.util.HashMap) HttpClient(org.apache.ofbiz.base.util.HttpClient) IOException(java.io.IOException)

Example 18 with HttpClient

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

the class AIMPaymentServices method processCard.

private static Map<String, Object> processCard(Map<String, Object> request, Properties props, Locale locale) {
    Map<String, Object> result = new HashMap<String, Object>();
    String url = props.getProperty("url");
    if (UtilValidate.isEmpty(url)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingAuthorizeNetTransactionUrlNotFound", locale));
    }
    if (isTestMode()) {
        Debug.logInfo("TEST Authorize.net using url [" + url + "]", module);
        Debug.logInfo("TEST Authorize.net request string " + request.toString(), module);
        Debug.logInfo("TEST Authorize.net properties string " + props.toString(), module);
    }
    // card present has a different layout from standard AIM; this determines how to parse the response
    int apiType = UtilValidate.isEmpty(props.get("cpMarketType")) ? AuthorizeResponse.AIM_RESPONSE : AuthorizeResponse.CP_RESPONSE;
    try {
        HttpClient httpClient = new HttpClient(url, request);
        String certificateAlias = props.getProperty("certificateAlias");
        httpClient.setClientCertificateAlias(certificateAlias);
        String httpResponse = httpClient.post();
        Debug.logInfo("transaction response: " + httpResponse, module);
        AuthorizeResponse ar = new AuthorizeResponse(httpResponse, apiType);
        if (ar.isApproved()) {
            result.put("authResult", Boolean.TRUE);
        } else {
            result.put("authResult", Boolean.FALSE);
            if (Debug.infoOn()) {
                Debug.logInfo("transactionId:  " + ar.getTransactionId(), module);
                Debug.logInfo("responseCode:   " + ar.getResponseCode(), module);
                Debug.logInfo("responseReason: " + ar.getReasonCode(), module);
                Debug.logInfo("reasonText:     " + ar.getReasonText(), module);
            }
        }
        result.put("httpResponse", httpResponse);
        result.put("authorizeResponse", ar);
    } catch (HttpClientException e) {
        Debug.logInfo(e, "Could not complete Authorize.Net transaction: " + e.toString(), module);
    }
    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    return result;
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) HashMap(java.util.HashMap) HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 19 with HttpClient

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

the class TestEvent method httpClientTest.

public static String httpClientTest(HttpServletRequest request, HttpServletResponse response) {
    try {
        HttpClient http = new HttpClient("http://ofbiz.apache.org/cgi-bin/http_test.pl");
        http.setHeader("Cookie", "name=value,value=name");
        http.setHeader("User-Agent", "Mozilla/4.0");
        http.setParameter("testId", "testing");
        Debug.logInfo(http.post(), module);
    } catch (Exception e) {
        Debug.logInfo(e, "HttpClientException Caught.", module);
    }
    return "success";
}
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