Skip to main content

Version: 23.10

Submission Preprocessor

The Submission Preprocessor is called when the form XML data has been saved or submitted (POST) to the server. This service is invoked before the form submission transaction has started, and provides an interceptor point where you can validate the form XML before it is saved.

This service is configured via the Form Version Services tab.

note

This service is not available for TransactField App submissions.

Service Invoke Parameters

Parameters are not nullable except where otherwise indicated.

ParameterDescription
svcDefSvcDef
A service definition value object.
txnTxn
Nullable. A transaction record value object.
formXmlDocument
An XML Document object that is used to pre-fill the form.
requestHttpServletRequest
A HTTP servlet request.
userUser
Nullable. An authenticated user.

Script Result

The script returns a submissionDataBean with the submitted XML data.

Error Handling

If your Groovy script throws an error when executing, the exception is logged to the Journey Manager database error log, and the user is redirected to a form error page that contains an Error Reference Number. This reference number is a Journey Manager database error log ID that you can use to look up the error. The Error Log record contains useful contextual information about the error.

If you want to send the user away to an arbitrary page without logging an error, throw a RedirectException to the target page.

Templates

Service

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.util.*
import com.avoka.tm.vo.*
import javax.servlet.http.*

class FluentSubmissionPreprocessor {

/*
* Perform a form save/submission preprocessor
*
* return: null if there are no changes to form XML, or a modified form XML string
* throws: RedirectException to reject submission and redirect user to another page
*/
String invoke(SvcDef svcDef, String formXml, Txn txn, HttpServletRequest request, User user) throws RedirectException {

if (!Security.isXmlTextSafe(formXml)) {
throw new RedirectException('../not-authorized.htm')
}

// If XML valid return null to continue
return null
}
}

Unit Test Template

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.query.*
import com.avoka.tm.svc.*
import com.avoka.tm.test.*
import com.avoka.tm.util.*
import com.avoka.tm.vo.*
import org.junit.Test

class UnitTest extends AbstractJUnitTest {

/*
* Perform service unit test
*
* throws exception if unit test fails
*/
@Test
void test() throws Exception {

String formXml = testParams['Test XML Data']
MockRequest request = new MockRequest()

Map params = [
"svcDef": svcDef,
"formXml": formXml,
"txn": null,
"request": request,
"user": null
]

try {
def result = new ServiceInvoker(svcDef).invoke(params)

assert result == null

} catch (RedirectException re) {
assert false
}

// Test XSS attack data
params.formXml = "<smartform>alert('hi there')</smartform>"

try {
def result = new ServiceInvoker(svcDef).invoke(params)

assert false

} catch (RedirectException re) {
logger.info re
assert re.target == '../not-authorized.htm'
}
}
}