-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcfScribe.cfc
168 lines (143 loc) · 8.09 KB
/
cfScribe.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<cfcomponent output="false">
<!--- VARIABLES --->
<cfset variables.instance = {}>
<!--- SERVICE INIT --->
<cffunction name="twitterInit" returntype="Any">
<cfargument name="force_login" type="boolean" required="true" default="false">
<cfargument name="callback" type="string" required="false" default="" hint="used to customize the default callback URL">
<cfset var local = {}>
<cfset local.config = createObject("component", "config").twitterInit()>
<cfset local.paths = []>
<cfset local.paths[1] = local.config.getScribePath()>
<cfset local.paths[2] = local.config.getCommonsCodecPath()>
<cfset local.paths[3] = local.config.getHttpCorePath()>
<cfset local.paths[4] = local.config.getHttpMimePath()>
<cfset variables.instance.javaloader = createObject("component", "components.javaloader.JavaLoader").init(local.paths)>
<cfif arguments.force_login>
<cfset variables.instance.TwitterApi = variables.instance.javaloader.create("org.scribe.builder.api.TwitterApi$AuthorizeForce")>
<cfelse>
<cfset variables.instance.TwitterApi = variables.instance.javaloader.create("org.scribe.builder.api.TwitterApi")>
</cfif>
<cfif arguments.callback neq "">
<cfset local.config.setCallback(arguments.callback)>
</cfif>
<cfset variables.instance.token = variables.instance.javaloader.create("org.scribe.model.Token")>
<cfset variables.instance.verb = variables.instance.javaloader.create("org.scribe.model.Verb")>
<cfset variables.instance.scribeService = variables.instance.javaloader.create("org.scribe.builder.ServiceBuilder").init()
.provider(variables.instance.TwitterApi.getClass())
.apiKey(local.config.getConsumerKey())
.apiSecret(local.config.getConsumerSecret())
.callback(local.config.getCallback())
.build()>
<cfreturn this />
</cffunction>
<!--- GETTER FUNCTIONS--->
<cffunction name="getRequestToken" output="false" access="public" returntype="string">
<cfset getRequestToken = variables.instance.scribeService.getRequestToken()>
<cfreturn getRequestToken>
</cffunction>
<cffunction name="getAuthorizationUrl" output="false" access="public" returntype="any">
<cfargument name="requestToken" required="true">
<cfreturn variables.instance.scribeService.getAuthorizationUrl(arguments.requestToken) />
</cffunction>
<cffunction name="getVerifier" output="false" access="public" returntype="string">
<cfargument name="oAuthVerifier" required="true">
<cfreturn variables.instance.javaloader.create("org.scribe.model.Verifier").init(arguments.oAuthVerifier) />
</cffunction>
<cffunction name="getAccessToken" output="false" access="public" returntype="string">
<cfargument name="requestToken" required="true">
<cfargument name="verifier" required="true">
<cfreturn variables.instance.scribeService.getAccessToken(arguments.requestToken,arguments.verifier) />
</cffunction>
<!--- SETTER FUNCTIONS--->
<cffunction name="setRequestToken" output="false" access="public" returntype="any">
<cfargument name="token" required="true" type="string">
<cfargument name="tokenSecret" required="true" type="string">
<cfreturn variables.instance.token.init(arguments.token,arguments.tokenSecret)>
</cffunction>
<cffunction name="setRequest" output="false" access="public" returntype="string">
<cfargument name="URI" required="true">
<cfargument name="method" required="true">
<cfswitch expression="#arguments.method#">
<!---
dunno if we need all of these, but they are valid options in scribe:
GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE
--->
<cfcase value="post">
<cfreturn variables.instance.javaloader.create("org.scribe.model.OAuthRequest").init(variables.instance.verb.post,arguments.URI) />
</cfcase>
<cfdefaultcase>
<cfreturn variables.instance.javaloader.create("org.scribe.model.OAuthRequest").init(variables.instance.verb.get,arguments.URI) />
</cfdefaultcase>
</cfswitch>
</cffunction>
<cffunction name="setSignRequest" output="false" access="public" returntype="string">
<cfargument name="accessToken" required="true">
<cfargument name="request" required="true">
<cfreturn variables.instance.scribeService.signRequest(arguments.accessToken,arguments.request) />
</cffunction>
<cffunction name="setRequestParams" output="false" access="public" returntype="string" hint="I take a request object and add a set of params to it">
<cfargument name="method" required="true" type="string">
<cfargument name="stctParams" required="true" type="struct">
<cfargument name="objRequest" required="true" type="string">
<cfswitch expression="#arguments.method#">
<cfcase value="post">
<cfloop collection="#arguments.stctParams#" item="theVar">
<!--- only add a param if it contains a value --->
<cfif structFind(arguments.stctParams, theVar) neq "">
<cfset objRequest.addBodyParameter(theVar, structFind(arguments.stctParams, theVar))>
</cfif>
</cfloop>
</cfcase>
<cfdefaultcase>
<cfloop collection="#arguments.stctParams#" item="theVar">
<!--- only add a param if it contains a value --->
<cfif structFind(arguments.stctParams, theVar) neq "">
<cfset objRequest.addQuerystringParameter(theVar, structFind(arguments.stctParams, theVar))>
</cfif>
</cfloop>
</cfdefaultcase>
</cfswitch>
<cfreturn objRequest>
</cffunction>
<cffunction name="setRequestParamsWithMedia" output="true" access="public" returntype="string" hint="I take a media object, convert it to base64, and add it to the request payload">
<cfargument name="objRequest" required="true" type="string">
<cfargument name="stctParams" required="true" type="struct">
<cfargument name="media" required="true" type="string" hint="local system path to media file">
<cfargument name="mediaLabel" required="true" default="image" type="string" hint="The label the API needs to identify the image value">
<!--- scribe doesn't create a multipart request for us, so we have to create it ourselves --->
<!--- start by init'ing the classes we need to build a multipart request --->
<cfset variables.instance.ctEntity = variables.instance.javaloader.create("org.apache.http.entity.ContentType")>
<cfset variables.instance.mpBuilder = variables.instance.javaloader.create("org.apache.http.entity.mime.MultipartEntityBuilder")>
<!--- TODO checkmedia --->
<!--- read the file into binary --->
<cflock type="readonly" name="MimeLock#Hash(arguments.media)#" timeout="10" throwontimeout="no">
<cffile action="readbinary" file="#arguments.media#" variable="theBinaryFile" />
</cflock>
<!--- create a new http request entity that we can add our image and params to --->
<cfset var reqEntity = variables.instance.mpBuilder.create()>
<!--- add the text (form field) params --->
<cfloop collection="#arguments.stctParams#" item="theVar">
<!--- only add a param if it contains a value --->
<cfif structFind(arguments.stctParams, theVar) neq "">
<cfset reqEntity.addTextBody(theVar, structFind(arguments.stctParams, theVar), variables.instance.ctEntity.TEXT_PLAIN)>
</cfif>
</cfloop>
<!--- add the image as multipart binary data --->
<cfset reqEntity.addBinaryBody(arguments.mediaLabel,
theBinaryFile,
variables.instance.ctEntity.MULTIPART_FORM_DATA,
GetFileFromPath(arguments.media)
)>
<!--- add the image to a binary output stream that we'll need to extract values from to properly form our request --->
<cfset var bos = createObject("java", "java.io.ByteArrayOutputStream").init()>
<cfset var msg = reqEntity.build()>
<cfset msg.writeTo(bos)>
<cfset var contentType = msg.getContentType()>
<cfset objRequest.addHeader("Content-length", msg.getContentLength())>
<cfset objRequest.addHeader(contentType.getName(), contentType.getValue())>
<!--- add our newly formed http request (with images and fields) to our OAuth request object --->
<cfset objRequest.addPayload(bos.toByteArray())>
<cfreturn objRequest>
</cffunction>
</cfcomponent>