Skip to content

Adjustment to library to make working with it more easy. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.1 (04/05/18)
## 1.0.2 (04/12/2018)
- More specific exception classes thrown when requests fail.
- Mark internal classes as not final to allow mocking more easily.

## 1.0.1 (04/05/2018)
- Update Jackson dependency to 2.9.5 [CVE-2018-7489](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-7489).

## 1.0.0 (03/01/18)
## 1.0.0 (03/01/2018)
- Initial release!
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.sourcelab.kafka.connect.apiclient.request.put.PutConnectorPluginConfigValidate;
import org.sourcelab.kafka.connect.apiclient.request.put.PutConnectorResume;
import org.sourcelab.kafka.connect.apiclient.rest.HttpClientRestClient;
import org.sourcelab.kafka.connect.apiclient.rest.InvalidRequestException;
import org.sourcelab.kafka.connect.apiclient.rest.exceptions.InvalidRequestException;
import org.sourcelab.kafka.connect.apiclient.rest.RestClient;
import org.sourcelab.kafka.connect.apiclient.rest.RestResponse;

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

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

// Check for invalid http status codes
if (responseCode >= 200 && responseCode < 300) {
Expand All @@ -292,7 +292,7 @@ private <T> T submitRequest(final Request<T> request) {
// Attempt to parse error response
try {
final RequestErrorResponse errorResponse = JacksonFactory.newInstance().readValue(responseStr, RequestErrorResponse.class);
throw new InvalidRequestException(errorResponse.getMessage(), errorResponse.getErrorCode());
throw InvalidRequestException.factory(errorResponse);
} catch (final IOException e) {
// swallow
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String toString() {
/**
* Represents a Task.
*/
private static final class TaskDefinition {
public static final class TaskDefinition {
private String connector;
private int task;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Represents the status of a deployed connector.
*/
public final class ConnectorStatus {
public class ConnectorStatus {
private String name;
private Map<String, String> connector;
private List<TaskStatus> tasks;
Expand Down Expand Up @@ -52,7 +52,7 @@ public String toString() {
/**
* Defines the status of a Task.
*/
private static final class TaskStatus {
public static class TaskStatus {
private int id;
private String state;
private String workerId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private <T> T submitGetRequest(final String url, final Map<String, String> getPa
// Conditionally add content-type header?
get.addHeader(new BasicHeader("Content-Type", "application/json"));

logger.info("Executing request {}", get.getRequestLine());
logger.debug("Executing request {}", get.getRequestLine());

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

post.setEntity(new StringEntity(jsonPayloadStr));

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

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

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

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

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

// Execute and return
return httpClient.execute(delete, responseHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,18 @@
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.sourcelab.kafka.connect.apiclient.rest;
package org.sourcelab.kafka.connect.apiclient.rest.exceptions;

/**
* Represents when a request is invalid.
* Represents when a request is rejected due to concurrent configuration modification.
*/
public class InvalidRequestException extends RuntimeException {
private final int errorCode;
public class ConcurrentConfigModificationException extends InvalidRequestException {

public InvalidRequestException(final String message, final int errorCode) {
super(message);
this.errorCode = errorCode;
}

public InvalidRequestException(final String message, final Throwable cause) {
super(message, cause);
this.errorCode = -1;
}

public int getErrorCode() {
return errorCode;
/**
* Contructor.
* @param message Error message.
*/
public ConcurrentConfigModificationException(String message) {
super(message, 409);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2018 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.sourcelab.kafka.connect.apiclient.rest.exceptions;

import com.google.common.base.Preconditions;
import org.sourcelab.kafka.connect.apiclient.request.RequestErrorResponse;

/**
* Represents when a request is invalid.
*/
public class InvalidRequestException extends RuntimeException {
private final int errorCode;

/**
* Constructor.
* @param message Error message.
* @param errorCode Http Error Code.
*/
public InvalidRequestException(final String message, final int errorCode) {
super(message);
this.errorCode = errorCode;
}

/**
* @return Http Error Code.
*/
public int getErrorCode() {
return errorCode;
}

/**
* Factory method to create proper exception class based on the error.
* @param errorResponse Parsed error response from server.
* @return Appropriate Exception class.
*/
public static InvalidRequestException factory(final RequestErrorResponse errorResponse) {
Preconditions.checkNotNull(errorResponse, "Invalid RequestErrorResponse parameter, must not be null");

switch (errorResponse.getErrorCode()) {
case 404:
return new ResourceNotFoundException(errorResponse.getMessage());
case 409:
return new ConcurrentConfigModificationException(errorResponse.getMessage());
default:
return new InvalidRequestException(errorResponse.getMessage(), errorResponse.getErrorCode());

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2018 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.sourcelab.kafka.connect.apiclient.rest.exceptions;

/**
* Represents when a request is rejected due because the resource is unable to be located.
*/
public class ResourceNotFoundException extends InvalidRequestException {

/**
* Constructor.
* @param message Error message.
*/
public ResourceNotFoundException(String message) {
super(message, 404);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public RestResponse handleResponse(final HttpResponse response) {
return new RestResponse(responseStr, statusCode);
} catch (final IOException exception) {
logger.error("Failed to read entity: {}", exception.getMessage(), exception);
// TODO throw exception
// TODO throw exceptions
return null;
}
}
Expand Down