Search in sources :

Example 1 with RestException

use of org.apache.pulsar.common.util.RestException in project pulsar by apache.

the class WebSocketProxyStatsBase method internalGetStats.

protected ProxyTopicStat internalGetStats(TopicName topicName) {
    validateUserAccess(topicName);
    ProxyTopicStat stats = getStat(topicName);
    if (stats == null) {
        throw new RestException(Status.NOT_FOUND, "Topic does not exist");
    }
    return stats;
}
Also used : ProxyTopicStat(org.apache.pulsar.websocket.stats.ProxyTopicStat) RestException(org.apache.pulsar.common.util.RestException)

Example 2 with RestException

use of org.apache.pulsar.common.util.RestException in project pulsar by apache.

the class WebSocketWebResource method clientAppId.

/**
 * Gets a caller id (IP + role).
 *
 * @return the web service caller identification
 */
public String clientAppId() {
    if (isBlank(clientId)) {
        try {
            String authMethodName = httpRequest.getHeader(AuthenticationFilter.PULSAR_AUTH_METHOD_NAME);
            if (authMethodName != null && service().getAuthenticationService().getAuthenticationProvider(authMethodName) != null) {
                authenticationDataSource = service().getAuthenticationService().getAuthenticationProvider(authMethodName).newHttpAuthState(httpRequest).getAuthDataSource();
                clientId = service().getAuthenticationService().authenticateHttpRequest(httpRequest, authenticationDataSource);
            } else {
                clientId = service().getAuthenticationService().authenticateHttpRequest(httpRequest);
                authenticationDataSource = new AuthenticationDataHttps(httpRequest);
            }
        } catch (AuthenticationException e) {
            if (service().getConfig().isAuthenticationEnabled()) {
                throw new RestException(Status.UNAUTHORIZED, "Failed to get clientId from request");
            }
        }
        if (isBlank(clientId) && service().getConfig().isAuthenticationEnabled()) {
            throw new RestException(Status.UNAUTHORIZED, "Failed to get auth data from the request");
        }
    }
    return clientId;
}
Also used : AuthenticationException(javax.naming.AuthenticationException) RestException(org.apache.pulsar.common.util.RestException) AuthenticationDataHttps(org.apache.pulsar.broker.authentication.AuthenticationDataHttps)

Example 3 with RestException

use of org.apache.pulsar.common.util.RestException in project pulsar by apache.

the class FunctionApiV2ResourceTest method testRegisterFunctionUploadFailure.

@Test(expectedExceptions = RestException.class, expectedExceptionsMessageRegExp = "upload failure")
public void testRegisterFunctionUploadFailure() throws Exception {
    try {
        mockWorkerUtils(ctx -> {
            ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new IOException("upload failure"));
        });
        when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(false);
        registerDefaultFunction();
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatusInfo(), Response.Status.INTERNAL_SERVER_ERROR);
        throw re;
    }
}
Also used : RestException(org.apache.pulsar.common.util.RestException) IOException(java.io.IOException) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.testng.annotations.Test)

Example 4 with RestException

use of org.apache.pulsar.common.util.RestException in project pulsar by apache.

the class FunctionApiV2ResourceTest method testUpdateFunctionUploadFailure.

@Test(expectedExceptions = RestException.class, expectedExceptionsMessageRegExp = "upload failure")
public void testUpdateFunctionUploadFailure() throws Exception {
    try {
        mockWorkerUtils(ctx -> {
            ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new IOException("upload failure"));
        });
        when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(true);
        updateDefaultFunction();
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatusInfo(), Response.Status.INTERNAL_SERVER_ERROR);
        throw re;
    }
}
Also used : RestException(org.apache.pulsar.common.util.RestException) IOException(java.io.IOException) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.testng.annotations.Test)

Example 5 with RestException

use of org.apache.pulsar.common.util.RestException in project pulsar by apache.

the class SinkApiV3ResourceTest method testUpdateSinkFailedWithWrongPackageName.

@Test(timeOut = 20000)
public void testUpdateSinkFailedWithWrongPackageName() throws Exception {
    when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(true);
    try {
        doThrow(new PulsarAdminException("package name is invalid")).when(mockedPackages).download(anyString(), anyString());
        updateDefaultSinkWithPackageUrl("function://");
    } catch (RestException e) {
        // expected exception
        assertEquals(e.getResponse().getStatusInfo(), Response.Status.BAD_REQUEST);
    }
}
Also used : RestException(org.apache.pulsar.common.util.RestException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test)

Aggregations

RestException (org.apache.pulsar.common.util.RestException)178 IOException (java.io.IOException)101 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)101 WebApplicationException (javax.ws.rs.WebApplicationException)66 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)65 RestUtils.throwUnavailableException (org.apache.pulsar.functions.worker.rest.RestUtils.throwUnavailableException)54 Test (org.testng.annotations.Test)45 File (java.io.File)41 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)36 Function (org.apache.pulsar.functions.proto.Function)33 Namespace (org.apache.distributedlog.api.namespace.Namespace)21 SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)21 ExecutionException (java.util.concurrent.ExecutionException)18 NamespaceNotFoundException (org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException)18 StreamNotFoundException (org.apache.bookkeeper.clients.exceptions.StreamNotFoundException)18 FunctionAuthData (org.apache.pulsar.functions.auth.FunctionAuthData)18 FunctionAuthUtils.getFunctionAuthData (org.apache.pulsar.functions.auth.FunctionAuthUtils.getFunctionAuthData)18 FunctionRuntimeManager (org.apache.pulsar.functions.worker.FunctionRuntimeManager)15 URI (java.net.URI)14 FunctionConfig (org.apache.pulsar.common.functions.FunctionConfig)12