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 * <h5 class='section'>See Also:</h5>
026 * <ul>
027 *    <li class='jf'>{@link RestContext#REST_callHandler}
028 * </ul>
029 */
030public interface RestCallHandler {
031
032   /**
033    * Represents no RestCallHandler.
034    *
035    * <p>
036    * Used on annotation to indicate that the value should be inherited from the parent class, and
037    * ultimately {@link BasicRestCallHandler} if not specified at any level.
038    */
039   public interface Null extends RestCallHandler {}
040
041   /**
042    * Creates a {@link RestRequest} object based on the specified incoming {@link HttpServletRequest} object.
043    *
044    * @param req The request object from the {@link #service(HttpServletRequest, HttpServletResponse)} method.
045    * @return The wrapped request object.
046    * @throws ServletException If any errors occur trying to interpret the request.
047    */
048   public RestRequest createRequest(HttpServletRequest req) throws ServletException;
049
050   /**
051    * Creates a {@link RestResponse} object based on the specified incoming {@link HttpServletResponse} object
052    * and the request returned by {@link #createRequest(HttpServletRequest)}.
053    *
054    * @param req The request object returned by {@link #createRequest(HttpServletRequest)}.
055    * @param res The response object from the {@link #service(HttpServletRequest, HttpServletResponse)} method.
056    * @return The wrapped response object.
057    * @throws ServletException If any errors occur trying to interpret the request or response.
058    */
059   public RestResponse createResponse(RestRequest req, HttpServletResponse res) throws ServletException;
060
061   /**
062    * The main service method.
063    *
064    * @param r1 The incoming HTTP servlet request object.
065    * @param r2 The incoming HTTP servlet response object.
066    * @throws ServletException
067    * @throws IOException
068    */
069   public void service(HttpServletRequest r1, HttpServletResponse r2) throws ServletException, IOException;
070
071   /**
072    * The main method for serializing POJOs passed in through the {@link RestResponse#setOutput(Object)} method or
073    * returned by the Java method.
074    *
075    * @param req The HTTP request.
076    * @param res The HTTP response.
077    * @throws IOException
078    * @throws RestException
079    */
080   public void handleResponse(RestRequest req, RestResponse res) throws IOException, RestException ;
081
082   /**
083    * Handle the case where a matching method was not found.
084    *
085    * @param rc The HTTP response code.
086    * @param req The HTTP request.
087    * @param res The HTTP response.
088    * @throws Exception
089    */
090   public void handleNotFound(int rc, RestRequest req, RestResponse res) throws Exception;
091
092   /**
093    * Method for handling response errors.
094    *
095    * @param req The servlet request.
096    * @param res The servlet response.
097    * @param e The exception that occurred.
098    * @throws IOException Can be thrown if a problem occurred trying to write to the output stream.
099    */
100   public void handleError(HttpServletRequest req, HttpServletResponse res, Throwable e) throws IOException;
101
102   /**
103    * Returns the session objects for the specified request.
104    *
105    * @param req The REST request.
106    * @return The session objects for that request.
107    */
108   public Map<String,Object> getSessionObjects(RestRequest req);
109}