Search in sources :

Example 1 with ByteBufferSender

use of org.apache.qpid.server.transport.ByteBufferSender in project qpid-broker-j by apache.

the class ProtocolEngine_1_0_0Test method setUp.

@Before
public void setUp() throws Exception {
    _networkConnection = mock(ServerNetworkConnection.class);
    when(_networkConnection.getLocalAddress()).thenReturn(new InetSocketAddress(0));
    when(_networkConnection.getSelectedHost()).thenReturn("localhost");
    _broker = mock(Broker.class);
    when(_broker.getModel()).thenReturn(BrokerModel.getInstance());
    when(_broker.getNetworkBufferSize()).thenReturn(256 * 1026);
    final TaskExecutor taskExecutor = new TaskExecutorImpl();
    taskExecutor.start();
    when(_broker.getChildExecutor()).thenReturn(taskExecutor);
    when(_broker.getTaskExecutor()).thenReturn(taskExecutor);
    when(_broker.getId()).thenReturn(UUID.randomUUID());
    when(_broker.getEventLogger()).thenReturn(new EventLogger());
    when(((Broker) _broker).getCategoryClass()).thenReturn(Broker.class);
    _port = mock(AmqpPort.class);
    when(_port.getChildExecutor()).thenReturn(taskExecutor);
    when(_port.getCategoryClass()).thenReturn(Port.class);
    when(_port.getModel()).thenReturn(BrokerModel.getInstance());
    final SubjectCreator subjectCreator = mock(SubjectCreator.class);
    _authenticationProvider = mock(AuthenticationProvider.class);
    when(_port.getAuthenticationProvider()).thenReturn(_authenticationProvider);
    _virtualHost = mock(VirtualHost.class);
    when(_virtualHost.getChildExecutor()).thenReturn(taskExecutor);
    when(_virtualHost.getModel()).thenReturn(BrokerModel.getInstance());
    when(_virtualHost.getState()).thenReturn(State.ACTIVE);
    when(_virtualHost.isActive()).thenReturn(true);
    final ArgumentCaptor<AMQPConnection> connectionCaptor = ArgumentCaptor.forClass(AMQPConnection.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            _connection = connectionCaptor.getValue();
            throw new SoleConnectionEnforcementPolicyException(null, Collections.emptySet(), "abc1");
        }
    }).when(_virtualHost).registerConnection(connectionCaptor.capture());
    when(_virtualHost.getPrincipal()).thenReturn(mock(VirtualHostPrincipal.class));
    when(_port.getAddressSpace(anyString())).thenReturn(_virtualHost);
    when(_port.getSubjectCreator(anyBoolean(), anyString())).thenReturn(subjectCreator);
    final ArgumentCaptor<Principal> userCaptor = ArgumentCaptor.forClass(Principal.class);
    when(subjectCreator.createSubjectWithGroups(userCaptor.capture())).then(new Answer<Subject>() {

        @Override
        public Subject answer(final InvocationOnMock invocation) throws Throwable {
            Subject subject = new Subject();
            subject.getPrincipals().add(userCaptor.getValue());
            return subject;
        }
    });
    final ByteBufferSender sender = mock(ByteBufferSender.class);
    when(_networkConnection.getSender()).thenReturn(sender);
    AMQPDescribedTypeRegistry registry = AMQPDescribedTypeRegistry.newInstance().registerTransportLayer().registerMessagingLayer().registerTransactionLayer().registerSecurityLayer();
    _frameWriter = new FrameWriter(registry, new ByteBufferSender() {

        @Override
        public boolean isDirectBufferPreferred() {
            return false;
        }

        @Override
        public void send(final QpidByteBuffer msg) {
            _protocolEngine_1_0_0.received(msg);
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() {
        }
    });
}
Also used : ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) InetSocketAddress(java.net.InetSocketAddress) VirtualHostPrincipal(org.apache.qpid.server.virtualhost.VirtualHostPrincipal) AmqpPort(org.apache.qpid.server.model.port.AmqpPort) ServerNetworkConnection(org.apache.qpid.server.transport.ServerNetworkConnection) TaskExecutorImpl(org.apache.qpid.server.configuration.updater.TaskExecutorImpl) AMQPDescribedTypeRegistry(org.apache.qpid.server.protocol.v1_0.type.codec.AMQPDescribedTypeRegistry) Broker(org.apache.qpid.server.model.Broker) SoleConnectionEnforcementPolicyException(org.apache.qpid.server.protocol.v1_0.type.extensions.soleconn.SoleConnectionEnforcementPolicyException) EventLogger(org.apache.qpid.server.logging.EventLogger) AuthenticationProvider(org.apache.qpid.server.model.AuthenticationProvider) Subject(javax.security.auth.Subject) FrameWriter(org.apache.qpid.server.protocol.v1_0.codec.FrameWriter) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) TaskExecutor(org.apache.qpid.server.configuration.updater.TaskExecutor) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) VirtualHost(org.apache.qpid.server.model.VirtualHost) SubjectCreator(org.apache.qpid.server.security.SubjectCreator) VirtualHostPrincipal(org.apache.qpid.server.virtualhost.VirtualHostPrincipal) Principal(java.security.Principal) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) Before(org.junit.Before)

Example 2 with ByteBufferSender

use of org.apache.qpid.server.transport.ByteBufferSender in project qpid-broker-j by apache.

the class FrameEncoder method encode.

@Override
public ByteBuffer encode(final Object msg) {
    if (msg instanceof AMQFrame) {
        List<ByteBuffer> buffers = new ArrayList<>();
        FrameWriter _frameWriter = new FrameWriter(TYPE_REGISTRY, new ByteBufferSender() {

            @Override
            public boolean isDirectBufferPreferred() {
                return false;
            }

            @Override
            public void send(final QpidByteBuffer msg) {
                byte[] data = new byte[msg.remaining()];
                msg.get(data);
                buffers.add(ByteBuffer.wrap(data));
            }

            @Override
            public void flush() {
            }

            @Override
            public void close() {
            }
        });
        AMQFrame frame = (AMQFrame) msg;
        FRAME_LOGGER.debug("SEND: " + frame.getFrameBody());
        _frameWriter.send(frame);
        int remaining = 0;
        for (ByteBuffer byteBuffer : buffers) {
            remaining += byteBuffer.remaining();
        }
        ByteBuffer result = ByteBuffer.allocate(remaining);
        for (ByteBuffer byteBuffer : buffers) {
            result.put(byteBuffer);
        }
        result.flip();
        return result;
    }
    return null;
}
Also used : ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ArrayList(java.util.ArrayList) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AMQFrame(org.apache.qpid.server.protocol.v1_0.framing.AMQFrame) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) FrameWriter(org.apache.qpid.server.protocol.v1_0.codec.FrameWriter)

Example 3 with ByteBufferSender

use of org.apache.qpid.server.transport.ByteBufferSender in project qpid-broker-j by apache.

the class FrameEncoder method encode.

@Override
public ByteBuffer encode(final Object msg) {
    if (msg instanceof ProtocolEvent) {
        final List<ByteBuffer> buffers = new ArrayList<>();
        final AtomicInteger totalSize = new AtomicInteger();
        Disassembler disassembler = new Disassembler(new ByteBufferSender() {

            @Override
            public boolean isDirectBufferPreferred() {
                return false;
            }

            @Override
            public void send(final QpidByteBuffer msg) {
                int remaining = msg.remaining();
                byte[] data = new byte[remaining];
                ByteBuffer byteBuffer = ByteBuffer.wrap(data);
                msg.get(data);
                buffers.add(byteBuffer);
                totalSize.addAndGet(remaining);
            }

            @Override
            public void flush() {
            }

            @Override
            public void close() {
            }
        }, 512);
        disassembler.send((ProtocolEvent) msg);
        ByteBuffer data = ByteBuffer.allocate(totalSize.get());
        for (ByteBuffer buffer : buffers) {
            data.put(buffer);
        }
        data.flip();
        return data;
    }
    return null;
}
Also used : ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ProtocolEvent(org.apache.qpid.server.protocol.v0_10.transport.ProtocolEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ByteBufferSender

use of org.apache.qpid.server.transport.ByteBufferSender in project qpid-broker-j by apache.

the class FrameDecoder method decode.

@Override
public Collection<Response<?>> decode(final ByteBuffer inputBuffer) throws Exception {
    _clientDecoder.decodeBuffer(inputBuffer);
    List<AMQDataBlock> receivedFrames = new ArrayList<>(_methodProcessor.getProcessedMethods());
    List<Response<?>> result = new ArrayList<>();
    for (AMQDataBlock frame : receivedFrames) {
        if (frame instanceof AMQFrame) {
            AMQFrame amqFrame = (AMQFrame) frame;
            if (amqFrame.getBodyFrame() instanceof ConnectionTuneBody) {
                ConnectionTuneBody tuneBody = (ConnectionTuneBody) amqFrame.getBodyFrame();
                _clientDecoder.setMaxFrameSize((int) tuneBody.getFrameMax());
            }
            result.add(new PerformativeResponse(amqFrame.getChannel(), amqFrame.getSize(), amqFrame.getBodyFrame()));
        } else if (frame instanceof ProtocolInitiation) {
            byte[] data = new byte[(int) frame.getSize()];
            frame.writePayload(new ByteBufferSender() {

                @Override
                public boolean isDirectBufferPreferred() {
                    return false;
                }

                @Override
                public void send(final QpidByteBuffer msg) {
                    msg.copyTo(data);
                }

                @Override
                public void flush() {
                }

                @Override
                public void close() {
                }
            });
            result.add(new HeaderResponse(data));
        } else {
            throw new IllegalArgumentException(String.format("Unexpected data block received %s", frame));
        }
    }
    _methodProcessor.getProcessedMethods().removeAll(receivedFrames);
    return result;
}
Also used : ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ArrayList(java.util.ArrayList) AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) Response(org.apache.qpid.tests.protocol.Response) HeaderResponse(org.apache.qpid.tests.protocol.HeaderResponse) HeaderResponse(org.apache.qpid.tests.protocol.HeaderResponse) ProtocolInitiation(org.apache.qpid.server.protocol.v0_8.transport.ProtocolInitiation) ConnectionTuneBody(org.apache.qpid.server.protocol.v0_8.transport.ConnectionTuneBody) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AMQFrame(org.apache.qpid.server.protocol.v0_8.transport.AMQFrame)

Example 5 with ByteBufferSender

use of org.apache.qpid.server.transport.ByteBufferSender in project qpid-broker-j by apache.

the class FrameEncoder method encode.

@Override
public ByteBuffer encode(final Object msg) {
    if (msg instanceof AMQDataBlock) {
        final List<ByteBuffer> buffers = new ArrayList<>();
        ((AMQDataBlock) msg).writePayload(new ByteBufferSender() {

            @Override
            public boolean isDirectBufferPreferred() {
                return false;
            }

            @Override
            public void send(final QpidByteBuffer msg) {
                byte[] data = new byte[msg.remaining()];
                msg.get(data);
                buffers.add(ByteBuffer.wrap(data));
            }

            @Override
            public void flush() {
            }

            @Override
            public void close() {
            }
        });
        int remaining = 0;
        for (ByteBuffer byteBuffer : buffers) {
            remaining += byteBuffer.remaining();
        }
        ByteBuffer result = ByteBuffer.allocate(remaining);
        for (ByteBuffer byteBuffer : buffers) {
            result.put(byteBuffer);
        }
        result.flip();
        return result;
    }
    return null;
}
Also used : AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ArrayList(java.util.ArrayList) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)5 ByteBufferSender (org.apache.qpid.server.transport.ByteBufferSender)5 ArrayList (java.util.ArrayList)4 ByteBuffer (java.nio.ByteBuffer)3 AMQDataBlock (org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock)2 FrameWriter (org.apache.qpid.server.protocol.v1_0.codec.FrameWriter)2 InetSocketAddress (java.net.InetSocketAddress)1 Principal (java.security.Principal)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Subject (javax.security.auth.Subject)1 TaskExecutor (org.apache.qpid.server.configuration.updater.TaskExecutor)1 TaskExecutorImpl (org.apache.qpid.server.configuration.updater.TaskExecutorImpl)1 EventLogger (org.apache.qpid.server.logging.EventLogger)1 AuthenticationProvider (org.apache.qpid.server.model.AuthenticationProvider)1 Broker (org.apache.qpid.server.model.Broker)1 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)1 VirtualHost (org.apache.qpid.server.model.VirtualHost)1 AmqpPort (org.apache.qpid.server.model.port.AmqpPort)1 ProtocolEvent (org.apache.qpid.server.protocol.v0_10.transport.ProtocolEvent)1 AMQFrame (org.apache.qpid.server.protocol.v0_8.transport.AMQFrame)1