001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.MalformedURLException;
006import java.net.URL;
007
008/**
009 * Represents a search item when searching shared links.
010 *
011 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
012 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
013 * handling for errors related to the Box REST API, you should capture this exception explicitly.*
014 */
015public class BoxSearchSharedLink extends BoxJSONObject {
016    private String type;
017    private BoxItem.Info item;
018    private URL accessibleViaSharedLink;
019    private BoxAPIConnection api;
020
021    /**
022     * Construct a BoxRecentItem.
023     *
024     * @param jsonObject the parsed JSON object.
025     * @param api        the API connection to be used to fetch interacted item
026     */
027    public BoxSearchSharedLink(JsonObject jsonObject, BoxAPIConnection api) {
028        super(jsonObject);
029        this.api = api;
030    }
031
032    @Override
033    protected void parseJSONMember(JsonObject.Member member) {
034        super.parseJSONMember(member);
035
036        String memberName = member.getName();
037        JsonValue value = member.getValue();
038        try {
039            if (memberName.equals("type")) {
040                this.type = value.asString();
041            } else if (memberName.equals("item")) {
042                JsonObject jsonObject = value.asObject();
043                String type = jsonObject.get("type").asString();
044                String id = jsonObject.get("id").asString();
045
046                if (type.equals("folder")) {
047                    BoxFolder folder = new BoxFolder(this.api, id);
048                    this.item = folder.new Info(jsonObject);
049                } else if (type.equals("file")) {
050                    BoxFile file = new BoxFile(this.api, id);
051                    this.item = file.new Info(jsonObject);
052                } else if (type.equals("web_link")) {
053                    BoxWebLink link = new BoxWebLink(this.api, id);
054                    this.item = link.new Info(jsonObject);
055                } else {
056                    assert false : "Unsupported item type: " + type;
057                    throw new BoxAPIException("Unsupported item type: " + type);
058                }
059            } else if (memberName.equals("accessible_via_shared_link")) {
060                this.accessibleViaSharedLink = new URL(value.asString());
061            }
062        } catch (MalformedURLException e) {
063            assert false : "A ParseException indicates a bug in the SDK.";
064        }
065    }
066
067    /**
068     * Get item type.
069     *
070     * @return type of item
071     */
072    public String getType() {
073        return this.type;
074    }
075
076    /**
077     * Get the item which was interacted with.
078     *
079     * @return box item
080     */
081    public BoxItem.Info getItem() {
082        return this.item;
083    }
084
085    /**
086     * Get the optional shared link through which the user has access to this item.
087     *
088     * @return shared link
089     */
090    public URL getAccessibleViaSharedLink() {
091        return this.accessibleViaSharedLink;
092    }
093
094}
095