Skip to content
This repository was archived by the owner on Sep 24, 2025. It is now read-only.

Commit 9c0eab6

Browse files
authored
Merge pull request #9 from gettyimages/authFailureHandling
Auth failure handling
2 parents f1d3e9d + ec73b6a commit 9c0eab6

File tree

5 files changed

+144
-9
lines changed

5 files changed

+144
-9
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ fabric.properties
5959
**/bin/
6060
*.iml
6161
.project
62+
63+
gettyimagesapi-sdk/.settings/
64+
.classpath
65+
.settings/org.eclipse.core.resources.prefs
66+
.settings/org.eclipse.m2e.core.prefs

gettyimagesapi-sdk/src/main/java/com/gettyimages/api/Credentials.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.http.HttpEntity;
44
import org.apache.http.HttpResponse;
55
import org.apache.http.NameValuePair;
6+
import org.apache.http.StatusLine;
67
import org.apache.http.client.entity.UrlEncodedFormEntity;
78
import org.apache.http.client.methods.HttpPost;
89
import org.apache.http.impl.client.CloseableHttpClient;
@@ -12,8 +13,6 @@
1213
import org.json.JSONException;
1314
import org.json.JSONObject;
1415

15-
import java.io.IOException;
16-
import java.net.MalformedURLException;
1716
import java.net.URL;
1817
import java.util.*;
1918

@@ -103,7 +102,7 @@ public Token GetAccessToken() throws SdkException {
103102
return accessToken;
104103
} catch (JSONException e) {
105104
e.printStackTrace();
106-
throw new SdkException("Unable to get access token. Probably invalid credentials.");
105+
throw new HttpSystemErrorException(e);
107106
}
108107
}
109108

@@ -152,7 +151,7 @@ public Map<String,String> GetCredentialsDictionary()
152151
return dict;
153152
}
154153

155-
public String PostForm(Map<String, String> formParameters, String path) {
154+
public String PostForm(Map<String, String> formParameters, String path) throws SdkException {
156155
try {
157156
URL url = new URL(baseUrl + path);
158157
CloseableHttpClient httpClient = HttpClients.createDefault();
@@ -165,15 +164,23 @@ public String PostForm(Map<String, String> formParameters, String path) {
165164
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
166165

167166
HttpResponse response = httpClient.execute(httpPost);
167+
StatusLine statusLine = response.getStatusLine();
168+
int statusCode = statusLine.getStatusCode();
169+
if (statusCode >= 400 && statusCode <= 499) {
170+
throw new HttpClientErrorException(statusLine);
171+
} else if (statusCode >= 500 && statusCode <= 599) {
172+
throw new HttpSystemErrorException(statusLine);
173+
}
174+
168175
HttpEntity responseEntity = response.getEntity();
169176
String content = EntityUtils.toString(responseEntity);
170177

171178
return content;
172-
} catch (MalformedURLException ex) {
173-
String s = ex.toString();
174-
} catch (IOException ex) {
175-
String s = ex.toString();
179+
} catch (SdkException e) {
180+
throw e;
181+
}
182+
catch (Exception e) {
183+
throw new HttpSystemErrorException(e);
176184
}
177-
return null;
178185
}
179186
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.gettyimages.api;
2+
3+
import org.apache.http.StatusLine;
4+
5+
public class HttpClientErrorException extends SdkException {
6+
7+
private final int statusCode;
8+
private final String reasonPhrase;
9+
10+
public HttpClientErrorException(final StatusLine statusLine) {
11+
super("Client Error (" + statusLine.getStatusCode() + "): " + statusLine.getReasonPhrase());
12+
this.statusCode = statusLine.getStatusCode();
13+
this.reasonPhrase = statusLine.getReasonPhrase();
14+
}
15+
16+
public int getStatusCode() {
17+
return statusCode;
18+
}
19+
20+
public String getReasonPhrase() {
21+
return reasonPhrase;
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.gettyimages.api;
2+
3+
import org.apache.http.StatusLine;
4+
5+
public class HttpSystemErrorException extends SdkException {
6+
7+
private final int statusCode;
8+
private final String reasonPhrase;
9+
10+
public HttpSystemErrorException(final StatusLine statusLine) {
11+
super("System Error (" + statusLine.getStatusCode() + "): " + statusLine.getReasonPhrase());
12+
this.statusCode = statusLine.getStatusCode();
13+
this.reasonPhrase = statusLine.getReasonPhrase();
14+
}
15+
16+
public int getStatusCode() {
17+
return statusCode;
18+
}
19+
20+
public String getReasonPhrase() {
21+
return reasonPhrase;
22+
}
23+
24+
public HttpSystemErrorException(final Exception e) {
25+
super("System Error (500): Unexpected Error");
26+
this.statusCode = 500;
27+
this.reasonPhrase = "UnexpectedError"; // TODO: de-dupe
28+
}
29+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import static org.junit.Assert.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
4+
import static org.mockserver.model.HttpRequest.request;
5+
import static org.mockserver.model.HttpResponse.response;
6+
7+
import java.lang.reflect.Field;
8+
9+
import com.gettyimages.api.ApiClient;
10+
import com.gettyimages.api.HttpClientErrorException;
11+
import com.gettyimages.api.HttpSystemErrorException;
12+
import com.gettyimages.api.Search.SearchImages;
13+
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.mockserver.client.server.MockServerClient;
18+
import org.mockserver.integration.ClientAndServer;
19+
import org.mockserver.model.Parameter;
20+
21+
public class AuthFailureTest {
22+
private ClientAndServer mockServer;
23+
24+
@BeforeEach
25+
public void startProxy() throws Exception {
26+
final Field field = ApiClient.class.getDeclaredField("baseUrl");
27+
field.setAccessible(true);
28+
field.set(null, "http://127.0.0.1:1080/");
29+
mockServer = startClientAndServer(1080);
30+
}
31+
32+
@Test
33+
void clientErrorOnAuthentication() throws Exception {
34+
final int statusCode = 400;
35+
createMock(statusCode);
36+
final ApiClient client = ApiClient.GetApiClientWithClientCredentials("apiKey", "apiSecret");
37+
final SearchImages search = client.searchimages()
38+
.withPhrase("cat");
39+
40+
final HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> { search.executeAsync(); } );
41+
assertEquals(exception.getStatusCode(), statusCode);
42+
}
43+
44+
@Test
45+
void systemErrorOnAuthentication() throws Exception {
46+
final int statusCode = 500;
47+
createMock(statusCode);
48+
final ApiClient client = ApiClient.GetApiClientWithClientCredentials("apiKey", "apiSecret");
49+
final SearchImages search = client.searchimages()
50+
.withPhrase("cat");
51+
52+
final HttpSystemErrorException exception = assertThrows(HttpSystemErrorException.class, () -> { search.executeAsync(); } );
53+
assertEquals(exception.getStatusCode(), statusCode);
54+
}
55+
56+
57+
@AfterEach
58+
public void stopProxy() {
59+
mockServer.stop();
60+
}
61+
62+
private void createMock(final int statusCode) {
63+
final MockServerClient client = new MockServerClient("127.0.0.1", 1080);
64+
65+
client.when(request().withMethod("POST").withPath("/oauth2/token"))
66+
.respond(response().withStatusCode(statusCode));
67+
client.when(request().withMethod("GET").withPath("/search/images")
68+
.withQueryStringParameters(new Parameter("phrase", "cat")))
69+
.respond(response().withStatusCode(200).withBody("success"));
70+
}
71+
}

0 commit comments

Comments
 (0)