Gostaria de saber qual é o problema possível de se misturar métodos com regras de negócios em classes Vo? se tem algum padrão ou boa prática que esse tipo de coisa pode afetar? Segue um exemplo:
public class RequestHttp {
private Protocol protocol;
private String url;
private String urlParam;
private String body;
private Method method;
private String charset;
private int connTO;
private int readTO;
private List<String[]> headers;
private String proxy;
private String connectionBucket;
private boolean useCircuitBreaker;
/*
.
.
.
*/
public RequestHttp setHeader(String key, String val) {
if (key != null) {
if (val == null) {
if (this.headers != null) {
for (int i = 0; i < this.headers.size(); i++) {
String[] h = this.headers.get(i);
if (h != null && h.length == 2 && key.equalsIgnoreCase(h[0])) {
this.headers.remove(i);
break;
}
}
}
}
else {
if (this.headers == null) {
this.headers = new ArrayList<String[]>();
}
this.headers.add(new String[]{key, val});
}
}
return this;
}
public List<String[]> getHeaders() {
return (this.headers == null ? Collections.<String[]>emptyList() : this.headers);
}
public boolean isSameCall(RequestHttp other) {
if (other != null && other.getProtocol() == getProtocol() && getFullUrl() != null && getFullUrl().equals(other.getFullUrl()) && getEffectiveMethod() == other.getEffectiveMethod()) {
if (getEffectiveMethod() == Method.POST) {
return ((getBody() == null || getBody().isEmpty()) && (other.getBody() == null || other.getBody().isEmpty())) || getBody().equals(other.getBody());
}
else {
return true;
}
}
return false;
}
@Override
public String toString() {
return "RequestHttp [protocol=" + protocol + ", url=" + url + ", urlParam=" + urlParam + ", body=" + body + ", method=" + method
+ ", charset=" + charset + ", connTO=" + connTO + ", readTO=" + readTO + ", headers=" + CollectionsUtil.toString(headers, array -> Arrays.toString(array)) + "]";
}
public String toStringLine() {
if (protocol == Protocol.SMS) {
return protocol + ":" + getUrl();
}
else {
return getEffectiveMethod().name() + " " + getFullUrl() + (getBody() == null ? "" : " body=" + getBody()) + (headers == null || headers.isEmpty()? "" : " headers=" + CollectionsUtil.toString(headers, array -> Arrays.toString(array)));
}
}
public static String toStringShort(RequestHttp request) {
if (request == null) {
return "null";
}
else {
return request.toStringLine();
}
}
private static String generateField(String param, String value) {
String fieldToAdd;
value = (value == null ? "" : value);
try {
fieldToAdd = param + "=" + URLEncoder.encode(value, "UTF-8");
}
catch (UnsupportedEncodingException e) {
fieldToAdd = param + "=" + value;
}
return fieldToAdd;
}
private static String urlEncodeQueryString(String stringToEncode) {
String ret = null;
if (stringToEncode != null) {
String[] fields = stringToEncode.split("&");
for (String f: fields) {
String[] params = f.split("=",2);
String fieldToAdd;
if (params.length == 2) {
fieldToAdd = generateField(params[0], params[1]);
}
else {
fieldToAdd = generateField(params[0], "");
}
ret = (ret == null? fieldToAdd : ret + "&" + fieldToAdd);
}
}
return ret;
}
}