001// Copyright 2021 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// 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
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.http.services;
016
017import java.util.Optional;
018
019import javax.servlet.http.HttpServletRequest;
020
021import org.apache.tapestry5.commons.services.TypeCoercer;
022
023/**
024 * Service which provides REST-related utilities.
025 * @since 5.8.0
026 */
027public interface RestSupport
028{
029    
030    /**
031     * Is this request a GET?
032     * @return <code>true</code> or <code>false</code>
033     */
034    boolean isHttpGet();
035    
036    /**
037     * Is this request a POST?
038     * @return <code>true</code> or <code>false</code>
039     */
040    boolean isHttpPost();
041
042    /**
043     * Is this request a HEAD?
044     * @return <code>true</code> or <code>false</code>
045     */
046    boolean isHttpHead();
047
048    /**
049     * Is this request a PUT?
050     * @return <code>true</code> or <code>false</code>
051     */
052    boolean isHttpPut();
053    
054    /**
055     * Is this request a HEAD?
056     * @return <code>true</code> or <code>false</code>
057     */
058    boolean isHttpDelete();
059
060    /**
061     * Is this request a HEAD?
062     * @return <code>true</code> or <code>false</code>
063     */
064    boolean isHttpPatch();
065
066    /**
067     * Returns, if present, the body of the request body coerced to a given type. If the body is empty,
068     * an empty {@linkplain Optional} is returned. Coercions are done through, which uses
069     * {@linkplain TypeCoercer} as a fallback (coercing {@linkplain HttpServletRequest} to the target type).
070     * @param <T> the type of the return value.
071     * @param type the target type.
072     * @return an <code>Optional</code> wrapping the resulting object.
073     */
074    <T> Optional<T> getRequestBodyAs(Class<T> type);
075    
076}