Search in sources :

Example 1 with Extension

use of org.eclipse.jetty.websocket.api.extensions.Extension in project jetty.project by eclipse.

the class IdentityExtensionTest method testOutgoingFrames.

/**
     * Verify that outgoing frames are unmodified
     * @throws IOException on test failure
     */
@Test
public void testOutgoingFrames() throws IOException {
    OutgoingFramesCapture capture = new OutgoingFramesCapture();
    Extension ext = new IdentityExtension();
    ext.setNextOutgoingFrames(capture);
    Frame frame = new TextFrame().setPayload("hello");
    ext.outgoingFrame(frame, null, BatchMode.OFF);
    capture.assertFrameCount(1);
    capture.assertHasFrame(OpCode.TEXT, 1);
    WebSocketFrame actual = capture.getFrames().getFirst();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.TEXT));
    Assert.assertThat("Frame.fin", actual.isFin(), is(true));
    Assert.assertThat("Frame.rsv1", actual.isRsv1(), is(false));
    Assert.assertThat("Frame.rsv2", actual.isRsv2(), is(false));
    Assert.assertThat("Frame.rsv3", actual.isRsv3(), is(false));
    ByteBuffer expected = BufferUtil.toBuffer("hello", StandardCharsets.UTF_8);
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(expected.remaining()));
    ByteBufferAssert.assertEquals("Frame.payload", expected, actual.getPayload().slice());
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension) IdentityExtension(org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IdentityExtension(org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) OutgoingFramesCapture(org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture) Test(org.junit.Test)

Example 2 with Extension

use of org.eclipse.jetty.websocket.api.extensions.Extension in project jetty.project by eclipse.

the class ExtensionStack method negotiate.

/**
     * Perform the extension negotiation.
     * <p>
     * For the list of negotiated extensions, use {@link #getNegotiatedExtensions()}
     * 
     * @param configs
     *            the configurations being requested
     */
public void negotiate(List<ExtensionConfig> configs) {
    if (LOG.isDebugEnabled())
        LOG.debug("Extension Configs={}", configs);
    this.extensions = new ArrayList<>();
    String[] rsvClaims = new String[3];
    for (ExtensionConfig config : configs) {
        Extension ext = factory.newInstance(config);
        if (ext == null) {
            // Extension not present on this side
            continue;
        }
        // Check RSV
        if (ext.isRsv1User() && (rsvClaims[0] != null)) {
            LOG.debug("Not adding extension {}. Extension {} already claimed RSV1", config, rsvClaims[0]);
            continue;
        }
        if (ext.isRsv2User() && (rsvClaims[1] != null)) {
            LOG.debug("Not adding extension {}. Extension {} already claimed RSV2", config, rsvClaims[1]);
            continue;
        }
        if (ext.isRsv3User() && (rsvClaims[2] != null)) {
            LOG.debug("Not adding extension {}. Extension {} already claimed RSV3", config, rsvClaims[2]);
            continue;
        }
        // Add Extension
        extensions.add(ext);
        addBean(ext);
        if (LOG.isDebugEnabled())
            LOG.debug("Adding Extension: {}", config);
        // Record RSV Claims
        if (ext.isRsv1User()) {
            rsvClaims[0] = ext.getName();
        }
        if (ext.isRsv2User()) {
            rsvClaims[1] = ext.getName();
        }
        if (ext.isRsv3User()) {
            rsvClaims[2] = ext.getName();
        }
    }
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)

Example 3 with Extension

use of org.eclipse.jetty.websocket.api.extensions.Extension in project jetty.project by eclipse.

the class WebSocketExtensionFactory method newInstance.

@Override
public Extension newInstance(ExtensionConfig config) {
    if (config == null) {
        return null;
    }
    String name = config.getName();
    if (StringUtil.isBlank(name)) {
        return null;
    }
    Class<? extends Extension> extClass = getExtension(name);
    if (extClass == null) {
        return null;
    }
    try {
        Extension ext = container.getObjectFactory().createInstance(extClass);
        if (ext instanceof AbstractExtension) {
            AbstractExtension aext = (AbstractExtension) ext;
            aext.init(container);
            aext.setConfig(config);
        }
        return ext;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new WebSocketException("Cannot instantiate extension: " + extClass, e);
    }
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException)

Example 4 with Extension

use of org.eclipse.jetty.websocket.api.extensions.Extension in project jetty.project by eclipse.

the class ExtensionStack method toString.

@Override
public String toString() {
    StringBuilder s = new StringBuilder();
    s.append("ExtensionStack[");
    s.append("queueSize=").append(getQueueSize());
    s.append(",extensions=");
    if (extensions == null) {
        s.append("<null>");
    } else {
        s.append('[');
        boolean delim = false;
        for (Extension ext : extensions) {
            if (delim) {
                s.append(',');
            }
            if (ext == null) {
                s.append("<null>");
            } else {
                s.append(ext.getName());
            }
            delim = true;
        }
        s.append(']');
    }
    s.append(",incoming=").append((this.nextIncoming == null) ? "<null>" : this.nextIncoming.getClass().getName());
    s.append(",outgoing=").append((this.nextOutgoing == null) ? "<null>" : this.nextOutgoing.getClass().getName());
    s.append("]");
    return s.toString();
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension)

Example 5 with Extension

use of org.eclipse.jetty.websocket.api.extensions.Extension in project jetty.project by eclipse.

the class ExtensionStack method dump.

@Override
public void dump(Appendable out, String indent) throws IOException {
    super.dump(out, indent);
    IncomingFrames websocket = getLastIncoming();
    OutgoingFrames network = getLastOutgoing();
    out.append(indent).append(" +- Stack").append(System.lineSeparator());
    out.append(indent).append("     +- Network  : ").append(network.toString()).append(System.lineSeparator());
    for (Extension ext : extensions) {
        out.append(indent).append("     +- Extension: ").append(ext.toString()).append(System.lineSeparator());
    }
    out.append(indent).append("     +- Websocket: ").append(websocket.toString()).append(System.lineSeparator());
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension) IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)

Aggregations

Extension (org.eclipse.jetty.websocket.api.extensions.Extension)8 IdentityExtension (org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension)3 Test (org.junit.Test)3 ByteBuffer (java.nio.ByteBuffer)2 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)2 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)2 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)2 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)2 ArrayList (java.util.ArrayList)1 ContainerLifeCycle (org.eclipse.jetty.util.component.ContainerLifeCycle)1 LifeCycle (org.eclipse.jetty.util.component.LifeCycle)1 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)1 IncomingFrames (org.eclipse.jetty.websocket.api.extensions.IncomingFrames)1 OutgoingFrames (org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)1 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)1 OutgoingFramesCapture (org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture)1