Search in sources :

Example 1 with JSONObjectAdapterImpl

use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl 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)

Example 2 with JSONObjectAdapterImpl

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

the class NodeTranslationUtils method objectToBytes.

/**
 * Convert an object to bytes.
 *
 * @param encoding
 * @param annos
 * @param value
 */
public static byte[] objectToBytes(Object value, ObjectSchema schema) {
    if (value == null)
        throw new IllegalArgumentException("Value cannot be null");
    if (schema == null)
        throw new IllegalArgumentException("Schema cannot be null");
    // Is this a string
    if (TYPE.STRING == schema.getType()) {
        // String are stored as UTF-8 bytes
        try {
            // String are stored as UTF-8 bytes
            return ((String) value).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } else if (TYPE.OBJECT == schema.getType()) {
        // Extract the JSON string from this entity
        JSONEntity valueEntity = (JSONEntity) value;
        // Get the JSONString
        try {
            String jsonString = EntityFactory.createJSONStringForEntity(valueEntity);
            // Save the UTF-8 bytes of this string
            return jsonString.getBytes("UTF-8");
        } catch (JSONObjectAdapterException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } else if (TYPE.ARRAY == schema.getType()) {
        // Extract the JSON string from this entity
        Collection<JSONEntity> valueList = (Collection<JSONEntity>) value;
        // Get the JSONString
        try {
            // Build up an array using the object.
            JSONArrayAdapterImpl adapterArray = new JSONArrayAdapterImpl();
            int index = 0;
            for (JSONEntity entity : valueList) {
                adapterArray.put(index, entity.writeToJSONObject(new JSONObjectAdapterImpl()));
                index++;
            }
            String jsonString = adapterArray.toJSONString();
            // Save the UTF-8 bytes of this string
            return jsonString.getBytes("UTF-8");
        } catch (JSONObjectAdapterException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } else {
        // Unknown binary type
        throw new IllegalArgumentException("Unknown schema type: " + schema.getType() + ". Can only convert Strings, JSONEntity objects to byte[]");
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) Collection(java.util.Collection) JSONArrayAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONArrayAdapterImpl) JSONEntity(org.sagebionetworks.schema.adapter.JSONEntity) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Example 3 with JSONObjectAdapterImpl

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

the class ServletTestHelper method deserializePaginatedResults.

public static <T extends JSONEntity> PaginatedResults<T> deserializePaginatedResults(String json, Class<T> clazz) {
    try {
        PaginatedResults<T> prs = new PaginatedResults<T>(clazz);
        prs.initializeFromJSONObject(new JSONObjectAdapterImpl(json));
        return prs;
    } catch (JSONObjectAdapterException e) {
        throw new RuntimeException(e);
    }
}
Also used : PaginatedResults(org.sagebionetworks.repo.model.PaginatedResults) VariableContentPaginatedResults(org.sagebionetworks.repo.model.VariableContentPaginatedResults) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Example 4 with JSONObjectAdapterImpl

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

the class PreviewTest method testRoundTripPreview.

@Test
public void testRoundTripPreview() throws JSONObjectAdapterException {
    Preview p1 = new Preview();
    JSONObjectAdapter adapter1 = new JSONObjectAdapterImpl();
    JSONObjectAdapter adapter2 = new JSONObjectAdapterImpl();
    Date d = new Date();
    p1.setAccessControlList("/acl");
    p1.setAnnotations("/annotations");
    p1.setCreatedBy("createdBy");
    p1.setCreatedOn(d);
    p1.setDescription("description");
    p1.setEtag("1");
    p1.setId("1");
    p1.setModifiedBy("modifiedBy");
    p1.setModifiedOn(d);
    p1.setName("name");
    p1.setParentId("0");
    p1.setUri("uri");
    List<String> headers = new ArrayList<String>();
    headers.add("header1");
    headers.add("header2");
    p1.setHeaders(headers);
    p1.setPreviewString("previewString");
    List<Row> rows = new ArrayList<Row>();
    Row r = new Row();
    List<String> cells = new ArrayList<String>();
    cells.add("cell1");
    cells.add("cell2");
    r.setCells(cells);
    r.setCells(cells);
    rows.add(r);
    p1.setRows(rows);
    adapter1 = p1.writeToJSONObject(adapter1);
    String s = adapter1.toJSONString();
    adapter2 = new JSONObjectAdapterImpl(s);
    Preview p2 = new Preview(adapter2);
    assertEquals(p1, p2);
    return;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) ArrayList(java.util.ArrayList) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) Date(java.util.Date) Test(org.junit.Test)

Example 5 with JSONObjectAdapterImpl

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

the class DBOUserProfileDAOImplTest method setUp.

@Before
public void setUp() throws Exception {
    individualGroup = userGroupDAO.findGroup(TEST_USER_NAME, true);
    if (individualGroup == null) {
        individualGroup = new UserGroup();
        individualGroup.setName(TEST_USER_NAME);
        individualGroup.setIsIndividual(true);
        individualGroup.setCreationDate(new Date());
        individualGroup.setId(userGroupDAO.create(individualGroup));
    }
    String jsonString = (String) UserProfile.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
    schema = new ObjectSchema(new JSONObjectAdapterImpl(jsonString));
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) Date(java.util.Date) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) UserGroup(org.sagebionetworks.repo.model.UserGroup) Before(org.junit.Before)

Aggregations

JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)62 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)49 Test (org.junit.Test)30 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)24 JSONObject (org.json.JSONObject)19 ArrayList (java.util.ArrayList)15 Date (java.util.Date)12 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)11 PaginatedResults (org.sagebionetworks.repo.model.PaginatedResults)10 SynapseException (org.sagebionetworks.client.exceptions.SynapseException)9 VariableContentPaginatedResults (org.sagebionetworks.repo.model.VariableContentPaginatedResults)7 EntityHeader (org.sagebionetworks.repo.model.EntityHeader)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 StringEntity (org.apache.http.entity.StringEntity)5 EntityBundle (org.sagebionetworks.repo.model.EntityBundle)5 UserProfile (org.sagebionetworks.repo.model.UserProfile)5 AccessRequirement (org.sagebionetworks.repo.model.AccessRequirement)4 Annotations (org.sagebionetworks.repo.model.Annotations)4 HttpServlet (javax.servlet.http.HttpServlet)3