Java Code Examples for com.eclipsesource.json.JsonObject#readFrom()

The following examples show how to use com.eclipsesource.json.JsonObject#readFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BoxFile.java    From box-java-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public BoxItem.Info move(BoxFolder destination, String newName) {
    URL url = FILE_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
    BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");

    JsonObject parent = new JsonObject();
    parent.add("id", destination.getID());

    JsonObject updateInfo = new JsonObject();
    updateInfo.add("parent", parent);
    if (newName != null) {
        updateInfo.add("name", newName);
    }

    request.setBody(updateInfo.toString());
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    BoxFile movedFile = new BoxFile(this.getAPI(), responseJSON.get("id").asString());
    return movedFile.new Info(responseJSON);
}
 
Example 2
Source File: BoxFile.java    From box-java-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a list of any tasks on this file with requested fields.
 *
 * @param fields optional fields to retrieve for this task.
 * @return a list of tasks on this file.
 */
public List<BoxTask.Info> getTasks(String... fields) {
    QueryStringBuilder builder = new QueryStringBuilder();
    if (fields.length > 0) {
        builder.appendParam("fields", fields).toString();
    }
    URL url = GET_TASKS_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());

    int totalCount = responseJSON.get("total_count").asInt();
    List<BoxTask.Info> tasks = new ArrayList<BoxTask.Info>(totalCount);
    JsonArray entries = responseJSON.get("entries").asArray();
    for (JsonValue value : entries) {
        JsonObject taskJSON = value.asObject();
        BoxTask task = new BoxTask(this.getAPI(), taskJSON.get("id").asString());
        BoxTask.Info info = task.new Info(taskJSON);
        tasks.add(info);
    }

    return tasks;
}
 
Example 3
Source File: BoxGroup.java    From box-java-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Gets information about all of the collaborations for this group.
 * @return a collection of information about the collaborations for this group.
 */
public Collection<BoxCollaboration.Info> getCollaborations() {
    BoxAPIConnection api = this.getAPI();
    URL url = COLLABORATIONS_URL_TEMPLATE.build(api.getBaseURL(), this.getID());

    BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());

    int entriesCount = responseJSON.get("total_count").asInt();
    Collection<BoxCollaboration.Info> collaborations = new ArrayList<BoxCollaboration.Info>(entriesCount);
    JsonArray entries = responseJSON.get("entries").asArray();
    for (JsonValue entry : entries) {
        JsonObject entryObject = entry.asObject();
        BoxCollaboration collaboration = new BoxCollaboration(api, entryObject.get("id").asString());
        BoxCollaboration.Info info = collaboration.new Info(entryObject);
        collaborations.add(info);
    }

    return collaborations;
}
 
Example 4
Source File: BoxCommentTest.java    From box-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructorWithJsonObjectParameter() {
    // given
    String expectedType = "comment";
    Date expectedCreatedAt = new Date();
    Date expectedModifiedAt = new Date();
    Boolean expectedIsReplyComment = false;
    String expectedMessage = "These tigers are cool!";
    String expectedTaggedMessage = "These tigers are cool!";
    BoxUser expectedCreatedBy = BoxUser.createFromId("1");
    BoxItem expectedItem = BoxFile.createFromId("1");

    String commentJson =   "{" +
            "\"type\":\"" + expectedType + "\"," +
            "\"is_reply_comment\":" + expectedIsReplyComment + "," +
            "\"message\":\"" + expectedMessage + "\"," +
            "\"tagged_message\":\"" + expectedTaggedMessage + "\"," +
            "\"created_by\":" + expectedCreatedBy.toJson() + "," +
            "\"created_at\":\"" + BoxDateFormat.format(expectedCreatedAt) + "\"," +
            "\"modified_at\":\"" + BoxDateFormat.format(expectedModifiedAt) + "\"," +
            "\"item\":" + expectedItem.toJson() + "}";

    JsonObject jsonObj = JsonObject.readFrom(commentJson);

    // when
    BoxComment comment = new BoxComment(jsonObj);

    // then
    Assert.assertEquals(expectedType, comment.getType());
    DateUtil.assertSameDateSecondPrecision(expectedCreatedAt, comment.getCreatedAt());
    DateUtil.assertSameDateSecondPrecision(expectedModifiedAt, comment.getModifiedAt());
    Assert.assertEquals(expectedMessage, comment.getMessage());
    Assert.assertEquals(expectedCreatedBy, comment.getCreatedBy());
    Assert.assertEquals(expectedTaggedMessage, comment.getTaggedMessage());
    Assert.assertEquals(expectedItem, comment.getItem());
    Assert.assertEquals(expectedIsReplyComment, comment.getIsReplyComment());
}
 
Example 5
Source File: BoxFileVersionLegalHold.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * @param fields the fields to retrieve.
 * @return information about this file version legal hold.
 */
public BoxFileVersionLegalHold.Info getInfo(String ... fields) {
    QueryStringBuilder builder = new QueryStringBuilder();
    if (fields.length > 0) {
        builder.appendParam("fields", fields);
    }
    URL url = FILE_VERSION_HOLD_URL_TEMPLATE.buildWithQuery(
            this.getAPI().getBaseURL(), builder.toString(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    return new Info(responseJSON);
}
 
Example 6
Source File: BoxGroup.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Gets information about this group.
 * @param fields the fields to retrieve.
 * @return info about this group.
 */
public Info getInfo(String ... fields) {
    QueryStringBuilder builder = new QueryStringBuilder();
    if (fields.length > 0) {
        builder.appendParam("fields", fields);
    }
    URL url = GROUP_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    return new Info(responseJSON);
}
 
Example 7
Source File: BoxItem.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an item that was shared with a password-protected shared link.
 * @param  api        the API connection to be used by the shared item.
 * @param  sharedLink the shared link to the item.
 * @param  password   the password for the shared link.
 * @return            info about the shared item.
 */
public static BoxItem.Info getSharedItem(BoxAPIConnection api, String sharedLink, String password) {
    BoxAPIConnection newAPI = new SharedLinkAPIConnection(api, sharedLink, password);
    URL url = SHARED_ITEM_URL_TEMPLATE.build(newAPI.getBaseURL());
    BoxAPIRequest request = new BoxAPIRequest(newAPI, url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject json = JsonObject.readFrom(response.getJSON());
    return (BoxItem.Info) BoxResource.parseInfo(newAPI, json);
}
 
Example 8
Source File: BoxWebHook.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * @param fields the fields to retrieve.
 * @return Gets information about this {@link BoxWebHook}.
 */
public BoxWebHook.Info getInfo(String ... fields) {
    QueryStringBuilder builder = new QueryStringBuilder();
    if (fields.length > 0) {
        builder.appendParam("fields", fields);
    }
    URL url = WEBHOOK_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    return new Info(JsonObject.readFrom(response.getJSON()));
}
 
Example 9
Source File: BoxFolder.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Creates metadata on this folder using a specified scope and template.
 *
 * @param templateName the name of the metadata template.
 * @param scope        the scope of the template (usually "global" or "enterprise").
 * @param metadata     the new metadata values.
 * @return the metadata returned from the server.
 */
public Metadata createMetadata(String templateName, String scope, Metadata metadata) {
    URL url = METADATA_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "POST");
    request.addHeader("Content-Type", "application/json");
    request.setBody(metadata.toString());
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    return new Metadata(JsonObject.readFrom(response.getJSON()));
}
 
Example 10
Source File: BoxUser.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Moves all of the owned content from within one user’s folder into a new folder in another user's account.
 * You can move folders across users as long as the you have administrative permissions and the 'source'
 * user owns the folders. Per the documentation at the link below, this will move everything from the root
 * folder, as this is currently the only mode of operation supported.
 *
 * See also https://developer.box.com/en/reference/put-users-id-folders-id/
 *
 * @param destinationUserID the user id of the user that you wish to transfer content to.
 * @return  info for the newly created folder.
 */
public BoxFolder.Info transferContent(String destinationUserID) {
    URL url = MOVE_FOLDER_TO_USER_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), "0");
    BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
    JsonObject destinationUser = new JsonObject();
    destinationUser.add("id", destinationUserID);
    JsonObject ownedBy = new JsonObject();
    ownedBy.add("owned_by", destinationUser);
    request.setBody(ownedBy.toString());
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    BoxFolder movedFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());

    return movedFolder.new Info(responseJSON);
}
 
Example 11
Source File: BoxUser.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the information about this user with any info fields that have been modified locally.
 *
 * <p>Note: This method is only available to enterprise admins.</p>
 *
 * @param info info the updated info.
 */
public void updateInfo(BoxUser.Info info) {
    URL url = USER_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
    BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
    request.setBody(info.getPendingChanges());
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
    info.update(jsonObject);
}
 
Example 12
Source File: BoxFile.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Creates metadata on this file in the specified template type.
 *
 * @param typeName the metadata template type name.
 * @param scope    the metadata scope (global or enterprise).
 * @param metadata the new metadata values.
 * @return the metadata returned from the server.
 */
public Metadata createMetadata(String typeName, String scope, Metadata metadata) {
    URL url = METADATA_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID(), scope, typeName);
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "POST");
    request.addHeader("Content-Type", "application/json");
    request.setBody(metadata.toString());
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    return new Metadata(JsonObject.readFrom(response.getJSON()));
}
 
Example 13
Source File: BoxUser.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Gets information about this user.
 * @param  fields the optional fields to retrieve.
 * @return        info about this user.
 */
public BoxUser.Info getInfo(String... fields) {
    URL url;
    if (fields.length > 0) {
        String queryString = new QueryStringBuilder().appendParam("fields", fields).toString();
        url = USER_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
    } else {
        url = USER_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
    }
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
    return new Info(jsonObject);
}
 
Example 14
Source File: MetadataTest.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
@Test
@Category(UnitTest.class)
public void testMetaProperties() {
    String json = "{\"$id\":\"123\",\"$type\":\"my type\",\"$parent\":\"456\"}";
    Metadata m = new Metadata(JsonObject.readFrom(json));
    Assert.assertEquals("123", m.getID());
    Assert.assertEquals("my type", m.getTypeName());
    Assert.assertEquals("456", m.getParentID());
}
 
Example 15
Source File: BoxTaskAssignment.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Gets information about this task assignment.
 * @return info about this task assignment.
 */
public Info getInfo() {
    URL url = TASK_ASSIGNMENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    return new Info(responseJSON);
}
 
Example 16
Source File: BoxGroup.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Gets information about this group.
 * @return info about this group.
 */
public Info getInfo() {
    URL url = GROUP_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    return new Info(responseJSON);
}
 
Example 17
Source File: RealtimeServerConnection.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
RealtimeServerConnection(BoxAPIConnection api) {
    BoxAPIRequest request = new BoxAPIRequest(api, EVENT_URL.build(api.getBaseURL()), "OPTIONS");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
    JsonArray entries = jsonObject.get("entries").asArray();
    JsonObject firstEntry = entries.get(0).asObject();
    this.serverURLString = firstEntry.get("url").asString();
    this.retries = Integer.parseInt(firstEntry.get("max_retries").asString());
    this.timeout = firstEntry.get("retry_timeout").asInt();
    this.api = api;
}
 
Example 18
Source File: BoxTask.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Gets information about this task.
 * @param fields the fields to retrieve.
 * @return info about this task.
 */
public Info getInfo(String... fields) {
    QueryStringBuilder builder = new QueryStringBuilder();
    if (fields.length > 0) {
        builder.appendParam("fields", fields);
    }
    URL url = TASK_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    return new Info(responseJSON);
}
 
Example 19
Source File: EventLog.java    From box-java-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Gets all the enterprise events that occurred within a specified date range, starting from a given position
 * within the event stream.
 * @param  api      the API connection to use.
 * @param  position the starting position of the event stream.
 * @param  after    the lower bound on the timestamp of the events returned.
 * @param  before   the upper bound on the timestamp of the events returned.
 * @param  limit    the number of entries to be returned in the response.
 * @param  types    an optional list of event types to filter by.
 * @return          a log of all the events that met the given criteria.
 */
public static EventLog getEnterpriseEvents(BoxAPIConnection api, String position, Date after, Date before,
                                           int limit, BoxEvent.Type... types) {

    URL url = ENTERPRISE_EVENT_URL_TEMPLATE.build(api.getBaseURL());

    if (position != null || types.length > 0 || after != null
        || before != null || limit != ENTERPRISE_LIMIT) {
        QueryStringBuilder queryBuilder = new QueryStringBuilder(url.getQuery());

        if (after != null) {
            queryBuilder.appendParam("created_after",
                BoxDateFormat.format(after));
        }

        if (before != null) {
            queryBuilder.appendParam("created_before",
                BoxDateFormat.format(before));
        }

        if (position != null) {
            queryBuilder.appendParam("stream_position", position);
        }

        if (limit != ENTERPRISE_LIMIT) {
            queryBuilder.appendParam("limit", limit);
        }

        if (types.length > 0) {
            StringBuilder filterBuilder = new StringBuilder();
            for (BoxEvent.Type filterType : types) {
                filterBuilder.append(filterType.name());
                filterBuilder.append(',');
            }
            filterBuilder.deleteCharAt(filterBuilder.length() - 1);
            queryBuilder.appendParam("event_type", filterBuilder.toString());
        }

        try {
            url = queryBuilder.addToURL(url);
        } catch (MalformedURLException e) {
            throw new BoxAPIException("Couldn't append a query string to the provided URL.");
        }
    }

    BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
    EventLog log = new EventLog(api, responseJSON, position, limit);
    log.setStartDate(after);
    log.setEndDate(before);
    return log;
}
 
Example 20
Source File: BoxWebLink.java    From box-java-sdk with Apache License 2.0 3 votes vote down vote up
/**
 * Updates the information about this weblink with any info fields that have been modified locally.
 *
 * <p>The only fields that will be updated are the ones that have been modified locally. For example, the following
 * code won't update any information (or even send a network request) since none of the info's fields were
 * changed:</p>
 *
 * <pre>BoxWebLink webLink = new BoxWebLink(api, id);
 *BoxWebLink.Info info = webLink.getInfo();
 *webLink.updateInfo(info);</pre>
 *
 * @param  info the updated info.
 */
public void updateInfo(BoxWebLink.Info info) {
    URL url = WEB_LINK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
    BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
    request.setBody(info.getPendingChanges());
    String body = info.getPendingChanges();
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
    info.update(jsonObject);
}