|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package issue11102 |
| 21 | + |
| 22 | +import org.springframework.web.context.request.async.DeferredResult |
| 23 | +import org.springframework.web.context.request.async.WebAsyncManager |
| 24 | +import org.springframework.web.context.request.async.WebAsyncUtils |
| 25 | +import org.springframework.web.servlet.ModelAndView |
| 26 | +import org.springframework.web.servlet.view.InternalResourceView |
| 27 | + |
| 28 | +import org.grails.web.mapping.ForwardUrlMappingInfo |
| 29 | +import org.grails.web.mapping.UrlMappingUtils |
| 30 | + |
| 31 | +class FailingController { |
| 32 | + |
| 33 | + def answeredByView() { |
| 34 | + throw new IllegalStateException('answered by the error view') |
| 35 | + } |
| 36 | + |
| 37 | + def answeredByController() { |
| 38 | + throw new UnsupportedOperationException('answered by the error controller') |
| 39 | + } |
| 40 | + |
| 41 | + def answeredByViewAsynchronously() { |
| 42 | + failAsynchronously(new IllegalStateException('answered by the error view')) |
| 43 | + } |
| 44 | + |
| 45 | + def answeredByControllerAsynchronously() { |
| 46 | + failAsynchronously(new UnsupportedOperationException('answered by the error controller')) |
| 47 | + } |
| 48 | + |
| 49 | + def answeredByViewAfterStartingAsync() { |
| 50 | + startAsyncThatTimesOut() |
| 51 | + throw new IllegalStateException('answered by the error view') |
| 52 | + } |
| 53 | + |
| 54 | + def answeredByControllerAfterStartingAsync() { |
| 55 | + startAsync(new DeferredResult<Object>()) |
| 56 | + throw new UnsupportedOperationException('answered by the error controller') |
| 57 | + } |
| 58 | + |
| 59 | + def answeredByViewForAnError() { |
| 60 | + throw new AssertionError('answered by the error view') |
| 61 | + } |
| 62 | + |
| 63 | + def answeredByControllerForAnError() { |
| 64 | + throw new StackOverflowError('answered by the error controller') |
| 65 | + } |
| 66 | + |
| 67 | + def answeredByForwardingController() { |
| 68 | + throw new ArithmeticException('answered by an error controller that forwards') |
| 69 | + } |
| 70 | + |
| 71 | + def answeredByIncludingController() { |
| 72 | + throw new NoSuchElementException('answered by an error controller that includes an action') |
| 73 | + } |
| 74 | + |
| 75 | + def forwardedToView() { |
| 76 | + forward(action: 'answeredByView') |
| 77 | + } |
| 78 | + |
| 79 | + def forwardedToViewAsynchronously() { |
| 80 | + forwardAsynchronously('/failing/answeredByView') |
| 81 | + } |
| 82 | + |
| 83 | + def forwardedToControllerAsynchronously() { |
| 84 | + forwardAsynchronously('/failing/answeredByController') |
| 85 | + } |
| 86 | + |
| 87 | + def forwardedToController() { |
| 88 | + forward(action: 'answeredByController') |
| 89 | + } |
| 90 | + |
| 91 | + def failsInInterceptor() { |
| 92 | + render 'never reached, because the interceptor fails' |
| 93 | + } |
| 94 | + |
| 95 | + def deniedByInterceptor() { |
| 96 | + render 'never reached, because the interceptor denies it' |
| 97 | + } |
| 98 | + |
| 99 | + def sendsNotFound() { |
| 100 | + response.sendError(404) |
| 101 | + } |
| 102 | + |
| 103 | + def notFoundHandler() { |
| 104 | + render status: 404, text: 'Answered by the not found handler' |
| 105 | + } |
| 106 | + |
| 107 | + def failsAfterInclude() { |
| 108 | + g.include(action: 'included') |
| 109 | + throw new IllegalStateException('answered by the error view') |
| 110 | + } |
| 111 | + |
| 112 | + def failsAfterIncludeAnsweredByController() { |
| 113 | + g.include(action: 'included') |
| 114 | + throw new UnsupportedOperationException('answered by the error controller') |
| 115 | + } |
| 116 | + |
| 117 | + def includesAnswerOfView() { |
| 118 | + render "before ${g.include(action: 'answeredByView')} after" |
| 119 | + } |
| 120 | + |
| 121 | + def includesAnswerOfController() { |
| 122 | + render "before ${g.include(action: 'answeredByController')} after" |
| 123 | + } |
| 124 | + |
| 125 | + def includesThroughUrlMappingUtils() { |
| 126 | + request.setAttribute('greeting', 'outer') |
| 127 | + response.writer.write('before ') |
| 128 | + UrlMappingUtils.includeRequestForUrlMappingInfo(request, response, new ForwardUrlMappingInfo(controller: 'failing', action: 'includedDetails', id: '9', params: [marker: 'included']), [greeting: 'inner'], false) |
| 129 | + render " after, as ${controllerName}/${actionName} with ${params.marker}, id ${params.id} and ${request.getAttribute('greeting')}" |
| 130 | + } |
| 131 | + |
| 132 | + def includedDetails() { |
| 133 | + render "included with id ${params.id} and ${request.getAttribute('greeting')}" |
| 134 | + } |
| 135 | + |
| 136 | + def included() { |
| 137 | + render 'included' |
| 138 | + } |
| 139 | + |
| 140 | + def errorHandler() { |
| 141 | + render status: 500, text: 'Answered by the error controller' |
| 142 | + } |
| 143 | + |
| 144 | + def forwardingErrorHandler() { |
| 145 | + forward(action: 'errorHandler') |
| 146 | + } |
| 147 | + |
| 148 | + def includingErrorHandler() { |
| 149 | + response.status = 500 |
| 150 | + response.writer.write('Answered by the error controller, with ') |
| 151 | + UrlMappingUtils.includeRequestForUrlMappingInfo(request, response, new ForwardUrlMappingInfo(controller: 'failing', action: 'included'), [:], false) |
| 152 | + render ' inside' |
| 153 | + } |
| 154 | + |
| 155 | + private void failAsynchronously(Exception failure) { |
| 156 | + DeferredResult<Object> result = new DeferredResult<>() |
| 157 | + startAsync(result) |
| 158 | + result.errorResult = failure |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Completes asynchronous processing with a view that forwards, as a view Spring MVC resolves from a |
| 163 | + * {@code forward:} name does, so that the action the path maps to answers on the asynchronous dispatch. |
| 164 | + */ |
| 165 | + private void forwardAsynchronously(String path) { |
| 166 | + DeferredResult<Object> result = new DeferredResult<>() |
| 167 | + startAsync(result) |
| 168 | + result.result = new ModelAndView(new InternalResourceView(path)) |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Starts asynchronous processing that nothing completes. Unless the container ends it when the action fails, as |
| 173 | + * Tomcat does when the exception is left on the request, it ends when it times out. |
| 174 | + */ |
| 175 | + private void startAsyncThatTimesOut() { |
| 176 | + startAsync(new DeferredResult<Object>(500L)) |
| 177 | + } |
| 178 | + |
| 179 | + private void startAsync(DeferredResult<Object> result) { |
| 180 | + WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request) |
| 181 | + asyncManager.asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response) |
| 182 | + asyncManager.startDeferredResultProcessing(result) |
| 183 | + } |
| 184 | +} |
0 commit comments