Search in sources :

Example 11 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project cxf by apache.

the class CXFNonSpringServlet method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    ClassLoaderHolder origLoader = null;
    Bus origBus = null;
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
        try {
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
            if (bus != null) {
                origBus = BusFactory.getAndSetThreadDefaultBus(bus);
            }
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            if (controller.filter(new HttpServletRequestFilter(httpRequest, super.getServletName()), (HttpServletResponse) response)) {
                return;
            }
        } finally {
            if (origBus != bus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
            if (origLoader != null) {
                origLoader.reset();
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Bus(org.apache.cxf.Bus) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 12 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project cxf by apache.

the class ClientMessageObserver method onMessage.

public void onMessage(Message m) {
    Message message = cfg.getConduitSelector().getEndpoint().getBinding().createMessage(m);
    message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    message.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
    PhaseInterceptorChain chain = AbstractClient.setupInInterceptorChain(cfg);
    message.setInterceptorChain(chain);
    message.getExchange().setInMessage(message);
    Bus bus = cfg.getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        // execute chain
        chain.doIntercept(message);
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
        synchronized (message.getExchange()) {
            message.getExchange().notifyAll();
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)

Example 13 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project cxf by apache.

the class ClientProxyImpl method doChainedInvocation.

// CHECKSTYLE:OFF
private Object doChainedInvocation(URI uri, MultivaluedMap<String, String> headers, OperationResourceInfo ori, Object[] methodParams, Object body, int bodyIndex, Exchange exchange, Map<String, Object> invocationContext) throws Throwable {
    // CHECKSTYLE:ON
    Bus configuredBus = getConfiguration().getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(configuredBus);
    ClassLoaderHolder origLoader = null;
    try {
        ClassLoader loader = configuredBus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Message outMessage = createMessage(body, ori.getHttpMethod(), headers, uri, exchange, invocationContext, true);
        if (bodyIndex != -1) {
            outMessage.put(Type.class, ori.getMethodToInvoke().getGenericParameterTypes()[bodyIndex]);
        }
        outMessage.getExchange().setOneWay(ori.isOneway());
        setSupportOnewayResponseProperty(outMessage);
        outMessage.setContent(OperationResourceInfo.class, ori);
        setPlainOperationNameProperty(outMessage, ori.getMethodToInvoke().getName());
        outMessage.getExchange().put(Method.class, ori.getMethodToInvoke());
        outMessage.put(Annotation.class.getName(), getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));
        outMessage.getExchange().put(Message.SERVICE_OBJECT, proxy);
        if (methodParams != null) {
            outMessage.put(List.class, Arrays.asList(methodParams));
        }
        if (body != null) {
            outMessage.put(PROXY_METHOD_PARAM_BODY_INDEX, bodyIndex);
        }
        outMessage.getInterceptorChain().add(bodyWriter);
        Map<String, Object> reqContext = getRequestContext(outMessage);
        reqContext.put(OperationResourceInfo.class.getName(), ori);
        reqContext.put(PROXY_METHOD_PARAM_BODY_INDEX, bodyIndex);
        // execute chain
        InvocationCallback<Object> asyncCallback = checkAsyncCallback(ori, reqContext);
        if (asyncCallback != null) {
            doInvokeAsync(ori, outMessage, asyncCallback);
            return null;
        }
        doRunInterceptorChain(outMessage);
        Object[] results = preProcessResult(outMessage);
        if (results != null && results.length == 1) {
            return results[0];
        }
        try {
            return handleResponse(outMessage, ori.getClassResourceInfo().getServiceClass());
        } finally {
            completeExchange(outMessage.getExchange(), true);
        }
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != configuredBus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Annotation(java.lang.annotation.Annotation)

Example 14 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project cxf by apache.

the class WebClient method doChainedInvocation.

// CHECKSTYLE:OFF
protected Response doChainedInvocation(String httpMethod, MultivaluedMap<String, String> headers, Object body, Class<?> requestClass, Type inType, Annotation[] inAnns, Class<?> respClass, Type outType, Exchange exchange, Map<String, Object> invContext) {
    // CHECKSTYLE:ON
    Bus configuredBus = getConfiguration().getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(configuredBus);
    ClassLoaderHolder origLoader = null;
    try {
        ClassLoader loader = configuredBus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType, inAnns, respClass, outType, exchange, invContext);
        doRunInterceptorChain(m);
        return doResponse(m, respClass, outType);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != configuredBus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)

Example 15 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project cxf by apache.

the class ClientProxyFactoryBean method create.

/**
 * Creates a proxy object that can be used to make remote invocations.
 *
 * @return the proxy. You must cast the returned object to the appropriate class before using it.
 */
public synchronized Object create() {
    ClassLoaderHolder orig = null;
    ClassLoader loader = null;
    try {
        if (getBus() != null) {
            loader = getBus().getExtension(ClassLoader.class);
            if (loader != null) {
                orig = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
        }
        configureObject();
        if (properties == null) {
            properties = new HashMap<>();
        }
        if (username != null) {
            AuthorizationPolicy authPolicy = new AuthorizationPolicy();
            authPolicy.setUserName(username);
            authPolicy.setPassword(password);
            properties.put(AuthorizationPolicy.class.getName(), authPolicy);
        }
        initFeatures();
        clientFactoryBean.setProperties(properties);
        if (bus != null) {
            clientFactoryBean.setBus(bus);
        }
        if (dataBinding != null) {
            clientFactoryBean.setDataBinding(dataBinding);
        }
        Client c = clientFactoryBean.create();
        if (getInInterceptors() != null) {
            c.getInInterceptors().addAll(getInInterceptors());
        }
        if (getOutInterceptors() != null) {
            c.getOutInterceptors().addAll(getOutInterceptors());
        }
        if (getInFaultInterceptors() != null) {
            c.getInFaultInterceptors().addAll(getInFaultInterceptors());
        }
        if (getOutFaultInterceptors() != null) {
            c.getOutFaultInterceptors().addAll(getOutFaultInterceptors());
        }
        ClientProxy handler = clientClientProxy(c);
        Class<?>[] classes = getImplementingClasses();
        Object obj = ProxyHelper.getProxy(getClassLoader(clientFactoryBean.getServiceClass()), classes, handler);
        this.getServiceFactory().sendEvent(FactoryBeanListener.Event.PROXY_CREATED, classes, handler, obj);
        return obj;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) Client(org.apache.cxf.endpoint.Client)

Aggregations

ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)36 Bus (org.apache.cxf.Bus)24 Message (org.apache.cxf.message.Message)15 IOException (java.io.IOException)7 Exchange (org.apache.cxf.message.Exchange)6 PhaseInterceptorChain (org.apache.cxf.phase.PhaseInterceptorChain)6 ServerImpl (org.apache.cxf.endpoint.ServerImpl)5 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)5 Endpoint (org.apache.cxf.endpoint.Endpoint)4 URISyntaxException (java.net.URISyntaxException)3 List (java.util.List)3 Application (javax.ws.rs.core.Application)3 Fault (org.apache.cxf.interceptor.Fault)3 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2