Search in sources :

Example 21 with GeneralException

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

the class DesCrypt method decrypt.

public static byte[] decrypt(Key key, byte[] bytes) throws GeneralException {
    Cipher cipher = DesCrypt.getCipher(key, Cipher.DECRYPT_MODE);
    byte[] decBytes = null;
    try {
        decBytes = cipher.doFinal(bytes);
    } catch (IllegalStateException | IllegalBlockSizeException | BadPaddingException e) {
        throw new GeneralException(e);
    }
    return decBytes;
}
Also used : GeneralException(org.apache.ofbiz.base.util.GeneralException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException)

Example 22 with GeneralException

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

the class DesCrypt method encrypt.

public static byte[] encrypt(Key key, byte[] bytes) throws GeneralException {
    Cipher cipher = DesCrypt.getCipher(key, Cipher.ENCRYPT_MODE);
    byte[] encBytes = null;
    try {
        encBytes = cipher.doFinal(bytes);
    } catch (IllegalStateException | IllegalBlockSizeException | BadPaddingException e) {
        throw new GeneralException(e);
    }
    return encBytes;
}
Also used : GeneralException(org.apache.ofbiz.base.util.GeneralException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException)

Example 23 with GeneralException

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

the class DesCrypt method getDesKey.

public static Key getDesKey(byte[] rawKey) throws GeneralException {
    SecretKeyFactory skf = null;
    try {
        skf = SecretKeyFactory.getInstance("DESede");
    } catch (NoSuchAlgorithmException e) {
        throw new GeneralException(e);
    }
    // load the raw key
    if (rawKey.length > 0) {
        DESedeKeySpec desedeSpec1 = null;
        try {
            desedeSpec1 = new DESedeKeySpec(rawKey);
        } catch (InvalidKeyException e) {
            throw new GeneralException(e);
        }
        // create the SecretKey Object
        Key key = null;
        try {
            key = skf.generateSecret(desedeSpec1);
        } catch (InvalidKeySpecException e) {
            throw new GeneralException(e);
        }
        return key;
    }
    throw new GeneralException("Not a valid DESede key!");
}
Also used : GeneralException(org.apache.ofbiz.base.util.GeneralException) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) SecretKeyFactory(javax.crypto.SecretKeyFactory) Key(java.security.Key)

Example 24 with GeneralException

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

the class EmailServices method sendMailFromScreen.

/**
 * JavaMail Service that gets body content from a Screen Widget
 * defined in the product store record and if available as attachment also.
 *@param dctx The DispatchContext that this service is operating in
 *@param rServiceContext Map containing the input parameters
 *@return Map with the result of the service, the output parameters
 */
public static Map<String, Object> sendMailFromScreen(DispatchContext dctx, Map<String, ? extends Object> rServiceContext) {
    Map<String, Object> serviceContext = UtilMisc.makeMapWritable(rServiceContext);
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String webSiteId = (String) serviceContext.remove("webSiteId");
    String bodyText = (String) serviceContext.remove("bodyText");
    String bodyScreenUri = (String) serviceContext.remove("bodyScreenUri");
    String xslfoAttachScreenLocationParam = (String) serviceContext.remove("xslfoAttachScreenLocation");
    String attachmentNameParam = (String) serviceContext.remove("attachmentName");
    List<String> xslfoAttachScreenLocationListParam = UtilGenerics.checkList(serviceContext.remove("xslfoAttachScreenLocationList"));
    List<String> attachmentNameListParam = UtilGenerics.checkList(serviceContext.remove("attachmentNameList"));
    VisualTheme visualTheme = (VisualTheme) rServiceContext.get("visualTheme");
    if (visualTheme == null) {
        visualTheme = ThemeFactory.resolveVisualTheme(null);
    }
    List<String> xslfoAttachScreenLocationList = new LinkedList<>();
    List<String> attachmentNameList = new LinkedList<>();
    if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationParam)) {
        xslfoAttachScreenLocationList.add(xslfoAttachScreenLocationParam);
    }
    if (UtilValidate.isNotEmpty(attachmentNameParam)) {
        attachmentNameList.add(attachmentNameParam);
    }
    if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationListParam)) {
        xslfoAttachScreenLocationList.addAll(xslfoAttachScreenLocationListParam);
    }
    if (UtilValidate.isNotEmpty(attachmentNameListParam)) {
        attachmentNameList.addAll(attachmentNameListParam);
    }
    List<String> attachmentTypeList = new LinkedList<>();
    String attachmentTypeParam = (String) serviceContext.remove("attachmentType");
    List<String> attachmentTypeListParam = UtilGenerics.checkList(serviceContext.remove("attachmentTypeList"));
    if (UtilValidate.isNotEmpty(attachmentTypeParam)) {
        attachmentTypeList.add(attachmentTypeParam);
    }
    if (UtilValidate.isNotEmpty(attachmentTypeListParam)) {
        attachmentTypeList.addAll(attachmentTypeListParam);
    }
    Locale locale = (Locale) serviceContext.get("locale");
    Map<String, Object> bodyParameters = UtilGenerics.checkMap(serviceContext.remove("bodyParameters"));
    if (bodyParameters == null) {
        bodyParameters = MapStack.create();
    }
    if (!bodyParameters.containsKey("locale")) {
        bodyParameters.put("locale", locale);
    } else {
        locale = (Locale) bodyParameters.get("locale");
    }
    String partyId = (String) serviceContext.get("partyId");
    if (partyId == null) {
        partyId = (String) bodyParameters.get("partyId");
    }
    String orderId = (String) bodyParameters.get("orderId");
    String returnId = (String) serviceContext.get("returnId");
    String custRequestId = (String) bodyParameters.get("custRequestId");
    bodyParameters.put("communicationEventId", serviceContext.get("communicationEventId"));
    NotificationServices.setBaseUrl(dctx.getDelegator(), webSiteId, bodyParameters);
    String contentType = (String) serviceContext.remove("contentType");
    StringWriter bodyWriter = new StringWriter();
    MapStack<String> screenContext = MapStack.create();
    screenContext.put("locale", locale);
    ScreenStringRenderer screenStringRenderer = null;
    try {
        screenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme(), "screen");
    } catch (TemplateException | IOException e) {
        Debug.logError(e, "Error rendering screen for email: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenEmailError", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    ScreenRenderer screens = new ScreenRenderer(bodyWriter, screenContext, screenStringRenderer);
    screens.populateContextForService(dctx, bodyParameters);
    screenContext.putAll(bodyParameters);
    if (bodyScreenUri != null) {
        try {
            screens.render(bodyScreenUri);
        } catch (GeneralException | IOException | SAXException | ParserConfigurationException e) {
            Debug.logError(e, "Error rendering screen for email: " + e.toString(), module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenEmailError", UtilMisc.toMap("errorString", e.toString()), locale));
        }
    }
    boolean isMultiPart = false;
    // check if attachment screen location passed in
    if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationList)) {
        List<Map<String, ? extends Object>> bodyParts = new LinkedList<Map<String, ? extends Object>>();
        if (bodyText != null) {
            bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale);
            bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyText, "type", UtilValidate.isNotEmpty(contentType) ? contentType : "text/html"));
        } else {
            bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyWriter.toString(), "type", UtilValidate.isNotEmpty(contentType) ? contentType : "text/html"));
        }
        for (int i = 0; i < xslfoAttachScreenLocationList.size(); i++) {
            String xslfoAttachScreenLocation = xslfoAttachScreenLocationList.get(i);
            String attachmentName = "Details.pdf";
            if (UtilValidate.isNotEmpty(attachmentNameList) && attachmentNameList.size() >= i) {
                attachmentName = attachmentNameList.get(i);
            }
            String attachmentType = MimeConstants.MIME_PDF;
            if (UtilValidate.isNotEmpty(attachmentTypeList) && attachmentTypeList.size() >= i) {
                attachmentType = attachmentTypeList.get(i);
            }
            isMultiPart = true;
            // start processing fo pdf attachment
            try {
                Writer writer = new StringWriter();
                // substitute the freemarker variables...
                ScreenStringRenderer foScreenStringRenderer = null;
                if (MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)) {
                    foScreenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme(), "screentext");
                } else {
                    foScreenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme(), "screenfop");
                }
                ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContext, foScreenStringRenderer);
                screensAtt.populateContextForService(dctx, bodyParameters);
                screensAtt.render(xslfoAttachScreenLocation);
                // create the output stream for the generation
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                if (MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)) {
                    baos.write(writer.toString().getBytes("UTF-8"));
                } else {
                    // create the input stream for the generation
                    StreamSource src = new StreamSource(new StringReader(writer.toString()));
                    Fop fop = ApacheFopWorker.createFopInstance(baos, attachmentType);
                    ApacheFopWorker.transform(src, null, fop);
                }
                // and generate the attachment
                baos.flush();
                baos.close();
                // store in the list of maps for sendmail....
                bodyParts.add(UtilMisc.<String, Object>toMap("content", baos.toByteArray(), "type", attachmentType, "filename", attachmentName));
            } catch (GeneralException | IOException | SAXException | ParserConfigurationException | TemplateException ge) {
                Debug.logError(ge, "Error rendering PDF attachment for email: " + ge.toString(), module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", ge.toString()), locale));
            }
            serviceContext.put("bodyParts", bodyParts);
        }
    } else {
        isMultiPart = false;
        // store body and type for single part message in the context.
        if (bodyText != null) {
            bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale);
            serviceContext.put("body", bodyText);
        } else {
            serviceContext.put("body", bodyWriter.toString());
        }
        // and would require specific handling.
        if (contentType != null && contentType.equalsIgnoreCase("text/plain")) {
            serviceContext.put("contentType", "text/plain");
        } else {
            serviceContext.put("contentType", "text/html");
        }
    }
    // also expand the subject at this point, just in case it has the FlexibleStringExpander syntax in it...
    String subject = (String) serviceContext.remove("subject");
    subject = FlexibleStringExpander.expandString(subject, screenContext, locale);
    if (Debug.infoOn()) {
        Debug.logInfo("Expanded email subject to: " + subject, module);
    }
    serviceContext.put("subject", subject);
    serviceContext.put("partyId", partyId);
    if (UtilValidate.isNotEmpty(orderId)) {
        serviceContext.put("orderId", orderId);
    }
    if (UtilValidate.isNotEmpty(returnId)) {
        serviceContext.put("returnId", returnId);
    }
    if (UtilValidate.isNotEmpty(custRequestId)) {
        serviceContext.put("custRequestId", custRequestId);
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("sendMailFromScreen sendMail context: " + serviceContext, module);
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    Map<String, Object> sendMailResult;
    Boolean hideInLog = (Boolean) serviceContext.get("hideInLog");
    try {
        if (!Boolean.TRUE.equals(hideInLog)) {
            if (isMultiPart) {
                sendMailResult = dispatcher.runSync("sendMailMultiPart", serviceContext);
            } else {
                sendMailResult = dispatcher.runSync("sendMail", serviceContext);
            }
        } else {
            if (isMultiPart) {
                sendMailResult = dispatcher.runSync("sendMailMultiPartHiddenInLog", serviceContext);
            } else {
                sendMailResult = dispatcher.runSync("sendMailHiddenInLog", serviceContext);
            }
        }
    } catch (Exception e) {
        Debug.logError(e, "Error send email:" + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendError", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    if (ServiceUtil.isError(sendMailResult)) {
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(sendMailResult));
    }
    result.put("messageWrapper", sendMailResult.get("messageWrapper"));
    result.put("body", bodyWriter.toString());
    result.put("subject", subject);
    result.put("communicationEventId", sendMailResult.get("communicationEventId"));
    if (UtilValidate.isNotEmpty(orderId)) {
        result.put("orderId", orderId);
    }
    if (UtilValidate.isNotEmpty(returnId)) {
        result.put("returnId", returnId);
    }
    if (UtilValidate.isNotEmpty(custRequestId)) {
        result.put("custRequestId", custRequestId);
    }
    return result;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneralException(org.apache.ofbiz.base.util.GeneralException) TemplateException(freemarker.template.TemplateException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedList(java.util.LinkedList) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) MessagingException(javax.mail.MessagingException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) SendFailedException(javax.mail.SendFailedException) SAXException(org.xml.sax.SAXException) GeneralException(org.apache.ofbiz.base.util.GeneralException) TemplateException(freemarker.template.TemplateException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Writer(java.io.Writer) StringWriter(java.io.StringWriter)

Example 25 with GeneralException

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

the class PreferenceServices method getUserPreference.

/**
 * Retrieves a single user preference from persistent storage. Call with
 * userPrefTypeId and optional userPrefLoginId. If userPrefLoginId isn't
 * specified, then the currently logged-in user's userLoginId will be
 * used. The retrieved preference is contained in the <b>userPrefMap</b> element.
 * @param ctx The DispatchContext that this service is operating in.
 * @param context Map containing the input arguments.
 * @return Map with the result of the service, the output parameters.
 */
public static Map<String, Object> getUserPreference(DispatchContext ctx, Map<String, ?> context) {
    Locale locale = (Locale) context.get("locale");
    if (!PreferenceWorker.isValidGetId(ctx, context)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.permissionError", locale));
    }
    Delegator delegator = ctx.getDelegator();
    String userPrefTypeId = (String) context.get("userPrefTypeId");
    if (UtilValidate.isEmpty(userPrefTypeId)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.invalidArgument", locale));
    }
    String userLoginId = PreferenceWorker.getUserLoginId(context, true);
    Map<String, String> fieldMap = UtilMisc.toMap("userLoginId", userLoginId, "userPrefTypeId", userPrefTypeId);
    String userPrefGroupTypeId = (String) context.get("userPrefGroupTypeId");
    if (UtilValidate.isNotEmpty(userPrefGroupTypeId)) {
        fieldMap.put("userPrefGroupTypeId", userPrefGroupTypeId);
    }
    Map<String, Object> userPrefMap = null;
    try {
        GenericValue preference = EntityQuery.use(delegator).from("UserPreference").where(fieldMap).cache(true).queryFirst();
        if (preference != null) {
            userPrefMap = PreferenceWorker.createUserPrefMap(preference);
        }
    } catch (GeneralException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.readFailure", new Object[] { e.getMessage() }, locale));
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("userPrefMap", userPrefMap);
    if (userPrefMap != null) {
        // Put the value in the result Map too, makes access easier for calling methods.
        Object userPrefValue = userPrefMap.get(userPrefTypeId);
        if (userPrefValue != null) {
            result.put("userPrefValue", userPrefValue);
        }
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator)

Aggregations

GeneralException (org.apache.ofbiz.base.util.GeneralException)216 GenericValue (org.apache.ofbiz.entity.GenericValue)133 Delegator (org.apache.ofbiz.entity.Delegator)101 Locale (java.util.Locale)81 HashMap (java.util.HashMap)71 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)68 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)68 IOException (java.io.IOException)65 BigDecimal (java.math.BigDecimal)55 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)54 Writer (java.io.Writer)29 LinkedList (java.util.LinkedList)29 Map (java.util.Map)29 Timestamp (java.sql.Timestamp)26 StringWriter (java.io.StringWriter)19 Environment (freemarker.core.Environment)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)14 HttpSession (javax.servlet.http.HttpSession)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13