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.converter; 018 019import org.apache.juneau.http.response.*; 020import org.apache.juneau.rest.*; 021import org.apache.juneau.rest.annotation.*; 022import org.apache.juneau.serializer.*; 023 024/** 025 * REST method response converter. 026 * 027 * <p> 028 * Implements a filter mechanism for REST method calls that allows response objects to be converted to some other POJO 029 * after invocation of the REST method. 030 * 031 * <p> 032 * Converters are associated with REST methods through the following: 033 * <ul class='javatree'> 034 * <li class='ja'>{@link Rest#converters()} 035 * <li class='ja'>{@link RestOp#converters()} 036 * <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()} 037 * </ul> 038 * 039 * <h5 class='section'>Example:</h5> 040 * <p class='bjava'> 041 * <jk>public class</jk> RequestEchoResource <jk>extends</jk> BasicRestServlet { 042 * 043 * <jc>// GET request handler</jc> 044 * <ja>@RestGet</ja>(path=<js>"/*"</js>, converters={Queryable.<jk>class</jk>,Traversable.<jk>class</jk>}) 045 * <jk>public</jk> HttpServletRequest doGet(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) { 046 * <jv>res</jv>.setTitle(<js>"Contents of HttpServletRequest object"</js>); 047 * <jk>return</jk> <jv>req</jv>; 048 * } 049 * } 050 * </p> 051 * 052 * <p> 053 * Converters can also be associated at the servlet level using the {@link Rest#converters() @Rest(converters)} annotation. 054 * <br>Applying converters at the resource level is equivalent to applying converters to each resource method individually. 055 * 056 * <h5 class='topic'>How to implement</h5> 057 * 058 * Implementers should simply implement the {@link #convert(RestRequest, Object)} and return back a 'converted' object. 059 * <br>It's up to the implementer to decide what this means. 060 * 061 * <p> 062 * Subclasses must implement one of the following constructors: 063 * <ul> 064 * <li><jk>public</jk> T(); <jc>// No-arg constructor</jc> 065 * <li><jk>public</jk> T(ContextProperties); <jc>// Property store of the RestContext</jc> 066 * </ul> 067 * 068 * <p> 069 * Subclasses can also be defined as inner classes of the resource class. 070 * 071 * <h5 class='topic'>Predefined converters</h5> 072 * <ul class='javatree'> 073 * <li class='jc'>{@link Traversable} - Allows URL additional path info to address individual elements in a POJO tree. 074 * <li class='jc'>{@link Queryable} - Allows query/view/sort functions to be performed on POJOs. 075 * <li class='jc'>{@link Introspectable} - Allows Java public methods to be invoked on the returned POJOs. 076 * </ul> 077 * 078 * <h5 class='section'>See Also:</h5><ul> 079 * <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()} 080 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Converters">Converters</a> 081 * </ul> 082 * </ul> 083 */ 084public interface RestConverter { 085 086 /** 087 * Performs post-call conversion on the specified response object. 088 * 089 * @param req The servlet request. 090 * @param res The response object set by the REST method through the {@link RestResponse#setContent(Object)} method. 091 * @return The converted object. 092 * @throws BasicHttpException Thrown if any errors occur during conversion. 093 * @throws SerializeException Generic serialization error occurred. 094 */ 095 Object convert(RestRequest req, Object res) throws BasicHttpException, SerializeException; 096}