1 package com.atlassian.marketplace.client.impl;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.net.URI;
6
7 import com.atlassian.marketplace.client.MarketplaceClient;
8 import com.atlassian.marketplace.client.MpacException;
9 import com.atlassian.marketplace.client.api.AddonCategories;
10 import com.atlassian.marketplace.client.api.Addons;
11 import com.atlassian.marketplace.client.api.Applications;
12 import com.atlassian.marketplace.client.api.Assets;
13 import com.atlassian.marketplace.client.api.LicenseTypes;
14 import com.atlassian.marketplace.client.api.Page;
15 import com.atlassian.marketplace.client.api.PageReference;
16 import com.atlassian.marketplace.client.api.Products;
17 import com.atlassian.marketplace.client.api.Vendors;
18 import com.atlassian.marketplace.client.http.HttpConfiguration;
19 import com.atlassian.marketplace.client.http.HttpTransport;
20 import com.atlassian.marketplace.client.model.Links;
21
22 import static com.google.common.base.Preconditions.checkNotNull;
23
24
25
26
27 public final class DefaultMarketplaceClient implements MarketplaceClient
28 {
29 public static final URI DEFAULT_SERVER_URI = URI.create("https://marketplace.atlassian.com");
30
31 protected final URI baseUri;
32 protected final HttpTransport httpTransport;
33 protected final EntityEncoding encoding;
34
35 public static final String API_VERSION = "2";
36
37 private final ApiHelper apiHelper;
38
39
40
41
42
43
44
45 public DefaultMarketplaceClient(URI baseUri, HttpConfiguration configuration)
46 {
47 this(baseUri, new CommonsHttpTransport(configuration, baseUri), new JsonEntityEncoding());
48 }
49
50
51
52
53
54
55
56
57
58 public DefaultMarketplaceClient(URI serverBaseUri, HttpTransport httpTransport, EntityEncoding encoding)
59 {
60 this.baseUri = ApiHelper.normalizeBaseUri(checkNotNull(serverBaseUri, "serverBaseUri"))
61 .resolve("rest/" + API_VERSION + "/");
62 this.httpTransport = checkNotNull(httpTransport, "httpTransport");
63 this.encoding = encoding;
64
65 this.apiHelper = new ApiHelper(this.baseUri, httpTransport, encoding);
66 }
67
68 @Override
69 public void close()
70 {
71 try
72 {
73 httpTransport.close();
74 }
75 catch (IOException e)
76 {
77 }
78 }
79
80 @Override
81 public boolean isReachable()
82 {
83 return apiHelper.checkReachable(baseUri);
84 }
85
86 @Override
87 public Links getRootLinks() throws MpacException
88 {
89 return getRoot().getLinks();
90 }
91
92 @Override
93 public HttpTransport getHttp()
94 {
95 return httpTransport;
96 }
97
98 @Override
99 public <T> String toJson(T entity) throws MpacException
100 {
101 ByteArrayOutputStream os = new ByteArrayOutputStream();
102 encoding.encode(os, entity, true);
103 return new String(os.toByteArray());
104 }
105
106 @Override
107 public Addons addons() throws MpacException
108 {
109 return new AddonsImpl(apiHelper, getRoot());
110 }
111
112 @Override
113 public AddonCategories addonCategories() throws MpacException
114 {
115 return new AddonCategoriesImpl(apiHelper, applications());
116 }
117
118 @Override
119 public Applications applications() throws MpacException
120 {
121 return new ApplicationsImpl(apiHelper, getRoot());
122 }
123
124 @Override
125 public Assets assets() throws MpacException
126 {
127 return new AssetsImpl(apiHelper, getRoot());
128 }
129
130 @Override
131 public LicenseTypes licenseTypes() throws MpacException
132 {
133 return new LicenseTypesImpl(apiHelper, getRoot());
134 }
135
136 @Override
137 public Products products() throws MpacException
138 {
139 return new ProductsImpl(apiHelper, getRoot());
140 }
141
142 @Override
143 public Vendors vendors() throws MpacException
144 {
145 return new VendorsImpl(apiHelper, getRoot());
146 }
147
148 @Override
149 public <T> Page<T> getMore(PageReference<T> ref) throws MpacException
150 {
151 return apiHelper.getMore(ref);
152 }
153
154 InternalModel.MinimalLinks getRoot() throws MpacException
155 {
156 return apiHelper.getEntity(baseUri, InternalModel.MinimalLinks.class);
157 }
158 }