Search in sources :

Example 81 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxCollaboration method getPendingCollaborations.

/**
 * Gets all pending collaboration invites for the current user.
 *
 * @param api the API connection to use.
 * @return a collection of pending collaboration infos.
 */
public static Collection<Info> getPendingCollaborations(BoxAPIConnection api) {
    URL url = PENDING_COLLABORATIONS_URL.build(api.getBaseURL());
    BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
    int entriesCount = responseJSON.get("total_count").asInt();
    Collection<BoxCollaboration.Info> collaborations = new ArrayList<>(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;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) URL(java.net.URL)

Example 82 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BatchAPIRequest method prepareRequest.

/**
 * @param requests list of api requests that has to be executed in batch.
 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
 * <p>
 * Prepare a batch api request using list of individual reuests.
 */
@Deprecated
protected void prepareRequest(List<BoxAPIRequest> requests) {
    JsonObject body = new JsonObject();
    JsonArray requestsJSONArray = new JsonArray();
    for (BoxAPIRequest request : requests) {
        JsonObject batchRequest = new JsonObject();
        batchRequest.add("method", request.getMethod());
        batchRequest.add("relative_url", request.getUrl().toString().substring(this.api.getBaseURL().length() - 1));
        // If the actual request has a JSON body then add it to vatch request
        if (request instanceof BoxJSONRequest) {
            BoxJSONRequest jsonRequest = (BoxJSONRequest) request;
            batchRequest.add("body", jsonRequest.getBodyAsJsonValue());
        }
        // Add any headers that are in the request, except Authorization
        if (request.getHeaders() != null) {
            JsonObject batchRequestHeaders = new JsonObject();
            for (RequestHeader header : request.getHeaders()) {
                if (header.getKey() != null && !header.getKey().isEmpty() && !HttpHeaders.AUTHORIZATION.equals(header.getKey())) {
                    batchRequestHeaders.add(header.getKey(), header.getValue());
                }
            }
            batchRequest.add("headers", batchRequestHeaders);
        }
        // Add the request to array
        requestsJSONArray.add(batchRequest);
    }
    // Add the requests array to body
    body.add("requests", requestsJSONArray);
    super.setBody(body);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject)

Example 83 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxCollection method getItemsRange.

/**
 * Retrieves a specific range of items in this collection.
 *
 * @param offset the index of the first item to retrieve.
 * @param limit  the maximum number of items to retrieve after the offset.
 * @param fields the fields to retrieve.
 * @return a partial collection containing the specified range of items.
 */
public PartialCollection<BoxItem.Info> getItemsRange(long offset, long limit, String... fields) {
    QueryStringBuilder builder = new QueryStringBuilder().appendParam("offset", offset).appendParam("limit", limit);
    if (fields.length > 0) {
        builder.appendParam("fields", fields);
    }
    URL url = GET_COLLECTION_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), builder.toString(), getID());
    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
    String totalCountString = responseJSON.get("total_count").toString();
    long fullSize = Double.valueOf(totalCountString).longValue();
    PartialCollection<BoxItem.Info> items = new PartialCollection<>(offset, limit, fullSize);
    JsonArray entries = responseJSON.get("entries").asArray();
    for (JsonValue entry : entries) {
        BoxItem.Info entryInfo = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), entry.asObject());
        if (entryInfo != null) {
            items.add(entryInfo);
        }
    }
    return items;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) URL(java.net.URL) JsonArray(com.eclipsesource.json.JsonArray)

Example 84 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFileUploadSession method getCommitBody.

/*
     * Creates the JSON body for the commit request.
     */
private String getCommitBody(List<BoxFileUploadSessionPart> parts, Map<String, String> attributes) {
    JsonObject jsonObject = new JsonObject();
    JsonArray array = new JsonArray();
    for (BoxFileUploadSessionPart part : parts) {
        JsonObject partObj = new JsonObject();
        partObj.add("part_id", part.getPartId());
        partObj.add("offset", part.getOffset());
        partObj.add("size", part.getSize());
        array.add(partObj);
    }
    jsonObject.add("parts", array);
    if (attributes != null) {
        JsonObject attrObj = new JsonObject();
        for (String key : attributes.keySet()) {
            attrObj.add(key, attributes.get(key));
        }
        jsonObject.add("attributes", attrObj);
    }
    return jsonObject.toString();
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject)

Example 85 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFileUploadSession method getFile.

/*
     * Creates the file isntance from the JSON body of the response.
     */
private BoxFile.Info getFile(BoxJSONResponse response) {
    JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
    JsonArray array = (JsonArray) jsonObject.get("entries");
    JsonObject fileObj = (JsonObject) array.get(0);
    BoxFile file = new BoxFile(this.getAPI(), fileObj.get("id").asString());
    return file.new Info(fileObj);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject)

Aggregations

JsonArray (com.eclipsesource.json.JsonArray)111 JsonObject (com.eclipsesource.json.JsonObject)96 JsonValue (com.eclipsesource.json.JsonValue)32 Test (org.junit.Test)30 URL (java.net.URL)28 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)11 Matchers.containsString (org.hamcrest.Matchers.containsString)6 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)5 Map (java.util.Map)5 Link (org.eclipse.leshan.Link)4 Date (java.util.Date)3 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)3 ClientEndPointDTO (com.hazelcast.internal.management.dto.ClientEndPointDTO)2 MalformedURLException (java.net.MalformedURLException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2