use of jakarta.servlet.ServletResponse in project tomcat by apache.
the class TestAsyncContextImpl method testAsyncListenerSupplyRequestResponse.
@Test
public void testAsyncListenerSupplyRequestResponse() {
final ServletRequest servletRequest = EasyMock.createMock(ServletRequest.class);
final ServletResponse servletResponse = EasyMock.createMock(ServletResponse.class);
final AsyncListener listener = new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
checkRequestResponse(event);
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
checkRequestResponse(event);
}
@Override
public void onError(AsyncEvent event) throws IOException {
checkRequestResponse(event);
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
checkRequestResponse(event);
}
private void checkRequestResponse(AsyncEvent event) {
Assert.assertEquals(servletRequest, event.getSuppliedRequest());
Assert.assertEquals(servletResponse, event.getSuppliedResponse());
}
};
final Context context = new TesterContext();
final Response response = new Response();
final Request request = new Request(null);
request.setCoyoteRequest(new org.apache.coyote.Request());
request.getMappingData().context = context;
final AsyncContextImpl ac = new AsyncContextImpl(request);
ac.addListener(listener, servletRequest, servletResponse);
ac.setStarted(context, request, response, true);
ac.addListener(listener, servletRequest, servletResponse);
ac.setErrorState(new Exception(), true);
ac.fireOnComplete();
}
use of jakarta.servlet.ServletResponse in project tomcat by apache.
the class Response method setResponse.
/**
* Set a wrapped HttpServletResponse to pass to the application. Components
* wishing to wrap the response should obtain the response via
* {@link #getResponse()}, wrap it and then call this method with the
* wrapped response.
*
* @param applicationResponse The wrapped response to pass to the
* application
*/
public void setResponse(HttpServletResponse applicationResponse) {
// Check the wrapper wraps this request
ServletResponse r = applicationResponse;
while (r instanceof HttpServletResponseWrapper) {
r = ((HttpServletResponseWrapper) r).getResponse();
}
if (r != facade) {
throw new IllegalArgumentException(sm.getString("response.illegalWrap"));
}
this.applicationResponse = applicationResponse;
}
use of jakarta.servlet.ServletResponse in project tomcat by apache.
the class AsyncContextImpl method setErrorState.
public void setErrorState(Throwable t, boolean fireOnError) {
if (t != null) {
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
}
request.getCoyoteRequest().action(ActionCode.ASYNC_ERROR, null);
if (fireOnError) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("asyncContextImpl.fireOnError"));
}
AsyncEvent errorEvent = new AsyncEvent(event.getAsyncContext(), event.getSuppliedRequest(), event.getSuppliedResponse(), t);
List<AsyncListenerWrapper> listenersCopy = new ArrayList<>(listeners);
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnError(errorEvent);
} catch (Throwable t2) {
ExceptionUtils.handleThrowable(t2);
log.warn(sm.getString("asyncContextImpl.onErrorError", listener.getClass().getName()), t2);
}
}
}
AtomicBoolean result = new AtomicBoolean();
request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, result);
if (result.get()) {
// No listener called dispatch() or complete(). This is an error.
// SRV.2.3.3.3 (search for "error dispatch")
// Take a local copy to avoid threading issues if another thread
// clears this (can happen during error handling with non-container
// threads)
ServletResponse servletResponse = this.servletResponse;
if (servletResponse instanceof HttpServletResponse) {
((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
Host host = (Host) context.getParent();
Valve stdHostValve = host.getPipeline().getBasic();
if (stdHostValve instanceof StandardHostValve) {
((StandardHostValve) stdHostValve).throwable(request, request.getResponse(), t);
}
request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, result);
if (result.get()) {
// Still in the error state. The error page did not call
// complete() or dispatch(). Complete the async processing.
complete();
}
}
}
use of jakarta.servlet.ServletResponse in project spring-security by spring-projects.
the class JaasApiIntegrationFilterTests method assertJaasSubjectEquals.
private void assertJaasSubjectEquals(final Subject expectedValue) throws Exception {
MockFilterChain chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
// See if the subject was updated
Subject currentSubject = Subject.getSubject(AccessController.getContext());
assertThat(currentSubject).isEqualTo(expectedValue);
// run so we know the chain was executed
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, chain);
// ensure that the chain was actually invoked
assertThat(chain.getRequest()).isNotNull();
}
use of jakarta.servlet.ServletResponse in project spring-framework by spring-projects.
the class ViewResolverTests method internalResourceViewResolverWithSpecificContextBeans.
@Test
public void internalResourceViewResolverWithSpecificContextBeans() throws Exception {
this.wac.registerSingleton("myBean", TestBean.class);
this.wac.registerSingleton("myBean2", TestBean.class);
this.wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
Properties props = new Properties();
props.setProperty("key1", "value1");
vr.setAttributes(props);
Map<String, Object> map = new HashMap<>();
map.put("key2", 2);
vr.setAttributesMap(map);
vr.setExposedContextBeanNames(new String[] { "myBean2" });
vr.setApplicationContext(this.wac);
HttpServletRequest request = new MockHttpServletRequest(this.sc) {
@Override
public RequestDispatcher getRequestDispatcher(String path) {
return new MockRequestDispatcher(path) {
@Override
public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
assertThat(forwardRequest.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
assertThat(forwardRequest.getAttribute("key1")).isEqualTo("value1");
assertThat(forwardRequest.getAttribute("key2")).isEqualTo(2);
assertThat(forwardRequest.getAttribute("myBean")).isNull();
assertThat(forwardRequest.getAttribute("myBean2")).isSameAs(wac.getBean("myBean2"));
}
};
}
};
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
View view = vr.resolveViewName("example1", Locale.getDefault());
view.render(new HashMap<String, Object>(), request, this.response);
}
Aggregations