org.springframework.web.bind.ServletRequestBindingException Java Examples

The following examples show how to use org.springframework.web.bind.ServletRequestBindingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: airsonic-advanced   Author: airsonic-advanced   File: StreamController.java    License: GNU General Public License v3.0 6 votes vote down vote up
private VideoTranscodingSettings createVideoTranscodingSettings(MediaFile file, HttpServletRequest request)
        throws ServletRequestBindingException {
    Integer existingWidth = file.getWidth();
    Integer existingHeight = file.getHeight();
    Integer maxBitRate = ServletRequestUtils.getIntParameter(request, "maxBitRate");
    int timeOffset = ServletRequestUtils.getIntParameter(request, "timeOffset", 0);
    double defaultDuration = file.getDuration() == null ? Double.MAX_VALUE : file.getDuration() - timeOffset;
    double duration = ServletRequestUtils.getDoubleParameter(request, "duration", defaultDuration);
    boolean hls = ServletRequestUtils.getBooleanParameter(request, "hls", false);

    Dimension dim = getRequestedVideoSize(request.getParameter("size"));
    if (dim == null) {
        dim = getSuitableVideoSize(existingWidth, existingHeight, maxBitRate);
    }

    return new VideoTranscodingSettings(dim.width, dim.height, timeOffset, duration, hls);
}
 
Example #2
Source Project: airsonic-advanced   Author: airsonic-advanced   File: RESTFilter.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void handleException(Throwable x, HttpServletRequest request, HttpServletResponse response) {
    if (x instanceof NestedServletException && x.getCause() != null) {
        x = x.getCause();
    }

    SubsonicRESTController.ErrorCode code = (x instanceof ServletRequestBindingException) ? SubsonicRESTController.ErrorCode.MISSING_PARAMETER : SubsonicRESTController.ErrorCode.GENERIC;
    String msg = getErrorMessage(x);

    // This happens often and outside of the control of the server, so
    // we catch Tomcat/Jetty "connection aborted by client" exceptions
    // and display a short error message.
    boolean shouldCatch = Util.isInstanceOfClassName(x, "org.apache.catalina.connector.ClientAbortException");
    if (shouldCatch) {
        LOG.info("{}: Client unexpectedly closed connection while loading {} ({})", request.getRemoteAddr(),
                Util.getAnonymizedURLForRequest(request), x.getMessage());
    } else {
        LOG.warn("Error in REST API", x);

        try {
            jaxbWriter.writeErrorResponse(request, response, code, msg);
        } catch (Exception e) {
            LOG.error("Failed to write error response.", e);
        }
    }

}
 
Example #3
Source Project: lams   Author: lamsfoundation   File: DefaultAnnotationHandlerMapping.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate the given type-level mapping metadata against the current request,
 * checking HTTP request method and parameter conditions.
 * @param mapping the mapping metadata to validate
 * @param request current HTTP request
 * @throws Exception if validation failed
 */
protected void validateMapping(RequestMapping mapping, HttpServletRequest request) throws Exception {
	RequestMethod[] mappedMethods = mapping.method();
	if (!ServletAnnotationMappingUtils.checkRequestMethod(mappedMethods, request)) {
		String[] supportedMethods = new String[mappedMethods.length];
		for (int i = 0; i < mappedMethods.length; i++) {
			supportedMethods[i] = mappedMethods[i].name();
		}
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), supportedMethods);
	}

	String[] mappedParams = mapping.params();
	if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
		throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
	}

	String[] mappedHeaders = mapping.headers();
	if (!ServletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
		throw new ServletRequestBindingException("Header conditions \"" +
				StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
				"\" not met for actual request");
	}
}
 
Example #4
Source Project: spring-analysis-note   Author: Vip-Augus   File: ResponseEntityExceptionHandlerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void controllerAdvice() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	ServletRequestBindingException ex = new ServletRequestBindingException("message");
	assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: ResponseEntityExceptionHandlerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	try {
		servlet.service(this.servletRequest, this.servletResponse);
	}
	catch (ServletException ex) {
		assertTrue(ex.getCause() instanceof IllegalStateException);
		assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException);
	}
}
 
Example #6
Source Project: java-technology-stack   Author: codeEngraver   File: ResponseEntityExceptionHandlerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void controllerAdvice() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	ServletRequestBindingException ex = new ServletRequestBindingException("message");
	assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example #7
Source Project: java-technology-stack   Author: codeEngraver   File: ResponseEntityExceptionHandlerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	try {
		servlet.service(this.servletRequest, this.servletResponse);
	}
	catch (ServletException ex) {
		assertTrue(ex.getCause() instanceof IllegalStateException);
		assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException);
	}
}
 
Example #8
Source Project: FEBS-Security   Author: febsteam   File: SmsCodeFilter.java    License: Apache License 2.0 6 votes vote down vote up
private void validateSmsCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobile = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "mobile");
    ValidateCode codeInSession = (ValidateCode) sessionStrategy.getAttribute(servletWebRequest, FebsConstant.SESSION_KEY_SMS_CODE + mobile);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在,请重新发送!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_SMS_CODE + mobile);
        throw new ValidateCodeException("验证码已过期,请重新发送!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_SMS_CODE + mobile);

}
 
Example #9
Source Project: FEBS-Security   Author: febsteam   File: ImageCodeFilter.java    License: Apache License 2.0 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, FebsConstant.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在,请重新发送!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期,请重新发送!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_IMAGE_CODE);

}
 
Example #10
Source Project: SpringAll   Author: wuyouzhuguli   File: ValidateCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #11
Source Project: SpringAll   Author: wuyouzhuguli   File: SmsCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

    SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #12
Source Project: SpringAll   Author: wuyouzhuguli   File: ValidateCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #13
Source Project: SpringAll   Author: wuyouzhuguli   File: ValidateCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #14
Source Project: SpringAll   Author: wuyouzhuguli   File: SmsCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

    SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #15
Source Project: SpringAll   Author: wuyouzhuguli   File: SmsCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

    SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #16
Source Project: SpringAll   Author: wuyouzhuguli   File: ValidateCodeFilter.java    License: MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #17
Source Project: imooc-security   Author: charlieDJ   File: SmsCodeFilter.java    License: Apache License 2.0 6 votes vote down vote up
private void validate(ServletWebRequest request) throws ServletRequestBindingException {
    ValidateCode codeInSession = (ValidateCode) sessionStrategy.getAttribute(request,ValidateCodeController.SESSION_KEY_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(), "smsCode");
    if(StringUtils.isBlank(codeInRequest)){
        throw new ValidateCodeException("验证码不能为空");
    }
    if(codeInSession==null){
        throw new ValidateCodeException("验证码不存在");
    }
    if(codeInSession.isExpired()){
        sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_CODE);
        throw new ValidateCodeException("验证码过期");
    }
    if(!StringUtils.equals(codeInSession.getCode(),codeInRequest)){
        throw new ValidateCodeException("验证码不匹配");
    }
    sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_CODE);
}
 
Example #18
Source Project: imooc-security   Author: charlieDJ   File: ValidateCodeFilter.java    License: Apache License 2.0 6 votes vote down vote up
private void validate(ServletWebRequest request) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(request,ValidateCodeController.SESSION_KEY_IMAGE);
    String codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(), "imageCode");
    if(StringUtils.isBlank(codeInRequest)){
        throw new ValidateCodeException("验证码不能为空");
    }
    if(codeInSession==null){
        throw new ValidateCodeException("验证码不存在");
    }
    if(codeInSession.isExpired()){
        sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_IMAGE);
        throw new ValidateCodeException("验证码过期");
    }
    if(!StringUtils.equals(codeInSession.getCode(),codeInRequest)){
        throw new ValidateCodeException("验证码不匹配");
    }
    sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_IMAGE);
}
 
Example #19
Source Project: airsonic   Author: airsonic   File: DownloadController.java    License: GNU General Public License v3.0 5 votes vote down vote up
public long getLastModified(HttpServletRequest request) {
    try {
        MediaFile mediaFile = getMediaFile(request);
        if (mediaFile == null || mediaFile.isDirectory() || mediaFile.getChanged() == null) {
            return -1;
        }
        return mediaFile.getChanged().getTime();
    } catch (ServletRequestBindingException e) {
        return -1;
    }
}
 
Example #20
Source Project: spring-analysis-note   Author: Vip-Augus   File: DefaultHandlerExceptionResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void handleServletRequestBindingException() {
	String message = "Missing required value - header, cookie, or pathvar";
	ServletRequestBindingException ex = new ServletRequestBindingException(message);
	ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
	assertNotNull("No ModelAndView returned", mav);
	assertTrue("No Empty ModelAndView returned", mav.isEmpty());
	assertEquals("Invalid status code", 400, response.getStatus());
}
 
Example #21
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractRequestAttributesArgumentResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void resolve() throws Exception {
	MethodParameter param = initMethodParameter(0);
	try {
		testResolveArgument(param);
		fail("Should be required by default");
	}
	catch (ServletRequestBindingException ex) {
		assertTrue(ex.getMessage().startsWith("Missing "));
	}

	Foo foo = new Foo();
	this.webRequest.setAttribute("foo", foo, getScope());
	assertSame(foo, testResolveArgument(param));
}
 
Example #22
Source Project: spring-analysis-note   Author: Vip-Augus   File: ResponseEntityExceptionHandlerTests.java    License: MIT License 5 votes vote down vote up
@Override
protected ResponseEntity<Object> handleServletRequestBindingException(
		ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

	headers.set("someHeader", "someHeaderValue");
	return handleExceptionInternal(ex, "error content", headers, status, request);
}
 
Example #23
Source Project: spring-analysis-note   Author: Vip-Augus   File: ComplexWebApplicationContext.java    License: MIT License 5 votes vote down vote up
@Override
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException {
	WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
	if (!(wac instanceof ComplexWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
		throw new ServletException("Not in a MultipartHttpServletRequest");
	}
	if (request.getParameter("fail") != null) {
		throw new ModelAndViewDefiningException(new ModelAndView("failed1"));
	}
	if (request.getParameter("access") != null) {
		throw new IllegalAccessException("illegal access");
	}
	if (request.getParameter("servlet") != null) {
		throw new ServletRequestBindingException("servlet");
	}
	if (request.getParameter("exception") != null) {
		throw new RuntimeException("servlet");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (RequestContextUtils.getTimeZone(request) != null) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) {
		throw new ServletException("Incorrect ThemeResolver");
	}
	if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
	RequestContext rc = new RequestContext(request);
	rc.changeLocale(Locale.US, TimeZone.getTimeZone("GMT+1"));
	rc.changeTheme("theme2");
	if (!Locale.US.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.US.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(RequestContextUtils.getTimeZone(request))) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
}
 
Example #24
Source Project: WeBASE-Front   Author: WeBankFinTech   File: ExceptionsHandler.java    License: Apache License 2.0 5 votes vote down vote up
@ResponseBody
@ExceptionHandler(value = ServletRequestBindingException.class)
public ResponseEntity bindExceptionHandler(ServletRequestBindingException ex) {
    log.warn("catch bindExceptionHandler", ex);

    Map<String, Object> map = new HashMap<>();
    //  map.put("exception", frontException);
    map.put("errorMessage", ex.getMessage());
    map.put("code", 400);
    log.warn("bindExceptionHandler return:{}", JsonUtils.toJSONString(map));
    return ResponseEntity.status(400).body(map);
}
 
Example #25
Source Project: open-capacity-platform   Author: open-capacity-platform   File: IValidateCodeServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 验证验证码
 */
@Override
public void validate(HttpServletRequest request) {
	String deviceId = request.getParameter("deviceId");
	if (StringUtils.isBlank(deviceId)) {
		throw new RuntimeException("请在请求参数中携带deviceId参数");
	}
	String code = this.getCode(deviceId);
	String codeInRequest;
	try {
		codeInRequest = ServletRequestUtils.getStringParameter(request, "validCode");
	} catch (ServletRequestBindingException e) {
		throw new AuthenticationException ("获取验证码的值失败"){};
	}
	if (StringUtils.isBlank(codeInRequest)) {
		throw new AuthenticationException ("请填写验证码"){};
	}

	if (code == null) {
		throw new AuthenticationException ("验证码不存在或已过期"){};
	}

	if (!StringUtils.equals(code, codeInRequest)) {
		throw new AuthenticationException ("验证码不正确"){};
	}

	this.remove(deviceId);
}
 
Example #26
Source Project: microservices-platform   Author: zlt2000   File: ValidateCodeServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 验证验证码
 */
@Override
public void validate(HttpServletRequest request) {
    String deviceId = request.getParameter("deviceId");
    if (StringUtils.isBlank(deviceId)) {
        throw new ValidateCodeException("请在请求参数中携带deviceId参数");
    }
    String code = this.getCode(deviceId);
    String codeInRequest;
    try {
        codeInRequest = ServletRequestUtils.getStringParameter(request, "validCode");
    } catch (ServletRequestBindingException e) {
        throw new ValidateCodeException("获取验证码的值失败");
    }
    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("请填写验证码");
    }

    if (code == null) {
        throw new ValidateCodeException("验证码不存在或已过期");
    }

    if (!StringUtils.equals(code, codeInRequest.toLowerCase())) {
        throw new ValidateCodeException("验证码不正确");
    }

    this.remove(deviceId);
}
 
Example #27
Source Project: ZTuoExchange_framework   Author: sengeiou   File: ExceptionControllerAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * 拦截绑定参数异常
 *
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = ServletRequestBindingException.class)
public MessageResult myErrorHandler(ServletRequestBindingException e) {
    e.printStackTrace();
    log.info(">>>拦截绑定参数异常>>",e);
    MessageResult result = MessageResult.error(3000, "参数绑定错误(如:必须参数没传递)!");
    return result;
}
 
Example #28
Source Project: fw-spring-cloud   Author: xuyisu   File: ValidateCodeServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void validate(ServletWebRequest request) {
	String imageRes = redisTemplate.opsForValue().get(SESSION_KEY);
	ImageCode codeInSession = JSONUtil.toBean(imageRes, ImageCode.class);
	String codeInRequest;
	try {
		codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(),
				"imageCode");
	} catch (ServletRequestBindingException e) {
		throw new ValidateCodeException("获取验证码的值失败");
	}

	if (StrUtil.isBlank(codeInRequest)) {
		throw new ValidateCodeException("验证码的值不能为空");
	}

	if (codeInSession == null) {
		throw new ValidateCodeException("验证码不存在");
	}

	if (codeInSession.isExpried()) {
		redisTemplate.delete(SESSION_KEY);
		throw new ValidateCodeException("验证码已过期");
	}

	if (!StrUtil.equals(codeInSession.getCode(), codeInRequest)) {
		throw new ValidateCodeException("验证码不匹配");
	}

	redisTemplate.delete(SESSION_KEY);
}
 
Example #29
Source Project: ZTuoExchange_framework   Author: homeyanmi   File: ExceptionControllerAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * 拦截绑定参数异常
 *
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = ServletRequestBindingException.class)
public MessageResult myErrorHandler(ServletRequestBindingException e) {
    e.printStackTrace();
    log.info(">>>拦截绑定参数异常>>",e);
    MessageResult result = MessageResult.error(3000, "参数绑定错误(如:必须参数没传递)!");
    return result;
}
 
Example #30
Source Project: java-technology-stack   Author: codeEngraver   File: DefaultHandlerExceptionResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void handleServletRequestBindingException() {
	String message = "Missing required value - header, cookie, or pathvar";
	ServletRequestBindingException ex = new ServletRequestBindingException(message);
	ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
	assertNotNull("No ModelAndView returned", mav);
	assertTrue("No Empty ModelAndView returned", mav.isEmpty());
	assertEquals("Invalid status code", 400, response.getStatus());
}