Advertisement
Guest User

Untitled

a guest
May 27th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import zero.core.groovysupport.ZeroObject
  2. import com.ibm.cics.server.*
  3. import zero.util.http.*
  4.  
  5. /**
  6. * Sample RESTful resource handler for CICS TSQs.
  7. * This handler implements a subset of the LCRUD operations:
  8. * <ul>
  9. * <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>
  10. * <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>
  11. * <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>
  12. * </ul>
  13. */
  14. class tsq implements ZeroObject {
  15.  
  16. /** The encoding in which to store strings in the TSQs. */
  17. final String TSQ_CHARSET = 'IBM1047'
  18.  
  19. /**
  20. * The contructor is executed on every event.
  21. * We use it to dynamically enhance the TSQ API with some convenience methods.
  22. */
  23. def tsq() {
  24. // Add a static factory method
  25. TSQ.metaClass.static.create = { String name ->
  26. def tsq = new TSQ()
  27. tsq.setName(name)
  28. return tsq
  29. }
  30.  
  31. // Add an iteration method
  32. TSQ.metaClass.each = { Closure closure ->
  33. def position = 0
  34. while (true) {
  35. def itemHolder = new ItemHolder()
  36. try {
  37. delegate.readItem(++position, itemHolder)
  38. } catch (ItemErrorException e) {
  39. break; // Reached end of queue
  40. }
  41. closure(itemHolder.value)
  42. }
  43. }
  44. }
  45.  
  46. /**
  47. * Read the full content of a TSQ. Each item in the queue is assumed
  48. * to be a string encoded in TSQ_CHARSET.
  49. */
  50. def onRetrieve() {
  51. def tsq = TSQ.create(request.params.tsqId[])
  52. def items = []
  53. try {
  54. tsq.each { item -> items += new String(item, TSQ_CHARSET) }
  55. } catch (InvalidQueueIdException e) {
  56. reportNotFound()
  57. return
  58. }
  59. println items
  60. }
  61.  
  62. /**
  63. * Write to a TSQ. The TSQ is created if it does not exist.
  64. */
  65. def onUpdate() {
  66. // Verify the request's Content-Type
  67. def contentType = request.headers.in.'Content-Type'[]
  68. if (!contentType || !contentType.startsWith('text')) {
  69. request.status = HttpURLConnection.HTTP_NOT_ACCEPTABLE
  70. println "Request Content-Type must be text/*"
  71. return
  72. }
  73.  
  74. // Get text sent in the request
  75. def inputText = getRequestText()
  76. if (!inputText) {
  77. request.status = HttpURLConnection.HTTP_BAD_REQUEST
  78. println "Request must contain data"
  79. return
  80. }
  81.  
  82. // Write data to the TSQ, transcoded to TSQ_CHARSET.
  83. def tsq = TSQ.create(request.params.tsqId[])
  84. tsq.writeItem(inputText.getBytes(TSQ_CHARSET))
  85.  
  86. println "Wrote [${inputText}] to tsq ${request.params.tsqId[]}."
  87. }
  88.  
  89. /**
  90. * Delete a TSQ.
  91. */
  92. def onDelete() {
  93. def tsq = TSQ.create(request.params.tsqId[])
  94. try {
  95. tsq.delete()
  96. } catch (InvalidQueueIdException e) {
  97. reportNotFound()
  98. return
  99. }
  100. println "TSQ ${request.params.tsqId[]} deleted."
  101. }
  102.  
  103.  
  104. /**
  105. * Convenience method to decode request data to a String, using
  106. * the encoding specified in the request's Content-Type header,
  107. * and defaulting to ISO8859-1 (HTTP default charset) if none is specified.
  108. */
  109. def getRequestText() {
  110. def contentType = request.headers.in.'Content-Type'[]
  111. def requestCharset = HttpHeaderUtil.getCharacterEncoding(contentType)
  112. return request.input[].getText(requestCharset ?: 'ISO8859-1')
  113. }
  114.  
  115. /**
  116. * Convenience method to report error HTTP 404: resource not found.
  117. */
  118. def reportNotFound() {
  119. request.status = HttpURLConnection.HTTP_NOT_FOUND
  120. println "No such queue: ${request.params.tsqId[]}."
  121. }
  122.  
  123. /**
  124. * Intercept invocation of unimplemented event handlers
  125. * and return HTTP 405 BAD METHOD.
  126. */
  127. def methodMissing(String name, args) {
  128. request.status = HttpURLConnection.HTTP_BAD_METHOD
  129. println "Method ${request.method[]} not supported for this URI. "
  130. // ...list the methods that are supported...
  131. }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement