1 package com.atlassian.marketplace.client.model;
2
3 import java.net.URI;
4 import java.util.Map;
5
6 import com.atlassian.fugue.Option;
7 import com.atlassian.marketplace.client.api.UriTemplate;
8
9 import com.google.common.collect.ImmutableList;
10 import com.google.common.collect.ImmutableMap;
11
12 import static com.atlassian.fugue.Option.none;
13 import static com.atlassian.fugue.Option.option;
14 import static com.atlassian.fugue.Option.some;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public final class Links
30 {
31 public static final String REST_TYPE = "application/json";
32 public static final String WEB_TYPE = "text/html";
33
34 private final Map<String, ImmutableList<Link>> items;
35
36 public Links(Map<String, ImmutableList<Link>> items)
37 {
38 this.items = ImmutableMap.copyOf(items);
39 }
40
41
42
43
44 public Map<String, ImmutableList<Link>> getItems()
45 {
46 return items;
47 }
48
49 URI requireUri(String rel)
50 {
51 for (URI uri: getUri(rel))
52 {
53 return uri;
54 }
55 throw new IllegalArgumentException("missing required REST link: " + rel);
56 }
57
58
59
60
61 public Option<Link> getLink(String rel)
62 {
63 for (Link link: getLinks(rel))
64 {
65 return some(link);
66 }
67 return none();
68 }
69
70
71
72
73 public Option<Link> getLink(String rel, String contentType)
74 {
75 for (Link link: getLinks(rel))
76 {
77 if (link.getType().isEmpty() && contentType.equals(REST_TYPE))
78 {
79 return some(link);
80 }
81 for (String type: link.getType())
82 {
83 if (type.equalsIgnoreCase(contentType))
84 {
85 return some(link);
86 }
87 }
88 }
89 return none();
90 }
91
92
93
94
95 public Iterable<Link> getLinks(String rel)
96 {
97 return option(items.get(rel)).getOrElse(ImmutableList.<Link>of());
98 }
99
100
101
102
103 public Option<URI> getUri(String rel)
104 {
105 for (Link link: getLink(rel))
106 {
107 return some(link.getUri());
108 }
109 return none();
110 }
111
112
113
114
115 public Option<URI> getUri(String rel, String contentType)
116 {
117 for (Link link: getLink(rel, contentType))
118 {
119 return some(link.getUri());
120 }
121 return none();
122 }
123
124
125
126
127
128 public Option<UriTemplate> getUriTemplate(String rel)
129 {
130 for (Link link: getLink(rel))
131 {
132 return link.getUriTemplate();
133 }
134 return none();
135 }
136
137
138
139
140
141 public Option<UriTemplate> getUriTemplate(String rel, String contentType)
142 {
143 for (Link link: getLink(rel, contentType))
144 {
145 return link.getUriTemplate();
146 }
147 return none();
148 }
149 }