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 java.io.*; 016 017import javax.servlet.http.*; 018 019import org.apache.juneau.*; 020import org.apache.juneau.rest.annotation.*; 021import org.apache.juneau.rest.response.*; 022 023/** 024 * Defines the interface for handlers that convert POJOs to appropriate HTTP responses. 025 * 026 * <p> 027 * The REST Server API uses the concept of registered response handlers for converting objects returned by REST 028 * methods or set through {@link RestResponse#setOutput(Object)} into appropriate HTTP responses. 029 * 030 * <p> 031 * Response handlers can be associated with REST resources via the following: 032 * <ul> 033 * <li class='ja'>{@link RestResource#responseHandlers} 034 * <li class='jm'>{@link RestContextBuilder#responseHandlers(Class...)} 035 * <li class='jm'>{@link RestContextBuilder#responseHandlers(ResponseHandler...)} 036 * </ul> 037 * 038 * <p> 039 * By default, REST resources are registered with the following response handlers: 040 * <ul class='spaced-list'> 041 * <li class='jc'> 042 * {@link DefaultHandler} - Serializes POJOs using the Juneau serializer API. 043 * <li class='jc'> 044 * {@link ReaderHandler} - Pipes the output of {@link Reader Readers} to the response writer 045 * ({@link RestResponse#getWriter()}). 046 * <li class='jc'> 047 * {@link InputStreamHandler} - Pipes the output of {@link InputStream InputStreams} to the response output 048 * stream ({@link RestResponse#getOutputStream()}). 049 * <li class='jc'> 050 * {@link RedirectHandler} - Handles {@link Redirect} objects. 051 * <li class='jc'> 052 * {@link WritableHandler} - Handles {@link Writable} objects. 053 * <li class='jc'> 054 * {@link StreamableHandler} - Handles {@link Streamable} objects. 055 * </ul> 056 * 057 * <p> 058 * Response handlers can be used to process POJOs that cannot normally be handled through Juneau serializers, or 059 * because it's simply easier to define response handlers for special cases. 060 * 061 * <p> 062 * The following example shows how to create a response handler to handle special <code>Foo</code> objects outside the 063 * normal Juneau architecture. 064 * <p class='bcode'> 065 * <ja>@RestResource</ja>( 066 * path=<js>"/example"</js>, 067 * responseHandlers=FooHandler.<jk>class</jk> 068 * ) 069 * <jk>public class</jk> Example <jk>extends</jk> RestServlet { 070 * 071 * <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/"</js>) 072 * <jk>public</jk> Foo test1() { 073 * <jk>return new</jk> Foo(<js>"123"</js>); 074 * } 075 * 076 * <jk>public static class</jk> FooHandler <jk>implements</jk> ResponseHandler { 077 * <ja>@Override</ja> 078 * <jk>public boolean</jk> handle(RestRequest req, RestResponse res, Object output) <jk>throws</jk> IOException, RestException { 079 * <jk>if</jk> (output <jk>instanceof</jk> Foo) { 080 * Foo foo = (Foo)output; 081 * <jc>// Set some headers and body content.</jc> 082 * res.setHeader(<js>"Foo-ID"</js>, foo.getId()); 083 * res.getWriter().write(<js>"foo.id="</js> + foo.getId()); 084 * <jk>return true</jk>; <jc>// We handled it.</jc> 085 * } 086 * <jk>return false</jk>; <jc>// We didn't handle it.</jc> 087 * } 088 * } 089 * } 090 * </p> 091 * 092 * <h5 class='section'>See Also:</h5> 093 * <ul> 094 * <li class='link'><a class="doclink" href="../../../../overview-summary.html#juneau-rest-server.MethodReturnTypes">Overview > juneau-rest-server > Method Return Types</a> 095 * </ul> 096 */ 097public interface ResponseHandler { 098 099 /** 100 * Process this response if possible. 101 * This method should return <jk>false</jk> if it wasn't able to process the response. 102 * 103 * @param req The HTTP servlet request. 104 * @param res The HTTP servlet response; 105 * @param output The POJO returned by the REST method that now needs to be sent to the response. 106 * @return true If this handler handled the response. 107 * @throws IOException 108 * If low-level exception occurred on output stream. 109 * Results in a {@link HttpServletResponse#SC_INTERNAL_SERVER_ERROR} error. 110 * @throws RestException 111 * If some other exception occurred. 112 * Can be used to provide an appropriate HTTP response code and message. 113 */ 114 boolean handle(RestRequest req, RestResponse res, Object output) throws IOException, RestException; 115}