Skip to content

Commit 7645872

Browse files
author
danf
committed
Add product support
1 parent 0543d39 commit 7645872

File tree

15 files changed

+577
-49
lines changed

15 files changed

+577
-49
lines changed

api/src/main/java/com/jfrog/bintray/client/api/BintrayClientConstatnts.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface BintrayClientConstatnts {
1313
String API_VER = "versions/";
1414
String API_GPG = "gpg/";
1515
String API_CONTENT = "content/";
16+
String API_PRODUCTS = "products/";
1617

1718
String API_PUBLISH = "/publish";
1819
String API_ATTR = "/attributes";
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.jfrog.bintray.client.api.details;
2+
3+
import com.jfrog.bintray.client.api.ObjectMapperHelper;
4+
import org.codehaus.jackson.annotate.JsonIgnore;
5+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
6+
import org.codehaus.jackson.annotate.JsonProperty;
7+
import org.codehaus.jackson.map.ObjectMapper;
8+
import org.joda.time.DateTime;
9+
10+
import java.util.List;
11+
12+
/**
13+
* This class is used to serialize and deserialize the needed json to
14+
* pass to or receive from Bintray when performing actions on a product
15+
* NOTE: when serializing this class use getObjectMapper to obtain a suitable mapper for this class
16+
*
17+
* @author Dan Feldman
18+
*/
19+
@JsonIgnoreProperties(ignoreUnknown = true)
20+
public class ProductDetails {
21+
22+
//Properties marked with @JsonPropery here are serialized to the create \ update version requests, the rest are
23+
// only deserialized when getting the version info
24+
@JsonProperty
25+
String name;
26+
@JsonIgnore
27+
String owner;
28+
@JsonProperty(value = "desc")
29+
String description;
30+
@JsonIgnore
31+
DateTime created;
32+
@JsonProperty(value = "website_url")
33+
String websiteUrl;
34+
@JsonProperty(value = "vcs_url")
35+
String vcsUrl;
36+
@JsonProperty
37+
List<String> packages;
38+
@JsonIgnore
39+
List<String> versions;
40+
@JsonProperty(value = "sign_url_expiry")
41+
Integer signUrlExpiry;
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public String getOwner() {
52+
return owner;
53+
}
54+
55+
public void setOwner(String owner) {
56+
this.owner = owner;
57+
}
58+
59+
public String getDescription() {
60+
return description;
61+
}
62+
63+
public void setDescription(String description) {
64+
this.description = description;
65+
}
66+
67+
@JsonIgnore
68+
public DateTime getCreated() {
69+
return created;
70+
}
71+
72+
@JsonProperty(value = "created")
73+
public void setCreated(DateTime created) {
74+
this.created = created;
75+
}
76+
77+
public String getWebsiteUrl() {
78+
return websiteUrl;
79+
}
80+
81+
public void setWebsiteUrl(String websiteUrl) {
82+
this.websiteUrl = websiteUrl;
83+
}
84+
85+
public String getVcsUrl() {
86+
return vcsUrl;
87+
}
88+
89+
public void setVcsUrl(String vcsUrl) {
90+
this.vcsUrl = vcsUrl;
91+
}
92+
93+
public List<String> getPackages() {
94+
return packages;
95+
}
96+
97+
public void setPackages(List<String> packages) {
98+
this.packages = packages;
99+
}
100+
101+
@JsonIgnore
102+
public List<String> getVersions() {
103+
return versions;
104+
}
105+
106+
@JsonProperty
107+
public void setVersions(List<String> versions) {
108+
this.versions = versions;
109+
}
110+
111+
public Integer getSignUrlExpiry() {
112+
return signUrlExpiry;
113+
}
114+
115+
public void setSignUrlExpiry(Integer signUrlExpiry) {
116+
this.signUrlExpiry = signUrlExpiry;
117+
}
118+
119+
public static ObjectMapper getObjectMapper() {
120+
return ObjectMapperHelper.get();
121+
}
122+
}

api/src/main/java/com/jfrog/bintray/client/api/details/RepositoryDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class RepositoryDetails {
2121

2222
//Properties marked with @JsonPropery here are serialized to the create \ update version requests, the rest are
23-
// only deserialized when getting the version info
23+
// only deserialized when getting the repo info
2424
@JsonIgnore
2525
String name;
2626
@JsonIgnore
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jfrog.bintray.client.api.handle;
2+
3+
import com.jfrog.bintray.client.api.BintrayCallException;
4+
import com.jfrog.bintray.client.api.details.ProductDetails;
5+
import com.jfrog.bintray.client.api.model.Product;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
/**
11+
* @author Dan Feldman
12+
*/
13+
public interface ProductHandle extends Handle<Product> {
14+
15+
String name();
16+
17+
SubjectHandle owner();
18+
19+
Product get() throws IOException, BintrayCallException;
20+
21+
ProductHandle update(ProductDetails productDetails) throws IOException, BintrayCallException;
22+
23+
ProductHandle addPackages(List<String> packages) throws IOException, BintrayCallException;
24+
25+
ProductHandle delete() throws BintrayCallException;
26+
27+
boolean exists() throws BintrayCallException;
28+
}

api/src/main/java/com/jfrog/bintray/client/api/handle/SubjectHandle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jfrog.bintray.client.api.handle;
22

33
import com.jfrog.bintray.client.api.BintrayCallException;
4+
import com.jfrog.bintray.client.api.details.ProductDetails;
45
import com.jfrog.bintray.client.api.details.RepositoryDetails;
56
import com.jfrog.bintray.client.api.model.Subject;
67

@@ -18,4 +19,8 @@ public interface SubjectHandle extends Handle<Subject> {
1819
RepositoryHandle repository(String name);
1920

2021
RepositoryHandle createRepo(RepositoryDetails repoDetails) throws IOException, BintrayCallException;
22+
23+
ProductHandle product(String name);
24+
25+
ProductHandle createProduct(ProductDetails productDetails) throws IOException, BintrayCallException;
2126
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jfrog.bintray.client.api.model;
2+
3+
import org.joda.time.DateTime;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author Dan Feldman
9+
*/
10+
public interface Product {
11+
12+
String getName();
13+
14+
String getOwner();
15+
16+
String getDescription();
17+
18+
DateTime getCreated();
19+
20+
String getWebsiteUrl();
21+
22+
String getVcsUrl();
23+
24+
List<String> getPackages();
25+
26+
List<String> getVersions();
27+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.jfrog.bintray.client.impl.handle;
2+
3+
import com.jfrog.bintray.client.api.BintrayCallException;
4+
import com.jfrog.bintray.client.api.ObjectMapperHelper;
5+
import com.jfrog.bintray.client.api.details.ProductDetails;
6+
import com.jfrog.bintray.client.api.handle.ProductHandle;
7+
import com.jfrog.bintray.client.api.handle.SubjectHandle;
8+
import com.jfrog.bintray.client.api.model.Product;
9+
import com.jfrog.bintray.client.impl.model.ProductImpl;
10+
import org.apache.commons.io.IOUtils;
11+
import org.apache.http.HttpResponse;
12+
import org.codehaus.jackson.map.ObjectMapper;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import static com.jfrog.bintray.client.api.BintrayClientConstatnts.API_PRODUCTS;
23+
24+
/**
25+
* @author Dan Feldman
26+
*/
27+
class ProductHandleImpl implements ProductHandle {
28+
private static final Logger log = LoggerFactory.getLogger(ProductHandleImpl.class);
29+
30+
private BintrayImpl bintrayHandle;
31+
private SubjectHandleImpl owner;
32+
private String name;
33+
34+
public ProductHandleImpl(BintrayImpl bintrayHandle, SubjectHandleImpl owner, String productName) {
35+
this.bintrayHandle = bintrayHandle;
36+
this.owner = owner;
37+
this.name = productName;
38+
}
39+
40+
@Override
41+
public String name() {
42+
return name;
43+
}
44+
45+
46+
@Override
47+
public SubjectHandle owner() {
48+
return owner;
49+
}
50+
51+
@Override
52+
public Product get() throws IOException, BintrayCallException {
53+
ProductDetails productDetails = getProductDetails();
54+
return new ProductImpl(productDetails);
55+
}
56+
57+
@Override
58+
public ProductHandle update(ProductDetails productDetails) throws IOException, BintrayCallException {
59+
Map<String, String> headers = new HashMap<>();
60+
String jsonContent = ObjectMapperHelper.get().writeValueAsString(productDetails);
61+
BintrayImpl.addContentTypeJsonHeader(headers);
62+
bintrayHandle.patch(getProductUri(), headers, IOUtils.toInputStream(jsonContent));
63+
return this;
64+
}
65+
66+
@Override
67+
public ProductHandle addPackages(List<String> packages) throws IOException, BintrayCallException {
68+
ProductDetails details = getProductDetails();
69+
packages.addAll(details.getPackages());
70+
details.setPackages(packages);
71+
update(details);
72+
return this;
73+
}
74+
75+
@Override
76+
public ProductHandle delete() throws BintrayCallException {
77+
bintrayHandle.delete(getProductUri(), null);
78+
return this;
79+
}
80+
81+
@Override
82+
public boolean exists() throws BintrayCallException {
83+
try {
84+
bintrayHandle.get(getProductUri(), null);
85+
} catch (BintrayCallException e) {
86+
if (e.getStatusCode() == 404) {
87+
return false;
88+
}
89+
throw e;
90+
}
91+
return true;
92+
}
93+
94+
private ProductDetails getProductDetails() throws IOException {
95+
HttpResponse response = bintrayHandle.get(getProductUri(), null);
96+
ProductDetails productDetails;
97+
InputStream jsonContentStream = response.getEntity().getContent();
98+
ObjectMapper mapper = ObjectMapperHelper.get();
99+
try {
100+
productDetails = mapper.readValue(jsonContentStream, ProductDetails.class);
101+
} catch (IOException e) {
102+
log.error("Can't parse the json file: " + e.getMessage());
103+
throw e;
104+
}
105+
return productDetails;
106+
}
107+
108+
private String getProductUri() {
109+
return String.format(API_PRODUCTS + "%s/%s", owner.name(), name);
110+
}
111+
}

impl/src/main/java/com/jfrog/bintray/client/impl/handle/SubjectHandleImpl.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.jfrog.bintray.client.api.BintrayCallException;
44
import com.jfrog.bintray.client.api.ObjectMapperHelper;
5+
import com.jfrog.bintray.client.api.details.ProductDetails;
56
import com.jfrog.bintray.client.api.details.RepositoryDetails;
67
import com.jfrog.bintray.client.api.details.SubjectDetails;
8+
import com.jfrog.bintray.client.api.handle.ProductHandle;
79
import com.jfrog.bintray.client.api.handle.RepositoryHandle;
810
import com.jfrog.bintray.client.api.handle.SubjectHandle;
911
import com.jfrog.bintray.client.api.model.Subject;
@@ -20,8 +22,7 @@
2022
import java.util.HashMap;
2123
import java.util.Map;
2224

23-
import static com.jfrog.bintray.client.api.BintrayClientConstatnts.API_REPOS;
24-
import static com.jfrog.bintray.client.api.BintrayClientConstatnts.API_USERS;
25+
import static com.jfrog.bintray.client.api.BintrayClientConstatnts.*;
2526

2627
/**
2728
* @author Dan Feldman
@@ -56,6 +57,20 @@ public RepositoryHandle createRepo(RepositoryDetails repoDetails) throws IOExcep
5657
return new RepositoryHandleImpl(bintrayHandle, this, repoDetails.getName());
5758
}
5859

60+
@Override
61+
public ProductHandle product(String name) {
62+
return new ProductHandleImpl(bintrayHandle, this, name);
63+
}
64+
65+
@Override
66+
public ProductHandle createProduct(ProductDetails productDetails) throws IOException, BintrayCallException {
67+
Map<String, String> headers = new HashMap<>();
68+
String jsonContent = ObjectMapperHelper.get().writeValueAsString(productDetails);
69+
BintrayImpl.addContentTypeJsonHeader(headers);
70+
bintrayHandle.post(getProductUri(), headers, IOUtils.toInputStream(jsonContent));
71+
return new ProductHandleImpl(bintrayHandle, this, productDetails.getName());
72+
}
73+
5974
@Override
6075
public Subject get() throws IOException, BintrayCallException {
6176
HttpResponse response = bintrayHandle.get(API_USERS + subject, null);
@@ -74,4 +89,8 @@ public Subject get() throws IOException, BintrayCallException {
7489
private String getRepoUri(RepositoryDetails repoDetails) {
7590
return String.format(API_REPOS + "%s/%s", subject, repoDetails.getName());
7691
}
92+
93+
private String getProductUri() {
94+
return API_PRODUCTS + "/" + subject;
95+
}
7796
}

0 commit comments

Comments
 (0)