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