Search in sources :

Example 86 with HttpServletRequest

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);
        }
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) MultipartResolver(org.springframework.web.multipart.MultipartResolver) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 87 with HttpServletRequest

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;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) MultipartRequest(org.springframework.web.multipart.MultipartRequest) Nullable(org.springframework.lang.Nullable)

Example 88 with HttpServletRequest

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);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 89 with HttpServletRequest

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");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Row(org.apache.poi.ss.usermodel.Row) View(org.springframework.web.servlet.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Example 90 with HttpServletRequest

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");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) View(org.springframework.web.servlet.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletRequest (jakarta.servlet.http.HttpServletRequest)289 Test (org.junit.jupiter.api.Test)160 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)93 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)88 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)67 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)44 Authentication (org.springframework.security.core.Authentication)31 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)31 Test (org.junit.Test)28 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)26 IOException (java.io.IOException)21 ServletException (jakarta.servlet.ServletException)20 HttpServlet (jakarta.servlet.http.HttpServlet)19 HashMap (java.util.HashMap)17 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)16 FilterChain (jakarta.servlet.FilterChain)15 HttpSession (jakarta.servlet.http.HttpSession)14 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)14 ServletRequest (jakarta.servlet.ServletRequest)13