All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockserver.model.ParameterBody Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.model;

import com.google.common.base.Charsets;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @author jamesdbloom
 */
public class ParameterBody extends Body {

    private final List parameters;

    public static ParameterBody params(Parameter... parameters) {
        return new ParameterBody(parameters);
    }

    public static ParameterBody params(List parameters) {
        return new ParameterBody(parameters);
    }

    public ParameterBody(Parameter... parameters) {
        this(Arrays.asList(parameters));
    }

    public ParameterBody(List parameters) {
        super(Type.PARAMETERS);
        this.parameters = parameters;
    }

    public List getParameters() {
        return parameters;
    }

    @Override
    public String toString() {
        StringBuilder body = new StringBuilder();
        List bodyParameters = parameters;
        for (int i = 0; i < bodyParameters.size(); i++) {
            Parameter parameter = bodyParameters.get(i);
            if (parameter.getValues().isEmpty()) {
                body.append(parameter.getName());
                body.append('=');
            } else {
                List values = parameter.getValues();
                for (int j = 0; j < values.size(); j++) {
                    String value = values.get(j);
                    body.append(parameter.getName());
                    body.append('=');
                    try {
                        body.append(URLEncoder.encode(value, Charsets.UTF_8.name()));
                    } catch (UnsupportedEncodingException uee) {
                        throw new RuntimeException("UnsupportedEncodingException while encoding body parameters for " + parameters, uee);
                    }
                    if (j < (values.size() - 1)) {
                        body.append('&');
                    }
                }
            }
            if (i < (bodyParameters.size() - 1)) {
                body.append('&');
            }
        }
        return body.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy