下面列出了org.springframework.core.convert.ConverterNotFoundException#javax.websocket.Decoder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Object decode(String message) throws DecodeException {
// Handle primitives
if (primitiveType != null) {
return Util.coerceToType(primitiveType, message);
}
// Handle full decoders
for (Decoder decoder : decoders) {
if (decoder instanceof Text) {
if (((Text<?>) decoder).willDecode(message)) {
return ((Text<?>) decoder).decode(message);
}
} else {
StringReader r = new StringReader(message);
try {
return ((TextStream<?>) decoder).decode(r);
} catch (IOException ioe) {
throw new DecodeException(message, sm.getString(
"pojoMessageHandlerWhole.decodeIoFail"), ioe);
}
}
}
return null;
}
public static List<DecoderEntry> getDecoders(
List<Class<? extends Decoder>> decoderClazzes)
throws DeploymentException{
List<DecoderEntry> result = new ArrayList<>();
if (decoderClazzes != null) {
for (Class<? extends Decoder> decoderClazz : decoderClazzes) {
// Need to instantiate decoder to ensure it is valid and that
// deployment can be failed if it is not
@SuppressWarnings("unused")
Decoder instance;
try {
instance = decoderClazz.getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new DeploymentException(
sm.getString("pojoMethodMapping.invalidDecoder",
decoderClazz.getName()), e);
}
DecoderEntry entry = new DecoderEntry(
Util.getDecoderType(decoderClazz), decoderClazz);
result.add(entry);
}
}
return result;
}
public Object decodeBinary(final Class<?> targetType, final byte[] bytes) throws DecodeException {
List<InstanceHandle<? extends Decoder>> decoders = binaryDecoders.get(targetType);
if (decoders != null) {
for (InstanceHandle<? extends Decoder> decoderHandle : decoders) {
Decoder decoder = decoderHandle.getInstance();
if (decoder instanceof Decoder.Binary) {
if (((Decoder.Binary) decoder).willDecode(ByteBuffer.wrap(bytes))) {
return ((Decoder.Binary) decoder).decode(ByteBuffer.wrap(bytes));
}
} else {
try {
return ((Decoder.BinaryStream) decoder).decode(new ByteArrayInputStream(bytes));
} catch (IOException e) {
throw new DecodeException(ByteBuffer.wrap(bytes), "Could not decode binary", e);
}
}
}
}
throw new DecodeException(ByteBuffer.wrap(bytes), "Could not decode binary");
}
@Override
protected Object decode(ByteBuffer message) throws DecodeException {
for (Decoder decoder : decoders) {
if (decoder instanceof Binary) {
if (((Binary<?>) decoder).willDecode(message)) {
return ((Binary<?>) decoder).decode(message);
}
} else {
byte[] array = new byte[message.limit() - message.position()];
message.get(array);
ByteArrayInputStream bais = new ByteArrayInputStream(array);
try {
return ((BinaryStream<?>) decoder).decode(bais);
} catch (IOException ioe) {
throw new DecodeException(message, sm.getString(
"pojoMessageHandlerWhole.decodeIoFail"), ioe);
}
}
}
return null;
}
@Override
protected Object decode(String message) throws DecodeException {
// Handle primitives
if (primitiveType != null) {
return Util.coerceToType(primitiveType, message);
}
// Handle full decoders
for (Decoder decoder : decoders) {
if (decoder instanceof Text) {
if (((Text<?>) decoder).willDecode(message)) {
return ((Text<?>) decoder).decode(message);
}
} else {
StringReader r = new StringReader(message);
try {
return ((TextStream<?>) decoder).decode(r);
} catch (IOException ioe) {
throw new DecodeException(message, sm.getString(
"pojoMessageHandlerWhole.decodeIoFail"), ioe);
}
}
}
return null;
}
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;
}
}
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);
}
}
@Override
protected Object decode(ByteBuffer message) throws DecodeException {
for (Decoder decoder : decoders) {
if (decoder instanceof Binary) {
if (((Binary<?>) decoder).willDecode(message)) {
return ((Binary<?>) decoder).decode(message);
}
} else {
byte[] array = new byte[message.limit() - message.position()];
message.get(array);
ByteArrayInputStream bais = new ByteArrayInputStream(array);
try {
return ((BinaryStream<?>) decoder).decode(bais);
} catch (IOException ioe) {
throw new DecodeException(message, sm.getString(
"pojoMessageHandlerWhole.decodeIoFail"), ioe);
}
}
}
return null;
}
@Override
protected Object decode(String message) throws DecodeException {
// Handle primitives
if (primitiveType != null) {
return Util.coerceToType(primitiveType, message);
}
// Handle full decoders
for (Decoder decoder : decoders) {
if (decoder instanceof Text) {
if (((Text<?>) decoder).willDecode(message)) {
return ((Text<?>) decoder).decode(message);
}
} else {
StringReader r = new StringReader(message);
try {
return ((TextStream<?>) decoder).decode(r);
} catch (IOException ioe) {
throw new DecodeException(message, sm.getString(
"pojoMessageHandlerWhole.decodeIoFail"), ioe);
}
}
}
return null;
}
public PojoEndpointClient(Object pojo,
List<Class<? extends Decoder>> decoders) throws DeploymentException {
setPojo(pojo);
setMethodMapping(
new PojoMethodMapping(pojo.getClass(), decoders, null));
setPathParameters(Collections.<String,String>emptyMap());
}
private static List<Class<? extends Decoder>> matchDecoders(Class<?> target,
EndpointConfig endpointConfig, boolean binary) {
DecoderMatch decoderMatch = matchDecoders(target, endpointConfig);
if (binary) {
if (decoderMatch.getBinaryDecoders().size() > 0) {
return decoderMatch.getBinaryDecoders();
}
} else if (decoderMatch.getTextDecoders().size() > 0) {
return decoderMatch.getTextDecoders();
}
return null;
}
private static DecoderMatch matchDecoders(Class<?> target,
EndpointConfig endpointConfig) {
DecoderMatch decoderMatch;
try {
List<Class<? extends Decoder>> decoders =
endpointConfig.getDecoders();
List<DecoderEntry> decoderEntries = getDecoders(decoders);
decoderMatch = new DecoderMatch(target, decoderEntries);
} catch (DeploymentException e) {
throw new IllegalArgumentException(e);
}
return decoderMatch;
}
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;
}
@Test
public void decodeFromTextCannotConvert() throws Exception {
setup(NoConvertersConfig.class);
Decoder.Text<MyType> decoder = new MyTextDecoder();
assertThat(decoder.willDecode(CONVERTED_TEXT), is(false));
assertThatExceptionOfType(DecodeException.class).isThrownBy(() ->
decoder.decode(CONVERTED_TEXT))
.withCauseInstanceOf(ConverterNotFoundException.class);
}
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;
}
/**
* Gets a decoder for a given type.
*
* @param type The type
* @param endpointConfiguration The endpoint configuration
* @return A list of decoders, or null if no decoders exist
*/
public static List<Decoder> getDecodersForType(final Class<?> type, final EndpointConfig endpointConfiguration) {
// final List<Decoder> decoders = new ArrayList<>();
// for (final Decoder decoder : endpointConfiguration.getDecoders()) {
// final Class<?> clazz = ClassUtils.getDecoderType(decoder.getClass());
// if (type.isAssignableFrom(clazz)) {
// decoders.add(decoder);
// }
// }
// if (!decoders.isEmpty()) {
// return decoders;
// }
return null;
}
/**
* Returns the Object type for which the {@link Encoder} can be used.
*/
public static Class<?> getDecoderType(Class<? extends Decoder> clazz) {
Method[] methods = clazz.getMethods();
for (Method m : methods) {
if ("decode".equals(m.getName()) && !m.isBridge()) {
return m.getReturnType();
}
}
throw JsrWebSocketMessages.MESSAGES.couldNotDetermineDecoderTypeFor(clazz);
}
@Test
public void decodeFromTextCannotConvert() throws Exception {
setup(NoConvertersConfig.class);
Decoder.Text<MyType> decoder = new MyTextDecoder();
assertThat(decoder.willDecode(CONVERTED_TEXT), is(false));
thown.expect(DecodeException.class);
thown.expectCause(isA(ConverterNotFoundException.class));
decoder.decode(CONVERTED_TEXT);
}
public PojoEndpointClient(Object pojo,
List<Class<? extends Decoder>> decoders) throws DeploymentException {
setPojo(pojo);
setMethodMapping(
new PojoMethodMapping(pojo.getClass(), decoders, null));
setPathParameters(Collections.<String,String>emptyMap());
}
private static List<Class<? extends Decoder>> matchDecoders(Class<?> target,
EndpointConfig endpointConfig, boolean binary) {
DecoderMatch decoderMatch = matchDecoders(target, endpointConfig);
if (binary) {
if (decoderMatch.getBinaryDecoders().size() > 0) {
return decoderMatch.getBinaryDecoders();
}
} else if (decoderMatch.getTextDecoders().size() > 0) {
return decoderMatch.getTextDecoders();
}
return null;
}
private static DecoderMatch matchDecoders(Class<?> target,
EndpointConfig endpointConfig) {
DecoderMatch decoderMatch;
try {
List<Class<? extends Decoder>> decoders =
endpointConfig.getDecoders();
List<DecoderEntry> decoderEntries = getDecoders(decoders);
decoderMatch = new DecoderMatch(target, decoderEntries);
} catch (DeploymentException e) {
throw new IllegalArgumentException(e);
}
return decoderMatch;
}
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;
}
private static List<Class<? extends Decoder>> matchDecoders(Class<?> target,
EndpointConfig endpointConfig, boolean binary) {
DecoderMatch decoderMatch = matchDecoders(target, endpointConfig);
if (binary) {
if (decoderMatch.getBinaryDecoders().size() > 0) {
return decoderMatch.getBinaryDecoders();
}
} else if (decoderMatch.getTextDecoders().size() > 0) {
return decoderMatch.getTextDecoders();
}
return null;
}
@Test
public void decodeFromTextCannotConvert() throws Exception {
setup(NoConvertersConfig.class);
Decoder.Text<MyType> decoder = new MyTextDecoder();
assertThat(decoder.willDecode(CONVERTED_TEXT), is(false));
thown.expect(DecodeException.class);
thown.expectCause(isA(ConverterNotFoundException.class));
decoder.decode(CONVERTED_TEXT);
}
@Test
public void decodeFromBinaryCannotConvert() throws Exception {
setup(NoConvertersConfig.class);
Decoder.Binary<MyType> decoder = new MyBinaryDecoder();
assertThat(decoder.willDecode(CONVERTED_BYTES), is(false));
thown.expect(DecodeException.class);
thown.expectCause(isA(ConverterNotFoundException.class));
decoder.decode(CONVERTED_BYTES);
}
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;
}
public List<Class<? extends Decoder>> getBinaryDecoders() {
return binaryDecoders;
}
@Override
protected void onClose() {
for (Decoder decoder : decoders) {
decoder.destroy();
}
}
@Override
protected void onClose() {
for (Decoder decoder : decoders) {
decoder.destroy();
}
}
public DecoderEntry(Class<?> clazz,
Class<? extends Decoder> decoderClazz) {
this.clazz = clazz;
this.decoderClazz = decoderClazz;
}