Skip to content

Instantly share code, notes, and snippets.

@rewbs
Created June 6, 2010 22:11
Show Gist options
  • Save rewbs/427956 to your computer and use it in GitHub Desktop.
Save rewbs/427956 to your computer and use it in GitHub Desktop.
import zero.core.groovysupport.ZeroObject
import com.ibm.cics.server.*
import zero.util.http.*
/**
* Sample RESTful resource handler for CICS TSQs.
* This handler implements a subset of the LCRUD operations:
* <ul>
* <li>Retrieve: A GET request to /resources/tsq/queueName and returns the full content of the queue, or HTTP 404 if it doesn't exist. </li>
* <li>Update: A PUT request to /resources/tsq/queueName invokes onUpdate() and adds the request body data to the queue, creating the queue if it doesn't exisit. </li>
* <li>Delete: A DELETE request to /resources/tsq/queueName invokes onDelete() and deletes the queue, or responds with HTTP 404 if it doesn't exist. </li>
* </ul>
*/
class tsq implements ZeroObject {
/** The encoding in which to store strings in the TSQs. */
final String TSQ_CHARSET = 'IBM1047'
/**
* The contructor is executed on every event.
* We use it to dynamically enhance the TSQ API with some convenience methods.
*/
def tsq() {
// Add a static factory method
TSQ.metaClass.static.create = { String name ->
def tsq = new TSQ()
tsq.setName(name)
return tsq
}
// Add an iteration method
TSQ.metaClass.each = { Closure closure ->
def position = 0
while (true) {
def itemHolder = new ItemHolder()
try {
delegate.readItem(++position, itemHolder)
} catch (ItemErrorException e) {
break; // Reached end of queue
}
closure(itemHolder.value)
}
}
}
/**
* Read the full content of a TSQ. Each item in the queue is assumed
* to be a string encoded in TSQ_CHARSET.
*/
def onRetrieve() {
def tsq = TSQ.create(request.params.tsqId[])
def items = []
try {
tsq.each { item -> items += new String(item, TSQ_CHARSET) }
} catch (InvalidQueueIdException e) {
reportNotFound()
return
}
println items
}
/**
* Write to a TSQ. The TSQ is created if it does not exist.
*/
def onUpdate() {
// Verify the request's Content-Type
def contentType = request.headers.in.'Content-Type'[]
if (!contentType || !contentType.startsWith('text')) {
request.status = HttpURLConnection.HTTP_NOT_ACCEPTABLE
println "Request Content-Type must be text/*"
return
}
// Get text sent in the request
def inputText = getRequestText()
if (!inputText) {
request.status = HttpURLConnection.HTTP_BAD_REQUEST
println "Request must contain data"
return
}
// Write data to the TSQ, transcoded to TSQ_CHARSET.
def tsq = TSQ.create(request.params.tsqId[])
tsq.writeItem(inputText.getBytes(TSQ_CHARSET))
println "Wrote [${inputText}] to tsq ${request.params.tsqId[]}."
}
/**
* Delete a TSQ.
*/
def onDelete() {
def tsq = TSQ.create(request.params.tsqId[])
try {
tsq.delete()
} catch (InvalidQueueIdException e) {
reportNotFound()
return
}
println "TSQ ${request.params.tsqId[]} deleted."
}
/**
* Convenience method to decode request data to a String, using
* the encoding specified in the request's Content-Type header,
* and defaulting to ISO8859-1 (HTTP default charset) if none is specified.
*/
def getRequestText() {
def contentType = request.headers.in.'Content-Type'[]
def requestCharset = HttpHeaderUtil.getCharacterEncoding(contentType)
return request.input[].getText(requestCharset ?: 'ISO8859-1')
}
/**
* Convenience method to report error HTTP 404: resource not found.
*/
def reportNotFound() {
request.status = HttpURLConnection.HTTP_NOT_FOUND
println "No such queue: ${request.params.tsqId[]}."
}
/**
* Intercept invocation of unimplemented event handlers
* and return HTTP 405 BAD METHOD.
*/
def methodMissing(String name, args) {
request.status = HttpURLConnection.HTTP_BAD_METHOD
println "Method ${request.method[]} not supported for this URI. "
// ...list the methods that are supported...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment