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
015
016import java.io.*;
017import java.util.*;
018
019import javax.servlet.*;
020import javax.servlet.http.*;
021
022/**
023 * Class that handles the basic lifecycle of an HTTP REST call.
024 *
025 * <ul class='seealso'>
026 *    <li class='jf'>{@link RestContext#REST_callHandler}
027 * </ul>
028 */
029public interface RestCallHandler {
030
031   /**
032    * Represents no RestCallHandler.
033    *
034    * <p>
035    * Used on annotation to indicate that the value should be inherited from the parent class, and
036    * ultimately {@link BasicRestCallHandler} if not specified at any level.
037    */
038   public interface Null extends RestCallHandler {}
039
040   /**
041    * Creates a {@link RestRequest} object based on the specified incoming {@link HttpServletRequest} object.
042    *
043    * @param req The request object from the {@link #service(HttpServletRequest, HttpServletResponse)} method.
044    * @return The wrapped request object.
045    * @throws ServletException If any errors occur trying to interpret the request.
046    */
047   public RestRequest createRequest(HttpServletRequest req) throws ServletException;
048
049   /**
050    * Creates a {@link RestResponse} object based on the specified incoming {@link HttpServletResponse} object
051    * and the request returned by {@link #createRequest(HttpServletRequest)}.
052    *
053    * @param req The request object returned by {@link #createRequest(HttpServletRequest)}.
054    * @param res The response object from the {@link #service(HttpServletRequest, HttpServletResponse)} method.
055    * @return The wrapped response object.
056    * @throws ServletException If any errors occur trying to interpret the request or response.
057    */
058   public RestResponse createResponse(RestRequest req, HttpServletResponse res) throws ServletException;
059
060   /**
061    * The main service method.
062    *
063    * @param r1 The incoming HTTP servlet request object.
064    * @param r2 The incoming HTTP servlet response object.
065    * @throws ServletException Error occurred.
066    * @throws IOException Thrown by underlying stream.
067    */
068   public void service(HttpServletRequest r1, HttpServletResponse r2) throws ServletException, IOException;
069
070   /**
071    * The main method for serializing POJOs passed in through the {@link RestResponse#setOutput(Object)} method or
072    * returned by the Java method.
073    *
074    * @param req The HTTP request.
075    * @param res The HTTP response.
076    * @throws Exception Can be thrown if error occurred while handling response.
077    */
078   public void handleResponse(RestRequest req, RestResponse res) throws Exception;
079
080   /**
081    * Handle the case where a matching method was not found.
082    *
083    * @param rc The HTTP response code.
084    * @param req The HTTP request.
085    * @param res The HTTP response.
086    * @throws Exception Can be thrown if error occurred while handling response.
087    */
088   public void handleNotFound(int rc, RestRequest req, RestResponse res) throws Exception;
089
090   /**
091    * Method for handling response errors.
092    *
093    * @param req The servlet request.
094    * @param res The servlet response.
095    * @param e The exception that occurred.
096    * @throws Exception Can be thrown if error occurred while handling response.
097    */
098   public void handleError(HttpServletRequest req, HttpServletResponse res, Throwable e) throws Exception;
099
100   /**
101    * Method for converting thrown exceptions into other types before they are handled.
102    *
103    * @param t The thrown object.
104    * @return The converted thrown object.
105    */
106   public Throwable convertThrowable(Throwable t);
107
108   /**
109    * Returns the session objects for the specified request.
110    *
111    * @param req The REST request.
112    * @param res The REST response.
113    * @return The session objects for that request.
114    */
115   public Map<String,Object> getSessionObjects(RestRequest req, RestResponse res);
116}