package org.xins.client;
import java.util.List;
import org.xins.common.service.TargetDescriptor;
import org.xins.common.text.TextUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xins.common.xml.ElementList;
public class InvalidRequestException extends StandardErrorCodeException {
InvalidRequestException(XINSCallRequest request,
TargetDescriptor target,
long duration,
XINSCallResultData resultData)
throws IllegalArgumentException {
super(request, target, duration, resultData,
determineDetail(resultData));
}
private static String determineDetail(XINSCallResultData result) {
String errorCode = result.getErrorCode();
if (errorCode == null) {
throw new IllegalArgumentException("result.getErrorCode() == null");
}
Element element = result.getDataElement();
return createMessage(element);
}
static String createMessage(Element element) {
if (element == null) {
return null;
}
StringBuffer detail = new StringBuffer(250);
ElementList missingParamElements = new ElementList(element, "missing-param");
for (Element e : missingParamElements) {
String paramName = e.getAttribute("param");
String elementName = e.getAttribute("element");
if (TextUtils.isEmpty(elementName) && !TextUtils.isEmpty(paramName)) {
detail.append("No value given for required parameter \"" + paramName + "\". ");
} else if (!TextUtils.isEmpty(elementName) && !TextUtils.isEmpty(paramName)) {
detail.append("No value given for required attribute \"" +
paramName + "\" in the element \"" + elementName + "\". ");
}
}
ElementList invalidValueElements = new ElementList(element, "invalid-value-for-type");
for (Element e : invalidValueElements) {
String paramName = e.getAttribute("param");
String invalidValue = e.getAttribute("value");
String typeName = e.getAttribute("type");
String elementName = e.getAttribute("element");
if (!TextUtils.isEmpty(elementName) && !TextUtils.isEmpty(paramName)) {
detail.append("The value ");
if (!TextUtils.isEmpty(invalidValue)) {
detail.append("\"" + invalidValue + "\" ");
}
detail.append("for the attribute \"" + paramName + "\" in the element \""
+ elementName + "\" is considered invalid for the type \""
+ typeName + "\". ");
} else if (!TextUtils.isEmpty(paramName)) {
detail.append("The value ");
if (!TextUtils.isEmpty(invalidValue)) {
detail.append("\"" + invalidValue + "\" ");
}
detail.append("for the parameter \"" + paramName
+ "\" is considered invalid for the type \"" + typeName + "\". ");
}
}
ElementList paramComboElements = new ElementList(element, "param-combo");
for (Element e : paramComboElements) {
String typeName = e.getAttribute("type");
if (TextUtils.isEmpty(typeName)) {
continue;
}
ElementList paramList = new ElementList(e, "param");
if (paramList.size() < 2) {
continue;
}
detail.append("Violated param-combo constraint of type \"");
detail.append(typeName);
detail.append("\" on parameters ");
int paramCount = paramList.size();
for (int j = 0; j < paramCount; j++) {
Element e2 = (Element) paramList.get(j);
String paramName = e2.getAttribute("name");
if (TextUtils.isEmpty(paramName)) {
continue;
}
detail.append("\"");
detail.append(paramName);
detail.append("\"");
if (j == (paramCount - 1)) {
detail.append(". ");
} else if (j == (paramCount - 2)) {
detail.append(" and ");
} else {
detail.append(", ");
}
}
}
if (detail.length() > 0) {
detail.deleteCharAt(detail.length() - 1);
return detail.toString();
} else {
return null;
}
}
}