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