use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class MultipartFilter method doFilterInternal.
/**
* Check for a multipart request via this filter's MultipartResolver,
* and wrap the original request with a MultipartHttpServletRequest if appropriate.
* <p>All later elements in the filter chain, most importantly servlets, benefit
* from proper parameter extraction in the multipart case, and are able to cast to
* MultipartHttpServletRequest if they need to.
*/
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
MultipartResolver multipartResolver = lookupMultipartResolver(request);
HttpServletRequest processedRequest = request;
if (multipartResolver.isMultipart(processedRequest)) {
if (logger.isTraceEnabled()) {
logger.trace("Resolving multipart request");
}
processedRequest = multipartResolver.resolveMultipart(processedRequest);
} else {
// A regular request...
if (logger.isTraceEnabled()) {
logger.trace("Not a multipart request");
}
}
try {
filterChain.doFilter(processedRequest, response);
} finally {
if (processedRequest instanceof MultipartHttpServletRequest) {
multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
}
}
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class MultipartResolutionDelegate method resolveMultipartRequest.
@Nullable
public static MultipartRequest resolveMultipartRequest(NativeWebRequest webRequest) {
MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class);
if (multipartRequest != null) {
return multipartRequest;
}
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
if (servletRequest != null && isMultipartContent(servletRequest)) {
return new StandardMultipartHttpServletRequest(servletRequest);
}
return null;
}
use of jakarta.servlet.http.HttpServletRequest 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);
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class XlsViewTests method testXls.
@Test
@SuppressWarnings("resource")
public void testXls() throws Exception {
View excelView = new AbstractXlsView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), request, response);
Workbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class XlsViewTests method testXlsxView.
@Test
@SuppressWarnings("resource")
public void testXlsxView() throws Exception {
View excelView = new AbstractXlsxView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), request, response);
Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
Aggregations