use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.
the class ServletModelAttributeMethodProcessor method resolveConstructorArgument.
@Override
@Nullable
public Object resolveConstructorArgument(String paramName, Class<?> paramType, NativeWebRequest request) throws Exception {
Object value = super.resolveConstructorArgument(paramName, paramType, request);
if (value != null) {
return value;
}
ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
if (servletRequest != null) {
String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
@SuppressWarnings("unchecked") Map<String, String> uriVars = (Map<String, String>) servletRequest.getAttribute(attr);
return uriVars.get(paramName);
}
return null;
}
use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.
the class StreamingResponseBodyReturnValueHandler method handleReturnValue.
@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
Assert.state(response != null, "No HttpServletResponse");
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
if (returnValue instanceof ResponseEntity) {
ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
response.setStatus(responseEntity.getStatusCodeValue());
outputMessage.getHeaders().putAll(responseEntity.getHeaders());
returnValue = responseEntity.getBody();
if (returnValue == null) {
mavContainer.setRequestHandled(true);
outputMessage.flush();
return;
}
}
ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
Assert.state(request != null, "No ServletRequest");
ShallowEtagHeaderFilter.disableContentCaching(request);
Assert.isInstanceOf(StreamingResponseBody.class, returnValue, "StreamingResponseBody expected");
StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue;
Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody);
WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.
the class FormTag method processAction.
/**
* Process the action through a {@link RequestDataValueProcessor} instance
* if one is configured or otherwise returns the action unmodified.
*/
private String processAction(String action) {
RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
ServletRequest request = this.pageContext.getRequest();
if (processor != null && request instanceof HttpServletRequest) {
action = processor.processAction((HttpServletRequest) request, action, getHttpMethod());
}
return action;
}
use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.
the class RequestContextFilterTests method testFilterInvocation.
private void testFilterInvocation(final ServletException sex) throws Exception {
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setAttribute("myAttr", "myValue");
final MockHttpServletResponse resp = new MockHttpServletResponse();
// Expect one invocation by the filter being tested
class DummyFilterChain implements FilterChain {
public int invocations = 0;
@Override
public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
++invocations;
if (invocations == 1) {
assertThat(RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST)).isSameAs("myValue");
if (sex != null) {
throw sex;
}
} else {
throw new IllegalStateException("Too many invocations");
}
}
}
DummyFilterChain fc = new DummyFilterChain();
MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
RequestContextFilter rbf = new RequestContextFilter();
rbf.init(mfc);
try {
rbf.doFilter(req, resp, fc);
assertThat(sex).isNull();
} catch (ServletException ex) {
assertThat(sex).isNotNull();
}
assertThatIllegalStateException().isThrownBy(RequestContextHolder::currentRequestAttributes);
assertThat(fc.invocations).isEqualTo(1);
}
use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.
the class ViewResolverTests method internalResourceViewResolverWithContextBeans.
@Test
public void internalResourceViewResolverWithContextBeans() 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.setExposeContextBeansAsAttributes(true);
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")).isSameAs(wac.getBean("myBean"));
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