org.mockserver.model.StringBody 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.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.net.MediaType;
import java.nio.charset.Charset;
import static org.mockserver.mappers.ContentTypeMapper.DEFAULT_HTTP_CHARACTER_SET;
/**
* @author jamesdbloom
*/
public class StringBody extends BodyWithContentType {
private final String value;
private final byte[] rawBinaryData;
public StringBody(String value) {
this(value, (MediaType) null);
}
public StringBody(String value, Charset charset) {
this(value, (charset != null ? MediaType.create("text", "plain").withCharset(charset) : null));
}
public StringBody(String value, MediaType contentType) {
super(Type.STRING, contentType);
this.value = value;
if (value != null) {
this.rawBinaryData = value.getBytes(determineCharacterSet(contentType, DEFAULT_HTTP_CHARACTER_SET));
} else {
this.rawBinaryData = new byte[0];
}
}
public static StringBody exact(String body) {
return new StringBody(body);
}
public static StringBody exact(String body, Charset charset) {
return new StringBody(body, charset);
}
public static StringBody exact(String body, MediaType contentType) {
return new StringBody(body, contentType);
}
public String getValue() {
return value;
}
public byte[] getRawBytes() {
return rawBinaryData;
}
@Override
public String toString() {
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy