Search in sources :

Example 11 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project cas by apereo.

the class AbstractServiceValidateController method validateAssertion.

/**
     * Validate assertion.
     *
     * @param request the request
     * @param serviceTicketId the service ticket id
     * @param assertion the assertion
     * @return true/false
     */
private boolean validateAssertion(final HttpServletRequest request, final String serviceTicketId, final Assertion assertion) {
    this.validationSpecification.reset();
    final ServletRequestDataBinder binder = new ServletRequestDataBinder(this.validationSpecification, "validationSpecification");
    initBinder(request, binder);
    binder.bind(request);
    if (!this.validationSpecification.isSatisfiedBy(assertion, request)) {
        LOGGER.warn("Service ticket [{}] does not satisfy validation specification.", serviceTicketId);
        return false;
    }
    return true;
}
Also used : ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder)

Example 12 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class CommonsMultipartResolverTests method doTestBinding.

private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServletRequest originalRequest, MultipartHttpServletRequest request) throws UnsupportedEncodingException {
    MultipartTestBean1 mtb1 = new MultipartTestBean1();
    assertArrayEquals(null, mtb1.getField1());
    assertEquals(null, mtb1.getField2());
    ServletRequestDataBinder binder = new ServletRequestDataBinder(mtb1, "mybean");
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    binder.bind(request);
    List<MultipartFile> file1List = request.getFiles("field1");
    CommonsMultipartFile file1a = (CommonsMultipartFile) file1List.get(0);
    CommonsMultipartFile file1b = (CommonsMultipartFile) file1List.get(1);
    CommonsMultipartFile file2 = (CommonsMultipartFile) request.getFile("field2");
    assertEquals(file1a, mtb1.getField1()[0]);
    assertEquals(file1b, mtb1.getField1()[1]);
    assertEquals(new String(file2.getBytes()), new String(mtb1.getField2()));
    MultipartTestBean2 mtb2 = new MultipartTestBean2();
    assertArrayEquals(null, mtb2.getField1());
    assertEquals(null, mtb2.getField2());
    binder = new ServletRequestDataBinder(mtb2, "mybean");
    binder.registerCustomEditor(String.class, "field1", new StringMultipartFileEditor());
    binder.registerCustomEditor(String.class, "field2", new StringMultipartFileEditor("UTF-16"));
    binder.bind(request);
    assertEquals(new String(file1a.getBytes()), mtb2.getField1()[0]);
    assertEquals(new String(file1b.getBytes()), mtb2.getField1()[1]);
    assertEquals(new String(file2.getBytes(), "UTF-16"), mtb2.getField2());
    resolver.cleanupMultipart(request);
    assertTrue(((MockFileItem) file1a.getFileItem()).deleted);
    assertTrue(((MockFileItem) file1b.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
    resolver.setEmpty(true);
    request = resolver.resolveMultipart(originalRequest);
    binder.setBindEmptyMultipartFiles(false);
    String firstBound = mtb2.getField2();
    binder.bind(request);
    assertFalse(mtb2.getField2().isEmpty());
    assertEquals(firstBound, mtb2.getField2());
    request = resolver.resolveMultipart(originalRequest);
    binder.setBindEmptyMultipartFiles(true);
    binder.bind(request);
    assertTrue(mtb2.getField2().isEmpty());
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) StringMultipartFileEditor(org.springframework.web.multipart.support.StringMultipartFileEditor) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) ByteArrayMultipartFileEditor(org.springframework.web.multipart.support.ByteArrayMultipartFileEditor)

Example 13 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class ExtendedServletRequestDataBinderTests method noUriTemplateVars.

@Test
public void noUriTemplateVars() throws Exception {
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);
    assertEquals(null, target.getName());
    assertEquals(0, target.getAge());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) TestBean(org.springframework.tests.sample.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) Test(org.junit.Test)

Example 14 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class ExtendedServletRequestDataBinderTests method uriTemplateVarAndRequestParam.

@Test
public void uriTemplateVarAndRequestParam() throws Exception {
    request.addParameter("age", "35");
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "nameValue");
    uriTemplateVars.put("age", "25");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);
    assertEquals("nameValue", target.getName());
    assertEquals(35, target.getAge());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) Test(org.junit.Test)

Example 15 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class ExtendedServletRequestDataBinderTests method createBinder.

@Test
public void createBinder() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "nameValue");
    uriTemplateVars.put("age", "25");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);
    assertEquals("nameValue", target.getName());
    assertEquals(25, target.getAge());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) Test(org.junit.Test)

Aggregations

ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)29 Test (org.junit.Test)25 TestBean (org.springframework.tests.sample.beans.TestBean)24 PageContext (javax.servlet.jsp.PageContext)22 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)22 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)21 Errors (org.springframework.validation.Errors)16 BindStatus (org.springframework.web.servlet.support.BindStatus)14 SimpleDateFormat (java.text.SimpleDateFormat)7 CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)7 DateFormat (java.text.DateFormat)6 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 HashMap (java.util.HashMap)2 PropertyEditorSupport (java.beans.PropertyEditorSupport)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 ServletRequest (javax.servlet.ServletRequest)1 JspException (javax.servlet.jsp.JspException)1 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)1 SdvFilterDataBean (org.akaza.openclinica.controller.helper.SdvFilterDataBean)1