diff --git a/pulsar-common/pom.xml b/pulsar-common/pom.xml index a9cb2e2fdbbd3..604941f0efab1 100644 --- a/pulsar-common/pom.xml +++ b/pulsar-common/pom.xml @@ -151,6 +151,11 @@ jackson-dataformat-yaml + + javax.ws.rs + javax.ws.rs-api + + diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestException.java b/pulsar-common/src/main/java/org/apache/pulsar/common/util/RestException.java similarity index 80% rename from pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestException.java rename to pulsar-common/src/main/java/org/apache/pulsar/common/util/RestException.java index 537ec1db4c3cb..3d986065901f4 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestException.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/util/RestException.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.pulsar.functions.worker.rest; +package org.apache.pulsar.common.util; import java.io.PrintWriter; import java.io.StringWriter; @@ -26,7 +26,6 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import org.apache.pulsar.client.admin.PulsarAdminException; import org.apache.pulsar.common.policies.data.ErrorData; /** @@ -34,7 +33,7 @@ */ @SuppressWarnings("serial") public class RestException extends WebApplicationException { - static String getExceptionData(Throwable t) { + public static String getExceptionData(Throwable t) { StringWriter writer = new StringWriter(); writer.append("\n --- An unexpected error occurred in the server ---\n\n"); if (t != null) { @@ -58,21 +57,16 @@ public RestException(Throwable t) { super(getResponse(t)); } - public RestException(PulsarAdminException cae) { - this(cae.getStatusCode(), cae.getHttpError()); - } - private static Response getResponse(Throwable t) { - if (t instanceof RestException - || t instanceof WebApplicationException) { + if (t instanceof WebApplicationException) { WebApplicationException e = (WebApplicationException) t; return e.getResponse(); } else { return Response - .status(Status.INTERNAL_SERVER_ERROR) - .entity(getExceptionData(t)) - .type(MediaType.TEXT_PLAIN) - .build(); + .status(Status.INTERNAL_SERVER_ERROR) + .entity(getExceptionData(t)) + .type(MediaType.TEXT_PLAIN) + .build(); } } -} \ No newline at end of file +} diff --git a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/server/ServerManager.java b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/server/ServerManager.java index 945074c55e7fd..fa4761c4f090d 100644 --- a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/server/ServerManager.java +++ b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/server/ServerManager.java @@ -28,8 +28,8 @@ import javax.servlet.Servlet; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.common.util.SecurityUtility; -import org.apache.pulsar.discovery.service.web.RestException; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; diff --git a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceServlet.java b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceServlet.java index 71c42b4503563..6324ca7ecc837 100644 --- a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceServlet.java +++ b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceServlet.java @@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.Response.Status; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport; import org.apache.pulsar.zookeeper.ZooKeeperClientFactory; import org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl; diff --git a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/RestException.java b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/RestException.java deleted file mode 100644 index 844b25ddeaad3..0000000000000 --- a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/RestException.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.pulsar.discovery.service.web; - -import java.io.PrintWriter; -import java.io.StringWriter; - -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.apache.pulsar.common.policies.data.ErrorData; - -/** - * Exception used to provide better error messages to clients of the REST API. - */ -@SuppressWarnings("serial") -public class RestException extends WebApplicationException { - static String getExceptionData(Throwable t) { - StringWriter writer = new StringWriter(); - writer.append("\n --- An unexpected error occurred in the server ---\n\n"); - writer.append("Message: ").append(t.getMessage()).append("\n\n"); - writer.append("Stacktrace:\n\n"); - - t.printStackTrace(new PrintWriter(writer)); - return writer.toString(); - } - - public RestException(Response.Status status, String message) { - this(status.getStatusCode(), message); - } - - public RestException(int code, String message) { - super(Response.status(code).entity(new ErrorData(message)).type(MediaType.APPLICATION_JSON).build()); - } - - public RestException(Throwable t) { - super(Response.status(500).entity(getExceptionData(t)).type(MediaType.TEXT_PLAIN).build()); - } - -} diff --git a/pulsar-discovery-service/src/test/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceWebTest.java b/pulsar-discovery-service/src/test/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceWebTest.java index bf149699fb059..1aa84641282bc 100644 --- a/pulsar-discovery-service/src/test/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceWebTest.java +++ b/pulsar-discovery-service/src/test/java/org/apache/pulsar/discovery/service/web/DiscoveryServiceWebTest.java @@ -56,6 +56,7 @@ import org.apache.bookkeeper.util.ZkUtils; import org.apache.pulsar.common.policies.data.BundlesData; import org.apache.pulsar.common.util.ObjectMapperFactory; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.discovery.service.server.ServerManager; import org.apache.pulsar.discovery.service.server.ServiceConfig; import org.apache.pulsar.policies.data.loadbalancer.LoadReport; @@ -247,12 +248,11 @@ public void testTlsEnable() throws Exception { @Test public void testException() { RestException exception1 = new RestException(BAD_GATEWAY, "test-msg"); - assertTrue(exception1.getMessage().contains(BAD_GATEWAY.toString())); + assertTrue(exception1.getMessage().contains("test-msg")); RestException exception2 = new RestException(BAD_GATEWAY.getStatusCode(), "test-msg"); - assertTrue(exception2.getMessage().contains(BAD_GATEWAY.toString())); + assertTrue(exception2.getMessage().contains("test-msg")); RestException exception3 = new RestException(exception2); - assertTrue(exception3.getMessage().contains(INTERNAL_SERVER_ERROR.toString())); - assertTrue(RestException.getExceptionData(exception2).contains(BAD_GATEWAY.toString())); + assertTrue(exception3.getMessage().contains(BAD_GATEWAY.toString())); } public List validateRequest(List brokers, String method, String url, BundlesData bundle) { diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestUtils.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestUtils.java index 6378b342c920d..db065cbef9a82 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestUtils.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/RestUtils.java @@ -26,6 +26,8 @@ import javax.ws.rs.core.Response; +import org.apache.pulsar.common.util.RestException; + @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class RestUtils { diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java index 98cc3a5eb83bc..b2dbf5b15c69e 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java @@ -50,6 +50,7 @@ import org.apache.pulsar.common.policies.data.FunctionStats; import org.apache.pulsar.common.policies.data.TenantInfo; import org.apache.pulsar.common.util.Codec; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.instance.InstanceUtils; import org.apache.pulsar.functions.proto.Function; import org.apache.pulsar.functions.proto.Function.FunctionDetails; @@ -68,7 +69,6 @@ import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; import org.apache.pulsar.functions.worker.request.RequestResult; -import org.apache.pulsar.functions.worker.rest.RestException; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import javax.ws.rs.WebApplicationException; diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java index 026fb77bf0918..6e931b1eda0e3 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java @@ -28,6 +28,7 @@ import org.apache.pulsar.common.functions.Utils; import org.apache.pulsar.common.policies.data.ExceptionInformation; import org.apache.pulsar.common.policies.data.FunctionStatus; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.auth.FunctionAuthData; import org.apache.pulsar.functions.auth.FunctionAuthProvider; import org.apache.pulsar.functions.instance.InstanceUtils; @@ -39,7 +40,6 @@ import org.apache.pulsar.functions.worker.FunctionMetaDataManager; import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; -import org.apache.pulsar.functions.worker.rest.RestException; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import javax.ws.rs.WebApplicationException; diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImplV2.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImplV2.java index 50fe96c5c90b7..86118cea3b208 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImplV2.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImplV2.java @@ -24,13 +24,13 @@ import org.apache.pulsar.common.functions.FunctionState; import org.apache.pulsar.common.io.ConnectorDefinition; import org.apache.pulsar.common.policies.data.FunctionStatus; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.proto.Function; import org.apache.pulsar.functions.proto.InstanceCommunication; import org.apache.pulsar.functions.utils.FunctionCommon; import org.apache.pulsar.functions.utils.FunctionConfigUtils; import org.apache.pulsar.functions.worker.FunctionMetaDataManager; import org.apache.pulsar.functions.worker.WorkerService; -import org.apache.pulsar.functions.worker.rest.RestException; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import javax.ws.rs.core.Response; diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SinksImpl.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SinksImpl.java index 70e7b85149574..76e5686353513 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SinksImpl.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SinksImpl.java @@ -29,6 +29,7 @@ import org.apache.pulsar.common.io.SinkConfig; import org.apache.pulsar.common.policies.data.ExceptionInformation; import org.apache.pulsar.common.policies.data.SinkStatus; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.auth.FunctionAuthData; import org.apache.pulsar.functions.instance.InstanceUtils; import org.apache.pulsar.functions.proto.Function; @@ -39,7 +40,6 @@ import org.apache.pulsar.functions.worker.FunctionMetaDataManager; import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; -import org.apache.pulsar.functions.worker.rest.RestException; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import javax.ws.rs.WebApplicationException; diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SourcesImpl.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SourcesImpl.java index 8cec959e990bb..4c98004aaed7d 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SourcesImpl.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/SourcesImpl.java @@ -29,6 +29,7 @@ import org.apache.pulsar.common.io.SourceConfig; import org.apache.pulsar.common.policies.data.ExceptionInformation; import org.apache.pulsar.common.policies.data.SourceStatus; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.auth.FunctionAuthData; import org.apache.pulsar.functions.instance.InstanceUtils; import org.apache.pulsar.functions.proto.Function; @@ -39,7 +40,6 @@ import org.apache.pulsar.functions.worker.FunctionMetaDataManager; import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; -import org.apache.pulsar.functions.worker.rest.RestException; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import javax.ws.rs.WebApplicationException; diff --git a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/WorkerImpl.java b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/WorkerImpl.java index 801d32eb22d65..88e74689f87be 100644 --- a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/WorkerImpl.java +++ b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/WorkerImpl.java @@ -23,6 +23,7 @@ import org.apache.pulsar.common.io.ConnectorDefinition; import org.apache.pulsar.common.policies.data.FunctionStats; import org.apache.pulsar.common.policies.data.WorkerFunctionInstanceStats; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.proto.Function; import org.apache.pulsar.functions.utils.FunctionCommon; import org.apache.pulsar.functions.worker.FunctionRuntimeInfo; @@ -30,7 +31,6 @@ import org.apache.pulsar.functions.worker.MembershipManager; import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; -import org.apache.pulsar.functions.worker.rest.RestException; import javax.ws.rs.core.Response.Status; import java.io.IOException; diff --git a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v2/FunctionApiV2ResourceTest.java b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v2/FunctionApiV2ResourceTest.java index 90c134d902c94..88142e6476937 100644 --- a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v2/FunctionApiV2ResourceTest.java +++ b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v2/FunctionApiV2ResourceTest.java @@ -63,6 +63,7 @@ import org.apache.pulsar.common.functions.FunctionConfig; import org.apache.pulsar.common.policies.data.TenantInfo; import org.apache.pulsar.common.util.FutureUtil; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.api.Context; import org.apache.pulsar.functions.api.Function; import org.apache.pulsar.functions.instance.InstanceUtils; @@ -82,7 +83,6 @@ import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; import org.apache.pulsar.functions.worker.request.RequestResult; -import org.apache.pulsar.functions.worker.rest.RestException; import org.apache.pulsar.functions.worker.rest.api.FunctionsImpl; import org.apache.pulsar.functions.worker.rest.api.FunctionsImplV2; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; diff --git a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/FunctionApiV3ResourceTest.java b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/FunctionApiV3ResourceTest.java index 16e5e3077f72e..f1128ef5ad1e7 100644 --- a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/FunctionApiV3ResourceTest.java +++ b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/FunctionApiV3ResourceTest.java @@ -60,6 +60,7 @@ import org.apache.pulsar.common.functions.FunctionConfig; import org.apache.pulsar.common.policies.data.TenantInfo; import org.apache.pulsar.common.util.FutureUtil; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.api.Context; import org.apache.pulsar.functions.api.Function; import org.apache.pulsar.functions.instance.InstanceUtils; @@ -79,7 +80,6 @@ import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; import org.apache.pulsar.functions.worker.request.RequestResult; -import org.apache.pulsar.functions.worker.rest.RestException; import org.apache.pulsar.functions.worker.rest.api.FunctionsImpl; import org.apache.pulsar.functions.worker.rest.api.v2.FunctionsApiV2Resource; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; diff --git a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SinkApiV3ResourceTest.java b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SinkApiV3ResourceTest.java index c47f9e52b7b4d..b9aeccb58eec8 100644 --- a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SinkApiV3ResourceTest.java +++ b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SinkApiV3ResourceTest.java @@ -31,6 +31,7 @@ import org.apache.pulsar.common.nar.NarClassLoader; import org.apache.pulsar.common.policies.data.TenantInfo; import org.apache.pulsar.common.util.FutureUtil; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.api.utils.IdentityFunction; import org.apache.pulsar.functions.instance.InstanceUtils; import org.apache.pulsar.functions.proto.Function; @@ -46,7 +47,6 @@ import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; import org.apache.pulsar.functions.worker.request.RequestResult; -import org.apache.pulsar.functions.worker.rest.RestException; import org.apache.pulsar.functions.worker.rest.api.SinksImpl; import org.apache.pulsar.io.cassandra.CassandraStringSink; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; diff --git a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SourceApiV3ResourceTest.java b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SourceApiV3ResourceTest.java index faf3f8812ff95..88dcff4fcaf55 100644 --- a/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SourceApiV3ResourceTest.java +++ b/pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/rest/api/v3/SourceApiV3ResourceTest.java @@ -54,6 +54,7 @@ import org.apache.pulsar.common.nar.NarClassLoader; import org.apache.pulsar.common.policies.data.TenantInfo; import org.apache.pulsar.common.util.FutureUtil; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.functions.api.utils.IdentityFunction; import org.apache.pulsar.functions.instance.InstanceUtils; import org.apache.pulsar.functions.proto.Function.FunctionDetails; @@ -73,7 +74,6 @@ import org.apache.pulsar.functions.worker.WorkerService; import org.apache.pulsar.functions.worker.WorkerUtils; import org.apache.pulsar.functions.worker.request.RequestResult; -import org.apache.pulsar.functions.worker.rest.RestException; import org.apache.pulsar.functions.worker.rest.api.SourcesImpl; import org.apache.pulsar.io.twitter.TwitterFireHose; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/RestException.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/RestException.java deleted file mode 100644 index f57d19432286d..0000000000000 --- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/RestException.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.pulsar.websocket.admin; - -import java.io.PrintWriter; -import java.io.StringWriter; - -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.apache.pulsar.common.policies.data.ErrorData; - -/** - * Exception used to provide better error messages to clients of the REST API. - */ -@SuppressWarnings("serial") -public class RestException extends WebApplicationException { - static String getExceptionData(Throwable t) { - StringWriter writer = new StringWriter(); - writer.append("\n --- An unexpected error occurred in the server ---\n\n"); - writer.append("Message: ").append(t.getMessage()).append("\n\n"); - writer.append("Stacktrace:\n\n"); - - t.printStackTrace(new PrintWriter(writer)); - return writer.toString(); - } - - public RestException(Response.Status status, String message) { - this(status.getStatusCode(), message); - } - - public RestException(int code, String message) { - super(Response.status(code).entity(new ErrorData(message)).type(MediaType.APPLICATION_JSON).build()); - } - - public RestException(Throwable t) { - super(getResponse(t)); - } - - private static Response getResponse(Throwable t) { - if (t instanceof RestException) { - RestException e = (RestException) t; - return Response.status(e.getResponse().getStatus()).entity(e.getResponse().getEntity()) - .type(e.getResponse().getMediaType()).build(); - } else { - return Response.status(500).entity(getExceptionData(t)).type(MediaType.TEXT_PLAIN).build(); - } - } -} diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketProxyStatsBase.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketProxyStatsBase.java index cbbbddcf468c3..c3de46b79839b 100644 --- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketProxyStatsBase.java +++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketProxyStatsBase.java @@ -27,6 +27,7 @@ import org.apache.pulsar.common.naming.TopicName; import org.apache.pulsar.common.stats.Metrics; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.websocket.stats.ProxyTopicStat; import org.apache.pulsar.websocket.stats.ProxyTopicStat.ConsumerStats; import org.apache.pulsar.websocket.stats.ProxyTopicStat.ProducerStats; diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketWebResource.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketWebResource.java index 10e3664edf65d..255fe654a5353 100644 --- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketWebResource.java +++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/admin/WebSocketWebResource.java @@ -29,6 +29,7 @@ import org.apache.pulsar.broker.authentication.AuthenticationDataHttps; import org.apache.pulsar.common.naming.TopicName; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.websocket.WebSocketService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/admin/WebSocketWebResourceTest.java b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/admin/WebSocketWebResourceTest.java index 6d7e8cf084140..5d1d5a58ac49a 100644 --- a/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/admin/WebSocketWebResourceTest.java +++ b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/admin/WebSocketWebResourceTest.java @@ -50,6 +50,7 @@ import org.apache.pulsar.broker.authentication.AuthenticationDataSource; import org.apache.pulsar.broker.authorization.AuthorizationService; import org.apache.pulsar.common.naming.TopicName; +import org.apache.pulsar.common.util.RestException; import org.apache.pulsar.websocket.WebSocketService; public class WebSocketWebResourceTest {