use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class EmailServices method sendFailureNotification.
public static void sendFailureNotification(DispatchContext dctx, Map<String, ? extends Object> context, MimeMessage message, List<SMTPAddressFailedException> failures) {
Locale locale = (Locale) context.get("locale");
Map<String, Object> newContext = new LinkedHashMap<>();
newContext.put("userLogin", context.get("userLogin"));
newContext.put("sendFailureNotification", false);
newContext.put("sendFrom", context.get("sendFrom"));
newContext.put("sendTo", context.get("sendFrom"));
newContext.put("subject", UtilProperties.getMessage(resource, "CommonEmailSendUndeliveredMail", locale));
StringBuilder sb = new StringBuilder();
sb.append(UtilProperties.getMessage(resource, "CommonEmailDeliveryFailed", locale));
sb.append("/n/n");
for (SMTPAddressFailedException failure : failures) {
sb.append(failure.getAddress());
sb.append(": ");
sb.append(failure.getMessage());
sb.append("/n/n");
}
sb.append(UtilProperties.getMessage(resource, "CommonEmailDeliveryOriginalMessage", locale));
sb.append("/n/n");
List<Map<String, Object>> bodyParts = new LinkedList<>();
bodyParts.add(UtilMisc.<String, Object>toMap("content", sb.toString(), "type", "text/plain"));
try {
bodyParts.add(UtilMisc.<String, Object>toMap("content", message.getDataHandler()));
} catch (MessagingException e) {
Debug.logError(e, module);
}
newContext.put("bodyParts", bodyParts);
try {
dctx.getDispatcher().runSync("sendMailMultiPart", newContext);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class QRCodeEvents method serveQRCodeImage.
/**
* Streams QR Code to the output.
*/
public static String serveQRCodeImage(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Map<String, Object> parameters = UtilHttp.getParameterMap(request);
String message = (String) parameters.get("message");
GenericValue userLogin = (GenericValue) request.getAttribute("userLogin");
if (userLogin == null) {
userLogin = (GenericValue) session.getAttribute("userLogin");
}
if (userLogin == null) {
userLogin = (GenericValue) session.getAttribute("autoUserLogin");
}
Locale locale = UtilHttp.getLocale(request);
if (UtilValidate.isEmpty(message)) {
message = "Error get message parameter.";
}
String format = (String) parameters.get("format");
if (UtilValidate.isEmpty(format)) {
format = "jpg";
}
String mimeType = "image/" + format;
String width = (String) parameters.get("width");
String height = (String) parameters.get("height");
String encoding = (String) parameters.get("encoding");
Boolean verifyOutput = Boolean.valueOf((String) parameters.get("verifyOutput"));
String logoImageMaxWidth = (String) parameters.get("logoImageMaxWidth");
String logoImageMaxHeight = (String) parameters.get("logoImageMaxHeight");
try {
response.setContentType(mimeType);
OutputStream os = response.getOutputStream();
Map<String, Object> context = UtilMisc.<String, Object>toMap("message", message, "format", format, "userLogin", userLogin, "locale", locale);
if (UtilValidate.isNotEmpty(width)) {
try {
context.put("width", Integer.parseInt(width));
} catch (NumberFormatException e) {
Debug.logWarning(e, e.getMessage(), module);
}
if (UtilValidate.isEmpty(height)) {
try {
context.put("height", Integer.parseInt(width));
} catch (NumberFormatException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
}
if (UtilValidate.isNotEmpty(height)) {
try {
context.put("height", Integer.parseInt(height));
} catch (NumberFormatException e) {
Debug.logWarning(e, e.getMessage(), module);
}
if (UtilValidate.isEmpty(width)) {
try {
context.put("width", Integer.parseInt(height));
} catch (NumberFormatException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
}
if (UtilValidate.isNotEmpty(encoding)) {
context.put("encoding", encoding);
}
if (UtilValidate.isNotEmpty(verifyOutput) && verifyOutput.booleanValue()) {
context.put("verifyOutput", verifyOutput);
}
if (UtilValidate.isNotEmpty(logoImageMaxWidth)) {
try {
context.put("logoImageMaxWidth", Integer.parseInt(logoImageMaxWidth));
} catch (NumberFormatException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
if (UtilValidate.isNotEmpty(logoImageMaxHeight)) {
try {
context.put("logoImageMaxHeight", Integer.parseInt(logoImageMaxHeight));
} catch (NumberFormatException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
Map<String, Object> results = dispatcher.runSync("generateQRCodeImage", context);
if (ServiceUtil.isSuccess(results)) {
BufferedImage bufferedImage = (BufferedImage) results.get("bufferedImage");
if (!ImageIO.write(bufferedImage, format, os)) {
String errMsg = UtilProperties.getMessage("QRCodeUiLabels", "ErrorWriteFormatToFile", new Object[] { format }, locale);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
os.flush();
} else {
String errMsg = ServiceUtil.getErrorMessage(results);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
} catch (IOException | GenericServiceException e) {
String errMsg = UtilProperties.getMessage("QRCodeUiLabels", "ErrorGenerateQRCode", new Object[] { e.getMessage() }, locale);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
return "success";
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class ScriptHelperImpl method runService.
public Map<String, ? extends Object> runService(String serviceName, Map<String, ? extends Object> inputMap, Map<String, ? extends Object> args) throws ScriptException {
Assert.notNull("serviceName", serviceName, "args", args);
boolean includeUserLogin = !"false".equals(args.get("includeUserLoginStr"));
String requireNewTransactionStr = (String) args.get("requireNewTransaction");
int transactionTimeout = -1;
if (UtilValidate.isNotEmpty(requireNewTransactionStr)) {
String timeoutStr = (String) args.get("transactionTimout");
if (UtilValidate.isNotEmpty(timeoutStr)) {
try {
transactionTimeout = Integer.parseInt(timeoutStr);
} catch (NumberFormatException e) {
Debug.logWarning(e, "Setting timeout to 0 (default)", module);
transactionTimeout = 0;
}
}
}
Map<String, Object> inMap = new HashMap<String, Object>(inputMap);
if (includeUserLogin && !inMap.containsKey("userLogin")) {
GenericValue userLogin = ctxHelper.getUserLogin();
if (userLogin != null) {
inMap.put("userLogin", userLogin);
}
}
if (!inMap.containsKey("locale") && ctxHelper.getLocale() != null) {
inMap.put("locale", ctxHelper.getLocale());
}
if (!inMap.containsKey("timeZone") && ctxHelper.getTimeZone() != null) {
inMap.put("timeZone", ctxHelper.getTimeZone());
}
Map<String, Object> result = null;
try {
if (UtilValidate.isEmpty(requireNewTransactionStr) && transactionTimeout < 0) {
result = ctxHelper.getDispatcher().runSync(serviceName, inMap);
} else {
ModelService modelService = ctxHelper.getDispatcher().getDispatchContext().getModelService(serviceName);
boolean requireNewTransaction = modelService.requireNewTransaction;
int timeout = modelService.transactionTimeout;
if (UtilValidate.isNotEmpty(requireNewTransactionStr)) {
requireNewTransaction = "true".equals(requireNewTransactionStr);
}
if (transactionTimeout >= 0) {
timeout = transactionTimeout;
}
result = ctxHelper.getDispatcher().runSync(serviceName, inMap, timeout, requireNewTransaction);
}
} catch (GenericServiceException e) {
String errMsg = "Error running script " + ctxHelper.getScriptName() + " [problem invoking the [" + serviceName + "] service: " + e.getMessage();
Debug.logWarning(e, errMsg, module);
throw new ScriptException(errMsg);
}
return result;
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class EntitySyncServices method loadOfflineSyncData.
public static Map<String, Object> loadOfflineSyncData(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String fileName = (String) context.get("xmlFileName");
Locale locale = (Locale) context.get("locale");
URL xmlFile = UtilURL.fromResource(fileName);
if (xmlFile != null) {
Document xmlSyncDoc = null;
try {
xmlSyncDoc = UtilXml.readXmlDocument(xmlFile, false);
} catch (SAXException e) {
Debug.logError(e, module);
} catch (ParserConfigurationException e) {
Debug.logError(e, module);
} catch (IOException e) {
Debug.logError(e, module);
}
if (xmlSyncDoc == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtEntitySyncXMLDocumentIsNotValid", UtilMisc.toMap("fileName", fileName), locale));
}
List<? extends Element> syncElements = UtilXml.childElementList(xmlSyncDoc.getDocumentElement());
if (syncElements != null) {
for (Element entitySync : syncElements) {
String entitySyncId = entitySync.getAttribute("entitySyncId");
String startTime = entitySync.getAttribute("lastSuccessfulSynchTime");
String createString = UtilXml.childElementValue(entitySync, "values-to-create");
String storeString = UtilXml.childElementValue(entitySync, "values-to-store");
String removeString = UtilXml.childElementValue(entitySync, "keys-to-remove");
// de-serialize the value lists
try {
List<GenericValue> valuesToCreate = checkList(XmlSerializer.deserialize(createString, delegator), GenericValue.class);
List<GenericValue> valuesToStore = checkList(XmlSerializer.deserialize(storeString, delegator), GenericValue.class);
List<GenericEntity> keysToRemove = checkList(XmlSerializer.deserialize(removeString, delegator), GenericEntity.class);
Map<String, Object> storeContext = UtilMisc.toMap("entitySyncId", entitySyncId, "valuesToCreate", valuesToCreate, "valuesToStore", valuesToStore, "keysToRemove", keysToRemove, "userLogin", userLogin);
// store the value(s)
Map<String, Object> storeResult = dispatcher.runSync("storeEntitySyncData", storeContext);
if (ServiceUtil.isError(storeResult)) {
throw new Exception(ServiceUtil.getErrorMessage(storeResult));
}
// TODO create a response document to send back to the initial sync machine
} catch (GenericServiceException gse) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLoadXMLDocument", UtilMisc.toMap("entitySyncId", entitySyncId, "startTime", startTime, "errorString", gse.getMessage()), locale));
} catch (Exception e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLoadXMLDocument", UtilMisc.toMap("entitySyncId", entitySyncId, "startTime", startTime, "errorString", e.getMessage()), locale));
}
}
}
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtOfflineXMLFileNotFound", UtilMisc.toMap("fileName", fileName), locale));
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class EntityDataServices method importDelimitedFromDirectory.
public static Map<String, Object> importDelimitedFromDirectory(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Security security = dctx.getSecurity();
Locale locale = (Locale) context.get("locale");
// check permission
GenericValue userLogin = (GenericValue) context.get("userLogin");
if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale));
}
// get the directory & delimiter
String rootDirectory = (String) context.get("rootDirectory");
URL rootDirectoryUrl = UtilURL.fromResource(rootDirectory);
if (rootDirectoryUrl == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLocateRootDirectory", UtilMisc.toMap("rootDirectory", rootDirectory), locale));
}
String delimiter = (String) context.get("delimiter");
if (delimiter == null) {
// default delimiter is tab
delimiter = "\t";
}
File root = null;
try {
root = new File(new URI(rootDirectoryUrl.toExternalForm()));
} catch (URISyntaxException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLocateRootDirectoryURI", locale));
}
if (!root.exists() || !root.isDirectory() || !root.canRead()) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtRootDirectoryDoesNotExists", locale));
}
// get the file list
List<File> files = getFileList(root);
if (UtilValidate.isNotEmpty(files)) {
for (File file : files) {
try {
Map<String, Object> serviceCtx = UtilMisc.toMap("file", file, "delimiter", delimiter, "userLogin", userLogin);
dispatcher.runSyncIgnore("importDelimitedEntityFile", serviceCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
}
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtNoFileAvailableInTheRootDirectory", UtilMisc.toMap("rootDirectory", rootDirectory), locale));
}
return ServiceUtil.returnSuccess();
}
Aggregations