-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow writing to the async response from promises by providing the ab…
…ility to lookup the response from the executing thread
- Loading branch information
1 parent
1b47565
commit 88b2152
Showing
7 changed files
with
139 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ls-async/src/main/groovy/org/grails/async/decorator/PromiseDecoratorLookupStrategy.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.grails.async.decorator | ||
|
||
/** | ||
* A lookup strategy for PromiseDecorator instances | ||
* | ||
* @author Graeme Rocher | ||
* @since 2.3 | ||
*/ | ||
public interface PromiseDecoratorLookupStrategy { | ||
|
||
List<PromiseDecorator> findDecorators() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../codehaus/groovy/grails/plugins/web/async/WebRequestPromiseDecoratorLookupStrategy.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.codehaus.groovy.grails.plugins.web.async | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.transform.TypeCheckingMode | ||
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest | ||
import org.codehaus.groovy.grails.web.util.WebUtils | ||
import org.grails.async.decorator.PromiseDecorator | ||
import org.grails.async.decorator.PromiseDecoratorLookupStrategy | ||
|
||
/** | ||
* A promise decorated lookup strategy that binds a WebRequest to the promise thread | ||
* | ||
* @author Graeme Rocher | ||
* @since 2.3 | ||
*/ | ||
@CompileStatic | ||
class WebRequestPromiseDecoratorLookupStrategy implements PromiseDecoratorLookupStrategy{ | ||
@Override | ||
List<PromiseDecorator> findDecorators() { | ||
final webRequest = GrailsWebRequest.lookup() | ||
if (webRequest) { | ||
return [new WebRequestPromsiseDecorator(webRequest)] | ||
} | ||
return Collections.emptyList() | ||
} | ||
} | ||
@CompileStatic | ||
class WebRequestPromsiseDecorator implements PromiseDecorator{ | ||
GrailsWebRequest webRequest | ||
|
||
WebRequestPromsiseDecorator(GrailsWebRequest webRequest) { | ||
this.webRequest = webRequest | ||
} | ||
|
||
@Override | ||
def <D> Closure<D> decorate(Closure<D> c) { | ||
return (Closure<D>) { args -> | ||
def newWebRequest = new GrailsWebRequest(webRequest.currentRequest, webRequest.currentResponse, webRequest.servletContext,webRequest.applicationContext) | ||
WebUtils.storeGrailsWebRequest(newWebRequest) | ||
try { | ||
return invokeClosure(c, args) | ||
} | ||
finally { | ||
newWebRequest.requestCompleted() | ||
WebUtils.clearGrailsWebRequest() | ||
|
||
} | ||
} | ||
} | ||
|
||
@CompileStatic(TypeCheckingMode.SKIP) | ||
def invokeClosure(Closure c, args) { | ||
c.call(* args) | ||
} | ||
} |