-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathRequestService.cfc
More file actions
347 lines (296 loc) · 10.2 KB
/
RequestService.cfc
File metadata and controls
347 lines (296 loc) · 10.2 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
* www.ortussolutions.com
* ---
* This service takes care of preparing and creating request contexts. Facades to FORM and URL
*/
component extends="coldbox.system.web.services.BaseService" {
/**
* Constructor
*
* @controller The ColdBox Controller.
*/
function init( required controller ){
setController( arguments.controller );
variables.flashScope = "";
variables.flashData = "";
variables.flashDataHash = "";
return this;
}
/**
* Once configuration loads this method is fired by the service loader.
*/
function onConfigurationLoad(){
// Local Configuration data and dependencies
variables.log = controller.getLogBox().getLogger( this );
variables.eventName = controller.getSetting( "eventName" );
variables.eventCaching = controller.getSetting( "eventCaching" );
variables.interceptorService = controller.getInterceptorService();
variables.routingService = controller.getRoutingService();
variables.handlerService = controller.getHandlerService();
variables.cacheBox = controller.getCacheBox();
variables.cache = controller.getCache();
variables.templateCache = controller.getCache( "template" );
variables.flashData = controller.getSetting( "flash" );
variables.flashDataHash = hash( variables.flashData.toString() );
// build out Flash RAM
buildFlashScope();
}
/**
* I capture an incoming request. Returns: coldbox.system.web.context.RequestContext
*
* @event Override to instead use this as the main event instead of looking for it in form/url
* @proxyCall If true, we ignore routing, since this is a proxy remote call
*
* @return coldbox.system.web.context.RequestContext
*/
any function requestCapture( event, boolean proxyCall = false ){
var context = getContext();
var rc = context.getCollection();
var prc = context.getCollection( private = true );
// Capture FORM/URL or direct overrride
if ( isDefined( "FORM" ) ) {
structAppend( rc, FORM );
}
if ( isDefined( "URL" ) ) {
structAppend( rc, URL );
}
// If the inbound content body is a JSON payload capture it
if (
controller.getSetting( "jsonPayloadToRC" ) &&
len( context.getHTTPContent() ) &&
isJSON( context.getHTTPContent() )
) {
var payload = context.getHTTPContent( json = true );
if ( isStruct( payload ) ) {
structAppend( rc, payload );
}
}
// Configure decorator if available?
if ( structKeyExists( context, "configure" ) ) {
context.configure();
}
// First, process the request through the RoutingService
if ( !arguments.proxyCall ) {
variables.routingService.requestCapture( context );
}
// Do we have an override
if ( !isNull( arguments.event ) && len( arguments.event ) ) {
rc[ variables.eventName ] = arguments.event;
}
// Remove FW reserved commands just in case before collection snapshot
var fwCache = structKeyExists( rc, "fwCache" );
structDelete( rc, "fwCache" );
// Take snapshot of incoming collection
prc[ "cbox_incomingContextHash" ] = hash( rc.toString() );
// Do we have flash elements to inflate?
if ( variables.flashScope.flashExists() ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "Flash RAM detected, inflating flash." );
}
variables.flashScope.inflateFlash();
}
// Default Event Determination
if ( NOT structKeyExists( rc, variables.eventName ) ) {
rc[ variables.eventName ] = controller.getSetting( "DefaultEvent" );
}
// Event More Than 1 Check, grab the first event instance, other's are discarded
if ( listLen( rc[ variables.eventName ] ) GTE 2 ) {
rc[ variables.eventName ] = getToken( rc[ variables.eventName ], 2, "," );
}
// Default Event Action Checks
variables.handlerService.defaultActionCheck( context );
// Execute onRequestCapture interceptionPoint
variables.interceptorService.announce( "onRequestCapture" );
// Are we using event caching?
eventCachingTest( context, fwCache );
return context;
}
/**
* Tests if the incoming context is an event cache
*
* @context The request context to test for event caching
* @context.docbox_generic coldbox.system.web.context.RequestContext
* @fwCache Flag to hard purge the cache if needed
*/
RequestService function eventCachingTest( required context, boolean fwCache = false ){
var eventCache = {};
var oEventURLFacade = variables.templateCache.getEventURLFacade();
var currentEvent = arguments.context.getCurrentEvent();
// Are we using event caching?
if ( variables.eventCaching ) {
// Cleanup the cache key, just in case, maybe ses interceptor has been used.
arguments.context.removeEventCacheableEntry();
// Get metadata entry for event that's fired.
var eventDictionary = variables.handlerService.getEventMetaDataEntry( currentEvent );
// Verify that it is cacheable, else quit, no need for testing anymore.
if ( NOT eventDictionary.cacheable ) {
return this;
}
// Incorporate metadata about event
eventCache.append( eventDictionary, true );
// Build the event cache key according to incoming request
eventCache[ "cacheKey" ] = oEventURLFacade.buildEventKey(
targetEvent = currentEvent,
targetContext = arguments.context,
eventDictionary = eventDictionary
);
// Check for Event Cache Purge
if ( arguments.fwCache ) {
// Clear the key from the cache
variables.cacheBox.getCache( eventDictionary.provider ).clear( eventCache.cacheKey );
// Return don't show cached version
return this;
}
// Event has been found, flag it so we can render it from cache if it still survives
arguments.context.setEventCacheableEntry( eventCache );
// debug logging
if ( variables.log.canDebug() ) {
variables.log.debug( "Event caching detected for : #eventCache.toString()#" );
}
}
// end if using event caching.
return this;
}
/**
* Get the Request context from request scope or create a new one.
* Creation will be thread safe so two threads sharing the request scope
* don't both create a new context and overwrite
*
* @return coldbox.system.web.context.RequestContext
*/
function getContext( string classPath = "coldbox.system.web.context.RequestContext" ){
var thisContext = getContextFromScope();
if ( !isNull( thisContext ) ) {
return thisContext;
}
lock scope="request" timeout="30" {
// Double check once inside lock
var thisContext = getContextFromScope();
if ( !isNull( thisContext ) ) {
return thisContext;
}
return createContext( classPath );
}
}
/**
* Get the Request context from request scope or return null if not exists
*
* @return coldbox.system.web.context.RequestContext
*/
private function getContextFromScope(){
return request[ "cb_requestContext" ] ?: javacast( "null", "" );
}
/**
* Set the request context into the request scope
*
* @context Request Context object
* @RequestService
*/
RequestService function setContext( required context ){
request.cb_requestContext = arguments.context;
return this;
}
/**
* Remove the context from scope
*/
RequestService function removeContext(){
structDelete( request, "cb_requestContext" );
return this;
}
/**
* Does the request context exist in request scope
*/
boolean function contextExists(){
return structKeyExists( request, "cb_requestContext" );
}
/**
* Return the flash scope instance in use by the framework.
*/
any function getFlashScope(){
return variables.flashScope;
}
/**
* Rebuild's the Flash RAM Scope if the application spec has changed, else it ignores it
*/
RequestService function rebuildFlashScope(){
if ( variables.flashDataHash neq hash( controller.getSetting( "flash" ).toString() ) ) {
buildFlashScope();
}
return this;
}
/**
* Build's the Flash RAM Scope as defined in the application spec.
*/
RequestService function buildFlashScope(){
var flashPath = "";
// Verify Flash decisions
if ( variables.flashData.scope == "session" and !getApplicationMetadata().sessionManagement ) {
log.error(
"Flash RAM was set to use session but session is undefined, changing it to cache for you so we don't blow up."
);
variables.flashData.scope = "cache";
}
if ( variables.flashData.scope == "client" and !getApplicationMetadata().clientManagement ) {
log.error(
"Flash RAM was set to use client but client is undefined, changing it to cache for you so we don't blow up."
);
variables.flashData.scope = "cache";
}
// Shorthand Flash Types
switch ( variables.flashData.scope ) {
case "session": {
flashpath = "coldbox.system.web.flash.SessionFlash";
break;
}
case "client": {
flashpath = "coldbox.system.web.flash.ClientFlash";
break;
}
case "cache": {
flashpath = "coldbox.system.web.flash.ColdboxCacheFlash";
break;
}
case "mock": {
flashpath = "coldbox.system.web.flash.MockFlash";
break;
}
default: {
flashPath = variables.flashData.scope;
}
}
// Create Flash RAM object
variables.flashScope = createObject( "component", flashPath ).init( controller, variables.flashData );
return this;
}
/****************************************** PRIVATE ******************************************************/
/**
* Creates a new request context object
*
* @return coldbox.system.web.context.RequestContext
*/
function createContext( string classPath = "coldbox.system.web.context.RequestContext" ){
var oDecorator = "";
// Create the original request context
var oContext = createObject( "component", classPath ).init(
properties = controller.getConfigSettings(),
controller = controller
);
// Determine if we have a decorator, if we do, then decorate it.
if ( len( controller.getSetting( name = "RequestContextDecorator", defaultValue = "" ) ) ) {
// Create the decorator
oDecorator = createObject( "component", controller.getSetting( name = "RequestContextDecorator" ) ).init(
oContext,
controller
);
// Set Request Context in storage
setContext( oDecorator );
// Return
return oDecorator;
}
// Set Request Context in storage
setContext( oContext );
// Return Context
return oContext;
}
}