Search in sources :

Example 1 with HandlerAnnotationInfo

use of com.sun.xml.ws.util.HandlerAnnotationInfo in project metro-jax-ws by eclipse-ee4j.

the class HandlerChainsModel method parseHandlerFile.

/**
 * <p>This method is called internally by HandlerAnnotationProcessor,
 * and by
 * {@link com.sun.xml.ws.transport.http.DeploymentDescriptorParser}
 * directly when it reaches the handler chains element in the
 * descriptor file it is parsing.
 * @param reader should be on &lt;handler-chains&gt; element
 * @return A HandlerAnnotationInfo object that stores the
 * handlers and roles.
 */
public static HandlerAnnotationInfo parseHandlerFile(XMLStreamReader reader, ClassLoader classLoader, QName serviceName, QName portName, WSBinding wsbinding) {
    ensureProperName(reader, QNAME_HANDLER_CHAINS);
    String bindingId = wsbinding.getBindingId().toString();
    HandlerAnnotationInfo info = new HandlerAnnotationInfo();
    XMLStreamReaderUtil.nextElementContent(reader);
    List<Handler> handlerChain = new ArrayList<>();
    Set<String> roles = new HashSet<>();
    while (reader.getName().equals(QNAME_HANDLER_CHAIN)) {
        XMLStreamReaderUtil.nextElementContent(reader);
        if (reader.getName().equals(QNAME_CHAIN_PORT_PATTERN)) {
            if (portName == null) {
                logger.warning("handler chain sepcified for port " + "but port QName passed to parser is null");
            }
            boolean parseChain = JAXWSUtils.matchQNames(portName, XMLStreamReaderUtil.getElementQName(reader));
            if (!parseChain) {
                skipChain(reader);
                continue;
            }
            XMLStreamReaderUtil.nextElementContent(reader);
        } else if (reader.getName().equals(QNAME_CHAIN_PROTOCOL_BINDING)) {
            if (bindingId == null) {
                logger.warning("handler chain sepcified for bindingId " + "but bindingId passed to parser is null");
            }
            String bindingConstraint = XMLStreamReaderUtil.getElementText(reader);
            boolean skipThisChain = true;
            StringTokenizer stk = new StringTokenizer(bindingConstraint);
            List<String> bindingList = new ArrayList<>();
            while (stk.hasMoreTokens()) {
                String tokenOrURI = stk.nextToken();
                /*
                    Convert short-form tokens to API's binding ids
                    Unknown token, Put it as it is
                    */
                tokenOrURI = DeploymentDescriptorParser.getBindingIdForToken(tokenOrURI);
                String binding = BindingID.parse(tokenOrURI).toString();
                bindingList.add(binding);
            }
            if (bindingList.contains(bindingId)) {
                skipThisChain = false;
            }
            if (skipThisChain) {
                skipChain(reader);
                continue;
            }
            XMLStreamReaderUtil.nextElementContent(reader);
        } else if (reader.getName().equals(QNAME_CHAIN_SERVICE_PATTERN)) {
            if (serviceName == null) {
                logger.warning("handler chain sepcified for service " + "but service QName passed to parser is null");
            }
            boolean parseChain = JAXWSUtils.matchQNames(serviceName, XMLStreamReaderUtil.getElementQName(reader));
            if (!parseChain) {
                skipChain(reader);
                continue;
            }
            XMLStreamReaderUtil.nextElementContent(reader);
        }
        // process all <handler> elements
        while (reader.getName().equals(QNAME_HANDLER)) {
            Handler handler;
            XMLStreamReaderUtil.nextContent(reader);
            if (reader.getName().equals(QNAME_HANDLER_NAME)) {
                skipTextElement(reader);
            }
            // handler class
            ensureProperName(reader, QNAME_HANDLER_CLASS);
            try {
                handler = (Handler) loadClass(classLoader, XMLStreamReaderUtil.getElementText(reader).trim()).newInstance();
            } catch (InstantiationException | IllegalAccessException ie) {
                throw new RuntimeException(ie);
            }
            XMLStreamReaderUtil.nextContent(reader);
            // init params (ignored)
            while (reader.getName().equals(QNAME_HANDLER_PARAM)) {
                skipInitParamElement(reader);
            }
            // headers (ignored)
            while (reader.getName().equals(QNAME_HANDLER_HEADER)) {
                skipTextElement(reader);
            }
            // roles (not stored per handler)
            while (reader.getName().equals(QNAME_HANDLER_ROLE)) {
                roles.add(XMLStreamReaderUtil.getElementText(reader));
                XMLStreamReaderUtil.nextContent(reader);
            }
            // call @PostConstruct method on handler if present
            for (Method method : handler.getClass().getMethods()) {
                if (method.getAnnotation(PostConstruct.class) == null) {
                    continue;
                }
                try {
                    method.invoke(handler);
                    break;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            handlerChain.add(handler);
            // move past </handler>
            ensureProperName(reader, QNAME_HANDLER);
            XMLStreamReaderUtil.nextContent(reader);
        }
        // move past </handler-chain>
        ensureProperName(reader, QNAME_HANDLER_CHAIN);
        XMLStreamReaderUtil.nextContent(reader);
    }
    info.setHandlers(handlerChain);
    info.setRoles(roles);
    return info;
}
Also used : ArrayList(java.util.ArrayList) Handler(jakarta.xml.ws.handler.Handler) Method(java.lang.reflect.Method) UtilException(com.sun.xml.ws.util.UtilException) HandlerAnnotationInfo(com.sun.xml.ws.util.HandlerAnnotationInfo) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) List(java.util.List) PostConstruct(jakarta.annotation.PostConstruct) HashSet(java.util.HashSet)

Example 2 with HandlerAnnotationInfo

use of com.sun.xml.ws.util.HandlerAnnotationInfo in project metro-jax-ws by eclipse-ee4j.

the class DeploymentDescriptorParser method setHandlersAndRoles.

/**
 * Parses the handler and role information and sets it
 * on the {@link WSBinding}.
 *
 * @return true if &lt;handler-chains&gt; element present in DD
 *         false otherwise.
 */
protected boolean setHandlersAndRoles(WSBinding binding, XMLStreamReader reader, QName serviceName, QName portName) {
    if (reader.getEventType() == XMLStreamConstants.END_ELEMENT || !reader.getName().equals(HandlerChainsModel.QNAME_HANDLER_CHAINS)) {
        return false;
    }
    HandlerAnnotationInfo handlerInfo = HandlerChainsModel.parseHandlerFile(reader, classLoader, serviceName, portName, binding);
    binding.setHandlerChain(handlerInfo.getHandlers());
    if (binding instanceof SOAPBinding) {
        ((SOAPBinding) binding).setRoles(handlerInfo.getRoles());
    }
    // move past </handler-chains>
    XMLStreamReaderUtil.nextContent(reader);
    return true;
}
Also used : HandlerAnnotationInfo(com.sun.xml.ws.util.HandlerAnnotationInfo) SOAPBinding(jakarta.xml.ws.soap.SOAPBinding)

Example 3 with HandlerAnnotationInfo

use of com.sun.xml.ws.util.HandlerAnnotationInfo in project metro-jax-ws by eclipse-ee4j.

the class HandlerChainsModel method getHandlersForPortInfo.

public HandlerAnnotationInfo getHandlersForPortInfo(PortInfo info) {
    HandlerAnnotationInfo handlerInfo = new HandlerAnnotationInfo();
    List<Handler> handlerClassList = new ArrayList<>();
    Set<String> roles = new HashSet<>();
    for (HandlerChainType hchain : handlerChains) {
        boolean hchainMatched = (!hchain.isConstraintSet()) || JAXWSUtils.matchQNames(info.getServiceName(), hchain.getServiceNamePattern()) || JAXWSUtils.matchQNames(info.getPortName(), hchain.getPortNamePattern()) || hchain.getProtocolBindings().contains(info.getBindingID());
        if (hchainMatched) {
            for (HandlerType handler : hchain.getHandlers()) {
                try {
                    Handler handlerClass = (Handler) loadClass(annotatedClass.getClassLoader(), handler.getHandlerClass()).newInstance();
                    callHandlerPostConstruct(handlerClass);
                    handlerClassList.add(handlerClass);
                } catch (InstantiationException | IllegalAccessException ie) {
                    throw new RuntimeException(ie);
                }
                roles.addAll(handler.getSoapRoles());
            }
        }
    }
    handlerInfo.setHandlers(handlerClassList);
    handlerInfo.setRoles(roles);
    return handlerInfo;
}
Also used : ArrayList(java.util.ArrayList) Handler(jakarta.xml.ws.handler.Handler) HandlerAnnotationInfo(com.sun.xml.ws.util.HandlerAnnotationInfo) HashSet(java.util.HashSet)

Aggregations

HandlerAnnotationInfo (com.sun.xml.ws.util.HandlerAnnotationInfo)3 Handler (jakarta.xml.ws.handler.Handler)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 UtilException (com.sun.xml.ws.util.UtilException)1 PostConstruct (jakarta.annotation.PostConstruct)1 SOAPBinding (jakarta.xml.ws.soap.SOAPBinding)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1