*/
public class OsrsResponseFactory {
public OsrsResponse createResponse(OsrsRequest request, OPSEnvelope resp) throws OsrsException {
OsrsResponse response = createOsrsResponse(request.getModel());
if (resp == null) { //nothing came
response.setError(true);
return response;
}
// find error code and populate
DtAssoc dt = (DtAssoc) resp.getBody().getDataBlock().getDtAass().get(0);
List<Object> items = dt.getDtAassi();
for (Iterator<Object> iterator = items.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
if (object instanceof Item) {
Item item = (Item) object;
if ("action".equals(item.getKey())) {
response.setAction(item.getStringValue());
} else if ("object".equals(item.getKey())) {
response.setObject(item.getStringValue());
} else if ("response_text".equals(item.getKey())) {
response.setErrorMessage(item.getStringValue());
} else if ("response_code".equals(item.getKey())) {
try {
response.setErrorCode(Integer.parseInt(item.getStringValue()));
} catch (NumberFormatException e) {
response.setErrorCode(999);
}
} else if ("is_success".equals(item.getKey())) {
int tf = Integer.parseInt(item.getStringValue());
response.setError(tf==1?false:true);
} else if ("protocol".equals(item.getKey())) {
response.setProtocol(item.getStringValue());
}else {
populate(response, item);
}
}
}