com.formkiq.server.util.Strings Maven / Gradle / Ivy
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formkiq.server.util;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;
/**
* String helper utilities.
*
*/
public final class Strings {
/** Default deliminator to use to encode strings. */
public static final String DEFAULT_DELIM = ":";
/** Regex to extract label / value. */
private static final Pattern EXTRACT_LABEL_VALUE = Pattern
.compile("^(.*)\\[(.*)\\]$");
/**
* Decodes string using deliminator and Base64.
* @param base64 {@link String}
* @return String[]
*/
public static String decode(final String base64) {
byte[] bytes = Base64.decodeBase64(base64);
String s = new String(bytes, StandardCharsets.UTF_8);
return s;
}
/**
* Decodes string using deliminator and Base64.
* @param base64 {@link String}
* @param deliminator {@link String}
* @return String[]
*/
public static String[] decode(final String base64,
final String deliminator) {
return decode(base64).split(deliminator);
}
/**
* Encodes two strings together using deliminator and Base64.
* @param s0 {@link String}
* @param s1 {@link String}
* @param deliminator {@link String}
* @return {@link String}
*/
public static String encode(final String s0, final String s1,
final String deliminator) {
String encoded = s0 + deliminator + s1;
byte[] bytes = Base64
.encodeBase64(encoded.getBytes(StandardCharsets.UTF_8));
return new String(bytes, StandardCharsets.UTF_8);
}
/**
* Generate Token based off of Offset / Max Results.
* @param offset int
* @param maxResults int
* @return {@link String}
*/
public static String generateOffsetToken(final int offset,
final int maxResults) {
return encode("" + offset, "" + maxResults, DEFAULT_DELIM);
}
/**
* Get Max Results from Token.
* @param token {@link String}
* @param defaultMaxResults int
* @return int
*/
public static int getMaxResults(final String token,
final int defaultMaxResults) {
try {
String s = decode(token, DEFAULT_DELIM)[1];
return Integer.parseInt(s);
} catch (NullPointerException | ArrayIndexOutOfBoundsException
| NumberFormatException e) {
return defaultMaxResults;
}
}
/**
* Get Offset from token.
* @param token {@link String}
* @return int
*/
public static int getOffset(final String token) {
try {
String s = decode(token, DEFAULT_DELIM)[0];
return Integer.parseInt(s);
} catch (NullPointerException | ArrayIndexOutOfBoundsException
| NumberFormatException e) {
return 0;
}
}
/**
* private constructor.
*/
private Strings() {
}
/**
* Extract Label / Value from String.
* @param s {@link String}
* @return String[]
*/
public static String[] extractLabelAndValue(final String s) {
if (!StringUtils.isEmpty(s)) {
Matcher m = EXTRACT_LABEL_VALUE.matcher(s);
if (m.matches()) {
return new String[] { m.group(1), m.group(2)};
}
return new String[] { s, s };
}
return new String[] { "", "" };
}
}