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    * The main service method.
042    *
043    * @param r1 The incoming HTTP servlet request object.
044    * @param r2 The incoming HTTP servlet response object.
045    * @throws ServletException Error occurred.
046    * @throws IOException Thrown by underlying stream.
047    */
048   public void service(HttpServletRequest r1, HttpServletResponse r2) throws ServletException, IOException;
049
050   /**
051    * Wraps an incoming servlet request/response pair into a single {@link RestCall} object.
052    *
053    * @param req The rest request.
054    * @param res The rest response.
055    * @return The wrapped request/response pair.
056    */
057   public RestCall createCall(HttpServletRequest req, HttpServletResponse res);
058
059   /**
060    * Creates a {@link RestRequest} object based on the specified incoming {@link HttpServletRequest} object.
061    *
062    * @param call The current REST call.
063    * @return The wrapped request object.
064    * @throws ServletException If any errors occur trying to interpret the request.
065    */
066   public RestRequest createRequest(RestCall call) throws ServletException;
067
068   /**
069    * Creates a {@link RestResponse} object based on the specified incoming {@link HttpServletResponse} object
070    * and the request returned by {@link #createRequest(RestCall)}.
071    *
072    * @param call The current REST call.
073    * @return The wrapped response object.
074    * @throws ServletException If any errors occur trying to interpret the request or response.
075    */
076   public RestResponse createResponse(RestCall call) throws ServletException;
077
078   /**
079    * The main method for serializing POJOs passed in through the {@link RestResponse#setOutput(Object)} method or
080    * returned by the Java method.
081    *
082    * @param call The current REST call.
083    * @throws Exception Can be thrown if error occurred while handling response.
084    */
085   public void handleResponse(RestCall call) throws Exception;
086
087   /**
088    * Handle the case where a matching method was not found.
089    *
090    * @param call The current REST call.
091    * @throws Exception Can be thrown if error occurred while handling response.
092    */
093   public void handleNotFound(RestCall call) throws Exception;
094
095   /**
096    * Method for handling response errors.
097    *
098    * @param call The current REST call.
099    * @param e The exception that occurred.
100    * @throws Exception Can be thrown if error occurred while handling response.
101    */
102   public void handleError(RestCall call, Throwable e) throws Exception;
103
104   /**
105    * Method for converting thrown exceptions into other types before they are handled.
106    *
107    * @param t The thrown object.
108    * @return The converted thrown object.
109    */
110   public Throwable convertThrowable(Throwable t);
111
112   /**
113    * Returns the session objects for the specified request.
114    *
115    * @param req The REST request.
116    * @param res The REST response.
117    * @return The session objects for that request.
118    */
119   public Map<String,Object> getSessionObjects(RestRequest req, RestResponse res);
120}