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;
}
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:"));
}
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:"));
}
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;
}
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;
}
Aggregations