Skip to content

Commit 9fd34e5

Browse files
author
Jevgeni Koltšin
committed
Merge remote-tracking branch 'remotes/origin/s3-integration'
Conflicts: src/main/java/com/creatubbles/api/core/CreatubblesRequest.java
2 parents 06a2f47 + f6a0c52 commit 9fd34e5

11 files changed

Lines changed: 162 additions & 5 deletions

File tree

src/main/java/com/creatubbles/api/CreatubblesAPI.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.creatubbles.api;
22

3+
import com.creatubbles.api.core.Creation;
4+
import com.creatubbles.api.core.Credentials;
35
import com.creatubbles.api.core.Gallery;
6+
import com.creatubbles.api.request.amazon.GetAmazonTokenRequest;
7+
import com.creatubbles.api.request.amazon.UploadS3ImageRequest;
8+
import com.creatubbles.api.request.auth.SignInRequest;
9+
import com.creatubbles.api.request.creation.UpdateCreationRequest;
10+
import com.creatubbles.api.request.creation.UploadCreationRequest;
11+
import com.creatubbles.api.response.amazon.GetAmazonTokenResponse;
12+
import com.creatubbles.api.response.auth.SignInResponse;
413
import com.creatubbles.api.response.auth.SignUpResponse;
14+
import com.creatubbles.api.response.creation.UploadCreationResponse;
515
import com.creatubbles.api.response.creator.CreateCreatorResponse;
616
import com.creatubbles.api.response.gallery.CreateUserGalleryResponse;
717
import com.creatubbles.api.response.user.UserProfileResponse;
@@ -12,12 +22,17 @@
1222
import org.glassfish.jersey.client.JerseyClient;
1323
import org.glassfish.jersey.client.JerseyClientBuilder;
1424

25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
1529
public class CreatubblesAPI {
1630
public final static Gson GSON = new GsonBuilder()
1731
.registerTypeAdapter(SignUpResponse.class, new SignUpResponse())
1832
.registerTypeAdapter(UserProfileResponse.class, new UserProfileResponse())
1933
.registerTypeAdapter(CreateCreatorResponse.class, new CreateCreatorResponse())
2034
.registerTypeAdapter(CreateUserGalleryResponse.class, new CreateUserGalleryResponse())
35+
.registerTypeAdapter(CreateUserGalleryResponse.class, new CreateUserGalleryResponse())
2136
.registerTypeAdapter(Gallery.class, new Gallery())
2237
.create();
2338

@@ -29,4 +44,5 @@ public class CreatubblesAPI {
2944
public static String buildURL(final String endPoint) {
3045
return EndPoints.URL_BASE + endPoint;
3146
}
47+
3248
}

src/main/java/com/creatubbles/api/core/Creation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public class Creation {
77
public String last_bubbled_at, last_commented_at, last_submitted_at;
88
public Image image;
99
public Creator[] creators;
10+
public String store_dir;
11+
public String url;
1012
}

src/main/java/com/creatubbles/api/core/CreatubblesRequest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.creatubbles.api.CreatubblesAPI;
44
import com.creatubbles.api.util.HttpMethod;
55
import org.glassfish.jersey.client.JerseyWebTarget;
6+
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
67

78
import javax.ws.rs.client.Entity;
89
import javax.ws.rs.client.Invocation;
@@ -21,6 +22,7 @@ public abstract class CreatubblesRequest<T extends CreatubblesResponse> {
2122
private Response response;
2223
private Future<Response> futureResponse;
2324
private T responseCache;
25+
private static final String EMPTY_RESPONSE = "{}";
2426

2527
public CreatubblesRequest(String endPoint, HttpMethod httpMethod) {
2628
this(endPoint, httpMethod, null, null);
@@ -108,7 +110,7 @@ public CreatubblesRequest<T> setUrlParameter(String key, String value) {
108110

109111
public abstract Class<? extends T> getResponseClass();
110112

111-
private void resetResponse() {
113+
public void resetResponse() {
112114
if (response != null || futureResponse != null) {
113115
response = null;
114116
futureResponse = null;
@@ -121,11 +123,15 @@ public boolean isDone() {
121123

122124
public boolean wasSuccessful() {
123125
if (isDone()) {
124-
return getRawResponse().getStatus() == 200;
126+
return isSuccessStatus(getRawResponse());
125127
}
126128
return false;
127129
}
128130

131+
private boolean isSuccessStatus(Response response) {
132+
return response.getStatus() == 200 || response.getStatus() == 204;
133+
}
134+
129135
public void cancelRequest() {
130136
if (futureResponse != null & !futureResponse.isDone()) {
131137
futureResponse.cancel(true);
@@ -151,6 +157,10 @@ public T getResponse() {
151157
Response response = getRawResponse();
152158
Class<? extends T> responseClass = getResponseClass();
153159
if (response != null && responseClass != null) {
160+
String json = response.readEntity(String.class);
161+
if (isSuccessStatus(response) && json.isEmpty()) {
162+
json = EMPTY_RESPONSE;
163+
}
154164
T creatubblesResponse = CreatubblesAPI.GSON.fromJson(response.readEntity(String.class), responseClass);
155165
creatubblesResponse.setOriginatingRequest(this);
156166
responseCache = creatubblesResponse;
@@ -170,6 +180,8 @@ public CreatubblesRequest<T> execute() {
170180
webTarget = webTarget.queryParam(paramKey, paramValue);
171181
}
172182
}
183+
HttpAuthenticationFeature basicAuth = HttpAuthenticationFeature.basic("c", "c");
184+
webTarget.register(basicAuth);
173185

174186
Invocation.Builder invocationBuilder = webTarget
175187
.request(MediaType.APPLICATION_JSON)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.creatubbles.api.request.amazon;
2+
3+
import com.amazonaws.services.s3.AmazonS3;
4+
import com.amazonaws.services.s3.model.CannedAccessControlList;
5+
import com.amazonaws.services.s3.model.ObjectMetadata;
6+
import com.amazonaws.services.s3.model.PutObjectRequest;
7+
import com.creatubbles.api.core.CreatubblesRequest;
8+
import com.creatubbles.api.response.amazon.UploadS3ImageResponse;
9+
import com.creatubbles.api.util.S3ClientUtil;
10+
11+
import java.io.ByteArrayInputStream;
12+
13+
/**
14+
* Created by Jevgeni on 28.10.2015.
15+
*/
16+
public class UploadS3ImageRequest extends CreatubblesRequest<UploadS3ImageResponse> {
17+
18+
private String filePath;
19+
private byte[] data;
20+
private String accessKey;
21+
private String secretKey;
22+
private String sessionToken;
23+
24+
public UploadS3ImageRequest(byte[] data, String filePath, String accessKey, String secretKey, String sessionToken) {
25+
super(null, null);
26+
this.filePath = filePath;
27+
this.data = data;
28+
this.accessKey = accessKey;
29+
this.secretKey = secretKey;
30+
this.sessionToken = sessionToken;
31+
}
32+
33+
@Override
34+
public Class<? extends UploadS3ImageResponse> getResponseClass() {
35+
return UploadS3ImageResponse.class;
36+
}
37+
38+
@Override
39+
public CreatubblesRequest<UploadS3ImageResponse> execute() {
40+
resetResponse();
41+
AmazonS3 client = S3ClientUtil.getClient(accessKey, secretKey, sessionToken);
42+
ObjectMetadata metadata = new ObjectMetadata();
43+
metadata.setContentLength(data.length);
44+
PutObjectRequest putObjectRequest = new PutObjectRequest(S3ClientUtil.AWS_S3_BUCKET_NAME, filePath, new ByteArrayInputStream(data), metadata)
45+
.withCannedAcl(CannedAccessControlList.PublicRead);
46+
client.putObject(putObjectRequest);
47+
return this;
48+
}
49+
50+
@Override
51+
public UploadS3ImageResponse getResponse() {
52+
return new UploadS3ImageResponse();
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.creatubbles.api.request.creation;
2+
3+
import com.creatubbles.api.CreatubblesAPI;
4+
import com.creatubbles.api.core.Creation;
5+
import com.creatubbles.api.core.CreatubblesRequest;
6+
import com.creatubbles.api.response.creation.UpdateCreationResponse;
7+
import com.creatubbles.api.util.EndPoints;
8+
import com.creatubbles.api.util.HttpMethod;
9+
import com.creatubbles.api.util.S3ClientUtil;
10+
import com.google.gson.JsonObject;
11+
12+
/**
13+
* Created by Jevgeni on 28.10.2015.
14+
*/
15+
public class UpdateCreationRequest extends CreatubblesRequest<UpdateCreationResponse> {
16+
17+
public UpdateCreationRequest(String accessToken, Creation creation) {
18+
super(String.format(EndPoints.CREATIONS_BY_ID, creation.id), HttpMethod.PUT, accessToken);
19+
JsonObject jsonObject = new JsonObject();
20+
if (creation.url != null && !creation.url.startsWith("http")) {
21+
creation.url = S3ClientUtil.AWS_S3_BASE_URL + creation.url;
22+
}
23+
jsonObject.add("creation", CreatubblesAPI.GSON.toJsonTree(creation));
24+
setData(jsonObject.toString());
25+
}
26+
27+
@Override
28+
public Class<? extends UpdateCreationResponse> getResponseClass() {
29+
return UpdateCreationResponse.class;
30+
}
31+
}

src/main/java/com/creatubbles/api/request/creation/UploadCreationRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// TODO Order of operations (and how the upload actually happens) is kind of vague in the API docs - figure out OoO
99
public class UploadCreationRequest extends CreatubblesRequest<UploadCreationResponse> {
1010

11-
public UploadCreationRequest() {
12-
super(EndPoints.CREATIONS, HttpMethod.POST);
11+
public UploadCreationRequest(String accessToken) {
12+
super(EndPoints.CREATIONS, HttpMethod.POST, accessToken);
1313
}
1414

1515
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.creatubbles.api.response.amazon;
2+
3+
import com.creatubbles.api.core.CreatubblesResponse;
4+
5+
/**
6+
* Created by Jevgeni on 28.10.2015.
7+
*/
8+
public class UploadS3ImageResponse extends CreatubblesResponse {
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.creatubbles.api.response.creation;
2+
3+
import com.creatubbles.api.core.CreatubblesResponse;
4+
5+
public class UpdateCreationResponse extends CreatubblesResponse {
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.creatubbles.api.response.creation;
22

3+
import com.creatubbles.api.core.Creation;
34
import com.creatubbles.api.core.CreatubblesResponse;
45

56
public class UploadCreationResponse extends CreatubblesResponse {
7+
8+
public Creation creation;
69
}

src/main/java/com/creatubbles/api/util/EndPoints.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class EndPoints {
44

5-
public final static String URL_BASE = "https://www.creatubbles.com/api/v1/";
5+
public final static String URL_BASE = "https://staging.creatubbles.com/api/v1/";
66

77
public static final String SIGN_IN = "users/sign_in.json";
88

@@ -23,6 +23,8 @@ public class EndPoints {
2323

2424
public static final String USERS_GALLERIES = "users/%s/galleries.json";
2525

26+
public static final String CREATIONS_BY_ID = "creations/%s.json";
27+
2628
public static final String CREATIONS = "creations.json";
2729

2830
public static final String CREATORS_CREATIONS = "creators/%d/creations.json?page=%d";

0 commit comments

Comments
 (0)