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

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

Introduction

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

Prototype

public static String defaultIfEmpty(String str, String defaultStr) 

Source Link

Document

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

Usage

From source file:net.ymate.framework.core.taglib.AbstractTagSupport.java

@Override
public int doStartTag() throws JspException {
    // Init/*www . jav a2  s.  c om*/
    __iterator = null;
    __sequence = 0;
    //
    Object _resultObj = doProcessTagData();
    // ?
    if (__iterator != null) {
        if (StringUtils.isBlank(getVar())) {
            throw new NullArgumentException("var");
        }
        if (__iterator.hasNext()) {
            __doProcessIteratorTagDataStatus(__iterator.next(), ++__sequence);
            return EVAL_BODY_AGAIN;
        } else {
            return SKIP_BODY;
        }
    } else {
        // ??
        if (_resultObj != null) {
            if (StringUtils.isNotBlank(getVar())) {
                switch (Scope
                        .valueOf(StringUtils.defaultIfEmpty(getScope(), Scope.PAGE.name()).toUpperCase())) {
                case APPLICATION:
                    pageContext.getServletContext().setAttribute(getVar(), _resultObj);
                    break;
                case REQUEST:
                    pageContext.getRequest().setAttribute(getVar(), _resultObj);
                    break;
                case SESSION:
                    pageContext.getSession().setAttribute(getVar(), _resultObj);
                    break;
                default:
                    pageContext.setAttribute(getVar(), _resultObj);
                }
            } else if (_resultObj instanceof String) {
                try {
                    pageContext.getOut().write(_resultObj.toString());
                } catch (IOException e) {
                    throw new JspException(e.getMessage(), RuntimeUtils.unwrapThrow(e));
                }
            }
            return EVAL_BODY_INCLUDE;
        } else {
            if (isAlways()) {
                return EVAL_BODY_INCLUDE;
            }
            return SKIP_BODY;
        }
    }
}

From source file:net.ymate.framework.core.taglib.CodecTag.java

@Override
protected Object doProcessTagData() throws JspException {
    String _result = null;//from   www. j  ava  2s .  c  o  m
    try {
        if ("base64encode".equalsIgnoreCase(method)) {
            _result = Base64.encodeBase64String(data.getBytes(StringUtils.defaultIfEmpty(charset, "UTF-8")));
        } else if ("base64decode".equalsIgnoreCase(method)) {
            _result = new String(Base64.decodeBase64(data), StringUtils.defaultIfEmpty(charset, "UTF-8"));
        } else {
            _result = DigestUtils.md5Hex(data);
        }
    } catch (UnsupportedEncodingException e) {
        throw new JspException(RuntimeUtils.unwrapThrow(e));
    }
    return _result;
}

From source file:net.ymate.framework.core.taglib.ui.LayoutTag.java

@Override
public int doAfterBody() throws JspException {
    try {/* ww w. jav  a2 s .c  o m*/
        if (this.bodyContent != null) {
            String _layoutBody = StringUtils.defaultIfEmpty(this.bodyContent.getString(), "");
            if (StringUtils.isNotBlank(__tmplContent)) {
                this.writerToBodyPart(_layoutBody);
            } else {
                __tmplContent = _layoutBody;
            }
            this.bodyContent.clearBody();
        }
    } catch (Exception e) {
        throw new JspException(RuntimeUtils.unwrapThrow(e));
    }
    return super.doAfterBody();
}

From source file:net.ymate.framework.core.taglib.ui.LayoutTag.java

@Override
public int doEndTag() throws JspException {
    try {//from w  w w .  j  av a  2s. c om
        __ui.writerToMetaPart(this.getMetaPartContent());
        __ui.writerToCssPart(this.getCssPartContent());
        __ui.writerToScriptPart(this.getScriptPartContent());
        __tmplContent = this.mergeContent(StringUtils.defaultIfEmpty(__tmplContent, ""));
        //
        if (StringUtils.isNotBlank(name) && !"body".equalsIgnoreCase(name)) {
            __ui.putProperty(name, !isCleanup() ? __tmplContent : WebUtils.replaceRegClear(__tmplContent));
        } else {
            __ui.writerToBodyPart(!isCleanup() ? __tmplContent : WebUtils.replaceRegClear(__tmplContent));
        }
    } catch (Exception e) {
        throw new JspException(RuntimeUtils.unwrapThrow(e));
    }
    //
    this.__ui = null;
    this.__tmplContent = null;
    this.name = null;
    return super.doEndTag();
}

From source file:net.ymate.framework.core.taglib.ui.UITag.java

@Override
public int doEndTag() throws JspException {
    if (__isCurrentUI) {
        try {/* w w w  . java  2 s  .  co m*/
            /* UI? */
            String __tmplContent = null;
            if (StringUtils.isNotBlank(this.getSrc())) {
                __tmplContent = WebUtils.includeJSP((HttpServletRequest) this.pageContext.getRequest(),
                        (HttpServletResponse) this.pageContext.getResponse(), this.buildSrcUrl(),
                        this.getCharsetEncoding());
            }
            __tmplContent = this.mergeContent(StringUtils.defaultIfEmpty(__tmplContent, "@{body}"));
            this.pageContext.getOut()
                    .write(!isCleanup() ? __tmplContent : WebUtils.replaceRegClear(__tmplContent));
        } catch (Exception e) {
            throw new JspException(RuntimeUtils.unwrapThrow(e));
        }
    }
    this.__isCurrentUI = false;
    return super.doEndTag();
}

From source file:net.ymate.framework.core.util.WebUtils.java

/**
 * @param request      HttpServletRequest
 * @param requestPath  //from  w w w  .  j a  va2s  .  c o m
 * @param withBasePath ???
 * @return URL
 */
public static String buildURL(HttpServletRequest request, String requestPath, boolean withBasePath) {
    requestPath = StringUtils.trimToEmpty(requestPath);
    if (withBasePath && !requestPath.equals("") && requestPath.charAt(0) == '/') {
        requestPath = StringUtils.substringAfter(requestPath, "/");
    }
    return (withBasePath ? WebUtils.baseURL(request) + requestPath : requestPath)
            + StringUtils.defaultIfEmpty(YMP.get().getConfig().getParam(Optional.REQUEST_SUFFIX), "");
}

From source file:net.ymate.framework.core.util.WebUtils.java

/**
 * @param request         HttpServletRequest
 * @param response        HttpServletResponse
 * @param jspFile         JSP// ww w  .  j a  va 2s . c  o  m
 * @param charsetEncoding ?
 * @return JSPHTML??
 * @throws ServletException ?
 * @throws IOException      ?
 */
public static String includeJSP(HttpServletRequest request, HttpServletResponse response, String jspFile,
        String charsetEncoding) throws ServletException, IOException {
    final OutputStream _output = new ByteArrayOutputStream();
    final PrintWriter _writer = new PrintWriter(new OutputStreamWriter(_output));
    final ServletOutputStream _servletOutput = new ServletOutputStream() {
        @Override
        public void write(int b) throws IOException {
            _output.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            _output.write(b, off, len);
        }
    };
    HttpServletResponse _response = new HttpServletResponseWrapper(response) {
        @Override
        public ServletOutputStream getOutputStream() {
            return _servletOutput;
        }

        @Override
        public PrintWriter getWriter() {
            return _writer;
        }
    };
    charsetEncoding = StringUtils.defaultIfEmpty(charsetEncoding, response.getCharacterEncoding());
    _response.setCharacterEncoding(StringUtils.defaultIfEmpty(charsetEncoding,
            WebMVC.get().getModuleCfg().getDefaultCharsetEncoding()));
    request.getRequestDispatcher(jspFile).include(request, _response);
    _writer.flush();
    return _output.toString();
}

From source file:net.ymate.framework.core.util.WebUtils.java

public static String messageWithTemplate(YMP owner, String title, Collection<ValidateResult> messages) {
    StringBuilder _messages = new StringBuilder();
    for (ValidateResult _vResult : messages) {
        ExpressionUtils _item = ExpressionUtils.bind(StringUtils.defaultIfEmpty(
                owner.getConfig().getParam(Optional.VALIDATION_TEMPLATE_ITEM), "${message}<br>"));
        _item.set("name", _vResult.getName());
        _item.set("message", _vResult.getMsg());
        ////from   w w w  .  j  a  v a2s.co m
        _messages.append(_item.clean().getResult());
    }
    ExpressionUtils _element = ExpressionUtils.bind(StringUtils
            .defaultIfEmpty(owner.getConfig().getParam(Optional.VALIDATION_TEMPLATE_ELEMENT), "${title}"));
    if (StringUtils.isNotBlank(title)) {
        _element.set("title", title);
    }
    return StringUtils.trimToEmpty(_element.set("items", _messages.toString()).clean().getResult());
}

From source file:net.ymate.framework.core.util.WebUtils.java

public static IView buildErrorView(IWebMvc owner, int code, String msg, String redirectUrl, int timeInterval,
        Map<String, Object> data) {
    IView _view = null;//from w  ww.j a  v a 2s. c o m
    String _errorViewPath = StringUtils
            .defaultIfEmpty(owner.getOwner().getConfig().getParam(Optional.ERROR_VIEW), "error.jsp");
    if (StringUtils.endsWithIgnoreCase(_errorViewPath, ".ftl")) {
        _view = View.freemarkerView(owner, _errorViewPath);
    } else if (StringUtils.endsWithIgnoreCase(_errorViewPath, ".vm")) {
        _view = View.velocityView(owner, _errorViewPath);
    } else {
        _view = View.jspView(owner, _errorViewPath);
    }
    _view.addAttribute("ret", code);
    _view.addAttribute("msg", msg);
    if (data != null && !data.isEmpty()) {
        _view.addAttribute("data", data);
    }
    //
    if (StringUtils.isNotBlank(redirectUrl) && timeInterval > 0) {
        _view.addHeader("REFRESH", timeInterval + ";URL=" + redirectUrl);
    }
    //
    return _view;
}

From source file:net.ymate.module.mailsender.impl.DefaultMailSendProvider.java

@Override
public IMailSendBuilder create(final MailSendServerCfgMeta serverCfgMeta) {
    return new AbstractMailSendBuilder() {
        @Override//from   w  ww .  j  a v a 2 s. c om
        public void send(final String content) throws Exception {
            __sendExecPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        MimeMessage _message = new MimeMessage(serverCfgMeta.createIfNeed());
                        //
                        for (String _to : getTo()) {
                            _message.addRecipient(Message.RecipientType.TO, new InternetAddress(_to));
                        }
                        for (String _cc : getCc()) {
                            _message.addRecipient(Message.RecipientType.CC, new InternetAddress(_cc));
                        }
                        for (String _bcc : getBcc()) {
                            _message.addRecipient(Message.RecipientType.BCC, new InternetAddress(_bcc));
                        }
                        //
                        if (getLevel() != null) {
                            switch (getLevel()) {
                            case LEVEL_HIGH:
                                _message.setHeader("X-MSMail-Priority", "High");
                                _message.setHeader("X-Priority", "1");
                                break;
                            case LEVEL_NORMAL:
                                _message.setHeader("X-MSMail-Priority", "Normal");
                                _message.setHeader("X-Priority", "3");
                                break;
                            case LEVEL_LOW:
                                _message.setHeader("X-MSMail-Priority", "Low");
                                _message.setHeader("X-Priority", "5");
                                break;
                            default:
                            }
                        }
                        //
                        String _charset = StringUtils.defaultIfEmpty(getCharset(), "UTF-8");
                        _message.setFrom(new InternetAddress(serverCfgMeta.getFromAddr(),
                                serverCfgMeta.getDisplayName(), _charset));
                        _message.setSubject(getSubject(), _charset);
                        // 
                        Multipart _container = new MimeMultipart();
                        // 
                        MimeBodyPart _textBodyPart = new MimeBodyPart();
                        if (getMimeType() == null) {
                            mimeType(IMailSender.MimeType.TEXT_PLAIN);
                        }
                        _textBodyPart.setContent(content, getMimeType().getMimeType() + ";charset=" + _charset);
                        _container.addBodyPart(_textBodyPart);
                        // ??<img src="cid:<CID_NAME>">
                        for (PairObject<String, File> _file : getAttachments()) {
                            if (_file.getValue() != null) {
                                MimeBodyPart _fileBodyPart = new MimeBodyPart();
                                FileDataSource _fileDS = new FileDataSource(_file.getValue());
                                _fileBodyPart.setDataHandler(new DataHandler(_fileDS));
                                if (_file.getKey() != null) {
                                    _fileBodyPart.setHeader("Content-ID", _file.getKey());
                                }
                                _fileBodyPart.setFileName(_fileDS.getName());
                                _container.addBodyPart(_fileBodyPart);
                            }
                        }
                        // ??
                        _message.setContent(_container);
                        Transport.send(_message);
                    } catch (Exception e) {
                        throw new RuntimeException(RuntimeUtils.unwrapThrow(e));
                    }
                }
            });
        }
    };
}