001package ball.http;
002/*-
003 * ##########################################################################
004 * Web API Client (HTTP) Utilities
005 * $Id: ProtocolInvocationHandler.java 7215 2021-01-03 18:39:51Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-http/trunk/src/main/java/ball/http/ProtocolInvocationHandler.java $
007 * %%
008 * Copyright (C) 2016 - 2021 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.lang.reflect.DefaultInterfaceMethodInvocationHandler;
024import java.lang.reflect.Method;
025import lombok.NonNull;
026import lombok.RequiredArgsConstructor;
027import lombok.ToString;
028import org.apache.http.HttpMessage;
029import org.apache.http.HttpResponse;
030import org.apache.http.client.methods.HttpUriRequest;
031
032/**
033 * Protocol {@link java.lang.reflect.InvocationHandler} for
034 * {@link ProtocolClient}.  The {@link #invoke(Object,Method,Object[])}
035 * translates the interface {@link Method} invocation into an
036 * {@link HttpMessage} through a {@link ProtocolRequestBuilder}.  If the
037 * {@link Method#getReturnType()} is assignable from the generated
038 * {@link HttpMessage}, then it is simply returned; otherwise, the request
039 * is executed through {@link ProtocolClient#client()} with the
040 * {@link ProtocolClient#context()}.  If the {@link Method#getReturnType()}
041 * is {@link HttpResponse} then the response is returned with no further
042 * processing (and the caller is responsible for consuming any entities).
043 * Otherwise, a {@link ProtocolResponseHandler} is provided to the call.
044 *
045 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
046 * @version $Revision: 7215 $
047 */
048@RequiredArgsConstructor @ToString
049public class ProtocolInvocationHandler implements DefaultInterfaceMethodInvocationHandler {
050    @NonNull private final ProtocolClient<?> client;
051
052    @Override
053    public Object invoke(Object proxy,
054                         Method method, Object[] argv) throws Throwable {
055        Object result = null;
056        Class<?> declarer = method.getDeclaringClass();
057
058        if (method.isDefault()) {
059            result =
060                DefaultInterfaceMethodInvocationHandler.super
061                .invoke(proxy, method, argv);
062        } else if (declarer.equals(Object.class)) {
063            result = method.invoke(proxy, argv);
064        } else {
065            Class<?> returnType = method.getReturnType();
066            HttpMessage request =
067                new ProtocolRequestBuilder(client).build(method, argv);
068
069            if (returnType.isAssignableFrom(request.getClass())) {
070                result = returnType.cast(request);
071            } else if (returnType.isAssignableFrom(HttpResponse.class)) {
072                result =
073                    client.client()
074                    .execute((HttpUriRequest) request, client.context());
075            } else {
076                result =
077                    client.client()
078                    .execute((HttpUriRequest) request,
079                             new ProtocolResponseHandler(client, method),
080                             client.context());
081            }
082        }
083
084        return result;
085    }
086}