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.processor; 018 019import java.io.*; 020 021import org.apache.juneau.http.response.*; 022import org.apache.juneau.rest.*; 023import org.apache.juneau.rest.annotation.*; 024 025import jakarta.servlet.http.*; 026 027/** 028 * Defines the interface for processors that convert POJOs to appropriate HTTP responses. 029 * 030 * <p> 031 * The REST Server API uses the concept of registered response processors for converting objects returned by REST 032 * methods or set through {@link RestResponse#setContent(Object)} into appropriate HTTP responses. 033 * 034 * <p> 035 * Response processors can be associated with REST resources via the following: 036 * <ul> 037 * <li class='ja'>{@link Rest#responseProcessors} 038 * <li class='jm'>{@link org.apache.juneau.rest.RestContext.Builder#responseProcessors()} 039 * </ul> 040 * 041 * <p> 042 * Response processors can be used to process POJOs that cannot normally be handled through Juneau serializers, or 043 * because it's simply easier to define response processors for special cases. 044 * 045 * <p> 046 * The following example shows how to create a response processor to handle special <c>Foo</c> objects outside the 047 * normal Juneau architecture. 048 * <p class='bjava'> 049 * <ja>@Rest</ja>( 050 * path=<js>"/example"</js>, 051 * responseProcessors=FooProcessor.<jk>class</jk> 052 * ) 053 * <jk>public class</jk> Example <jk>extends</jk> BasicRestServlet { 054 * 055 * <ja>@RestGet</ja>(<js>"/"</js>) 056 * <jk>public</jk> Foo test1() { 057 * <jk>return new</jk> Foo(<js>"123"</js>); 058 * } 059 * 060 * <jk>public static class</jk> FooProcessor <jk>implements</jk> ResponseProcessor { 061 * <ja>@Override</ja> 062 * <jk>public int</jk> process(RestOpSession <jv>opSession</jv>) { 063 * 064 * RestResponse <jv>res</jv> = <jv>opSession</jv>.getRestResponse(); 065 * Foo <jv>foo</jv> = <jv>res</jv>.getOutput(Foo.<jk>class</jk>); 066 * 067 * <jk>if</jk> (<jv>foo</jv> == <jk>null</jk>) 068 * <jk>return</jk> <jsf>NEXT</jsf>; <jc>// Let the next processor handle it.</jc> 069 * 070 * <jc>// Set some headers and content.</jc> 071 * <jv>res</jv>.setHeader(<js>"Foo-ID"</js>, <jv>foo</jv>.getId()); 072 * <jv>res</jv>.getWriter().write(<js>"foo.id="</js> + <jv>foo</jv>.getId()); 073 * 074 * <jk>return</jk> <jsf>FINISHED</jsf>; <jc>// We handled it.</jc> 075 * } 076 * } 077 * } 078 * </p> 079 * 080 * <h5 class='section'>See Also:</h5><ul> 081 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ResponseProcessors">Response Processors</a> 082 * </ul> 083 */ 084public interface ResponseProcessor { 085 086 /** 087 * Return code indicating to proceed to the next response processor in the chain. 088 */ 089 int NEXT = 0; 090 091 /** 092 * Return code indicating that processing is complete and to exit the chain. 093 */ 094 int FINISHED = 1; 095 096 /** 097 * Return code indicating to restart processing the chain from the beginning. 098 */ 099 int RESTART = 2; 100 101 /** 102 * Process this response if possible. 103 * 104 * @param opSession The HTTP call. 105 * @return One of the following codes: 106 * <ul> 107 * <li><c>0</c> - The processor could not handle the request. 108 * <li><c>1</c> - The processor was able to fully handle the request. 109 * <li><c>2</c> - The processor was able to partially handle the request by replacing the output. 110 * The response processors should start over. 111 * </ul> 112 * @throws IOException 113 * If low-level exception occurred on output stream. 114 * <br>Results in a {@link HttpServletResponse#SC_INTERNAL_SERVER_ERROR} error. 115 * @throws BasicHttpException 116 * If some other exception occurred. 117 * <br>Can be used to provide an appropriate HTTP response code and message. 118 */ 119 int process(RestOpSession opSession) throws IOException, BasicHttpException; 120}