Example usage for java.util StringJoiner add

List of usage examples for java.util StringJoiner add

Introduction

In this page you can find the example usage for java.util StringJoiner add.

Prototype

public StringJoiner add(CharSequence newElement) 

Source Link

Document

Adds a copy of the given CharSequence value as the next element of the StringJoiner value.

Usage

From source file:org.wso2.identity.integration.test.util.Utils.java

public static List<NameValuePair> getConsentRequiredClaimsFromResponse(HttpResponse response) throws Exception {

    String redirectUrl = Utils.getRedirectUrl(response);
    Map<String, String> queryParams = Utils.getQueryParams(redirectUrl);
    List<NameValuePair> urlParameters = new ArrayList<>();
    String requestedClaims = queryParams.get("requestedClaims");
    String mandatoryClaims = queryParams.get("mandatoryClaims");

    String consentRequiredClaims;

    if (isNotBlank(mandatoryClaims) && isNotBlank(requestedClaims)) {
        StringJoiner joiner = new StringJoiner(",");
        joiner.add(mandatoryClaims);
        joiner.add(requestedClaims);/*from  w ww  . j  a  v a  2  s.  c o  m*/
        consentRequiredClaims = joiner.toString();
    } else if (isNotBlank(mandatoryClaims)) {
        consentRequiredClaims = mandatoryClaims;
    } else {
        consentRequiredClaims = requestedClaims;
    }

    String[] claims;
    if (isNotBlank(consentRequiredClaims)) {
        claims = consentRequiredClaims.split(",");
    } else {
        claims = new String[0];
    }

    for (String claim : claims) {
        if (isNotBlank(claim)) {
            String[] claimMeta = claim.split("_", 2);
            if (claimMeta.length == 2) {
                urlParameters.add(new BasicNameValuePair("consent_" + claimMeta[0], "on"));
            }
        }
    }
    return urlParameters;
}

From source file:org.wso2.identity.integration.test.util.Utils.java

public static HttpResponse sendPOSTConsentMessage(HttpResponse response, String commonAuthUrl, String userAgent,
        String referer, HttpClient httpClient, String pastreCookie) throws Exception {
    String redirectUrl = getRedirectUrl(response);
    Map<String, String> queryParams = getQueryParams(redirectUrl);

    String sessionKey = queryParams.get("sessionDataKey");
    String mandatoryClaims = queryParams.get("mandatoryClaims");
    String requestedClaims = queryParams.get("requestedClaims");
    String consentRequiredClaims;

    if (isNotBlank(mandatoryClaims) && isNotBlank(requestedClaims)) {
        StringJoiner joiner = new StringJoiner(",");
        joiner.add(mandatoryClaims);
        joiner.add(requestedClaims);/* www.j ava  2 s  .  c om*/
        consentRequiredClaims = joiner.toString();
    } else if (isNotBlank(mandatoryClaims)) {
        consentRequiredClaims = mandatoryClaims;
    } else {
        consentRequiredClaims = requestedClaims;
    }

    String[] claims;
    if (isNotBlank(consentRequiredClaims)) {
        claims = consentRequiredClaims.split(",");
    } else {
        claims = new String[0];
    }

    HttpPost post = new HttpPost(commonAuthUrl);
    post.setHeader("User-Agent", userAgent);
    post.addHeader("Referer", referer);
    post.addHeader("Cookie", pastreCookie);
    List<NameValuePair> urlParameters = new ArrayList<>();

    for (int i = 0; i < claims.length; i++) {

        if (isNotBlank(claims[i])) {
            String[] claimMeta = claims[i].split("_", 2);
            if (claimMeta.length == 2) {
                urlParameters.add(new BasicNameValuePair("consent_" + claimMeta[0], "on"));
            }
        }
    }
    urlParameters.add(new BasicNameValuePair("sessionDataKey", sessionKey));
    urlParameters.add(new BasicNameValuePair("consent", "approve"));
    post.setEntity(new UrlEncodedFormEntity(urlParameters));
    return httpClient.execute(post);
}

From source file:org.trellisldp.rosid.file.FileUtils.java

/**
 * Partition an identifier into a directory structure
 * @param identifier the identifier/*from  w w w. j a  v  a 2  s . com*/
 * @return a string usable as a directory path
 */
public static String partition(final String identifier) {
    requireNonNull(identifier, "identifier must not be null!");

    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(identifier.getBytes(UTF_8));
    final String intermediate = Long.toHexString(hasher.getValue());

    range(0, intermediate.length() / LENGTH).limit(MAX)
            .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH)));

    joiner.add(md5Hex(identifier));
    return joiner.toString();
}

From source file:org.wso2.identity.scenarios.commons.util.SSOUtil.java

/**
 * Post user consent to Consent page//from  w w w. ja  va  2s.co m
 *
 * @param response      HttpResponse to be referred for request sending.
 * @param commonAuthUrl commonauth URL
 * @param userAgent     user-Agent
 * @param referer       referer URL
 * @param httpClient    HttpClient to be used for request sending.
 * @param pastreCookie  'pastre' Cookie
 * @return response after submitting consent
 * @throws Exception
 */
public static HttpResponse sendPOSTConsentMessage(HttpResponse response, String commonAuthUrl, String userAgent,
        String referer, HttpClient httpClient, String pastreCookie) throws Exception {
    String redirectUrl = getRedirectUrlFromResponse(response);
    Map<String, String> queryParams = getQueryParams(redirectUrl);

    String sessionKey = queryParams.get(PARAM_SESSION_DATA_KEY);
    String mandatoryClaims = queryParams.get(PARAM_MANDATORY_CLAIMS);
    String requestedClaims = queryParams.get(PARAM_REQUESTED_CLAIMS);
    String consentRequiredClaims;

    if (isNotBlank(mandatoryClaims) && isNotBlank(requestedClaims)) {
        StringJoiner joiner = new StringJoiner(",");
        joiner.add(mandatoryClaims);
        joiner.add(requestedClaims);
        consentRequiredClaims = joiner.toString();
    } else if (isNotBlank(mandatoryClaims)) {
        consentRequiredClaims = mandatoryClaims;
    } else {
        consentRequiredClaims = requestedClaims;
    }

    String[] claims;
    if (isNotBlank(consentRequiredClaims)) {
        claims = consentRequiredClaims.split(",");
    } else {
        claims = new String[0];
    }

    HttpPost post = new HttpPost(commonAuthUrl);
    post.setHeader(HttpHeaders.USER_AGENT, userAgent);
    post.addHeader(HttpHeaders.REFERER, referer);
    post.addHeader(COOKIE, pastreCookie);
    List<NameValuePair> urlParameters = new ArrayList<>();

    for (int i = 0; i < claims.length; i++) {

        if (isNotBlank(claims[i])) {
            String[] claimMeta = claims[i].split("_", 2);
            if (claimMeta.length == 2) {
                urlParameters.add(new BasicNameValuePair("consent_" + claimMeta[0], "on"));
            }
        }
    }
    urlParameters.add(new BasicNameValuePair(PARAM_SESSION_DATA_KEY, sessionKey));
    urlParameters.add(new BasicNameValuePair("consent", "approve"));
    post.setEntity(new UrlEncodedFormEntity(urlParameters));
    return httpClient.execute(post);
}

From source file:org.trellisldp.file.FileUtils.java

/**
 * Get a directory for a given resource identifier.
 * @param baseDirectory the base directory
 * @param identifier a resource identifier
 * @return a directory//w  w w.j  a  v a  2s.c o m
 */
public static File getResourceDirectory(final File baseDirectory, final IRI identifier) {
    requireNonNull(baseDirectory, "The baseDirectory may not be null!");
    requireNonNull(identifier, "The identifier may not be null!");
    final String id = identifier.getIRIString();
    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(id.getBytes(UTF_8));
    final String intermediate = Long.toHexString(hasher.getValue());

    range(0, intermediate.length() / LENGTH).limit(MAX)
            .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH)));

    joiner.add(md5Hex(id));
    return new File(baseDirectory, joiner.toString());
}

From source file:org.elasticsearch.client.Request.java

/**
 * Utility method to build request's endpoint.
 *///from ww w.  j  a  va  2s.com
static String endpoint(String... parts) {
    if (parts == null || parts.length == 0) {
        return DELIMITER;
    }

    StringJoiner joiner = new StringJoiner(DELIMITER, DELIMITER, "");
    for (String part : parts) {
        if (part != null) {
            joiner.add(part);
        }
    }
    return joiner.toString();
}

From source file:org.ballerinalang.langserver.command.CommandUtil.java

/**
 * Get the object constructor snippet generated from public object fields.
 *
 * @param fields     List of Fields// w w  w . j av a  2s  .c  o m
 * @param baseOffset Offset of snippet
 * @return {@link String}   Constructor snippet as String
 */
public static String getObjectConstructorSnippet(List<BLangSimpleVariable> fields, int baseOffset) {
    StringJoiner funcFields = new StringJoiner(", ");
    StringJoiner funcBody = new StringJoiner(CommonUtil.LINE_SEPARATOR);
    String offsetStr = String.join("", Collections.nCopies(baseOffset, " "));
    fields.stream().filter(bField -> ((bField.symbol.flags & Flags.PUBLIC) != Flags.PUBLIC)).forEach(var -> {
        funcFields.add(generateTypeDefinition(null, null, var) + " " + var.name.value);
        funcBody.add(offsetStr + "    self." + var.name.value + " = " + var.name.value + ";");
    });

    return offsetStr + "public function __init(" + funcFields.toString() + ") {" + CommonUtil.LINE_SEPARATOR
            + funcBody.toString() + CommonUtil.LINE_SEPARATOR + offsetStr + "}" + CommonUtil.LINE_SEPARATOR;
}

From source file:org.ballerinalang.langserver.command.testgen.ValueSpaceGenerator.java

/**
 * Returns a value space for a given BLangNode.
 *
 * @param importsAcceptor imports acceptor
 * @param currentPkgId    current package id
 * @param bLangNode       BLangNode to evaluate
 * @param template        templates to be modified
 * @return {@link String}  modified templates
 * @see #createTemplateArray(int)/* w  ww.j av  a  2 s .  c  om*/
 */
public static String[] getValueSpaceByNode(BiConsumer<String, String> importsAcceptor, PackageID currentPkgId,
        BLangNode bLangNode, String[] template) {
    if (bLangNode.type == null && bLangNode instanceof BLangTupleDestructure) {
        // Check for tuple assignment eg. (int, int)
        List<BLangExpression> varRefs = ((BLangTupleDestructure) bLangNode).getVariableRefs();
        String[][] list = new String[varRefs.size()][template.length];
        IntStream.range(0, varRefs.size()).forEach(j -> {
            BLangExpression bLangExpression = varRefs.get(j);
            if (bLangExpression.type != null) {
                String[] values = getValueSpaceByType(importsAcceptor, currentPkgId, bLangExpression.type,
                        createTemplateArray(template.length));
                IntStream.range(0, values.length).forEach(i -> list[i][j] = values[i]);
            }
        });
        IntStream.range(0, template.length).forEach(index -> {
            template[index] = template[index].replace(PLACE_HOLDER, "(" + String.join(", ", list[index]) + ")");
        });
        return template;
    } else if (bLangNode instanceof BLangLiteral) {
        return (String[]) Stream.of(template)
                .map(s -> s.replace(PLACE_HOLDER, ((BLangLiteral) bLangNode).getValue().toString())).toArray();
    } else if (bLangNode instanceof BLangAssignment) {
        return (String[]) Stream.of(template).map(s -> s.replace(PLACE_HOLDER, "0")).toArray();
    } else if (bLangNode instanceof BLangFunctionTypeNode) {
        BLangFunctionTypeNode funcType = (BLangFunctionTypeNode) bLangNode;
        TestFunctionGenerator generator = new TestFunctionGenerator(importsAcceptor, currentPkgId, funcType);
        StringJoiner params = new StringJoiner(", ");
        String[][] valueSpace = generator.getValueSpace();
        String[] typeSpace = generator.getTypeSpace();
        String[] nameSpace = generator.getNamesSpace();
        IntStream.range(0, typeSpace.length - 1).forEach(index -> {
            String type = typeSpace[index];
            String name = nameSpace[index];
            params.add(type + " " + name);
        });
        String returnType = "(" + typeSpace[typeSpace.length - 1] + ")";
        IntStream.range(0, template.length).forEach(index -> {
            String builder = "function (" + params.toString() + ") returns " + returnType + "{ "
                    + ((valueSpace != null) ? "return " + valueSpace[index][funcType.params.size()] : "")
                    + "; }";
            template[index] = template[index].replace(PLACE_HOLDER, builder);
        });
        return template;
    }
    return (bLangNode.type != null)
            ? getValueSpaceByType(importsAcceptor, currentPkgId, bLangNode.type, template)
            : template;
}

From source file:org.briljantframework.array.ArrayPrinter.java

/**
 * Format the {@link org.briljantframework.array.ArrayPrinter.ToStringArray}
 *
 * @param sb write resulting string representation to
 * @param arr the ToStringMatrix//w ww  .j  a va  2 s.co m
 * @throws java.io.IOException if an IO error occurs
 */
public static void print(Appendable sb, ToStringArray arr, String startChar, String endChar, boolean truncate,
        int dims, IntArray maxWidth) throws IOException {
    if (arr.size() == 0) {
        sb.append(startChar).append(arr.get(0)).append(endChar);
        return;
    }
    if (arr.dims() == 1) {
        StringJoiner joiner = new StringJoiner(", ", startChar, endChar);
        int max = arr.size();
        if (truncate) {
            max = visiblePerSlice < 0 ? arr.size() : visiblePerSlice;
        }
        for (int i = 0; i < arr.size(); i++) {
            String value = pad(arr.get(i), maxWidth.get(i % max));
            joiner.add(value);
            if (i >= max) {
                int left = arr.size() - i - 1;
                if (left > max) {
                    i += left - max - 1;
                    joiner.add("...");
                }
            }
        }
        sb.append(joiner.toString());
    } else {
        int len = arr.size(0);
        sb.append("[");
        print(sb, arr.select(0), startChar, endChar, truncate, dims, maxWidth);
        for (int i = 1; i < len; i++) {
            if (arr.dims() > 2) {
                sb.append(",\n\n");
            } else {
                sb.append(",\n");
            }
            for (int j = 0; j < dims - arr.dims(); j++) {
                sb.append(" ");
            }
            print(sb, arr.select(i), startChar, endChar, truncate, dims, maxWidth);
            int max = len;
            if (truncate) {
                max = printSlices < 0 ? len : printSlices;
            }
            if (i >= max) {
                int left = len - i - 1;
                if (left > max) {
                    i += left - max - 1;
                    sb.append(",\n");
                    for (int j = 0; j < dims - arr.dims(); j++) {
                        sb.append(" ");
                    }
                    sb.append("...");
                }
            }
        }
        sb.append("]");
    }
}

From source file:org.eclipse.winery.tools.copybaragenerator.CopybaraGenerator.java

public String generateCopybaraConfigFile() {
    StringJoiner copybaraConfig = new StringJoiner("\n");
    copybaraConfig.add("urlOrigin = \"https://github.com/OpenTOSCA/tosca-definitions-internal.git\"");
    copybaraConfig.add("urlDestination = \"file:///tmp/copybara/tosca-definitions-public\"");
    copybaraConfig.add("core.workflow(");
    copybaraConfig.add("    name = \"default\",");
    copybaraConfig.add("    origin = git.origin(");
    copybaraConfig.add("        url = urlOrigin,");
    copybaraConfig.add("        ref = \"master\",");
    copybaraConfig.add("    ),");
    copybaraConfig.add("    destination = git.destination(");
    copybaraConfig.add("        url = urlDestination,");
    copybaraConfig.add("        fetch = \"master\",");
    copybaraConfig.add("        push = \"master\",");
    copybaraConfig.add("    ),");
    copybaraConfig//from  w w  w .j a  v a 2 s  .  c om
            .add("    authoring = authoring.pass_thru(\"OpenTOSCA Bot <opentosca@iaas.uni-stuttgart.de>\"),");
    copybaraConfig.add("    " + generateOriginFilesConfig());
    copybaraConfig.add("    destination_files = glob([\"**\"], exclude = [\"README_INTERNAL.md\"]),");
    copybaraConfig.add(")");
    return copybaraConfig.toString();
}