View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Used to perform validation of UTF8 payload contents (for fast-fail reasons)
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         /* do nothing */
110     }
111 }