Search in sources :

Example 11 with HttpClient

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

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

the class ValueLinkApi method send.

/**
 * Transmit a request to ValueLink
 * @param url override URL from what is defined in the properties
 * @param request request Map of request parameters
 * @return Map of response parameters
 * @throws HttpClientException
 */
public Map<String, Object> send(String url, Map<String, Object> request) throws HttpClientException {
    if (debug) {
        Debug.logInfo("Request : " + url + " / " + request, module);
    }
    // read the timeout value
    String timeoutString = (String) props.get("payment.valuelink.timeout");
    int timeout = 34;
    try {
        timeout = Integer.parseInt(timeoutString);
    } catch (NumberFormatException e) {
        Debug.logError(e, "Unable to set timeout to " + timeoutString + " using default " + timeout);
    }
    // create the HTTP client
    HttpClient client = new HttpClient(url, request);
    client.setTimeout(timeout * 1000);
    client.setDebug(debug);
    client.setClientCertificateAlias((String) props.get("payment.valuelink.certificateAlias"));
    String response = client.post();
    // parse the response and return a map
    return this.parseResponse(response);
}
Also used : HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 13 with HttpClient

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

the class ClearCommerceException method sendRequest.

private static Document sendRequest(Document requestDocument, String paymentConfig, Delegator delegator) throws ClearCommerceException {
    if (UtilValidate.isEmpty(paymentConfig)) {
        paymentConfig = "payment.properties";
    }
    String serverURL = EntityUtilProperties.getPropertyValue(paymentConfig, "payment.clearcommerce.serverURL", delegator);
    if (UtilValidate.isEmpty(serverURL)) {
        throw new ClearCommerceException("Missing server URL; check your ClearCommerce configuration");
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("ClearCommerce server URL: " + serverURL, module);
    }
    OutputStream os = new ByteArrayOutputStream();
    try {
        UtilXml.writeXmlDocument(requestDocument, os, "UTF-8", true, false, 0);
    } catch (TransformerException e) {
        throw new ClearCommerceException("Error serializing requestDocument: " + e.getMessage());
    }
    String xmlString = os.toString();
    if (Debug.verboseOn()) {
        Debug.logVerbose("ClearCommerce XML request string: " + xmlString, module);
    }
    HttpClient http = new HttpClient(serverURL);
    http.setParameter("CLRCMRC_XML", xmlString);
    String response = null;
    try {
        response = http.post();
    } catch (HttpClientException hce) {
        Debug.logInfo(hce, module);
        throw new ClearCommerceException("ClearCommerce connection problem", hce);
    }
    Document responseDocument = null;
    try {
        responseDocument = UtilXml.readXmlDocument(response, false);
    } catch (Exception e) {
        throw new ClearCommerceException("Error reading response Document from a String: " + e.getMessage());
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("Result severity from clearCommerce:" + getMessageListMaxSev(responseDocument), module);
    }
    if (Debug.verboseOn() && getMessageListMaxSev(responseDocument) > maxSevComp) {
        Debug.logVerbose("Returned messages:" + getMessageList(responseDocument), module);
    }
    return responseDocument;
}
Also used : HttpClientException(org.apache.ofbiz.base.util.HttpClientException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) HttpClient(org.apache.ofbiz.base.util.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException)

Example 14 with HttpClient

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

the class WidgetMacroLibraryTests method testHtmlMacroLibrary.

public void testHtmlMacroLibrary() throws Exception {
    HttpClient http = initHttpClient();
    if (Start.getInstance().getConfig().portOffset != 0) {
        Integer port = 8443 + Start.getInstance().getConfig().portOffset;
        screenUrl = screenUrl.replace("8443", port.toString());
    }
    http.setUrl(screenUrl.concat(authentificationQuery));
    String screenOutString = http.post();
    assertNotNull("Response failed from ofbiz", screenOutString);
    assertEquals("Response contentType isn't good : " + http.getResponseContentType(), "text/html;charset=UTF-8", http.getResponseContentType());
    // Test if a ftl macro error is present
    assertFalse("Html Screen contains Macro on error : see " + screenUrl + " for more detail", screenOutString.contains("FreeMarker template error:"));
}
Also used : HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 15 with HttpClient

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

the class WidgetMacroLibraryTests method testCsvMacroLibrary.

public void testCsvMacroLibrary() throws Exception {
    String screencsvUrl = screenUrl.concat("Csv");
    HttpClient http = initHttpClient();
    http.setUrl(screencsvUrl.concat(authentificationQuery));
    String screenOutString = http.post();
    assertNotNull("Response failed from ofbiz", screenOutString);
    assertEquals("Response contentType isn't good : " + http.getResponseContentType(), "text/csv;charset=UTF-8", http.getResponseContentType());
    // Test if a ftl macro error is present
    assertFalse("Csv Screen contains Macro on error : see " + screencsvUrl + " 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