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