All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.icellmobilsoft.coffee.rest.utils.RequestUtil Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*-
 * #%L
 * Coffee
 * %%
 * Copyright (C) 2020 i-Cell Mobilsoft Zrt.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package hu.icellmobilsoft.coffee.rest.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.container.ContainerRequestContext;

import org.jboss.resteasy.core.ResourceMethodInvoker;

import hu.icellmobilsoft.coffee.se.logging.Logger;

/**
 * REST Request utils.
 *
 * @author mark.petrenyi
 * @since 1.0.0
 */
public class RequestUtil {

    // see https://download.eclipse.org/microprofile/microprofile-rest-client-1.3/microprofile-rest-client-1.3.html#_clientrequestfilter
    /** Constant ECLIPSE_MICROPROFILE_REST_CLIENT_INVOKED_METHOD_KEY="org.eclipse.microprofile.rest.client.in"{trunked} */
    public static final String ECLIPSE_MICROPROFILE_REST_CLIENT_INVOKED_METHOD_KEY = "org.eclipse.microprofile.rest.client.invokedMethod";

    /**
     * Default constructor, constructs a new object.
     */
    public RequestUtil() {
        super();
    }

    /**
     * Returns desired {@link Annotation} from given {@link ContainerRequestContext}.
     *
     * @param 
     *            type of desired {@code Annotation}
     * @param requestContext
     *            context
     * @param clazz
     *            class of desired {@code Annotation}
     * @return desired {@code Annotation} or null if it is not found
     *
     */
    public static  A getAnnotation(ContainerRequestContext requestContext, Class clazz) {
        if (requestContext == null || clazz == null) {
            Logger.getLogger(RequestUtil.class).warn("requestContext or clazz is null!!");
            return null;
        }
        ResourceMethodInvoker invoker = (ResourceMethodInvoker) requestContext.getProperty(ResourceMethodInvoker.class.getName());
        if (invoker == null) {
            // In manual REST client cases, it can be null
            return null;
        }
        Method serviceMethod = invoker.getMethod();
        return serviceMethod.getAnnotation(clazz);
    }

    /**
     * Returns desired {@link Annotation} from given {@link ClientRequestContext}.
     *
     * @param 
     *            type of desired {@code Annotation}
     * @param requestContext
     *            context
     * @param clazz
     *            class of desired {@code Annotation}
     * @return desired {@code Annotation} or null if it is not found
     * 
     */
    public static  A getAnnotation(ClientRequestContext requestContext, Class clazz) {
        if (requestContext == null || clazz == null) {
            Logger.getLogger(RequestUtil.class).warn("requestContext or clazz is null!!");
            return null;
        }
        Method invokedMethod = (Method) requestContext.getProperty(ECLIPSE_MICROPROFILE_REST_CLIENT_INVOKED_METHOD_KEY);
        if (invokedMethod == null) {
            // In manual REST client cases, it can be null
            return null;
        }
        return invokedMethod.getAnnotation(clazz);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy