Skip to content
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
Comment thread
okumin marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

import com.google.common.base.Preconditions;

import java.io.Closeable;
import java.io.IOException;
import java.time.Clock;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.servlet.http.HttpServletResponse;
import org.apache.iceberg.BaseTable;
import org.apache.iceberg.BaseTransaction;
import org.apache.iceberg.Table;
Expand All @@ -48,7 +49,6 @@
import org.apache.iceberg.exceptions.NoSuchTableException;
import org.apache.iceberg.exceptions.NoSuchViewException;
import org.apache.iceberg.exceptions.NotAuthorizedException;
import org.apache.iceberg.exceptions.RESTException;
import org.apache.iceberg.exceptions.UnprocessableEntityException;
import org.apache.iceberg.exceptions.ValidationException;
import org.apache.iceberg.relocated.com.google.common.base.Splitter;
Expand Down Expand Up @@ -83,7 +83,7 @@
* Original @ <a href="https://github.com/apache/iceberg/blob/apache-iceberg-1.9.1/core/src/test/java/org/apache/iceberg/rest/RESTCatalogAdapter.java">RESTCatalogAdapter.java</a>
* Adaptor class to translate REST requests into {@link Catalog} API calls.
*/
public class HMSCatalogAdapter implements RESTClient {
public class HMSCatalogAdapter implements Closeable {

Check warning on line 86 in standalone-metastore/metastore-rest-catalog/src/main/java/org/apache/iceberg/rest/HMSCatalogAdapter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this “Monster Class” into smaller and more specialized ones to reduce its dependencies on other classes from 23 to the maximum authorized 20 or less.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ37rtkAu9_C1pCLfjt3&open=AZ37rtkAu9_C1pCLfjt3&pullRequest=6425
private static final Logger LOG = LoggerFactory.getLogger(HMSCatalogAdapter.class);
private static final Splitter SLASH = Splitter.on('/');

Expand Down Expand Up @@ -506,7 +506,7 @@
String path,
Map<String, String> queryParams,
Object body,
Consumer<ErrorResponse> errorHandler) {
HttpServletResponse response) throws IOException {
ErrorResponse.Builder errorBuilder = ErrorResponse.builder();
Pair<Route, Map<String, String>> routeAndVars = Route.from(method, path);
if (routeAndVars != null) {
Expand All @@ -527,63 +527,9 @@
.withMessage(String.format("No route for request: %s %s", method, path));
}
ErrorResponse error = errorBuilder.build();
errorHandler.accept(error);
// if the error handler doesn't throw an exception, throw a generic one
throw new RESTException("Unhandled error: %s", error);
}

@Override
public <T extends RESTResponse> T delete(
String path,
Class<T> responseType,
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler) {
return execute(HTTPMethod.DELETE, path, null, null, errorHandler);
}

@Override
public <T extends RESTResponse> T delete(
String path,
Map<String, String> queryParams,
Class<T> responseType,
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler) {
return execute(HTTPMethod.DELETE, path, queryParams, null, errorHandler);
}

@Override
public <T extends RESTResponse> T post(
String path,
RESTRequest body,
Class<T> responseType,
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler) {
return execute(HTTPMethod.POST, path, null, body, errorHandler);
}

@Override
public <T extends RESTResponse> T get(
String path,
Map<String, String> queryParams,
Class<T> responseType,
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler) {
return execute(HTTPMethod.GET, path, queryParams, null, errorHandler);
}

@Override
public void head(String path, Map<String, String> headers, Consumer<ErrorResponse> errorHandler) {
execute(HTTPMethod.HEAD, path, null, headers, errorHandler);
}

@Override
public <T extends RESTResponse> T postForm(
String path,
Map<String, String> formData,
Class<T> responseType,
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler) {
return execute(HTTPMethod.POST, path, null, formData, errorHandler);
response.setStatus(error.code());
RESTObjectMapper.mapper().writeValue(response.getWriter(), error);
return null;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RESTServerCatalogAdapter on apache/iceberg extends a RESTClient. I don't know the exact reason. The trick is used in some integration tests.
Anyway, as our adapter never behaves as a client, I removed the dependency on RESTClient. As a good side effect, this change ensures only a single path invokes RESTServerCatalogAdapter#execute and allows us to simplify the implementation.

}

@Override
Expand Down Expand Up @@ -627,11 +573,15 @@

public static void configureResponseFromException(
Exception exc, ErrorResponse.Builder errorBuilder) {
errorBuilder
.responseCode(EXCEPTION_ERROR_CODES.getOrDefault(exc.getClass(), 500))
.withType(exc.getClass().getSimpleName())
.withMessage(exc.getMessage())
.withStackTrace(exc);
var errorCode = EXCEPTION_ERROR_CODES.getOrDefault(exc.getClass(), 500);
errorBuilder.responseCode(errorCode).withType(exc.getClass().getSimpleName()).withMessage(exc.getMessage());
// avoid exposing stack traces for client errors but include them for server errors to aid debugging
if (errorCode >= 500) {
LOG.error("A server error happened while processing REST request", exc);
errorBuilder.withStackTrace(exc);
} else {
LOG.info("A client error happened while processing REST request: {}", exc.getMessage());
}
}

private static Namespace namespaceFromPathVars(Map<String, String> pathVars) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
package org.apache.iceberg.rest;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -77,29 +75,17 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
context.path(),
context.queryParams(),
context.body(),
handle(response));
response);

if (responseBody != null) {
RESTObjectMapper.mapper().writeValue(response.getWriter(), responseBody);
}
} catch (RuntimeException | IOException e) {
// should be a RESTException but not able to see them through dependencies
LOG.error("Error processing REST request", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}

private Consumer<ErrorResponse> handle(HttpServletResponse response) {
return errorResponse -> {
response.setStatus(errorResponse.code());
try {
RESTObjectMapper.mapper().writeValue(response.getWriter(), errorResponse);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}

@Override
public void destroy() {
super.destroy();
Expand Down
Loading