001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.services;
014
015import java.io.IOException;
016import java.io.ObjectInputStream;
017
018/**
019 * A service used when a component or service needs to encode some amount of data on the client as a string. The string
020 * may be a query parameter, hidden form field, or a portion of a URL.  The default implementation converts the object
021 * output stream into a Base64 string.
022 *
023 * Starting in release 5.3.6, the encoded data incorporates an HMAC (hash based message authentication code) signature,
024 * as a prefix. HMAC requires a secret key, configured using the
025 * {@link org.apache.tapestry5.SymbolConstants#HMAC_PASSPHRASE} symbol.
026 *
027 * @since 5.1.0.1
028 */
029public interface ClientDataEncoder
030{
031    /**
032     * Creates a sink for client data.  The sink provides an output stream and ultimately, a string representation of
033     * the data sent to the stream.
034     *
035     * @return a new sink
036     */
037    ClientDataSink createSink();
038
039    /**
040     * Decodes data previously obtained from {@link ClientDataSink#getClientData()}.
041     *
042     * @param clientData
043     *         encoded client data
044     * @return stream of decoded data
045     * @throws IOException
046     *         if the client data has been corrupted (verified via the HMAC)
047     */
048    ObjectInputStream decodeClientData(String clientData) throws IOException;
049
050    /**
051     * Decodes client data obtained via {@link ClientDataSink#getEncodedClientData()}.
052     *
053     * @param clientData
054     *         URLEncoded client data
055     * @return stream of objects
056     * @throws IOException
057     *         if the client data has been corrupted (verified via the HMAC)
058     * @since 5.1.0.4
059     */
060    ObjectInputStream decodeEncodedClientData(String clientData) throws IOException;
061}