View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.sockjs.frame;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  /**
23   * Encode and decode messages to and from a SockJS message frame,
24   * essentially an array of JSON-encoded messages. For example:
25   *
26   * <pre class="code">
27   * a["message1","message2"]
28   * </pre>
29   *
30   * @author Rossen Stoyanchev
31   * @since 4.0
32   */
33  public interface SockJsMessageCodec {
34  
35  	/**
36  	 * Encode the given messages as a SockJS message frame. Aside from applying standard
37  	 * JSON quoting to each message, there are some additional JSON Unicode escaping
38  	 * rules. See the "JSON Unicode Encoding" section of SockJS protocol (i.e. the
39  	 * protocol test suite).
40  	 * @param messages the messages to encode
41  	 * @return the content for a SockJS message frame (never {@code null})
42  	 */
43  	String encode(String... messages);
44  
45  	/**
46  	 * Decode the given SockJS message frame.
47  	 * @param content the SockJS message frame
48  	 * @return an array of messages, or {@code null} if none
49  	 * @throws IOException if the content could not be parsed
50  	 */
51  	String[] decode(String content) throws IOException;
52  
53  	/**
54  	 * Decode the given SockJS message frame.
55  	 * @param content the SockJS message frame
56  	 * @return an array of messages, or {@code null} if none
57  	 * @throws IOException if the content could not be parsed
58  	 */
59  	String[] decodeInputStream(InputStream content) throws IOException;
60  
61  }