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 * @param output The output to serialize in the response. 078 * @throws IOException 079 * @throws RestException 080 */ 081 public void handleResponse(RestRequest req, RestResponse res, Object output) throws IOException, RestException ; 082 083 /** 084 * Handle the case where a matching method was not found. 085 * 086 * @param rc The HTTP response code. 087 * @param req The HTTP request. 088 * @param res The HTTP response. 089 * @throws Exception 090 */ 091 public void handleNotFound(int rc, RestRequest req, RestResponse res) throws Exception; 092 093 /** 094 * Method for handling response errors. 095 * 096 * @param req The servlet request. 097 * @param res The servlet response. 098 * @param e The exception that occurred. 099 * @throws IOException Can be thrown if a problem occurred trying to write to the output stream. 100 */ 101 public void handleError(HttpServletRequest req, HttpServletResponse res, RestException e) throws IOException; 102 103 /** 104 * Method for rendering response errors. 105 * 106 * @param req The servlet request. 107 * @param res The servlet response. 108 * @param e The exception that occurred. 109 * @throws IOException Can be thrown if a problem occurred trying to write to the output stream. 110 */ 111 public void renderError(HttpServletRequest req, HttpServletResponse res, RestException e) throws IOException; 112 113 /** 114 * Returns the session objects for the specified request. 115 * 116 * @param req The REST request. 117 * @return The session objects for that request. 118 */ 119 public Map<String,Object> getSessionObjects(RestRequest req); 120}