Skip to content

Commit 6fd8790

Browse files
authored
Merge pull request #3 from SourceLabOrg/sp/minorTweaking
Adjustment to library to make working with it more easy.
2 parents a054d49 + 0da1b29 commit 6fd8790

File tree

9 files changed

+122
-29
lines changed

9 files changed

+122
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/)
33
and this project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## 1.0.1 (04/05/18)
5+
## 1.0.2 (04/12/2018)
6+
- More specific exception classes thrown when requests fail.
7+
- Mark internal classes as not final to allow mocking more easily.
8+
9+
## 1.0.1 (04/05/2018)
610
- Update Jackson dependency to 2.9.5 [CVE-2018-7489](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-7489).
711

8-
## 1.0.0 (03/01/18)
12+
## 1.0.0 (03/01/2018)
913
- Initial release!

src/main/java/org/sourcelab/kafka/connect/apiclient/KafkaConnectClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import org.sourcelab.kafka.connect.apiclient.request.put.PutConnectorPluginConfigValidate;
4747
import org.sourcelab.kafka.connect.apiclient.request.put.PutConnectorResume;
4848
import org.sourcelab.kafka.connect.apiclient.rest.HttpClientRestClient;
49-
import org.sourcelab.kafka.connect.apiclient.rest.InvalidRequestException;
49+
import org.sourcelab.kafka.connect.apiclient.rest.exceptions.InvalidRequestException;
5050
import org.sourcelab.kafka.connect.apiclient.rest.RestClient;
5151
import org.sourcelab.kafka.connect.apiclient.rest.RestResponse;
5252

@@ -272,7 +272,7 @@ private <T> T submitRequest(final Request<T> request) {
272272
String responseStr = restResponse.getResponseStr();
273273

274274
// If we have a valid response
275-
logger.info("Response: {}", restResponse);
275+
logger.debug("Response: {}", restResponse);
276276

277277
// Check for invalid http status codes
278278
if (responseCode >= 200 && responseCode < 300) {
@@ -292,7 +292,7 @@ private <T> T submitRequest(final Request<T> request) {
292292
// Attempt to parse error response
293293
try {
294294
final RequestErrorResponse errorResponse = JacksonFactory.newInstance().readValue(responseStr, RequestErrorResponse.class);
295-
throw new InvalidRequestException(errorResponse.getMessage(), errorResponse.getErrorCode());
295+
throw InvalidRequestException.factory(errorResponse);
296296
} catch (final IOException e) {
297297
// swallow
298298
}

src/main/java/org/sourcelab/kafka/connect/apiclient/request/dto/ConnectorDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String toString() {
5858
/**
5959
* Represents a Task.
6060
*/
61-
private static final class TaskDefinition {
61+
public static final class TaskDefinition {
6262
private String connector;
6363
private int task;
6464

src/main/java/org/sourcelab/kafka/connect/apiclient/request/dto/ConnectorStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* Represents the status of a deployed connector.
2525
*/
26-
public final class ConnectorStatus {
26+
public class ConnectorStatus {
2727
private String name;
2828
private Map<String, String> connector;
2929
private List<TaskStatus> tasks;
@@ -52,7 +52,7 @@ public String toString() {
5252
/**
5353
* Defines the status of a Task.
5454
*/
55-
private static final class TaskStatus {
55+
public static class TaskStatus {
5656
private int id;
5757
private String state;
5858
private String workerId;

src/main/java/org/sourcelab/kafka/connect/apiclient/rest/HttpClientRestClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private <T> T submitGetRequest(final String url, final Map<String, String> getPa
211211
// Conditionally add content-type header?
212212
get.addHeader(new BasicHeader("Content-Type", "application/json"));
213213

214-
logger.info("Executing request {}", get.getRequestLine());
214+
logger.debug("Executing request {}", get.getRequestLine());
215215

216216
// Execute and return
217217
return httpClient.execute(get, responseHandler);
@@ -253,7 +253,7 @@ private <T> T submitPostRequest(final String url, final Object requestBody, fina
253253

254254
post.setEntity(new StringEntity(jsonPayloadStr));
255255

256-
logger.info("Executing request {} with {}", post.getRequestLine(), jsonPayloadStr);
256+
logger.debug("Executing request {} with {}", post.getRequestLine(), jsonPayloadStr);
257257

258258
// Execute and return
259259
return httpClient.execute(post, responseHandler);
@@ -292,7 +292,7 @@ private <T> T submitPutRequest(final String url, final Object requestBody, final
292292
final String jsonPayloadStr = JacksonFactory.newInstance().writeValueAsString(requestBody);
293293
put.setEntity(new StringEntity(jsonPayloadStr));
294294

295-
logger.info("Executing request {} with {}", put.getRequestLine(), jsonPayloadStr);
295+
logger.debug("Executing request {} with {}", put.getRequestLine(), jsonPayloadStr);
296296

297297
// Execute and return
298298
return httpClient.execute(put, responseHandler);
@@ -336,7 +336,7 @@ private <T> T submitDeleteRequest(final String url, final Object requestBody, fi
336336
// Convert to Json
337337
final String jsonPayloadStr = JacksonFactory.newInstance().writeValueAsString(requestBody);
338338

339-
logger.info("Executing request {} with {}", delete.getRequestLine(), jsonPayloadStr);
339+
logger.debug("Executing request {} with {}", delete.getRequestLine(), jsonPayloadStr);
340340

341341
// Execute and return
342342
return httpClient.execute(delete, responseHandler);

src/main/java/org/sourcelab/kafka/connect/apiclient/rest/InvalidRequestException.java renamed to src/main/java/org/sourcelab/kafka/connect/apiclient/rest/exceptions/ConcurrentConfigModificationException.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,18 @@
1515
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1616
*/
1717

18-
package org.sourcelab.kafka.connect.apiclient.rest;
18+
package org.sourcelab.kafka.connect.apiclient.rest.exceptions;
1919

2020
/**
21-
* Represents when a request is invalid.
21+
* Represents when a request is rejected due to concurrent configuration modification.
2222
*/
23-
public class InvalidRequestException extends RuntimeException {
24-
private final int errorCode;
23+
public class ConcurrentConfigModificationException extends InvalidRequestException {
2524

26-
public InvalidRequestException(final String message, final int errorCode) {
27-
super(message);
28-
this.errorCode = errorCode;
29-
}
30-
31-
public InvalidRequestException(final String message, final Throwable cause) {
32-
super(message, cause);
33-
this.errorCode = -1;
34-
}
35-
36-
public int getErrorCode() {
37-
return errorCode;
25+
/**
26+
* Contructor.
27+
* @param message Error message.
28+
*/
29+
public ConcurrentConfigModificationException(String message) {
30+
super(message, 409);
3831
}
3932
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright 2018 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.sourcelab.kafka.connect.apiclient.rest.exceptions;
19+
20+
import com.google.common.base.Preconditions;
21+
import org.sourcelab.kafka.connect.apiclient.request.RequestErrorResponse;
22+
23+
/**
24+
* Represents when a request is invalid.
25+
*/
26+
public class InvalidRequestException extends RuntimeException {
27+
private final int errorCode;
28+
29+
/**
30+
* Constructor.
31+
* @param message Error message.
32+
* @param errorCode Http Error Code.
33+
*/
34+
public InvalidRequestException(final String message, final int errorCode) {
35+
super(message);
36+
this.errorCode = errorCode;
37+
}
38+
39+
/**
40+
* @return Http Error Code.
41+
*/
42+
public int getErrorCode() {
43+
return errorCode;
44+
}
45+
46+
/**
47+
* Factory method to create proper exception class based on the error.
48+
* @param errorResponse Parsed error response from server.
49+
* @return Appropriate Exception class.
50+
*/
51+
public static InvalidRequestException factory(final RequestErrorResponse errorResponse) {
52+
Preconditions.checkNotNull(errorResponse, "Invalid RequestErrorResponse parameter, must not be null");
53+
54+
switch (errorResponse.getErrorCode()) {
55+
case 404:
56+
return new ResourceNotFoundException(errorResponse.getMessage());
57+
case 409:
58+
return new ConcurrentConfigModificationException(errorResponse.getMessage());
59+
default:
60+
return new InvalidRequestException(errorResponse.getMessage(), errorResponse.getErrorCode());
61+
62+
}
63+
}
64+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2018 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.sourcelab.kafka.connect.apiclient.rest.exceptions;
19+
20+
/**
21+
* Represents when a request is rejected due because the resource is unable to be located.
22+
*/
23+
public class ResourceNotFoundException extends InvalidRequestException {
24+
25+
/**
26+
* Constructor.
27+
* @param message Error message.
28+
*/
29+
public ResourceNotFoundException(String message) {
30+
super(message, 404);
31+
}
32+
}

src/main/java/org/sourcelab/kafka/connect/apiclient/rest/handlers/RestResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public RestResponse handleResponse(final HttpResponse response) {
4848
return new RestResponse(responseStr, statusCode);
4949
} catch (final IOException exception) {
5050
logger.error("Failed to read entity: {}", exception.getMessage(), exception);
51-
// TODO throw exception
51+
// TODO throw exceptions
5252
return null;
5353
}
5454
}

0 commit comments

Comments
 (0)