Example usage for org.apache.commons.lang StringUtils defaultIfBlank

List of usage examples for org.apache.commons.lang StringUtils defaultIfBlank

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils defaultIfBlank.

Prototype

public static String defaultIfBlank(String str, String defaultStr) 

Source Link

Document

Returns either the passed in String, or if the String is whitespace, empty ("") or null, the value of defaultStr.

Usage

From source file:net.ymate.framework.validation.UploadFileValidator.java

public ValidateResult validate(ValidateContext context) {
    // ??IUploadFileWrapper
    Object _paramValue = context.getParamValue();
    if (_paramValue != null) {
        String _paramName = StringUtils.defaultIfBlank(context.getParamLabel(), context.getParamName());
        _paramName = __doGetI18nFormatMessage(context, _paramName, _paramName);
        ////from   w w  w .  j  a  v a2  s.  co m
        VUploadFile _vUploadFile = (VUploadFile) context.getAnnotation();
        //
        List<String> _allowContentTypes = __doGetAllowContentTypes(context, _vUploadFile);
        //
        if (_paramValue.getClass().isArray() && _paramValue instanceof IUploadFileWrapper[]) {
            //
            IUploadFileWrapper[] _values = (IUploadFileWrapper[]) _paramValue;
            long _totalSize = 0;
            for (IUploadFileWrapper _value : _values) {
                _totalSize += _value.getSize();
                ValidateResult _result = __doValidateUploadFileWrapper(context, _allowContentTypes, _paramName,
                        _vUploadFile, _value);
                if (_result != null) {
                    return _result;
                }
            }
            if (_totalSize > _vUploadFile.totalMax()) {
                String _msg = StringUtils.trimToNull(_vUploadFile.msg());
                if (_msg != null) {
                    _msg = __doGetI18nFormatMessage(context, _msg, _msg, _paramName);
                } else {
                    _msg = __doGetI18nFormatMessage(context, "ymp.validation.upload_file_total_max",
                            "{0} total size must be lt {1}.", _paramName, _vUploadFile.totalMax());
                }
                return new ValidateResult(_paramName, _msg);
            }
        } else if (_paramValue instanceof IUploadFileWrapper) {
            IUploadFileWrapper _value = (IUploadFileWrapper) _paramValue;
            ValidateResult _result = __doValidateUploadFileWrapper(context, _allowContentTypes, _paramName,
                    _vUploadFile, _value);
            if (_result != null) {
                return _result;
            }
        }
    }
    return null;
}

From source file:net.ymate.framework.validation.VMobileValidator.java

public ValidateResult validate(ValidateContext context) {
    Object _paramValue = context.getParamValue();
    if (_paramValue != null) {
        if (!context.getParamValue().getClass().isArray()) {
            String _value = BlurObject.bind(_paramValue).toStringValue();
            if (StringUtils.isNotBlank(_value)) {
                /*//w  w w  .j a  va  2  s.  c om
                ???:
                - ?: 133?149?153?173?177?180?181?189?199
                - ??: 130?131?132?145?155?156?166?175?176?185?186
                - ?: 134(0-8)?135?136?137?138?139?147?150?151?152?157?158?159?178?182?183?184?187?188?198
                - ?: 14?????145147
                - ??
                + 1700?1701?1702
                + 1703?1705?1706
                + ?1704?1707?1708?1709?171
                 */
                String _regex = StringUtils.defaultIfBlank(((VMobile) context.getAnnotation()).regex(),
                        "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$");
                if (!_value.matches(_regex)) {
                    String _pName = StringUtils.defaultIfBlank(context.getParamLabel(), context.getParamName());
                    _pName = __doGetI18nFormatMessage(context, _pName, _pName);
                    //
                    String _msg = StringUtils.trimToNull(((VMobile) context.getAnnotation()).msg());
                    if (_msg != null) {
                        _msg = __doGetI18nFormatMessage(context, _msg, _msg, _pName);
                    } else {
                        _msg = __doGetI18nFormatMessage(context, "ymp.validation.mobile",
                                "{0} not a valid mobile phone number.", _pName);
                    }
                    return new ValidateResult(context.getParamName(), _msg);
                }
            }
        }
    }
    return null;
}

From source file:net.ymate.framework.validation.VTokenValidator.java

public ValidateResult validate(ValidateContext context) {
    boolean _matched = false;
    VToken _vToken = (VToken) context.getAnnotation();
    if (context.getParamValue() != null) {
        String _token = null;//from ww  w. j a  v  a  2 s  .c  o  m
        if (context.getParamValue().getClass().isArray()) {
            Object[] _objArr = (Object[]) context.getParamValue();
            if (_objArr.length > 0) {
                _token = BlurObject.bind(_objArr[0]).toStringValue();
            }
        } else {
            _token = BlurObject.bind(context.getParamValue()).toStringValue();
        }
        //
        if (!TokenProcessHelper.getInstance().isTokenValid(WebContext.getRequest(), _vToken.name(), _token,
                _vToken.reset())) {
            _matched = true;
        }
    }
    if (_matched) {
        String _pName = StringUtils.defaultIfBlank(context.getParamLabel(), context.getParamName());
        _pName = __doGetI18nFormatMessage(context, _pName, _pName);
        String _msg = StringUtils.trimToNull(_vToken.msg());
        if (_msg != null) {
            _msg = __doGetI18nFormatMessage(context, _msg, _msg, _pName);
        } else {
            _msg = __doGetI18nFormatMessage(context, "ymp.validation.token_invalid", "{0} is invalid.", _pName);
        }
        return new ValidateResult(context.getParamName(), _msg);
    }
    return null;
}

From source file:net.ymate.framework.webmvc.AbstractWebErrorProcessor.java

@Override
public void init(WebMVC owner) throws Exception {
    if (!__inited) {
        __resourceName = StringUtils
                .defaultIfBlank(owner.getOwner().getConfig().getParam(Optional.I18N_RESOURCE_NAME), "messages");
        __errorDefaultI18nKey = StringUtils.defaultIfBlank(
                owner.getOwner().getConfig().getParam(Optional.SYSTEM_ERROR_DEFAULT_I18N_KEY),
                Optional.SYSTEM_ERROR_DEFAULT_I18N_KEY);
        __errorDefaultViewFormat = StringUtils
                .trimToEmpty(owner.getOwner().getConfig().getParam(Optional.ERROR_DEFAULT_VIEW_FORMAT))
                .toLowerCase();//from   w w  w .  j av a  2  s. co  m
        __disabledAnalysis = BlurObject
                .bind(owner.getOwner().getConfig().getParam(Optional.SYSTEM_EXCEPTION_ANALYSIS_DISABLED))
                .toBooleanValue();
        //
        __doInit(owner);
        //
        __inited = true;
    }
}

From source file:net.ymate.framework.webmvc.AbstractWebErrorProcessor.java

public final String i18nMsg(String msgKey, String defaultMsg) {
    return I18N.formatMessage(__resourceName, StringUtils.defaultIfBlank(msgKey, __errorDefaultI18nKey),
            StringUtils.defaultIfBlank(defaultMsg, "?, ???!"));
}

From source file:net.ymate.framework.webmvc.intercept.AjaxAllowCrossDomainInterceptor.java

public Object intercept(InterceptContext context) throws Exception {
    if (Direction.BEFORE.equals(context.getDirection())) {
        boolean _allowCrossDomain = BlurObject
                .bind(context.getOwner().getConfig().getParam(Optional.ALLOW_CROSS_DOMAIN)).toBooleanValue();
        boolean _allowOptions = BlurObject
                .bind(StringUtils.defaultIfBlank(
                        context.getOwner().getConfig().getParam(Optional.ALLOW_OPTIONS_AUTO_REPLY), "true"))
                .toBooleanValue();//  w  w w . j a  v  a2  s  .  c  o m
        if (_allowCrossDomain) {
            String _hosts = context.getOwner().getConfig().getParam(Optional.ALLOW_ORIGIN_HOSTS);
            String _methods = context.getOwner().getConfig().getParam(Optional.ALLOW_CROSS_METHODS);
            String _headers = context.getOwner().getConfig().getParam(Optional.ALLOW_CROSS_HEADERS);
            __addHeadersToView(_hosts, _methods, _headers, BlurObject
                    .bind(context.getContextParams().get(Optional.NOT_ALLOW_CREDENTIALS)).toBooleanValue());
            //
            if (_allowOptions
                    && Type.HttpMethod.OPTIONS.equals(WebContext.getRequestContext().getHttpMethod())) {
                return View.nullView();
            }
        }
    }
    return null;
}

From source file:net.ymate.framework.webmvc.intercept.AjaxAllowCrossDomainInterceptor.java

private void __addHeadersToView(String hosts, String methods, String headers, boolean notAllowCredentials) {
    HttpServletResponse _response = WebContext.getResponse();
    _response.addHeader("Access-Control-Allow-Origin", StringUtils.defaultIfBlank(hosts, "*"));
    if (StringUtils.isNotBlank(methods)) {
        _response.addHeader("Access-Control-Allow-Methods", StringUtils.upperCase(methods));
    }/*ww w  .  ja  v a 2s.c  o m*/
    if (StringUtils.isNotBlank(headers)) {
        _response.addHeader("Access-Control-Allow-Headers", headers);
    }
    _response.addHeader("Access-Control-Allow-Credentials", notAllowCredentials ? "false" : "true");
}

From source file:net.ymate.framework.webmvc.intercept.RequestTokenInterceptor.java

@Override
public Object intercept(InterceptContext context) throws Exception {
    switch (context.getDirection()) {
    case BEFORE:/*from   w ww. ja va2 s. c o  m*/
        HttpServletRequest _request = WebContext.getRequest();
        String _tokenName = context.getContextParams().get(Optional.REQUEST_TOKEN_NAME);
        if (StringUtils.isBlank(_tokenName)) {
            _tokenName = StringUtils.defaultIfBlank(
                    context.getOwner().getConfig().getParam(Optional.REQUEST_TOKEN_NAME), "Request-Token");
        }
        // ???Cookies?
        boolean _headerFlag = false;
        boolean _cookieFlag = false;
        CookieHelper _cookieHelper = null;
        String _tokenStr = _request.getParameter(_tokenName);
        if (StringUtils.isBlank(_tokenStr)) {
            _tokenStr = _request.getHeader(_tokenName);
            if (StringUtils.isBlank(_tokenStr)) {
                _cookieHelper = CookieHelper.bind();
                _tokenStr = _cookieHelper.getCookie(_tokenName).toStringValue();
                _cookieFlag = StringUtils.isNotBlank(_tokenStr);
            } else {
                _headerFlag = true;
            }
        }
        // ?
        boolean _flag = TokenProcessHelper.getInstance().isTokenValid(_request, _tokenName, _tokenStr, true);
        // ??, ??
        if (_headerFlag || _cookieFlag) {
            _tokenStr = TokenProcessHelper.getInstance().saveToken(_request, _tokenName);
            if (_headerFlag) {
                WebContext.getResponse().addHeader(_tokenName, _tokenStr);
                CookieHelper.bind().removeCookie(_tokenName);
            } else {
                _cookieHelper.setCookie(_tokenName, _tokenStr);
            }
        }
        // ??
        if (!_flag) {
            return HttpStatusView.BAD_REQUEST;
        }
        break;
    }
    return null;
}

From source file:net.ymate.framework.webmvc.intercept.UserSessionCheckInterceptor.java

public Object intercept(InterceptContext context) throws Exception {
    // ??//from w  ww  .  j a v a  2 s.  c  om
    switch (context.getDirection()) {
    case BEFORE:
        UserSessionBean _sessionBean = UserSessionBean.current();
        if (_sessionBean == null) {
            if (UserSessionBean.getSessionHandler() != null) {
                _sessionBean = UserSessionBean.getSessionHandler().handle(context);
            }
        } else if (!_sessionBean.isVerified()) {
            _sessionBean.destroy();
            _sessionBean = null;
        }
        if (_sessionBean == null) {
            // URL?
            HttpServletRequest _request = WebContext.getRequest();
            StringBuffer _returnUrlBuffer = _request.getRequestURL();
            String _queryStr = _request.getQueryString();
            if (StringUtils.isNotBlank(_queryStr)) {
                _returnUrlBuffer.append("?").append(_queryStr);
            }
            //
            String _redirectUrl = WebUtils.buildRedirectURL(context,
                    StringUtils.defaultIfBlank(WebUtils.buildRedirectCustomURL(context, null),
                            "login?redirect_url=${redirect_url}"),
                    true);
            _redirectUrl = ExpressionUtils.bind(_redirectUrl)
                    .set(Optional.REDIRECT_URL, WebUtils.encodeURL(_returnUrlBuffer.toString())).getResult();
            //
            String _message = WebUtils.i18nStr(context.getOwner(), Optional.SYSTEM_SESSION_TIMEOUT_KEY,
                    "???");
            //
            if (WebUtils.isAjax(WebContext.getRequest(), true, true)) {
                WebResult _result = WebResult.CODE(ErrorCode.USER_SESSION_INVALID_OR_TIMEOUT).msg(_message)
                        .attr(Optional.REDIRECT_URL, _redirectUrl);
                return WebResult.formatView(_result);
            }
            if (context.getContextParams().containsKey(Optional.OBSERVE_SILENCE)) {
                return View.redirectView(_redirectUrl);
            }
            return WebUtils.buildErrorView(WebContext.getContext().getOwner(),
                    ErrorCode.USER_SESSION_INVALID_OR_TIMEOUT, _message, _redirectUrl,
                    BlurObject.bind(context.getOwner().getConfig().getParam(Optional.REDIRECT_TIME_INTERVAL))
                            .toIntValue());
        } else {
            // ??
            _sessionBean.touch();
        }
        break;
    }
    return null;
}

From source file:net.ymate.framework.webmvc.intercept.UserSessionConfirmInterceptor.java

public Object intercept(InterceptContext context) throws Exception {
    switch (context.getDirection()) {
    case BEFORE:// w  ww  .j a v  a2s.c om
        UserSessionBean _sessionBean = UserSessionBean.current();
        if (_sessionBean != null && _sessionBean.isVerified()) {
            IUserSessionConfirmHandler _handler = getSessionConfirmHandler();
            if (!_handler.handle(context)) {
                HttpServletRequest _request = WebContext.getRequest();
                StringBuffer _returnUrlBuffer = _request.getRequestURL();
                String _queryStr = _request.getQueryString();
                if (StringUtils.isNotBlank(_queryStr)) {
                    _returnUrlBuffer.append("?").append(_queryStr);
                }
                //
                String _redirectUrl = WebUtils.buildRedirectURL(context,
                        StringUtils.defaultIfBlank(
                                context.getOwner().getConfig().getParam(Optional.CONFIRM_REDIRECT_URL),
                                "confirm?redirect_url=${redirect_url}"),
                        true);
                _redirectUrl = ExpressionUtils.bind(_redirectUrl)
                        .set(Optional.REDIRECT_URL, WebUtils.encodeURL(_returnUrlBuffer.toString()))
                        .getResult();
                //
                if (WebUtils.isAjax(WebContext.getRequest(), true, true)) {
                    WebResult _result = WebResult.SUCCESS().attr(Optional.REDIRECT_URL, _redirectUrl);
                    return WebResult.formatView(_result);
                }
                return View.redirectView(_redirectUrl);
            }
        }
    }
    return null;
}