Java Code Examples for com.networknt.utility.StringUtils#isNotBlank()

The following examples show how to use com.networknt.utility.StringUtils#isNotBlank() . 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 File: RequestValidator.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
private Object getDeserializedValue(final HttpServerExchange exchange, final String name, final ParameterType type) {
 	if (null!=type && StringUtils.isNotBlank(name)) {
switch(type){
case QUERY:
	return OpenApiHandler.getQueryParameters(exchange,true).get(name);
case PATH:
	return OpenApiHandler.getPathParameters(exchange,true).get(name);
case HEADER:
	return OpenApiHandler.getHeaderParameters(exchange,true).get(name);
case COOKIE:
	return OpenApiHandler.getCookieParameters(exchange,true).get(name);
}   		
 	}
 	
 	return null;
 }
 
Example 2
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Removes empty parameters.</p>
 * <code>http://www.example.com/display?a=b&amp;a=&amp;c=d&amp;e=&amp;f=g
 * &rarr; http://www.example.com/display?a=b&amp;c=d&amp;f=g</code>
 * @return this instance
 */
public URLNormalizer removeEmptyParameters() {
    // Does it have query parameters?
    if (!url.contains("?")) {
        return this;
    }
    // It does, so proceed
    List<String> keyValues = new ArrayList<>();
    String queryString = StringUtils.substringAfter(url, "?");
    String[] params = StringUtils.split(queryString, '&');
    for (String param : params) {
        if (param.contains("=")
                && StringUtils.isNotBlank(
                StringUtils.substringAfter(param, "="))
                && StringUtils.isNotBlank(
                StringUtils.substringBefore(param, "="))) {
            keyValues.add(param);
        }
    }
    String cleanQueryString = StringUtils.join(keyValues, '&');
    if (StringUtils.isNotBlank(cleanQueryString)) {
        url = StringUtils.substringBefore(
                url, "?") + "?" + cleanQueryString;
    }
    return this;
}
 
Example 3
Source File: StyleParameterDeserializer.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
default Map<String, String> asExploadeMap(String str, String delimiter) {
	Map<String, String> valueMap = new HashMap<>();
	String[] items = str.split("\\"+delimiter);
	
	for (String item: items) {
		String[] tokens = item.split(Delimiters.EQUAL);
		
		String key=null;
		String value=null;
		
		if (tokens.length>0) {
			key = tokens[0];
		}
		
		if (tokens.length>1) {
			value = tokens[1];
		}
		
		if (StringUtils.isNotBlank(key)) {
			valueMap.put(key, StringUtils.trimToEmpty(value));
		}
	}
	
	return valueMap;
}
 
Example 4
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Adds a trailing slash (/) right after the domain for URLs with no
 * path, before any fragment (#) or query string (?).</p>
 *
 * <p><b>Please Note:</b> Adding a trailing slash to URLs could
 * potentially break its semantic equivalence.</p>
 * <code>http://www.example.com &rarr;
 *       http://www.example.com/</code>
 * @return this instance
 * @since 1.12.0
 */
public URLNormalizer addDomainTrailingSlash() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    if (StringUtils.isNotBlank(path)) {
        // there is a path so do nothing
        return this;
    }
    String urlRootAndPath = urlRoot + "/";
    url = StringUtils.replaceOnce(url, urlRoot, urlRootAndPath);
    return this;
}
 
Example 5
Source File: LightCluster.java    From light-4j with Apache License 2.0 5 votes vote down vote up
@Override
public void notify(URL registryUrl, List<URL> urls) {
    logger.debug("registryUrl is: {}", registryUrl);
    logger.debug("notify service: {} with updated urls: {}", serviceId, urls.toString());
    if(StringUtils.isNotBlank(serviceId)) {
        serviceMap.put(serviceId, urls == null ? new ArrayList<>() : urls);
    }
}
 
Example 6
Source File: DumpHelper.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param result a Map<String, Object> contains http request/response info which needs to be logged.
 * @param loggerFunc Consuer<T> getLoggerFuncBasedOnLevel(config.getLogLevel())
 */
private static void logResultUsingJson(Map<String, Object> result, Consumer<String> loggerFunc) {
    ObjectMapper mapper = new ObjectMapper();
    String resultJson = "";
    try {
        resultJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
    } catch (JsonProcessingException e) {
        logger.error(e.toString());
    }
    if(StringUtils.isNotBlank(resultJson)){
        loggerFunc.accept("Dump Info:\n" + resultJson);
    }
}
 
Example 7
Source File: BodyDumper.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * put bodyContent to result
 * @param result a Map<String, Object> you want to put dumping info to.
 */
@Override
protected void putDumpInfoTo(Map<String, Object> result) {
    if(StringUtils.isNotBlank(this.bodyContent)) {
        result.put(DumpConstants.BODY, this.bodyContent);
    }
}
 
Example 8
Source File: StatusCodeDumper.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * put this.statusCodeResult to result
 * @param result a Map you want to put dumping info to.
 */
@Override
protected void putDumpInfoTo(Map<String, Object> result) {
    if(StringUtils.isNotBlank(this.statusCodeResult)) {
        result.put(DumpConstants.STATUS_CODE, this.statusCodeResult);
    }
}
 
Example 9
Source File: UrlDumper.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * put this.url to result
 * @param result a Map you want to put dumping info to.
 */
@Override
protected void putDumpInfoTo(Map<String, Object> result) {
    if(StringUtils.isNotBlank(this.url)) {
        result.put(DumpConstants.URL, this.url);
    }
}
 
Example 10
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Sorts query parameters.</p>
 * <code>http://www.example.com/?z=bb&amp;y=cc&amp;z=aa &rarr;
 *       http://www.example.com/?y=cc&amp;z=bb&amp;z=aa</code>
 * @return this instance
 */
public URLNormalizer sortQueryParameters() {
    // Does it have query parameters?
    if (!url.contains("?")) {
        return this;
    }
    // It does, so proceed
    List<String> keyValues = new ArrayList<>();
    String queryString = StringUtils.substringAfter(url, "?");

    // extract and remove any fragments
    String fragment = StringUtils.substringAfter(queryString, "#");
    if (StringUtils.isNotBlank(fragment)) {
        fragment = "#" + fragment;
    }
    queryString = StringUtils.substringBefore(queryString, "#");

    String[] params = StringUtils.split(queryString, '&');
    for (String param : params) {
        keyValues.add(param);
    }
    // sort it so that query string are in order
    Collections.sort(keyValues);

    String sortedQueryString = StringUtils.join(keyValues, '&');
    if (StringUtils.isNotBlank(sortedQueryString)) {
        url = StringUtils.substringBefore(
                url, "?") + "?" + sortedQueryString + fragment;
    }

    return this;
}
 
Example 11
Source File: HttpURL.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a string representation of this URL, properly encoded.
 * @return URL as a string
 */
@Override
public String toString() {
    StringBuilder b = new StringBuilder();
    if (StringUtils.isNotBlank(protocol)) {
        b.append(protocol);
        b.append("://");
    }
    if (StringUtils.isNotBlank(host)) {
        b.append(host);
    }
    if (!isPortDefault() && port != -1) {
        b.append(':');
        b.append(port);
    }
    if (StringUtils.isNotBlank(path)) {
        // If no scheme/host/port, leave the path as is
        if (b.length() > 0 && !path.startsWith("/")) {
            b.append('/');
        }
        b.append(encodePath(path));
    }
    if (queryString != null && !queryString.isEmpty()) {
        b.append(queryString.toString());
    }
    if (fragment != null) {
        b.append("#");
        b.append(encodePath(fragment));
    }

    return b.toString();
}
 
Example 12
Source File: HeaderParameterDeserializer.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
@Override
public StyleParameterDeserializer getStyleDeserializer(String style) {
	if (StringUtils.isNotBlank(style) && !SIMPLE.equalsIgnoreCase(style)) {
		return null;
	}
	
	return new StyleParameterDeserializer() {

		@Override
		public Object deserialize(HttpServerExchange exchange, Parameter parameter, 
				ValueType valueType,
				boolean exploade) {
			Collection<String> values = exchange.getRequestHeaders().get(new HttpString(parameter.getName()));
			
			if (ValueType.ARRAY == valueType) {
				List<String> valueList = new ArrayList<>();
				
				values.forEach(v->valueList.addAll(asList(v, Delimiters.COMMA)));
				
				return valueList;			
			}else if (ValueType.OBJECT == valueType) {
				Map<String, String> valueMap = new HashMap<>();
				values.forEach(v->valueMap.putAll(exploade?asExploadeMap(v, Delimiters.COMMA):asMap(v, Delimiters.COMMA)));
				
				return valueMap;
			}
			
			return null;
		}
		
	};
}
 
Example 13
Source File: StyleParameterDeserializer.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
default String replace(String str, String target, String replacement) {
	if (StringUtils.isNotBlank(str)) {
		return str.replace(target, replacement);
	}
	
	return str;
}
 
Example 14
Source File: StyleParameterDeserializer.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
default String trimStart(String str, String start) {
	if (StringUtils.isNotBlank(str) && StringUtils.isNotBlank(start) && str.startsWith(start)) {
		int pos = start.length();
		
		return str.substring(pos);
	}
	
	return str;
}
 
Example 15
Source File: Jwt.java    From light-4j with Apache License 2.0 4 votes vote down vote up
public void setScopes(String scopesStr) {
    this.scopes = this.scopes == null ? new HashSet() : this.scopes;
    if(StringUtils.isNotBlank(scopesStr)) {
        scopes.addAll(Arrays.asList(scopesStr.split("(\\s)+")));
    }
}
 
Example 16
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Adds a trailing slash (/) to a URL ending with a directory.  A URL is
 * considered to end with a directory if the last path segment,
 * before fragment (#) or query string (?), does not contain a dot,
 * typically representing an extension.</p>
 *
 * <p><b>Please Note:</b> URLs do not always denote a directory structure
 * and many URLs can qualify to this method without truly representing a
 * directory. Adding a trailing slash to these URLs could potentially break
 * its semantic equivalence.</p>
 * <code>http://www.example.com/alice &rarr;
 *       http://www.example.com/alice/</code>
 * @return this instance
 * @since 1.11.0 (renamed from "addTrailingSlash")
 */
public URLNormalizer addDirectoryTrailingSlash() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    String urlRootAndPath = urlRoot + path;

    String name = StringUtils.substringAfterLast(path, "/");
    if (StringUtils.isNotBlank(name) && !name.contains(".")) {
        String newPath = path + "/";
        String newUrlRootAndPath = urlRoot + newPath;
        url = StringUtils.replaceOnce(
                url, urlRootAndPath, newUrlRootAndPath);
    }
    return this;
}
 
Example 17
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 3 votes vote down vote up
/**
 * <p>
 * Encodes space characters into plus signs (+) if they are part of the
 * query string. Spaces part of the URL path are percent-encoded to %20.
 * </p>
 * <p>
 * To encode all non-ASCII characters (including spaces), use
 * {@link #encodeNonURICharacters()} instead.
 * </p>
 * <code>http://www.example.com/a b c &rarr;
 *       http://www.example.com/a+b+c</code>
 * @return this instance
 * @since 1.8.0
 */
public URLNormalizer encodeSpaces() {
    String path = StringUtils.substringBefore(url, "?");
    path = StringUtils.replace(path, " ", "%20", -1);
    String qs = StringUtils.substringAfter(url, "?");
    if (StringUtils.isNotBlank(qs)) {
        qs = StringUtils.replace(qs, " ", "+", -1);
        url = path + "?" + qs;
    } else {
        url = path;
    }
    return this;
}