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