details: https://code.openbravo.com/erp/devel/pi/rev/23baba607b96 changeset: 32764:23baba607b96 user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Thu Sep 21 12:49:13 2017 +0200 summary: fixes issue 36882: JSExpressionCallout extends SimpleCallout
diffstat: modules/org.openbravo.client.application/src/org/openbravo/client/application/FixedValueExpressionCallout.java | 6 +- modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java | 112 +++------ modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/DefaultExpressionCallout.java | 6 +- 3 files changed, 47 insertions(+), 77 deletions(-) diffs (209 lines): diff -r e670fbf7006c -r 23baba607b96 modules/org.openbravo.client.application/src/org/openbravo/client/application/FixedValueExpressionCallout.java --- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/FixedValueExpressionCallout.java Wed Sep 20 23:44:34 2017 +0530 +++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/FixedValueExpressionCallout.java Thu Sep 21 12:49:13 2017 +0200 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2010 Openbravo SLU + * All portions are Copyright (C) 2010-2017 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -21,13 +21,13 @@ import org.openbravo.base.secureApp.VariablesSecureApp; /** + * This class is used to evaluate the Javascript fixed expression assigned to a parameter + * (OBUIAPP_Parameter) if it is marked as fixed. * * @author gorkaion */ public class FixedValueExpressionCallout extends JSExpressionCallout { - private static final long serialVersionUID = 1L; - @Override protected String getExpression(VariablesSecureApp vars) { String expression = ""; diff -r e670fbf7006c -r 23baba607b96 modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java --- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java Wed Sep 20 23:44:34 2017 +0530 +++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java Thu Sep 21 12:49:13 2017 +0200 @@ -11,108 +11,78 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2010 Openbravo SLU + * All portions are Copyright (C) 2010-2017 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ */ package org.openbravo.client.application; -import java.io.IOException; -import java.io.PrintWriter; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.openbravo.base.secureApp.HttpSecureAppServlet; import org.openbravo.base.secureApp.VariablesSecureApp; +import org.openbravo.client.kernel.RequestContext; +import org.openbravo.erpCommon.ad_callouts.SimpleCallout; import org.openbravo.erpCommon.utility.Utility; -import org.openbravo.xmlEngine.XmlDocument; /** + * An abstract class that can be extended for those callouts that need to evaluate a Javascript + * expression. * * @author gorkaion */ -public abstract class JSExpressionCallout extends HttpSecureAppServlet { +public abstract class JSExpressionCallout extends SimpleCallout { + private static final String JS_ERROR_MSG_PREFIX = "sun.org.mozilla.javascript.internal.EcmaError:"; + private static final String JS_ERROR_EVAL = "in <eval>"; - private static final long serialVersionUID = 1L; - private static final String CLEAR_MSG_CODE = "['MESSAGE','']"; - private static final String JS_ERROR_MSG_PREFIX = "sun.org.mozilla.javascript.internal.EcmaError:"; + protected abstract String getExpression(VariablesSecureApp vars); @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, - ServletException { - - VariablesSecureApp vars = new VariablesSecureApp(request); - - if (vars.commandIn("DEFAULT")) { - try { - printPage(vars, request, response); - } catch (ServletException ex) { - pageErrorCallOut(response); - } - } else { - pageError(response); + protected void execute(CalloutInfo info) throws ServletException { + info.addResult("MESSAGE", ""); + String expression = getExpression(info.vars); + if (expression.equals("")) { + return; + } + try { + HttpServletRequest request = RequestContext.get().getRequest(); + Object result = ParameterUtils.getJSExpressionResult(getParameterMap(request), + request.getSession(false), expression); + info.addResult("INFO", getInfoMessage(info.vars, result)); + } catch (Exception e) { + info.addResult("WARNING", getErrorMessage(info.vars, e.getMessage())); } } - private void printPage(VariablesSecureApp vars, HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException { - - XmlDocument xmlDocument = xmlEngine.readXmlTemplate( - "org/openbravo/erpCommon/ad_callouts/CallOut").createXmlDocument(); - - xmlDocument.setParameter("array", getResponse(vars, request, getExpression(vars))); - xmlDocument.setParameter("frameName", "appFrame"); - response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - out.println(xmlDocument.print()); - out.close(); - } - - protected abstract String getExpression(VariablesSecureApp vars); - - private String getResponse(VariablesSecureApp vars, HttpServletRequest request, String expression) - throws ServletException { - - StringBuffer sb = new StringBuffer(); - sb.append("var calloutName='").append(this.getClass().getName()).append("';\n"); - sb.append("var respuesta = ["); - - if (expression.equals("")) { - sb.append(CLEAR_MSG_CODE + "];\n"); - return sb.toString(); - } - - sb.append(CLEAR_MSG_CODE); - - try { - Object result = ParameterUtils.getJSExpressionResult(getParameterMap(request), - request.getSession(false), expression); - - sb.append(", ['INFO','" - + Utility.messageBD(this, "OBUIAPP_CALLOUT_JS_EXPR_RESULT", vars.getLanguage()) + " " - + (result) + "']"); - } catch (Exception e) { - String errorMsg = e.getMessage().substring(JS_ERROR_MSG_PREFIX.length()); - sb.append(", ['WARNING','" - + Utility.messageBD(this, "OBUIAPP_CALLOUT_JS_EXPR_ERROR", vars.getLanguage()) + " " - + errorMsg + "']"); - } - sb.append("];\n"); - return sb.toString(); - } - private Map<String, String> getParameterMap(HttpServletRequest request) { - final Map<String, String> parameterMap = new HashMap<String, String>(); + final Map<String, String> parameterMap = new HashMap<>(); for (Enumeration<?> keys = request.getParameterNames(); keys.hasMoreElements();) { final String key = (String) keys.nextElement(); parameterMap.put(key, request.getParameter(key)); } return parameterMap; } + + private String getInfoMessage(VariablesSecureApp vars, Object result) { + return Utility.messageBD(this, "OBUIAPP_CALLOUT_JS_EXPR_RESULT", vars.getLanguage()) + " " + + result; + } + + private String getErrorMessage(VariablesSecureApp vars, String exceptionMessage) { + String errorMessage; + if (exceptionMessage.contains(JS_ERROR_MSG_PREFIX)) { + errorMessage = exceptionMessage.substring(JS_ERROR_MSG_PREFIX.length()); + } else if (exceptionMessage.contains(JS_ERROR_EVAL)) { + errorMessage = exceptionMessage.substring(0, exceptionMessage.indexOf(JS_ERROR_EVAL)); + } else { + errorMessage = exceptionMessage; + } + return Utility.messageBD(this, "OBUIAPP_CALLOUT_JS_EXPR_ERROR", vars.getLanguage()) + " " + + errorMessage; + } } diff -r e670fbf7006c -r 23baba607b96 modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/DefaultExpressionCallout.java --- a/modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/DefaultExpressionCallout.java Wed Sep 20 23:44:34 2017 +0530 +++ b/modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/DefaultExpressionCallout.java Thu Sep 21 12:49:13 2017 +0200 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2010-2011 Openbravo SLU + * All portions are Copyright (C) 2010-2017 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -22,13 +22,13 @@ import org.openbravo.client.application.JSExpressionCallout; /** + * This class is used to evaluate the Javascript expressions that can be used as a filter expression + * of a selector or as the default expression of a selector field. * * @author iperdomo */ public class DefaultExpressionCallout extends JSExpressionCallout { - private static final long serialVersionUID = 1L; - @Override protected String getExpression(VariablesSecureApp vars) { String inpLastFieldChanged = vars.getStringParameter("inpLastFieldChanged"); ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits