use of org.apache.pulsar.broker.intercept.BrokerInterceptor in project pulsar by apache.
the class ProcessHandlerFilterTest method testChainDoFilter.
@Test
public void testChainDoFilter() throws ServletException, IOException {
PulsarService mockPulsarService = Mockito.mock(PulsarService.class);
BrokerInterceptor spyInterceptor = Mockito.mock(BrokerInterceptor.class);
HttpServletResponse mockHttpServletResponse = Mockito.mock(HttpServletResponse.class);
ServiceConfiguration mockConfig = Mockito.mock(ServiceConfiguration.class);
FilterChain spyFilterChain = Mockito.spy(FilterChain.class);
Mockito.doReturn(spyInterceptor).when(mockPulsarService).getBrokerInterceptor();
Mockito.doReturn(mockConfig).when(mockPulsarService).getConfig();
Mockito.doReturn(Sets.newHashSet()).when(mockConfig).getBrokerInterceptors();
// empty interceptor list
HttpServletRequest mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
ProcessHandlerFilter processHandlerFilter = new ProcessHandlerFilter(mockPulsarService);
processHandlerFilter.doFilter(mockHttpServletRequest, mockHttpServletResponse, spyFilterChain);
Mockito.verify(spyFilterChain).doFilter(mockHttpServletRequest, mockHttpServletResponse);
Mockito.clearInvocations(spyFilterChain);
// request has MULTIPART_FORM_DATA content-type
Mockito.doReturn(Sets.newHashSet("Interceptor1", "Interceptor2")).when(mockConfig).getBrokerInterceptors();
HttpServletRequest mockHttpServletRequest2 = Mockito.mock(HttpServletRequest.class);
Mockito.doReturn(MediaType.MULTIPART_FORM_DATA).when(mockHttpServletRequest2).getContentType();
ProcessHandlerFilter processHandlerFilter2 = new ProcessHandlerFilter(mockPulsarService);
processHandlerFilter2.doFilter(mockHttpServletRequest2, mockHttpServletResponse, spyFilterChain);
Mockito.verify(spyFilterChain).doFilter(mockHttpServletRequest2, mockHttpServletResponse);
Mockito.clearInvocations(spyFilterChain);
// request has APPLICATION_OCTET_STREAM content-type
Mockito.doReturn(MediaType.APPLICATION_OCTET_STREAM).when(mockHttpServletRequest2).getContentType();
processHandlerFilter2.doFilter(mockHttpServletRequest2, mockHttpServletResponse, spyFilterChain);
Mockito.verify(spyFilterChain).doFilter(mockHttpServletRequest2, mockHttpServletResponse);
}
use of org.apache.pulsar.broker.intercept.BrokerInterceptor in project pulsar by yahoo.
the class ServerCnx method completeConnect.
// complete the connect and sent newConnected command
private void completeConnect(int clientProtoVersion, String clientVersion) {
ctx.writeAndFlush(Commands.newConnected(clientProtoVersion, maxMessageSize));
state = State.Connected;
service.getPulsarStats().recordConnectionCreateSuccess();
if (log.isDebugEnabled()) {
log.debug("[{}] connect state change to : [{}]", remoteAddress, State.Connected.name());
}
setRemoteEndpointProtocolVersion(clientProtoVersion);
if (isNotBlank(clientVersion) && !clientVersion.contains(" ")) /* ignore default version: pulsar client */
{
this.clientVersion = clientVersion.intern();
}
BrokerInterceptor brokerInterceptor = getBrokerService().getInterceptor();
if (brokerInterceptor != null) {
brokerInterceptor.onConnectionCreated(this);
}
}
use of org.apache.pulsar.broker.intercept.BrokerInterceptor in project incubator-pulsar by apache.
the class ServerCnx method completeConnect.
// complete the connect and sent newConnected command
private void completeConnect(int clientProtoVersion, String clientVersion) {
ctx.writeAndFlush(Commands.newConnected(clientProtoVersion, maxMessageSize));
state = State.Connected;
service.getPulsarStats().recordConnectionCreateSuccess();
if (log.isDebugEnabled()) {
log.debug("[{}] connect state change to : [{}]", remoteAddress, State.Connected.name());
}
setRemoteEndpointProtocolVersion(clientProtoVersion);
if (isNotBlank(clientVersion) && !clientVersion.contains(" ")) /* ignore default version: pulsar client */
{
this.clientVersion = clientVersion.intern();
}
BrokerInterceptor brokerInterceptor = getBrokerService().getInterceptor();
if (brokerInterceptor != null) {
brokerInterceptor.onConnectionCreated(this);
}
}
use of org.apache.pulsar.broker.intercept.BrokerInterceptor in project incubator-pulsar by apache.
the class TransactionTest method testEndTxnWhenCommittingOrAborting.
@Test
public void testEndTxnWhenCommittingOrAborting() throws Exception {
Transaction commitTxn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
Transaction abortTxn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
Class<TransactionImpl> transactionClass = TransactionImpl.class;
Field field = transactionClass.getDeclaredField("state");
field.setAccessible(true);
field.set(commitTxn, TransactionImpl.State.COMMITTING);
field.set(abortTxn, TransactionImpl.State.ABORTING);
BrokerInterceptor listener = getPulsarServiceList().get(0).getBrokerInterceptor();
assertEquals(((CounterBrokerInterceptor) listener).getTxnCount(), 2);
abortTxn.abort().get();
assertEquals(((CounterBrokerInterceptor) listener).getAbortedTxnCount(), 1);
commitTxn.commit().get();
assertEquals(((CounterBrokerInterceptor) listener).getCommittedTxnCount(), 1);
}
use of org.apache.pulsar.broker.intercept.BrokerInterceptor in project incubator-pulsar by apache.
the class ProcessHandlerFilterTest method testChainDoFilter.
@Test
public void testChainDoFilter() throws ServletException, IOException {
PulsarService mockPulsarService = Mockito.mock(PulsarService.class);
BrokerInterceptor spyInterceptor = Mockito.mock(BrokerInterceptor.class);
HttpServletResponse mockHttpServletResponse = Mockito.mock(HttpServletResponse.class);
ServiceConfiguration mockConfig = Mockito.mock(ServiceConfiguration.class);
FilterChain spyFilterChain = Mockito.spy(FilterChain.class);
Mockito.doReturn(spyInterceptor).when(mockPulsarService).getBrokerInterceptor();
Mockito.doReturn(mockConfig).when(mockPulsarService).getConfig();
Mockito.doReturn(Sets.newHashSet()).when(mockConfig).getBrokerInterceptors();
// empty interceptor list
HttpServletRequest mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
ProcessHandlerFilter processHandlerFilter = new ProcessHandlerFilter(mockPulsarService);
processHandlerFilter.doFilter(mockHttpServletRequest, mockHttpServletResponse, spyFilterChain);
Mockito.verify(spyFilterChain).doFilter(mockHttpServletRequest, mockHttpServletResponse);
Mockito.clearInvocations(spyFilterChain);
// request has MULTIPART_FORM_DATA content-type
Mockito.doReturn(Sets.newHashSet("Interceptor1", "Interceptor2")).when(mockConfig).getBrokerInterceptors();
HttpServletRequest mockHttpServletRequest2 = Mockito.mock(HttpServletRequest.class);
Mockito.doReturn(MediaType.MULTIPART_FORM_DATA).when(mockHttpServletRequest2).getContentType();
ProcessHandlerFilter processHandlerFilter2 = new ProcessHandlerFilter(mockPulsarService);
processHandlerFilter2.doFilter(mockHttpServletRequest2, mockHttpServletResponse, spyFilterChain);
Mockito.verify(spyFilterChain).doFilter(mockHttpServletRequest2, mockHttpServletResponse);
Mockito.clearInvocations(spyFilterChain);
// request has APPLICATION_OCTET_STREAM content-type
Mockito.doReturn(MediaType.APPLICATION_OCTET_STREAM).when(mockHttpServletRequest2).getContentType();
processHandlerFilter2.doFilter(mockHttpServletRequest2, mockHttpServletResponse, spyFilterChain);
Mockito.verify(spyFilterChain).doFilter(mockHttpServletRequest2, mockHttpServletResponse);
}
Aggregations