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
022import org.springframework.lang.Nullable;
023
024/**
025 * Encode and decode messages to and from a SockJS message frame,
026 * essentially an array of JSON-encoded messages. For example:
027 *
028 * <pre class="code">
029 * a["message1","message2"]
030 * </pre>
031 *
032 * @author Rossen Stoyanchev
033 * @since 4.0
034 */
035public interface SockJsMessageCodec {
036
037        /**
038         * Encode the given messages as a SockJS message frame. Aside from applying standard
039         * JSON quoting to each message, there are some additional JSON Unicode escaping
040         * rules. See the "JSON Unicode Encoding" section of SockJS protocol (i.e. the
041         * protocol test suite).
042         * @param messages the messages to encode
043         * @return the content for a SockJS message frame (never {@code null})
044         */
045        String encode(String... messages);
046
047        /**
048         * Decode the given SockJS message frame.
049         * @param content the SockJS message frame
050         * @return an array of messages, or {@code null} if none
051         * @throws IOException if the content could not be parsed
052         */
053        @Nullable
054        String[] decode(String content) throws IOException;
055
056        /**
057         * Decode the given SockJS message frame.
058         * @param content the SockJS message frame
059         * @return an array of messages, or {@code null} if none
060         * @throws IOException if the content could not be parsed
061         */
062        @Nullable
063        String[] decodeInputStream(InputStream content) throws IOException;
064
065}