Search in sources :

Example 1 with JSONObjectAdapterException

use of org.sagebionetworks.schema.adapter.JSONObjectAdapterException in project Synapse-Repository-Services by Sage-Bionetworks.

the class BatchResults method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter adapter) throws JSONObjectAdapterException {
    if (adapter == null)
        throw new IllegalArgumentException("Adapter cannot be null");
    totalNumberOfResults = adapter.getLong("totalNumberOfResults");
    if (!adapter.isNull("results")) {
        this.results = new ArrayList<T>();
        JSONArrayAdapter array = adapter.getJSONArray("results");
        for (int i = 0; i < array.length(); i++) {
            JSONObjectAdapter childAdapter = array.getJSONObject(i);
            try {
                T newInstance = (T) factory.newInstance(clazz.getName());
                newInstance.initializeFromJSONObject(childAdapter);
                this.results.add(newInstance);
            } catch (Exception e) {
                throw new JSONObjectAdapterException(e);
            }
        }
    }
    return adapter;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Example 2 with JSONObjectAdapterException

use of org.sagebionetworks.schema.adapter.JSONObjectAdapterException in project Synapse-Repository-Services by Sage-Bionetworks.

the class PaginatedResults method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter adapter) throws JSONObjectAdapterException {
    if (adapter == null)
        throw new IllegalArgumentException("Adapter cannot be null");
    totalNumberOfResults = adapter.getLong("totalNumberOfResults");
    if (!adapter.isNull("results")) {
        this.results = new ArrayList<T>();
        JSONArrayAdapter array = adapter.getJSONArray("results");
        for (int i = 0; i < array.length(); i++) {
            JSONObjectAdapter childAdapter = array.getJSONObject(i);
            try {
                T newInstance = (T) factory.newInstance(clazz.getName());
                newInstance.initializeFromJSONObject(childAdapter);
                this.results.add(newInstance);
            } catch (Exception e) {
                throw new JSONObjectAdapterException(e);
            }
        }
    }
    if (!adapter.isNull("paging")) {
        JSONObjectAdapter pagingAdapter = adapter.getJSONObject("paging");
        this.paging = new HashMap<String, String>();
        Iterator<String> it = pagingAdapter.keys();
        while (it.hasNext()) {
            String key = it.next();
            String value = pagingAdapter.getString(key);
            this.paging.put(key, value);
        }
    }
    return adapter;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Example 3 with JSONObjectAdapterException

use of org.sagebionetworks.schema.adapter.JSONObjectAdapterException in project Synapse-Repository-Services by Sage-Bionetworks.

the class VariableContentPaginatedResults method initializeFromJSONObject.

@Override
public JSONObjectAdapter initializeFromJSONObject(JSONObjectAdapter adapter) throws JSONObjectAdapterException {
    if (adapter == null)
        throw new IllegalArgumentException("Adapter cannot be null");
    totalNumberOfResults = adapter.getLong("totalNumberOfResults");
    if (!adapter.isNull("results")) {
        this.results = new ArrayList<T>();
        JSONArrayAdapter array = adapter.getJSONArray("results");
        for (int i = 0; i < array.length(); i++) {
            JSONObjectAdapter childAdapter = array.getJSONObject(i);
            try {
                this.results.add((T) EntityClassHelper.deserialize(childAdapter));
            } catch (Exception e) {
                throw new JSONObjectAdapterException(e);
            }
        }
    }
    if (!adapter.isNull("paging")) {
        JSONObjectAdapter pagingAdapter = adapter.getJSONObject("paging");
        this.paging = new HashMap<String, String>();
        Iterator<String> it = pagingAdapter.keys();
        while (it.hasNext()) {
            String key = it.next();
            String value = pagingAdapter.getString(key);
            this.paging.put(key, value);
        }
    }
    return adapter;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Example 4 with JSONObjectAdapterException

use of org.sagebionetworks.schema.adapter.JSONObjectAdapterException in project Synapse-Repository-Services by Sage-Bionetworks.

the class MessageUtils method createMessage.

/**
 * Create an Amazon message from a ChangeMessage.  This is used for testing.
 * @param message
 * @return
 */
public static Message createMessage(ChangeMessage message, String messageId, String receiptHandle) {
    if (message == null)
        throw new IllegalArgumentException("Message cannot be null");
    try {
        Message result = new Message().withMessageId(messageId).withReceiptHandle(receiptHandle);
        result.setBody(EntityFactory.createJSONStringForEntity(message));
        return result;
    } catch (JSONObjectAdapterException e) {
        throw new RuntimeException(e);
    }
}
Also used : ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage) Message(com.amazonaws.services.sqs.model.Message) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Example 5 with JSONObjectAdapterException

use of org.sagebionetworks.schema.adapter.JSONObjectAdapterException in project Synapse-Repository-Services by Sage-Bionetworks.

the class MessageUtils method extractMessageBody.

/**
 * Extract a ChangeMessage from an Amazon Message
 * @param message
 * @return
 */
public static ChangeMessage extractMessageBody(Message message) {
    if (message == null)
        throw new IllegalArgumentException("Message cannot be null");
    try {
        JSONObject object = new JSONObject(message.getBody());
        if (object.has("objectId")) {
            // This is a message pushed directly to a queue
            JSONObjectAdapterImpl adapter = new JSONObjectAdapterImpl(object);
            return new ChangeMessage(adapter);
        }
        if (object.has("TopicArn") && object.has("Message")) {
            // This is a message that was pushed to a topic then forwarded to a queue.
            JSONObject innerObject = new JSONObject(object.getString("Message"));
            JSONObjectAdapterImpl adapter = new JSONObjectAdapterImpl(innerObject);
            return new ChangeMessage(adapter);
        } else {
            throw new IllegalArgumentException("Unknown message type: " + message.getBody());
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } catch (JSONObjectAdapterException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage) JSONException(org.json.JSONException) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Aggregations

JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)54 JSONObject (org.json.JSONObject)28 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)23 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)22 SynapseException (org.sagebionetworks.client.exceptions.SynapseException)19 IOException (java.io.IOException)7 PaginatedResults (org.sagebionetworks.repo.model.PaginatedResults)7 VariableContentPaginatedResults (org.sagebionetworks.repo.model.VariableContentPaginatedResults)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Annotations (org.sagebionetworks.repo.model.Annotations)6 JSONException (org.json.JSONException)5 DatastoreException (org.sagebionetworks.repo.model.DatastoreException)5 NamedAnnotations (org.sagebionetworks.repo.model.NamedAnnotations)4 JSONEntity (org.sagebionetworks.schema.adapter.JSONEntity)4 ChangeMessage (org.sagebionetworks.repo.model.message.ChangeMessage)3 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)3 JSONArrayAdapter (org.sagebionetworks.schema.adapter.JSONArrayAdapter)3 Field (java.lang.reflect.Field)2 List (java.util.List)2 AccessRequirement (org.sagebionetworks.repo.model.AccessRequirement)2