001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.socket.sockjs.frame;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022/**
023 * Encode and decode messages to and from a SockJS message frame,
024 * essentially an array of JSON-encoded messages. For example:
025 *
026 * <pre class="code">
027 * a["message1","message2"]
028 * </pre>
029 *
030 * @author Rossen Stoyanchev
031 * @since 4.0
032 */
033public interface SockJsMessageCodec {
034
035        /**
036         * Encode the given messages as a SockJS message frame. Aside from applying standard
037         * JSON quoting to each message, there are some additional JSON Unicode escaping
038         * rules. See the "JSON Unicode Encoding" section of SockJS protocol (i.e. the
039         * protocol test suite).
040         * @param messages the messages to encode
041         * @return the content for a SockJS message frame (never {@code null})
042         */
043        String encode(String... messages);
044
045        /**
046         * Decode the given SockJS message frame.
047         * @param content the SockJS message frame
048         * @return an array of messages, or {@code null} if none
049         * @throws IOException if the content could not be parsed
050         */
051        String[] decode(String content) throws IOException;
052
053        /**
054         * Decode the given SockJS message frame.
055         * @param content the SockJS message frame
056         * @return an array of messages, or {@code null} if none
057         * @throws IOException if the content could not be parsed
058         */
059        String[] decodeInputStream(InputStream content) throws IOException;
060
061}