Author: markt Date: Fri Mar 22 22:01:21 2013 New Revision: 1460023 URL: http://svn.apache.org/r1460023 Log: More plumbing for handling primitives. Still some bits missing.
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties tomcat/trunk/java/org/apache/tomcat/websocket/Util.java tomcat/trunk/java/org/apache/tomcat/websocket/pojo/LocalStrings.properties tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMessageHandlerWholeText.java tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMethodMapping.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties?rev=1460023&r1=1460022&r2=1460023&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties Fri Mar 22 22:01:21 2013 @@ -15,6 +15,8 @@ backgroundProcessManager.processFailed=A background process failed +util.invalidType=Unable to coerce value [{0}] to type [{1}]. That type is not supported. + # Note the wsFrame.* messages are used as close reasons in WebSocket control # frames and therefore must be 123 bytes (not characters) or less in length. # Messages are encoded using UTF-8 where a single character may be encoded in Modified: tomcat/trunk/java/org/apache/tomcat/websocket/Util.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/Util.java?rev=1460023&r1=1460022&r2=1460023&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/Util.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/Util.java Fri Mar 22 22:01:21 2013 @@ -30,12 +30,16 @@ import javax.websocket.Decoder; import javax.websocket.Encoder; import javax.websocket.MessageHandler; +import org.apache.tomcat.util.res.StringManager; + /** * Utility class for internal use only within the * {@link org.apache.tomcat.websocket} package. */ public class Util { + private static final StringManager sm = + StringManager.getManager(Constants.PACKAGE_NAME); private static final Queue<SecureRandom> randoms = new ConcurrentLinkedQueue<>(); @@ -234,4 +238,31 @@ public class Util { } return false; } + + + public static Object coerceToType(Class<?> type, String value) { + if (type.equals(String.class)) { + return value; + } else if (type.equals(boolean.class) || type.equals(Boolean.class)) { + return Boolean.valueOf(value); + } else if (type.equals(byte.class) || type.equals(Byte.class)) { + return Byte.valueOf(value); + } else if (value.length() == 1 && + (type.equals(char.class) || type.equals(Character.class))) { + return Character.valueOf(value.charAt(0)); + } else if (type.equals(double.class) || type.equals(Double.class)) { + return Double.valueOf(value); + } else if (type.equals(float.class) || type.equals(Float.class)) { + return Float.valueOf(value); + } else if (type.equals(int.class) || type.equals(Integer.class)) { + return Integer.valueOf(value); + } else if (type.equals(long.class) || type.equals(Long.class)) { + return Long.valueOf(value); + } else if (type.equals(short.class) || type.equals(Short.class)) { + return Short.valueOf(value); + } else { + throw new IllegalArgumentException(sm.getString( + "util.invalidType", value, type.getName())); + } + } } Modified: tomcat/trunk/java/org/apache/tomcat/websocket/pojo/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/pojo/LocalStrings.properties?rev=1460023&r1=1460022&r2=1460023&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/pojo/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/pojo/LocalStrings.properties Fri Mar 22 22:01:21 2013 @@ -22,7 +22,6 @@ pojoMethodMapping.duplicateLastParam=Mul pojoMethodMapping.duplicateMessageParam=Multiple message parameters present on the method [{0}] of class [{1}] that was annotated with OnMessage pojoMethodMapping.duplicatePongMessageParam=Multiple PongMessage parameters present on the method [{0}] of class [{1}] that was annotated with OnMessage pojoMethodMapping.duplicateSessionParam=Multiple session parameters present on the method [{0}] of class [{1}] that was annotated with OnMessage -pojoMethodMapping.invalidType=Unable to coerce value [{0}] to type [{1}]. That type is not supported. pojoMethodMapping.noPayload=No payload parameter present on the method [{0}] of class [{1}] that was annotated with OnMessage pojoMethodMapping.onErrorNoThrowable=No Throwable parameter was present on the method [{0}] of class [{1}] that was annotated with OnError pojoMethodMapping.partialInputStream=Invalid InputStream and boolean parameters present on the method [{0}] of class [{1}] that was annotated with OnMessage Modified: tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMessageHandlerWholeText.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMessageHandlerWholeText.java?rev=1460023&r1=1460022&r2=1460023&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMessageHandlerWholeText.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMessageHandlerWholeText.java Fri Mar 22 22:01:21 2013 @@ -30,6 +30,8 @@ import javax.websocket.EndpointConfig; import javax.websocket.Session; import org.apache.tomcat.util.res.StringManager; +import org.apache.tomcat.websocket.Util; + /** * Text specific concrete implementation for handling whole messages. @@ -41,12 +43,23 @@ public class PojoMessageHandlerWholeText StringManager.getManager(Constants.PACKAGE_NAME); private final List<Decoder> decoders = new ArrayList<>(); + private final Class<?> primitiveType; public PojoMessageHandlerWholeText(Object pojo, Method method, Session session, EndpointConfig config, Object[] params, int indexPayload, boolean convert, int indexSession) { super(pojo, method, session, params, indexPayload, convert, indexSession); + + // Check for primitives + Class<?> type = method.getParameterTypes()[indexPayload]; + if (Util.isPrimitive(type)) { + primitiveType = type; + return; + } else { + primitiveType = null; + } + try { for (Class<? extends Decoder> decoderClazz : config.getDecoders()) { if (Text.class.isAssignableFrom(decoderClazz)) { @@ -70,6 +83,11 @@ public class PojoMessageHandlerWholeText @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)) { Modified: tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMethodMapping.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMethodMapping.java?rev=1460023&r1=1460022&r2=1460023&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMethodMapping.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/pojo/PojoMethodMapping.java Fri Mar 22 22:01:21 2013 @@ -236,7 +236,7 @@ public class PojoMethodMapping { if (value == null) { result[i] = null; } else { - result[i] = coerceToType(type, value); + result[i] = Util.coerceToType(type, value); } } } @@ -244,33 +244,6 @@ public class PojoMethodMapping { } - private static Object coerceToType(Class<?> type, String value) { - if (type.equals(String.class)) { - return value; - } else if (type.equals(boolean.class) || type.equals(Boolean.class)) { - return Boolean.valueOf(value); - } else if (type.equals(byte.class) || type.equals(Byte.class)) { - return Byte.valueOf(value); - } else if (value.length() == 1 && - (type.equals(char.class) || type.equals(Character.class))) { - return Character.valueOf(value.charAt(0)); - } else if (type.equals(double.class) || type.equals(Double.class)) { - return Double.valueOf(value); - } else if (type.equals(float.class) || type.equals(Float.class)) { - return Float.valueOf(value); - } else if (type.equals(int.class) || type.equals(Integer.class)) { - return Integer.valueOf(value); - } else if (type.equals(long.class) || type.equals(Long.class)) { - return Long.valueOf(value); - } else if (type.equals(short.class) || type.equals(Short.class)) { - return Short.valueOf(value); - } else { - throw new IllegalArgumentException(sm.getString( - "pojoMethodMapping.invalidType", value, type.getName())); - } - } - - private static class MessageMethod { private final Method m; @@ -509,7 +482,7 @@ public class PojoMethodMapping { String valueString = pathParameters.get(pathParam.getName()); Object value = null; if (valueString != null) { - value = coerceToType(pathParam.getType(), valueString); + value = Util.coerceToType(pathParam.getType(), valueString); } params[entry.getKey().intValue()] = value; } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org