1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.common.io.payload;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
24 import org.eclipse.jetty.util.BufferUtil;
25 import org.eclipse.jetty.util.Utf8Appendable;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
28 import org.eclipse.jetty.websocket.api.BadPayloadException;
29 import org.eclipse.jetty.websocket.api.extensions.Frame;
30
31
32
33
34 public class UTF8Validator extends Utf8Appendable implements PayloadProcessor
35 {
36 private static class EmptyAppender implements Appendable
37 {
38 private int length = 0;
39
40 @Override
41 public Appendable append(char c) throws IOException
42 {
43 length++;
44 return this;
45 }
46
47 @Override
48 public Appendable append(CharSequence csq) throws IOException
49 {
50 length += csq.length();
51 return this;
52 }
53
54 @Override
55 public Appendable append(CharSequence csq, int start, int end) throws IOException
56 {
57 length += (end - start);
58 return this;
59 }
60
61 public int getLength()
62 {
63 return length;
64 }
65 }
66
67 private static final Logger LOG = Log.getLogger(UTF8Validator.class);
68
69 private EmptyAppender buffer;
70
71 public UTF8Validator()
72 {
73 super(new EmptyAppender());
74 this.buffer = (EmptyAppender)_appendable;
75 }
76
77 @Override
78 public int length()
79 {
80 return this.buffer.getLength();
81 }
82
83 @Override
84 public void process(ByteBuffer payload)
85 {
86 if (LOG.isDebugEnabled())
87 {
88 LOG.debug("Payload: {}",BufferUtil.toDetailString(payload));
89 }
90
91 if ((payload == null) || (payload.remaining() <= 0))
92 {
93 return;
94 }
95
96 try
97 {
98 append(payload.slice());
99 }
100 catch (NotUtf8Exception e)
101 {
102 throw new BadPayloadException(e);
103 }
104 }
105
106 @Override
107 public void reset(Frame frame)
108 {
109
110 }
111 }