org.mockserver.model.ParameterBody Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockserver-core Show documentation
Show all versions of mockserver-core Show documentation
Functionality used by all MockServer modules for matching and expectations
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