Search in sources :

Example 1 with ClientHeaderParam

use of org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam in project cxf by apache.

the class MicroProfileClientProxyImpl method handleHeaders.

@Override
@SuppressWarnings("unchecked")
protected void handleHeaders(Method m, Object[] params, MultivaluedMap<String, String> headers, List<Parameter> beanParams, MultivaluedMap<ParameterType, Parameter> map) {
    super.handleHeaders(m, params, headers, beanParams, map);
    try {
        Class<?> declaringClass = m.getDeclaringClass();
        ClientHeaderParam[] clientHeaderAnnosOnInterface = declaringClass.getAnnotationsByType(ClientHeaderParam.class);
        ClientHeaderParam[] clientHeaderAnnosOnMethod = m.getAnnotationsByType(ClientHeaderParam.class);
        RegisterClientHeaders headersFactoryAnno = declaringClass.getAnnotation(RegisterClientHeaders.class);
        if (clientHeaderAnnosOnInterface.length < 1 && clientHeaderAnnosOnMethod.length < 1 && headersFactoryAnno == null) {
            return;
        }
        for (ClientHeaderParam methodAnno : clientHeaderAnnosOnMethod) {
            String headerName = methodAnno.name();
            if (!headers.containsKey(headerName)) {
                Parameter p = createClientHeaderParameter(methodAnno, declaringClass);
                if (p != null) {
                    headers.putSingle(p.getName(), p.getDefaultValue());
                }
            }
        }
        for (ClientHeaderParam intfAnno : clientHeaderAnnosOnInterface) {
            String headerName = intfAnno.name();
            if (!headers.containsKey(headerName)) {
                Parameter p = createClientHeaderParameter(intfAnno, declaringClass);
                if (p != null) {
                    headers.putSingle(p.getName(), p.getDefaultValue());
                }
            }
        }
        if (headersFactoryAnno != null) {
            Class<ClientHeadersFactory> headersFactoryClass = (Class<ClientHeadersFactory>) headersFactoryAnno.value();
            mergeHeaders(headersFactoryClass, headers);
        }
    } catch (Throwable t) {
        throwException(t);
    }
}
Also used : RegisterClientHeaders(org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders) ClientHeaderParam(org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam) Parameter(org.apache.cxf.jaxrs.model.Parameter) ClientHeadersFactory(org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory)

Example 2 with ClientHeaderParam

use of org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam in project cxf by apache.

the class Validator method checkForInvalidClientHeaderParams.

private static void checkForInvalidClientHeaderParams(Class<?> userType, Method[] methods) {
    ClientHeaderParam[] interfaceAnnotations = userType.getAnnotationsByType(ClientHeaderParam.class);
    checkClientHeaderParamAnnotation(interfaceAnnotations, userType, methods);
    for (Method method : methods) {
        ClientHeaderParam[] methodAnnotations = method.getAnnotationsByType(ClientHeaderParam.class);
        checkClientHeaderParamAnnotation(methodAnnotations, userType, methods);
    }
}
Also used : ClientHeaderParam(org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method)

Example 3 with ClientHeaderParam

use of org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam in project cxf by apache.

the class Validator method checkClientHeaderParamAnnotation.

private static void checkClientHeaderParamAnnotation(ClientHeaderParam[] annos, Class<?> userType, Method[] methods) {
    Set<String> headerNames = new HashSet<>();
    for (ClientHeaderParam anno : annos) {
        String name = anno.name();
        if (headerNames.contains(name)) {
            throwException("CLIENT_HEADER_MULTIPLE_SAME_HEADER_NAMES", userType.getName());
        }
        headerNames.add(name);
        if (name == null || "".equals(name)) {
            throwException("CLIENT_HEADER_NO_NAME", userType.getName());
        }
        String[] values = anno.value();
        for (String value : values) {
            if (StringUtils.isEmpty(value)) {
                throwException("CLIENT_HEADER_NO_VALUE", userType.getName());
            }
            if (value.startsWith("{") && value.endsWith("}")) {
                if (values.length > 1) {
                    throwException("CLIENT_HEADER_MULTI_METHOD", userType.getName());
                }
                String computeValue = value.substring(1, value.length() - 1);
                boolean usingOtherClass = false;
                if (computeValue.contains(".")) {
                    usingOtherClass = true;
                    String className = computeValue.substring(0, computeValue.lastIndexOf('.'));
                    computeValue = computeValue.substring(computeValue.lastIndexOf('.') + 1);
                    try {
                        Class<?> computeClass = ClassLoaderUtils.loadClass(className, userType);
                        methods = Arrays.stream(computeClass.getDeclaredMethods()).filter(m -> {
                            int i = m.getModifiers();
                            return Modifier.isPublic(i) && Modifier.isStatic(i);
                        }).toArray(Method[]::new);
                    } catch (ClassNotFoundException ex) {
                        if (LOG.isLoggable(Level.FINEST)) {
                            LOG.log(Level.FINEST, "Unable to load specified compute method class", ex);
                        }
                        throwException("CLIENT_HEADER_COMPUTE_CLASS_NOT_FOUND", userType.getName(), ex);
                    }
                }
                boolean foundMatchingMethod = false;
                for (Method method : methods) {
                    Class<?> returnType = method.getReturnType();
                    if ((usingOtherClass || method.isDefault()) && (String.class.equals(returnType) || String[].class.equals(returnType)) && computeValue.equals(method.getName())) {
                        Class<?>[] args = method.getParameterTypes();
                        if (args.length == 0 || (args.length == 1 && String.class.equals(args[0]))) {
                            foundMatchingMethod = true;
                            break;
                        }
                    }
                }
                if (!foundMatchingMethod) {
                    throwException("CLIENT_HEADER_INVALID_COMPUTE_METHOD", userType.getName(), computeValue);
                }
            }
        }
    }
}
Also used : ClientHeaderParam(org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) HashSet(java.util.HashSet)

Aggregations

ClientHeaderParam (org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam)3 Method (java.lang.reflect.Method)2 HttpMethod (javax.ws.rs.HttpMethod)2 HashSet (java.util.HashSet)1 Parameter (org.apache.cxf.jaxrs.model.Parameter)1 RegisterClientHeaders (org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders)1 ClientHeadersFactory (org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory)1