Search in sources :

Example 6 with HttpClient

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

the class WidgetMacroLibraryTests method initHttpClient.

/*
     * Prepare the http client to call the demo layou screen
     */
public HttpClient initHttpClient() throws HttpClientException {
    HttpClient http = new HttpClient();
    http.followRedirects(true);
    http.setAllowUntrusted(true);
    http.setHostVerificationLevel(SSLUtil.getHostCertNoCheck());
    return http;
}
Also used : HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 7 with HttpClient

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

the class WidgetMacroLibraryTests method testXlsMacroLibrary.

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

Example 8 with HttpClient

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

the class WidgetMacroLibraryTests method testTextMacroLibrary.

public void testTextMacroLibrary() throws Exception {
    String screentextUrl = screenUrl.concat("Text");
    HttpClient http = initHttpClient();
    http.setUrl(screentextUrl.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("Text Screen contains Macro on error : see " + screentextUrl + " for more detail", screenOutString.contains("FreeMarker template error:"));
}
Also used : HttpClient(org.apache.ofbiz.base.util.HttpClient)

Example 9 with HttpClient

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

Example 10 with HttpClient

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

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