/core/resource.cfc

http://github.com/atuttle/Taffy · ColdFusion CFScript · 141 lines · 121 code · 20 blank · 0 comment · 14 complexity · efe5c710f556d51ea40cde04ab00e772 MD5 · raw file

  1. <cfcomponent hint="base class for taffy REST components">
  2. <cffunction name="forceString">
  3. <cfargument name="data" required="true" hint="the data that is being forced to serialize as a string" />
  4. <cfreturn chr(2) & arguments.data />
  5. </cffunction>
  6. <cfset variables.encode = structNew() />
  7. <cfset variables.encode.string = forceString />
  8. <!--- helper functions --->
  9. <cffunction name="representationOf" access="public" output="false" hint="returns an object capable of serializing the data in a variety of formats">
  10. <cfargument name="data" required="true" hint="any simple or complex data that should be returned for the request" />
  11. <cfreturn getRepInstance().setData(arguments.data) />
  12. </cffunction>
  13. <cffunction name="rep" access="public" output="false" hint="alias for representationOf">
  14. <cfargument name="data" required="true" />
  15. <cfreturn representationOf(arguments.data) />
  16. </cffunction>
  17. <cffunction name="noData" access="private" output="false" hint="use this function to return only headers to the consumer, no data">
  18. <cfreturn getRepInstance().noData() />
  19. </cffunction>
  20. <cffunction name="noContent" access="private" output="false" hint="use this function to return only headers to the consumer, no data">
  21. <cfreturn getRepInstance().noContent() />
  22. </cffunction>
  23. <cffunction name="streamFile" access="private" output="false" hint="Use this function to specify a file name (eg c:\tmp\kitten.jpg) to be streamed to the client. When you use this method it is *required* that you also use .withMime() to specify the mime type.">
  24. <cfargument name="fileName" required="true" hint="fully qualified file path (eg c:\tmp\kitten.jpg)" />
  25. <cfreturn getRepInstance().setFileName(arguments.fileName) />
  26. </cffunction>
  27. <cffunction name="streamBinary" access="private" output="false" hint="Use this function to stream binary data, like a generated PDF object, to the client. When you use this method it is *required* that you also use .withMime() to specify the mime type.">
  28. <cfargument name="binaryData" required="true" hint="binary file data (eg a PDF object) that you want to return to the client" />
  29. <cfreturn getRepInstance().setFileData(arguments.binaryData) />
  30. </cffunction>
  31. <cffunction name="streamImage" access="private" output="false" hint="Use this function to stream binary data, like a generated PDF object, to the client. When you use this method it is *required* that you also use .withMime() to specify the mime type.">
  32. <cfargument name="binaryData" required="true" hint="binary file data (eg a PDF object or image data) that you want to return to the client" />
  33. <cfreturn getRepInstance().setImageData(arguments.binaryData) />
  34. </cffunction>
  35. <cffunction name="saveLog">
  36. <cfargument name="exception" />
  37. <cfset logger = createObject("component", application._taffy.settings.exceptionLogAdapter).init(
  38. application._taffy.settings.exceptionLogAdapterConfig
  39. ) />
  40. <cfset logger.saveLog(exception) />
  41. </cffunction>
  42. <cffunction name="queryToArray" access="private" returntype="array" output="false">
  43. <cfargument name="q" type="query" required="yes" />
  44. <cfargument name="cb" type="any" required="no" />
  45. <cfscript>
  46. var local = {};
  47. if (structKeyExists(server, "railo") or structKeyExists(server, "lucee")) {
  48. local.Columns = listToArray(arguments.q.getColumnList(false));
  49. }
  50. else {
  51. local.Columns = arguments.q.getMetaData().getColumnLabels();
  52. }
  53. local.QueryArray = ArrayNew(1);
  54. for (local.RowIndex = 1; local.RowIndex <= arguments.q.RecordCount; local.RowIndex++){
  55. local.Row = {};
  56. local.numCols = ArrayLen( local.Columns );
  57. for (local.ColumnIndex = 1; local.ColumnIndex <= local.numCols; local.ColumnIndex++){
  58. local.ColumnName = local.Columns[ local.ColumnIndex ];
  59. if( local.ColumnName NEQ "" ) {
  60. local.Row[ local.ColumnName ] = arguments.q[ local.ColumnName ][ local.RowIndex ];
  61. }
  62. }
  63. if ( structKeyExists( arguments, "cb" ) ) {
  64. local.Row = cb( local.Row );
  65. }
  66. ArrayAppend( local.QueryArray, local.Row );
  67. }
  68. return( local.QueryArray );
  69. </cfscript>
  70. </cffunction>
  71. <cffunction name="queryToStruct" access="private" returntype="struct" output="false">
  72. <cfargument name="q" type="query" required="yes" />
  73. <cfargument name="cb" type="any" required="no" />
  74. <cfset var local = {} />
  75. <cfif q.recordcount gt 1>
  76. <cfthrow message="Unable to convert query resultset with more than one record to a simple struct, use queryToArray() instead" />
  77. </cfif>
  78. <cfscript>
  79. if (structKeyExists(server, "railo") or structKeyExists(server, "lucee")) {
  80. local.Columns = listToArray(arguments.q.getColumnList(false));
  81. }
  82. else {
  83. local.Columns = arguments.q.getMetaData().getColumnLabels();
  84. }
  85. local.QueryStruct = {};
  86. local.numCols = ArrayLen( local.Columns );
  87. for (local.ColumnIndex = 1; local.ColumnIndex <= local.numCols; local.ColumnIndex++){
  88. local.ColumnName = local.Columns[ local.ColumnIndex ];
  89. if( local.ColumnName NEQ "" ) {
  90. if ( structKeyExists( arguments, "cb" ) ) {
  91. local.QueryStruct[ local.ColumnName ] = cb( local.ColumnName, arguments.q[ local.ColumnName ][1] );
  92. } else {
  93. local.QueryStruct[ local.ColumnName ] = arguments.q[ local.ColumnName ][1];
  94. }
  95. }
  96. }
  97. return( local.QueryStruct );
  98. </cfscript>
  99. </cffunction>
  100. <!---
  101. function that gets the representation class instance
  102. -- if the argument is blank, we use the default from taffy settings
  103. -- if the argument is a beanName, the bean is returned from the factory;
  104. -- otherwise it is assumed to be a cfc path and that cfc instance is returned
  105. --->
  106. <cffunction name="getRepInstance" access="private" output="false">
  107. <cfargument name="repClass" type="string" default="" />
  108. <cfif repClass eq "">
  109. <!--- recursion not the most efficient path here, but it's damn readable --->
  110. <cfreturn getRepInstance(application._taffy.settings.serializer) />
  111. <cfelseif application._taffy.factory.containsBean(arguments.repClass)>
  112. <cfreturn application._taffy.factory.getBean(arguments.repClass) />
  113. <cfelse>
  114. <cfreturn createObject("component", arguments.repClass) />
  115. </cfif>
  116. </cffunction>
  117. <cffunction name="addDebugData" access="package" output="false">
  118. <cfargument name="data" type="any" />
  119. <cfset request.debugData = arguments.data />
  120. </cffunction>
  121. </cfcomponent>