javax.websocket.Encoder Java Examples

The following examples show how to use javax.websocket.Encoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WsRemoteEndpointImplBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
protected void setEncoders(EndpointConfig endpointConfig)
        throws DeploymentException {
    encoderEntries.clear();
    for (Class<? extends Encoder> encoderClazz :
            endpointConfig.getEncoders()) {
        Encoder instance;
        try {
            instance = encoderClazz.getConstructor().newInstance();
            instance.init(endpointConfig);
        } catch (ReflectiveOperationException e) {
            throw new DeploymentException(
                    sm.getString("wsRemoteEndpoint.invalidEncoder",
                            encoderClazz.getName()), e);
        }
        EncoderEntry entry = new EncoderEntry(
                Util.getEncoderType(encoderClazz), instance);
        encoderEntries.add(entry);
    }
}
 
Example #2
Source File: TestWsRemoteEndpointImplServer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(
            Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>();
    encoders.add(Bug58624Encoder.class);
    ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(
            Bug58624Endpoint.class, PATH).encoders(encoders).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: WsServerContainer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private static void validateEncoders(Class<? extends Encoder>[] encoders)
        throws DeploymentException {

    for (Class<? extends Encoder> encoder : encoders) {
        // Need to instantiate decoder to ensure it is valid and that
        // deployment can be failed if it is not
        @SuppressWarnings("unused")
        Encoder instance;
        try {
            encoder.getConstructor().newInstance();
        } catch(ReflectiveOperationException e) {
            throw new DeploymentException(sm.getString(
                    "serverContainer.encoderFail", encoder.getName()), e);
        }
    }
}
 
Example #4
Source File: DefaultServerEndpointConfig.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
DefaultServerEndpointConfig(Class<?> endpointClass,
                                String path,
                                List<String> subprotocols,
                                List<Extension> extensions,
                                List<Class<? extends Encoder>> encoders,
                                List<Class<? extends Decoder>> decoders,
                                ServerEndpointConfig.Configurator serverEndpointConfigurator) {
    this.path = path;
    this.endpointClass = endpointClass;
    this.subprotocols = Collections.unmodifiableList(subprotocols);
    this.extensions = Collections.unmodifiableList(extensions);
    this.encoders = Collections.unmodifiableList(encoders);
    this.decoders = Collections.unmodifiableList(decoders);
    if (serverEndpointConfigurator == null) {
        this.serverEndpointConfigurator = ServerEndpointConfig.Configurator.fetchContainerDefaultConfigurator();
    } else{  
        this.serverEndpointConfigurator = serverEndpointConfigurator;
    }
}
 
Example #5
Source File: UndertowRequestUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private ConfiguredServerEndpoint createConfiguredServerEndpoint(String selectedProtocol,
		List<Extension> selectedExtensions, Endpoint endpoint, HttpServletRequest servletRequest) {

	String path = servletRequest.getRequestURI();  // shouldn't matter
	ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration(path, endpoint);
	endpointRegistration.setSubprotocols(Arrays.asList(selectedProtocol));
	endpointRegistration.setExtensions(selectedExtensions);

	EncodingFactory encodingFactory = new EncodingFactory(
			Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(),
			Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap(),
			Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(),
			Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap());
	try {
		return (endpointConstructorWithEndpointFactory ?
				endpointConstructor.newInstance(endpointRegistration,
						new EndpointInstanceFactory(endpoint), null, encodingFactory, null) :
				endpointConstructor.newInstance(endpointRegistration,
						new EndpointInstanceFactory(endpoint), null, encodingFactory));
	}
	catch (Exception ex) {
		throw new HandshakeFailureException("Failed to instantiate ConfiguredServerEndpoint", ex);
	}
}
 
Example #6
Source File: TestWsRemoteEndpointImplServer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(
            Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>();
    encoders.add(Bug58624Encoder.class);
    ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(
            Bug58624Endpoint.class, PATH).encoders(encoders).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source File: EncodingFactory.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
private static Class<?> findEncodeMethod(final Class<? extends Encoder> encoder, final Class<?> returnType, Class<?>... otherParameters) throws DeploymentException {
    for (Method method : encoder.getMethods()) {
        if (method.getName().equals("encode") && !method.isBridge() &&
                method.getParameterCount() == 1 + otherParameters.length &&
                method.getReturnType() == returnType) {
            boolean ok = true;
            for (int i = 1; i < method.getParameterCount(); ++i) {
                if (method.getParameterTypes()[i] != otherParameters[i - 1]) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                return method.getParameterTypes()[0];
            }
        }
    }
    throw JsrWebSocketMessages.MESSAGES.couldNotDetermineTypeOfEncodeMethodForClass(encoder);
}
 
Example #8
Source File: WsRemoteEndpointImplBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private Encoder findEncoder(Object obj) {
    for (EncoderEntry entry : encoderEntries) {
        if (entry.getClazz().isAssignableFrom(obj.getClass())) {
            return entry.getEncoder();
        }
    }
    return null;
}
 
Example #9
Source File: DefaultServerEndpointConfig.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
DefaultServerEndpointConfig(
        Class<?> endpointClass, String path,
        List<String> subprotocols, List<Extension> extensions,
        List<Class<? extends Encoder>> encoders,
        List<Class<? extends Decoder>> decoders,
        Configurator serverEndpointConfigurator) {
    this.endpointClass = endpointClass;
    this.path = path;
    this.subprotocols = subprotocols;
    this.extensions = extensions;
    this.encoders = encoders;
    this.decoders = decoders;
    this.serverEndpointConfigurator = serverEndpointConfigurator;
}
 
Example #10
Source File: WsRemoteEndpointImplBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private Encoder findEncoder(Object obj) {
    for (EncoderEntry entry : encoderEntries) {
        if (entry.getClazz().isAssignableFrom(obj.getClass())) {
            return entry.getEncoder();
        }
    }
    return null;
}
 
Example #11
Source File: ClassUtils.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the Object type for which the {@link Encoder} can be used.
 */
public static Class<?> getEncoderType(Class<? extends Encoder> clazz) {
    Method[] methods = clazz.getMethods();
    for (Method m : methods) {
        if ("encode".equals(m.getName()) && !m.isBridge()) {
            return m.getParameterTypes()[0];
        }
    }
    throw JsrWebSocketMessages.MESSAGES.unknownEncoderType(clazz);
}
 
Example #12
Source File: WsRemoteEndpointImplBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private Encoder findEncoder(Object obj) {
    for (EncoderEntry entry : encoderEntries) {
        if (entry.getClazz().isAssignableFrom(obj.getClass())) {
            return entry.getEncoder();
        }
    }
    return null;
}
 
Example #13
Source File: Encoding.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public ByteBuffer encodeBinary(final Object o) throws EncodeException {
    List<InstanceHandle<? extends Encoder>> encoders = binaryEncoders.get(o.getClass());

    if(encoders == null) {
        for(Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : binaryEncoders.entrySet()) {
            if(entry.getKey().isAssignableFrom(o.getClass())) {
                encoders = entry.getValue();
                break;
            }
        }
    }
    if (encoders != null) {
        for (InstanceHandle<? extends Encoder> decoderHandle : encoders) {
            Encoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Encoder.Binary) {
                return ((Encoder.Binary) decoder).encode(o);
            } else {
                try {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    ((Encoder.BinaryStream) decoder).encode(o, out);
                    return ByteBuffer.wrap(out.toByteArray());
                } catch (IOException e) {
                    throw new EncodeException(o, "Could not encode binary", e);
                }
            }
        }
    }
    throw new EncodeException(o, "Could not encode binary");
}
 
Example #14
Source File: Encoding.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public String encodeText(final Object o) throws EncodeException {
    List<InstanceHandle<? extends Encoder>> encoders = textEncoders.get(o.getClass());
    if(encoders == null) {
        for(Map.Entry<Class<?>, List<InstanceHandle<? extends Encoder>>> entry : textEncoders.entrySet()) {
            if(entry.getKey().isAssignableFrom(o.getClass())) {
                encoders = entry.getValue();
                break;
            }
        }
    }
    if (encoders != null) {
        for (InstanceHandle<? extends Encoder> decoderHandle : encoders) {
            Encoder decoder = decoderHandle.getInstance();
            if (decoder instanceof Encoder.Text) {
                return ((Encoder.Text) decoder).encode(o);
            } else {
                try {
                    StringWriter out = new StringWriter();
                    ((Encoder.TextStream) decoder).encode(o, out);
                    return out.toString();
                } catch (IOException e) {
                    throw new EncodeException(o, "Could not encode text", e);
                }
            }
        }
    }

    if (EncodingFactory.isPrimitiveOrBoxed(o.getClass())) {
        return o.toString();
    }
    throw new EncodeException(o, "Could not encode text");
}
 
Example #15
Source File: DefaultServerEndpointConfig.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
DefaultServerEndpointConfig(
        Class<?> endpointClass, String path,
        List<String> subprotocols, List<Extension> extensions,
        List<Class<? extends Encoder>> encoders,
        List<Class<? extends Decoder>> decoders,
        Configurator serverEndpointConfigurator) {
    this.endpointClass = endpointClass;
    this.path = path;
    this.subprotocols = subprotocols;
    this.extensions = extensions;
    this.encoders = encoders;
    this.decoders = decoders;
    this.serverEndpointConfigurator = serverEndpointConfigurator;
}
 
Example #16
Source File: ServerContainerInitializeListener.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
protected ServerEndpointConfig createServerEndpointConfig(ServletContext servletContext) {
    final List<Class<? extends Encoder>> encoders = new LinkedList<>();
    final List<Class<? extends Decoder>> decoders = new LinkedList<>();
    encoders.add(OutputMessageEncoder.class);
    decoders.add(InputMessageDecoder.class);
    final ServerEndpointConfig endpointConfig = create(WSConnectionImpl.class, "/ws")
            .configurator(createConfigurator()).encoders(encoders).decoders(decoders).build();
    endpointConfig.getUserProperties().put(EVERREST_PROCESSOR_ATTRIBUTE, getEverrestProcessor(servletContext));
    endpointConfig.getUserProperties().put(EVERREST_CONFIG_ATTRIBUTE, getEverrestConfiguration(servletContext));
    endpointConfig.getUserProperties().put(EXECUTOR_ATTRIBUTE, createExecutor(servletContext));
    return endpointConfig;
}
 
Example #17
Source File: TestWsRemoteEndpointImplServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected ServerEndpointConfig getServerEndpointConfig() {
    List<Class<? extends Encoder>> encoders = new ArrayList<>();
    encoders.add(Bug58624Encoder.class);
    return ServerEndpointConfig.Builder.create(
            Bug58624Endpoint.class, PATH).encoders(encoders).build();
}
 
Example #18
Source File: DefaultServerEndpointConfig.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
DefaultServerEndpointConfig(
        Class<?> endpointClass, String path,
        List<String> subprotocols, List<Extension> extensions,
        List<Class<? extends Encoder>> encoders,
        List<Class<? extends Decoder>> decoders,
        Configurator serverEndpointConfigurator) {
    this.endpointClass = endpointClass;
    this.path = path;
    this.subprotocols = subprotocols;
    this.extensions = extensions;
    this.encoders = encoders;
    this.decoders = decoders;
    this.serverEndpointConfigurator = serverEndpointConfigurator;
}
 
Example #19
Source File: ServerEndpointRegistration.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void setEncoders(List<Class<? extends Encoder>> encoders) {
	this.encoders = encoders;
}
 
Example #20
Source File: WsPerSessionServerEndpointConfig.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public List<Class<? extends Encoder>> getEncoders() {
    return perEndpointConfig.getEncoders();
}
 
Example #21
Source File: MessageEndpointConfig.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public List<Class<? extends Encoder>> getEncoders() {
  return Arrays.asList( MessageEncoder.class );
}
 
Example #22
Source File: DefaultServerEndpointConfig.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public List<Class<? extends Encoder>> getEncoders() {
    return this.encoders;
}
 
Example #23
Source File: TestEncodingDecoding.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    org.apache.tomcat.websocket.server.Constants.
                    SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(new ServerEndpointConfig() {
            @Override
            public Map<String, Object> getUserProperties() {
                return Collections.emptyMap();
            }
            @Override
            public List<Class<? extends Encoder>> getEncoders() {
                List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>(2);
                encoders.add(MsgStringEncoder.class);
                encoders.add(MsgByteEncoder.class);
                return encoders;
            }
            @Override
            public List<Class<? extends Decoder>> getDecoders() {
                List<Class<? extends Decoder>> decoders = new ArrayList<Class<? extends Decoder>>(2);
                decoders.add(MsgStringDecoder.class);
                decoders.add(MsgByteDecoder.class);
                return decoders;
            }
            @Override
            public List<String> getSubprotocols() {
                return Collections.emptyList();
            }
            @Override
            public String getPath() {
                return PATH_PROGRAMMATIC_EP;
            }
            @Override
            public List<Extension> getExtensions() {
                return Collections.emptyList();
            }
            @Override
            public Class<?> getEndpointClass() {
                return ProgrammaticEndpoint.class;
            }
            @Override
            public Configurator getConfigurator() {
                return new ServerEndpointConfig.Configurator() {
                };
            }
        });
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #24
Source File: DefaultServerEndpointConfig.java    From ameba with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public List<Class<? extends Encoder>> getEncoders() {
    return encoders;
}
 
Example #25
Source File: MockEndpointConfig.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public List<Class<? extends Encoder>> getEncoders() {
	return Collections.emptyList();
}
 
Example #26
Source File: QEndpointConfig.java    From qonduit with Apache License 2.0 4 votes vote down vote up
@Override
public List<Class<? extends Encoder>> getEncoders() {
    return Collections.emptyList();
}
 
Example #27
Source File: EndpointConnectionManager.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void setEncoders(List<Class<? extends Encoder>> encoders) {
	this.configBuilder.encoders(encoders);
}
 
Example #28
Source File: Util.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
static Class<?> getEncoderType(Class<? extends Encoder> encoder) {
    return Util.getGenericType(Encoder.class, encoder).getClazz();
}
 
Example #29
Source File: TestEncodingDecoding.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    org.apache.tomcat.websocket.server.Constants.
                    SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(new ServerEndpointConfig() {
            @Override
            public Map<String, Object> getUserProperties() {
                return Collections.emptyMap();
            }
            @Override
            public List<Class<? extends Encoder>> getEncoders() {
                List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>(2);
                encoders.add(MsgStringEncoder.class);
                encoders.add(MsgByteEncoder.class);
                return encoders;
            }
            @Override
            public List<Class<? extends Decoder>> getDecoders() {
                List<Class<? extends Decoder>> decoders = new ArrayList<Class<? extends Decoder>>(2);
                decoders.add(MsgStringDecoder.class);
                decoders.add(MsgByteDecoder.class);
                return decoders;
            }
            @Override
            public List<String> getSubprotocols() {
                return Collections.emptyList();
            }
            @Override
            public String getPath() {
                return PATH_PROGRAMMATIC_EP;
            }
            @Override
            public List<Extension> getExtensions() {
                return Collections.emptyList();
            }
            @Override
            public Class<?> getEndpointClass() {
                return ProgrammaticEndpoint.class;
            }
            @Override
            public Configurator getConfigurator() {
                return new ServerEndpointConfig.Configurator() {
                };
            }
        });
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #30
Source File: ServerEndpointRegistration.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public List<Class<? extends Encoder>> getEncoders() {
	return this.encoders;
}