Child pages
  • Simple-methods examples using Groovy

Access to add and change pages is restricted. See: https://cwiki.apache.org/confluence/display/OFBIZ/Wiki+access

Skip to end of metadata
Go to start of metadata

this code sample were taken from
specialpurpose/googlecheckout//script/org/ofbiz/googleCheckout/PrepareXMLTemplate.xml
it is a good example of how to extend simple-methods using java scripting.
though dated this is a source also http://ofbiz.apache.org/docs/minilang.html#MethodExample

Handy Hint

You may get errors because of xsd differences, See
Simple Method Cleanup Effort
refer to
Using locale XSD files

the Scripting using Java is accomplished using Beanshell (bsh) or Groovy (script)
both can use standard java, but each can use the unique scripting for that script.

Work in progress


feel free to add you examples.

    <simple-method method-name="transmitRequest" short-description="">
        <!-- Connect, there may be ofbiz HttpClient.java that may handle this well -->
        <call-simple-method method-name="connectWithRedirectHack"/>
        <log level="info" message="step 1 done"/>
        <!-- Get the Content -->
        <call-simple-method method-name="createXMLOutput"/>
        <log level="info" message="step 2 done"/>
        <!-- Setup the Content -->
        <call-simple-method method-name="setupContent"/>
        <log level="info" message="step 3 done ${content}"/>
        <!-- Get Response -->
        <call-simple-method method-name="getResponse"/>
        <log level="info" message="step 4 done"/>
        <!-- Get Redirect from xml document response -->
        <log level="info" message="${response}"/>
        <call-simple-method method-name="getReDirectFromXmlDoc"/>
        <log level="info" message="step 5 done"/>
        <field-to-result field-name="redirect"/>
    </simple-method>
<!-- Step 1 -->
    <simple-method method-name="connectWithRedirectHack" short-description="">
        <call-simple-method method-name="createRequestUrl"/>
        <call-simple-method method-name="getHttpAuth"/>
        <call-bsh><![CDATA[
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setDoInput (true);
            conn.setDoOutput (true);
            conn.setUseCaches (false);
            conn.setFollowRedirects(true);
            conn.setInstanceFollowRedirects(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", HttpAuth);
            conn.setRequestProperty("Host", conn.getURL().getHost());
            conn.setRequestProperty("content-type", "application/xml");
            conn.setRequestProperty("accept", "application/xml");
            conn.connect();
        return org.ofbiz.base.util.UtilMisc.toMap("conn", conn);
        ]]></call-bsh>
    </simple-method>
    <simple-method method-name="createRequestUrl" short-description="">
        <!-- Setup Url spec -->
        <property-to-field resource="googleCheckout" property="basePostUrl" field-name="basePostUrl"/>
        <property-to-field resource="googleCheckout" property="merchantId" field-name="merchantId"/>
        <property-to-field resource="googleCheckout" property="request" field-name="request"/>
        <set field="urlSpec" value="${basePostUrl}${merchantId}${request}"/>
        <create-object class-name="java.net.URL" field-name="url">
            <field field-name="urlSpec"/>
        </create-object>
    </simple-method>
    <simple-method method-name="getHttpAuth" short-description="">
        <property-to-field resource="googleCheckout" property="merchantId" field-name="merchantId"/>
        <property-to-field resource="googleCheckout" property="merchantKey" field-name="merchantKey"/>
        <set field="userNameAndPass" value="${merchantId}:${merchantKey}"/>
        <call-class-method method-name="base64Encode" class-name="org.ofbiz.base.util.Base64" ret-field-name="encodedValue">
            <field field-name="userNameAndPass"/>
        </call-class-method>
        <set field="HttpAuth" value="Basic ${encodedValue}"/>
    </simple-method>
<!-- Step 2 -->
    <simple-method method-name="createXMLOutput" short-description="">
        <!-- create writer -->
        <create-object class-name="java.io.StringWriter" field-name="XMLWriter"/>
        <!-- create screen context -->
        <call-class-method method-name="create" class-name="org.ofbiz.base.util.collections.MapStack" ret-field-name="xscreenContext"/>
        <!-- Add the shoppingCart to the MapStack -->
        <set field="inMap.shoppingCart" from-field="parameters.shoppingCart"/>
        <set-service-fields map-name="inMap" service-name="ConvertOfbizCartToGoogle" to-map-name="serMap"/>
        <call-service service-name="ConvertOfbizCartToGoogle" in-map-name="serMap">
            <result-to-field result-name="googleCart"/>
            <result-to-field result-name="flowSupport"/>
            <result-to-field result-name="sendInitialAuthDetails"/>
        </call-service>
        <set field="xscreenContext.googleCart" from-field="googleCart"/>
        <set field="xscreenContext.flowSupport" from-field="flowSupport"/>
        <set field="xscreenContext.sendInitialAuthDetails" from-field="sendInitialAuthDetails"/>
        <set field="xscreenContext.shoppingCart" from-field="parameters.shoppingCart"/>
        <!-- create string renderer -->
        <create-object class-name="org.ofbiz.widget.html.HtmlScreenRenderer" field-name="xhtmlStringRenderer"></create-object>
        <!-- create screen renderer -->
        <create-object class-name="org.ofbiz.widget.screen.ScreenRenderer" field-name="screens">
            <field field-name="XMLWriter" type="java.io.Writer"/>
            <field field-name="xscreenContext" type="org.ofbiz.base.util.collections.MapStack"/>
            <field field-name="xhtmlStringRenderer" type="org.ofbiz.widget.screen.ScreenStringRenderer"/>
        </create-object>
        <set field="xscreenContext.screens" from-field="screens"/>
        <create-object class-name="org.ofbiz.widget.screen.ScreenRenderer" field-name="xscreens">
            <field field-name="XMLWriter" type="java.io.Writer"/>
            <field field-name="xscreenContext" type="org.ofbiz.base.util.collections.MapStack"/>
            <field field-name="xhtmlStringRenderer" type="org.ofbiz.widget.screen.ScreenStringRenderer"/>
        </create-object>
        <!-- get uri -->
        <property-to-field resource="googleCheckout" property="xmlTemplateUri" field-name="screenUri"/>
        <call-object-method method-name="render" obj-field-name="xscreens">
            <field field-name="screenUri" type="String"/>
        </call-object-method>
        <set field="content" value="${bsh:XMLWriter.toString()}"/>
    </simple-method>
<!-- Step 3 -->
    <simple-method method-name="setupContent" short-description="">
        <call-bsh><![CDATA[
        sWriter = new OutputStreamWriter(conn.getOutputStream());
        return org.ofbiz.base.util.UtilMisc.toMap("sWriter", sWriter);
        ]]></call-bsh>
        <create-object class-name="java.io.BufferedWriter" field-name="writer">
            <field field-name="sWriter" type="java.io.Writer"/>
        </create-object>
        <call-object-method method-name="write" obj-field-name="writer">
            <field field-name="content" type="String"/>
        </call-object-method>
        <call-object-method method-name="close" obj-field-name="writer"/>
    </simple-method>
<!-- Step 4 -->
    <simple-method method-name="getResponse" short-description="">
        <clear-field field-name="sReader"/>
        <call-bsh><![CDATA[
        try {
          sReader = new InputStreamReader(conn.getInputStream());
        } catch (IOException ex) {
          sReader = new InputStreamReader(conn.getErrorStream());
        }
        return org.ofbiz.base.util.UtilMisc.toMap("sReader", sReader);
        ]]></call-bsh>

        <create-object class-name="java.io.BufferedReader" field-name="reader">
            <field field-name="sReader" type="java.io.Reader"/>
        </create-object>
        <call-bsh><![CDATA[
        StringBuffer buffer = new StringBuffer();
        String line = null;
        while (null != (line = reader.readLine())) {
            buffer.append(line);
        }
        return org.ofbiz.base.util.UtilMisc.toMap("buffer", buffer);
        ]]></call-bsh>
        <set field="response" value="${bsh:buffer.toString()}"/>
    </simple-method>
<!-- Step 5 -->
    <simple-method method-name="getReDirectFromXmlDoc" short-description="">
        <call-bsh><![CDATA[
            document = org.ofbiz.base.util.UtilXml.readXmlDocument(response, false);
        return org.ofbiz.base.util.UtilMisc.toMap("document", document);
        ]]></call-bsh>
        <call-object-method method-name="getDocumentElement" obj-field-name="document" ret-field-name="rootElement"/>
        <!-- May want to get the serial number to store eventually; would be an attribute of this rootElement,
                Skipping for now -->
        <call-simple-method method-name="getRedirectFromRootElement"/>
    </simple-method>
    <simple-method method-name="getRedirectFromRootElement" short-description="">
        <call-class-method method-name="childElementList" class-name="org.ofbiz.base.util.UtilXml" ret-field-name="childList">
            <field field-name="rootElement" type="org.w3c.dom.Element"/>
        </call-class-method>
        <call-bsh><![CDATA[
        redirect = null;
        childListIter = childList.iterator();
        while (childListIter.hasNext()){
            child = childListIter.next();
            org.ofbiz.base.util.Debug.logInfo("child =" + child,null);
            nodeName = child.getNodeName();
            if ("redirect-url".equals(nodeName)){
                redirect = org.ofbiz.base.util.UtilXml.elementValue(child);
            } else {
                redirect = document;
            }

        }
        return org.ofbiz.base.util.UtilMisc.toMap("redirect", redirect.toString());
        ]]></call-bsh>
    </simple-method>

    <simple-method method-name="setupHeaders" short-description="">
        <set field="true" value="true" type="Boolean"/>
        <set field="false" value="false" type="Boolean"/>

        <call-object-method method-name="setDoInput" obj-field-name="conn">
            <field field-name="true" type="boolean"/>
        </call-object-method>

        <call-object-method method-name="setDoOutput" obj-field-name="conn">
            <field field-name="true" type="boolean"/>
        </call-object-method>

        <call-object-method method-name="setUseCaches" obj-field-name="conn">
            <field field-name="false" type="boolean"/>
        </call-object-method>

        <call-object-method method-name="setFollowRedirects" obj-field-name="conn">
            <field field-name="true" type="boolean"/>
        </call-object-method>

        <call-object-method method-name="setInstanceFollowRedirects" obj-field-name="conn">
            <field field-name="true" type="boolean"/>
        </call-object-method>

        <call-object-method method-name="setRequestMethod" obj-field-name="conn">
            <string value="POST"/>
        </call-object-method>

        <call-simple-method method-name="getHttpAuth"/>
        <call-object-method method-name="setRequestProperty" obj-field-name="conn">
            <string value="Authorization"/>
            <field field-name="HttpAuth"/>
        </call-object-method>

        <call-simple-method method-name="getHttpHost"/>
        <call-object-method method-name="setRequestProperty" obj-field-name="conn">
            <string value="Host"/>
            <field field-name="HttpHost"/>
        </call-object-method>

        <call-object-method method-name="setRequestProperty" obj-field-name="conn">
            <string value="content-type"/>
            <string value="application/xml"/>
        </call-object-method>

        <call-object-method method-name="setRequestProperty" obj-field-name="conn">
            <string value="accept"/>
            <string value="application/xml"/>
        </call-object-method>

    </simple-method>
    <simple-method method-name="getHttpHost" short-description="">
        <call-bsh><![CDATA[
        HttpHost = conn.getURL().getHost();
        return org.ofbiz.base.util.UtilMisc.toMap("HttpHost", HttpHost);
        ]]></call-bsh>
    </simple-method>


  • No labels

1 Comment

  1. Note that it may be easier to use the script element when the Groovy/BSH snippet is rather long...

    The xsd says: <<Runs an external script (minilang, bsh, groovy) from the expanded location provided. Error messages go on the error list and are handled with the check-errors tag.>>