1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.websocket.mapper; |
20 | |
|
21 | |
import org.apache.johnzon.mapper.Mapper; |
22 | |
import org.apache.johnzon.websocket.internal.mapper.MapperLocator; |
23 | |
|
24 | |
import java.io.Reader; |
25 | |
import java.lang.annotation.Annotation; |
26 | |
import java.lang.reflect.Method; |
27 | |
import java.lang.reflect.Type; |
28 | |
import javax.websocket.DecodeException; |
29 | |
import javax.websocket.Decoder; |
30 | |
import javax.websocket.EndpointConfig; |
31 | |
import javax.websocket.OnMessage; |
32 | |
import javax.websocket.Session; |
33 | |
import javax.websocket.server.PathParam; |
34 | |
import javax.websocket.server.ServerEndpointConfig; |
35 | |
|
36 | |
public class JohnzonTextDecoder implements Decoder.TextStream<Object> { |
37 | |
protected Mapper mapper; |
38 | |
protected Type type; |
39 | |
|
40 | 0 | public JohnzonTextDecoder() { |
41 | |
|
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
public JohnzonTextDecoder(final Type type) { |
46 | 0 | this(null, type); |
47 | 0 | } |
48 | |
|
49 | 0 | public JohnzonTextDecoder(final Mapper mapper, final Type type) { |
50 | 0 | this.mapper = mapper; |
51 | 0 | this.type = type; |
52 | 0 | } |
53 | |
|
54 | |
@Override |
55 | |
public Object decode(final Reader stream) throws DecodeException { |
56 | 0 | return mapper.readObject(stream, type); |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public void init(final EndpointConfig endpointConfig) { |
61 | 0 | if (mapper == null) { |
62 | 0 | mapper = MapperLocator.locate(); |
63 | |
} |
64 | 0 | if (type != null) { |
65 | 0 | return; |
66 | |
} |
67 | |
|
68 | 0 | if (ServerEndpointConfig.class.isInstance(endpointConfig)) { |
69 | 0 | final Class<?> endpointClass = ServerEndpointConfig.class.cast(endpointConfig).getEndpointClass(); |
70 | 0 | for (final Method m : endpointClass.getMethods()) { |
71 | 0 | if (Object.class == m.getDeclaringClass()) { |
72 | 0 | continue; |
73 | |
} |
74 | 0 | if (m.getAnnotation(OnMessage.class) != null) { |
75 | 0 | final Type[] genericParameterTypes = m.getGenericParameterTypes(); |
76 | 0 | for (int i = 0; i < genericParameterTypes.length; i++) { |
77 | 0 | if (genericParameterTypes[i] == Session.class) { |
78 | 0 | continue; |
79 | |
} |
80 | 0 | boolean param = false; |
81 | 0 | for (final Annotation a : m.getParameterAnnotations()[i]) { |
82 | 0 | if (PathParam.class == a.annotationType()) { |
83 | 0 | param = true; |
84 | 0 | break; |
85 | |
} |
86 | |
} |
87 | 0 | if (!param) { |
88 | 0 | this.type = genericParameterTypes[i]; |
89 | 0 | break; |
90 | |
} |
91 | |
} |
92 | 0 | break; |
93 | |
} |
94 | |
} |
95 | 0 | if (type == null) { |
96 | 0 | throw new IllegalArgumentException("didn't find @OnMessage in " + endpointClass); |
97 | |
} |
98 | 0 | } else { |
99 | 0 | type = Type.class.cast(endpointConfig.getUserProperties().get("johnzon.websocket.message.type")); |
100 | 0 | if (type == null) { |
101 | 0 | throw new IllegalArgumentException("didn't find johnzon.websocket.message.type"); |
102 | |
} |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
public void destroy() { |
108 | |
|
109 | 0 | } |
110 | |
} |