Example usage for org.apache.commons.lang3 StringUtils defaultString

List of usage examples for org.apache.commons.lang3 StringUtils defaultString

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils defaultString.

Prototype

public static String defaultString(final String str) 

Source Link

Document

Returns either the passed in String, or if the String is null , an empty String ("").

 StringUtils.defaultString(null)  = "" StringUtils.defaultString("")    = "" StringUtils.defaultString("bat") = "bat" 

Usage

From source file:l2next.gameserver.network.serverpackets.SystemMessage.java

public SystemMessage addString(String text) {
    args.add(new Arg(TYPE_TEXT, StringUtils.defaultString(text)));
    return this;
}

From source file:net.xby1993.common.util.ExceptionUtil.java

/**
 *  ??: ? <-- RootCause??: ?//from  w  w w  .ja v a  2s  .c  om
 */
public static String toStringWithRootCause(Throwable t) {
    if (t == null) {
        return StringUtils.EMPTY;
    }

    final String clsName = ClassUtils.getShortClassName(t, null);
    final String message = StringUtils.defaultString(t.getMessage());
    Throwable cause = getRootCause(t);

    StringBuilder sb = new StringBuilder(128).append(clsName).append(": ").append(message);
    if (cause != t) {
        sb.append("; <---").append(toStringWithShortName(cause));
    }

    return sb.toString();
}

From source file:nl.armatiek.xslweb.web.filter.WebAppFilter.java

public static WebApp getWebApp(ServletRequest request) {
    HttpServletRequest req = (HttpServletRequest) request;
    String path = StringUtils.defaultString(req.getPathInfo()) + req.getServletPath();
    return Context.getInstance().getWebApp(path);
}

From source file:nl.armatiek.xslweb.web.filter.WebAppFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    String path = StringUtils.defaultString(req.getPathInfo()) + req.getServletPath();
    WebApp webApp = getWebApp(request);//  w ww  .  j  a v a  2  s. c  o  m
    if (webApp == null) {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    } else if (webApp.isClosed()) {
        resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    } else {
        req.setAttribute(Definitions.ATTRNAME_WEBAPP, webApp);
        Resource resource = webApp.matchesResource(webApp.getRelativePath(path));
        if (resource == null) {
            chain.doFilter(request, response);
        } else {
            resp.setContentType(resource.getMediaType());
            File file = webApp.getStaticFile(path);
            if (!file.isFile()) {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            } else {
                Date currentDate = new Date();
                long now = currentDate.getTime();
                long duration = resource.getDuration().getTimeInMillis(currentDate);
                resp.addHeader("Cache-Control", "max-age=" + duration / 1000);
                resp.setDateHeader("Expires", now + duration);
                FileUtils.copyFile(file, resp.getOutputStream());
            }
        }
    }
}

From source file:no.kantega.publishing.common.data.attributes.ListAttribute.java

@Override
public void setConfig(Element config, Map<String, String> model)
        throws InvalidTemplateException, SystemException {
    super.setConfig(config, model);

    if (config != null) {
        String multiple = config.getAttribute("multiple");
        if ("true".equalsIgnoreCase(multiple)) {
            this.multiple = true;
        }/* w w w .  j  ava 2 s. c o  m*/

        key = StringUtils.defaultString(config.getAttribute("key"));

        options = new ArrayList<>();

        try {
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList nodes = (NodeList) xpath.evaluate("options/option", config, XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) {
                Element elmOption = (Element) nodes.item(i);
                String optText = elmOption.getFirstChild().getNodeValue();
                String optVal = elmOption.getAttribute("value");
                String optSel = elmOption.getAttribute("selected");
                ListOption option = new ListOption();
                option.setText(optText);
                option.setValue(optVal);
                if ("true".equalsIgnoreCase(optSel)) {
                    option.setDefaultSelected(true);
                }
                options.add(option);
            }

        } catch (XPathExpressionException e) {
            log.error("Error getting list options", e);
        }
        ignoreVariant = Boolean.valueOf(config.getAttribute("ignorevariant"));
    }
}

From source file:org.apache.brooklyn.util.core.json.ShellEnvironmentSerializer.java

private String serializeShellEnv(Object value) {
    return StringUtils.defaultString(serialize(value));
}

From source file:org.apache.marmotta.platform.sparql.webservices.SparqlWebService.java

/**
 * Execute a SPARQL 1.1 Update request using update via URL-encoded POST; 
 * see details at http://www.w3.org/TR/sparql11-protocol/\#update-operation
 * //from w  w w  .j a  va  2 s  .  co m
 * @param request the servlet request (to retrieve the SPARQL 1.1 Update query passed in the
 *            body of the POST request)
 * @HTTP 200 in case the update was carried out successfully
 * @HTTP 400 in case the update query is missing or invalid
 * @HTTP 500 in case the update was not successful
 * @return empty content in case the update was successful, the error message in case an error
 *         occurred
 */
@POST
@Path(UPDATE)
@Consumes({ "application/x-www-url-form-urlencoded", "application/x-www-form-urlencoded" })
public Response updatePostUrlEncoded(@Context HttpServletRequest request) {
    try {
        Map<String, String> params = parseEncodedQueryParameters(CharStreams.toString(request.getReader()));
        String q = StringUtils.defaultString(params.get("update"));
        String resultType = StringUtils.defaultString(params.get("output"));
        return update(q, resultType, request);
    } catch (IOException e) {
        return Response.serverError().entity(WebServiceUtil.jsonErrorResponse(e)).build();
    }
}

From source file:org.apache.sling.contextaware.config.resource.impl.def.WebConsolePlugin.java

private void printResolutionTestTool(HttpServletRequest request, PrintWriter pw) {
    final String path = this.getParameter(request, "path", null);
    final String item = this.getParameter(request, "item", ".");
    final String user = this.getParameter(request, "user", null);

    ResourceResolver resolver = null;/*from  w  w w .  j  a  va2 s  .c  o m*/
    try {
        Resource content = null;
        if (path != null) {
            resolver = getResolver(user);
            if (resolver != null) {
                content = resolver.getResource(path);
            }
        }

        pw.println("<form method='get'>");

        tableStart(pw, "Test Configuration Resolution", 2);
        pw.println("<td style='width:20%'>Content Path</td>");
        pw.print("<td><input name='path' value='");
        pw.print(xssAPI.encodeForHTMLAttr(StringUtils.defaultString(path)));
        pw.println("' style='width:100%'/>");
        if (resolver != null && content == null) {
            pw.println("<div>");
            pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
            pw.println("<span style='float:left'>Path does not exist.</span>");
            pw.println("</div>");
        }
        pw.println("</td>");

        tableRows(pw);
        pw.println("<td>Item</td>");
        pw.print("<td><input name='item' value='");
        pw.print(xssAPI.encodeForHTMLAttr(item));
        pw.println("' style='width:100%'/></td>");
        tableRows(pw);

        pw.println("<td>User</td>");
        pw.println("<td><input name='user' value='");
        pw.print(xssAPI.encodeForHTMLAttr(StringUtils.defaultString(user)));
        pw.println("' style='width:50%'/>");
        if (path != null && resolver == null) {
            pw.println("<div>");
            pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
            pw.println("<span style='float:left'>User does not exist.</span>");
            pw.println("</div>");
        }
        pw.println("</td>");
        tableRows(pw);

        pw.println("<td></td>");
        pw.println("<td><input type='submit' value='Resolve'/></td>");
        tableEnd(pw);

        pw.println("</form>");

        pw.println("<br/>");

        if (content != null) {

            // TODO: use sensible bucket name or make it configurable
            final Resource confRsrc = configResolver.getResource(content, "sling:configs", item);

            tableStart(pw, "Resolved", 2);
            pw.println("<td style='width:20%'>Code</td>");
            pw.println("<td>");
            pw.print("<code>resolve(\"");
            pw.print(xssAPI.encodeForHTML(content.getPath()));
            pw.print("\", \"");
            pw.print(xssAPI.encodeForHTML(item));
            pw.println("\")</code>");
            pw.println("<br/>&nbsp;");
            pw.println("</td>");
            tableRows(pw);

            pw.println("<td style='width:20%'>Item</td>");
            if (confRsrc != null) {
                pw.print("<td>");
                pw.print(xssAPI.encodeForHTML(confRsrc.getPath()));
                pw.println("<br/>&nbsp;</td>");
            } else {
                pw.println("<td>");
                pw.println("<div>");
                pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
                pw.println("<span style='float:left'>No matching item found.</span>");
                pw.println("</div>");
                pw.println("<br/>&nbsp;</td>");
            }
            tableRows(pw);

            pw.println("<td>Config paths</td>");

            pw.println("<td>");
            Iterator<String> paths = ((DefaultConfigurationResourceResolvingStrategy) configResolver)
                    .getResolvePaths(content);
            while (paths.hasNext()) {
                String p = paths.next();
                if (confRsrc != null && confRsrc.getPath().startsWith(p + "/")) {
                    pw.print("<b>");
                    pw.print(xssAPI.encodeForHTML(p));
                    pw.println("</b>");
                } else {
                    pw.println(xssAPI.encodeForHTML(p));
                }
                pw.println("<br/>");
            }
            pw.println("</td>");

            tableEnd(pw);
        }

    } finally {
        if (resolver != null && user != null) {
            resolver.close();
        }
    }
}

From source file:org.apache.sling.testing.mock.jcr.MockQueryManager.java

@Override
public Query createQuery(String statement, String language) throws RepositoryException {
    if (!SUPPORTED_QUERY_LANGUAGES.contains(StringUtils.defaultString(language))) {
        throw new InvalidQueryException("Unsupported query language: " + language);
    }/*w w w  .j  a v  a2s. c om*/
    return new MockQuery(this, statement, language);
}

From source file:org.apache.struts2.views.java.Attributes.java

public Attributes add(String key, String value, boolean encode) {
    put(key, (encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(value)) : value));
    return this;
}