001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.converter;
014
015import org.apache.juneau.http.response.*;
016import org.apache.juneau.rest.*;
017import org.apache.juneau.rest.annotation.*;
018import org.apache.juneau.serializer.*;
019
020/**
021 * REST method response converter.
022 *
023 * <p>
024 * Implements a filter mechanism for REST method calls that allows response objects to be converted to some other POJO
025 * after invocation of the REST method.
026 *
027 * <p>
028 * Converters are associated with REST methods through the following:
029 * <ul class='javatree'>
030 *    <li class='ja'>{@link Rest#converters()}
031 *    <li class='ja'>{@link RestOp#converters()}
032 *    <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()}
033 * </ul>
034 *
035 * <h5 class='section'>Example:</h5>
036 * <p class='bjava'>
037 *    <jk>public class</jk> RequestEchoResource <jk>extends</jk> BasicRestServlet {
038 *
039 *       <jc>// GET request handler</jc>
040 *       <ja>@RestGet</ja>(path=<js>"/*"</js>, converters={Queryable.<jk>class</jk>,Traversable.<jk>class</jk>})
041 *       <jk>public</jk> HttpServletRequest doGet(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) {
042 *          <jv>res</jv>.setTitle(<js>"Contents of HttpServletRequest object"</js>);
043 *          <jk>return</jk> <jv>req</jv>;
044 *       }
045 *    }
046 * </p>
047 *
048 * <p>
049 * Converters can also be associated at the servlet level using the {@link Rest#converters() @Rest(converters)} annotation.
050 * <br>Applying converters at the resource level is equivalent to applying converters to each resource method individually.
051 *
052 * <h5 class='topic'>How to implement</h5>
053 *
054 * Implementers should simply implement the {@link #convert(RestRequest, Object)} and return back a 'converted' object.
055 * <br>It's up to the implementer to decide what this means.
056 *
057 * <p>
058 * Subclasses must implement one of the following constructors:
059 * <ul>
060 *    <li><jk>public</jk> T();  <jc>// No-arg constructor</jc>
061 *    <li><jk>public</jk> T(ContextProperties);  <jc>// Property store of the RestContext</jc>
062 * </ul>
063 *
064 * <p>
065 * Subclasses can also be defined as inner classes of the resource class.
066 *
067 * <h5 class='topic'>Predefined converters</h5>
068 * <ul class='javatree'>
069 *    <li class='jc'>{@link Traversable} - Allows URL additional path info to address individual elements in a POJO tree.
070 *    <li class='jc'>{@link Queryable} - Allows query/view/sort functions to be performed on POJOs.
071 *    <li class='jc'>{@link Introspectable} - Allows Java public methods to be invoked on the returned POJOs.
072 * </ul>
073 *
074 * <h5 class='section'>See Also:</h5><ul>
075 *    <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()}
076 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Converters">Converters</a>
077 * </ul>
078 * </ul>
079 */
080public interface RestConverter {
081
082   /**
083    * Performs post-call conversion on the specified response object.
084    *
085    * @param req The servlet request.
086    * @param res The response object set by the REST method through the {@link RestResponse#setContent(Object)} method.
087    * @return The converted object.
088    * @throws BasicHttpException Thrown if any errors occur during conversion.
089    * @throws SerializeException Generic serialization error occurred.
090    */
091   Object convert(RestRequest req, Object res) throws BasicHttpException, SerializeException;
092}