Search in sources :

Example 1 with ClientHeadersFactory

use of org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory 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 ClientHeadersFactory

use of org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory in project cxf by apache.

the class MicroProfileClientProxyImpl method mergeHeaders.

private void mergeHeaders(Class<ClientHeadersFactory> factoryCls, MultivaluedMap<String, String> existingHeaders) {
    try {
        ClientHeadersFactory factory;
        Message m = JAXRS_UTILS_GET_CURRENT_MESSAGE_METHOD == null ? null : (Message) JAXRS_UTILS_GET_CURRENT_MESSAGE_METHOD.invoke(null);
        if (m != null) {
            factory = CDIFacade.getInstanceFromCDI(factoryCls, m.getExchange().getBus()).map(this::mapInstance).orElse(factoryCls.newInstance());
            ProviderInfo<ClientHeadersFactory> pi = clientHeaderFactories.computeIfAbsent(factoryCls, k -> {
                return new ProviderInfo<ClientHeadersFactory>(factory, m.getExchange().getBus(), true);
            });
            InjectionUtils.injectContexts(factory, pi, m);
        } else {
            factory = CDIFacade.getInstanceFromCDI(factoryCls).map(this::mapInstance).orElse(factoryCls.newInstance());
        }
        MultivaluedMap<String, String> updatedHeaders = factory.update(getJaxrsHeaders(m), existingHeaders);
        existingHeaders.putAll(updatedHeaders);
    } catch (Throwable t) {
        // expected if not running in a JAX-RS server environment.
        if (LOG.isLoggable(Level.FINEST)) {
            LOG.log(Level.FINEST, "Caught exception getting JAX-RS incoming headers", t);
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ClientHeadersFactory(org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory)

Aggregations

ClientHeadersFactory (org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory)2 Parameter (org.apache.cxf.jaxrs.model.Parameter)1 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)1 Message (org.apache.cxf.message.Message)1 ClientHeaderParam (org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam)1 RegisterClientHeaders (org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders)1