Search in sources :

Example 21 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class WebFaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Fault f = (Fault) message.getContent(Exception.class);
    if (f == null) {
        return;
    }
    try {
        Throwable thr = f.getCause();
        SOAPFaultException sf = null;
        if (thr instanceof SOAPFaultException) {
            sf = (SOAPFaultException) thr;
        } else if (thr.getCause() instanceof SOAPFaultException) {
            sf = (SOAPFaultException) thr.getCause();
        }
        if (sf != null) {
            SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
            if (soapVersion != null && soapVersion.getVersion() != 1.1) {
                if (f instanceof SoapFault) {
                    for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
                        ((SoapFault) f).addSubCode(it.next());
                    }
                }
                if (sf.getFault().getFaultReasonLocales().hasNext()) {
                    Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
                    String convertedLang = lang.getLanguage();
                    String country = lang.getCountry();
                    if (country.length() > 0) {
                        convertedLang = convertedLang + '-' + country;
                    }
                    f.setLang(convertedLang);
                }
            }
            message.setContent(Exception.class, f);
        }
    } catch (Exception e) {
    // do nothing;
    }
    Throwable cause = f.getCause();
    WebFault fault = null;
    if (cause != null) {
        fault = getWebFaultAnnotation(cause.getClass());
        if (fault == null && cause.getCause() != null) {
            fault = getWebFaultAnnotation(cause.getCause().getClass());
            if (fault != null || cause instanceof RuntimeException) {
                cause = cause.getCause();
            }
        }
    }
    if (cause instanceof Exception && fault != null) {
        Exception ex = (Exception) cause;
        Object faultInfo;
        try {
            Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
            faultInfo = method.invoke(cause, new Object[0]);
        } catch (NoSuchMethodException e) {
            faultInfo = createFaultInfoBean(fault, cause);
        } catch (InvocationTargetException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
        } catch (IllegalAccessException | IllegalArgumentException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
        }
        Service service = message.getExchange().getService();
        try {
            DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
                Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
                writer.setSchema(schema);
            }
            OperationInfo op = null;
            // Prevent a NPE if we can't match the operation
            if (message.getExchange().getBindingOperationInfo() != null) {
                op = message.getExchange().getBindingOperationInfo().getOperationInfo();
            }
            QName faultName = getFaultName(fault, cause.getClass(), op);
            MessagePartInfo part = op != null ? getFaultMessagePart(faultName, op) : null;
            if (f.hasDetails()) {
                writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
            } else {
                writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
                if (!f.getDetail().hasChildNodes()) {
                    f.setDetail(null);
                }
            }
            f.setMessage(ex.getMessage());
        } catch (Fault f2) {
            message.setContent(Exception.class, f2);
            super.handleMessage(message);
        } catch (Exception nex) {
            // if exception occurs while writing a fault, we'll just let things continue
            // and let the rest of the chain try handling it as is.
            LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
        }
    } else if (cause instanceof SOAPFaultException && ((SOAPFaultException) cause).getFault().hasDetail()) {
        return;
    } else if (f instanceof SoapFault && f.getCause() instanceof SOAPFaultException && ((SOAPFaultException) f.getCause()).getFault().hasDetail()) {
        return;
    } else {
        FaultMode mode = message.get(FaultMode.class);
        if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
            // only convert checked exceptions with this
            // otherwise delegate down to the normal protocol specific stuff
            super.handleMessage(message);
        }
    }
}
Also used : Locale(java.util.Locale) OperationInfo(org.apache.cxf.service.model.OperationInfo) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) Schema(javax.xml.validation.Schema) Fault(org.apache.cxf.interceptor.Fault) WebFault(javax.xml.ws.WebFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) WebFault(javax.xml.ws.WebFault) FaultMode(org.apache.cxf.message.FaultMode) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion)

Example 22 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class WrapperClassInInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Exchange ex = message.getExchange();
    BindingOperationInfo boi = ex.getBindingOperationInfo();
    if (Boolean.TRUE.equals(message.get(Message.PARTIAL_RESPONSE_MESSAGE)) || boi == null) {
        return;
    }
    Method method = ex.get(Method.class);
    if (method != null && method.getName().endsWith("Async")) {
        Class<?> retType = method.getReturnType();
        if ("java.util.concurrent.Future".equals(retType.getName()) || "javax.xml.ws.Response".equals(retType.getName())) {
            return;
        }
    }
    if (boi.isUnwrappedCapable()) {
        BindingOperationInfo boi2 = boi.getUnwrappedOperation();
        OperationInfo op = boi2.getOperationInfo();
        BindingMessageInfo bmi;
        MessageInfo wrappedMessageInfo = message.get(MessageInfo.class);
        MessageInfo messageInfo;
        if (wrappedMessageInfo == boi.getOperationInfo().getInput()) {
            messageInfo = op.getInput();
            bmi = boi2.getInput();
        } else {
            messageInfo = op.getOutput();
            bmi = boi2.getOutput();
        }
        // Sometimes, an operation can be unwrapped according to WSDLServiceFactory,
        // but not according to JAX-WS. We should unify these at some point, but
        // for now check for the wrapper class.
        MessageContentsList lst = MessageContentsList.getContentsList(message);
        if (lst == null) {
            return;
        }
        message.put(MessageInfo.class, messageInfo);
        message.put(BindingMessageInfo.class, bmi);
        ex.put(BindingOperationInfo.class, boi2);
        if (isGET(message)) {
            LOG.fine("WrapperClassInInterceptor skipped in HTTP GET method");
            return;
        }
        MessagePartInfo wrapperPart = wrappedMessageInfo.getFirstMessagePart();
        Class<?> wrapperClass = wrapperPart.getTypeClass();
        Object wrappedObject = lst.get(wrapperPart.getIndex());
        if (wrapperClass == null || wrappedObject == null || !wrapperClass.isInstance(wrappedObject)) {
            return;
        }
        WrapperHelper helper = wrapperPart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
        if (helper == null) {
            Service service = ServiceModelUtil.getService(message.getExchange());
            DataBinding dataBinding = service.getDataBinding();
            if (dataBinding instanceof WrapperCapableDatabinding) {
                helper = createWrapperHelper((WrapperCapableDatabinding) dataBinding, messageInfo, wrappedMessageInfo, wrapperClass);
                wrapperPart.setProperty("WRAPPER_CLASS", helper);
            } else {
                return;
            }
        }
        MessageContentsList newParams;
        try {
            newParams = new MessageContentsList(helper.getWrapperParts(wrappedObject));
            List<Integer> removes = null;
            int count = 0;
            for (MessagePartInfo part : messageInfo.getMessageParts()) {
                if (Boolean.TRUE.equals(part.getProperty(ReflectionServiceFactoryBean.HEADER))) {
                    MessagePartInfo mpi = null;
                    for (MessagePartInfo mpi2 : wrappedMessageInfo.getMessageParts()) {
                        if (mpi2.getConcreteName().equals(part.getConcreteName())) {
                            mpi = mpi2;
                        }
                    }
                    if (mpi != null && lst.hasValue(mpi)) {
                        count++;
                        newParams.put(part, lst.get(mpi));
                    } else if (mpi == null || mpi.getTypeClass() == null) {
                        // header, but not mapped to a param on the method
                        if (removes == null) {
                            removes = new ArrayList<>();
                        }
                        removes.add(part.getIndex());
                    }
                } else {
                    ++count;
                }
            }
            if (count == 0) {
                newParams.clear();
            } else if (removes != null) {
                Collections.sort(removes, Collections.reverseOrder());
                for (Integer i : removes) {
                    if (i < newParams.size()) {
                        newParams.remove(i.intValue());
                    }
                }
            }
        } catch (Exception e) {
            throw new Fault(e);
        }
        message.setContent(List.class, newParams);
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) WrapperCapableDatabinding(org.apache.cxf.databinding.WrapperCapableDatabinding) ArrayList(java.util.ArrayList) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Exchange(org.apache.cxf.message.Exchange) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) DataBinding(org.apache.cxf.databinding.DataBinding) WrapperHelper(org.apache.cxf.databinding.WrapperHelper)

Example 23 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class JaxWsServiceFactoryBean method initializeWSDLOperationsForProvider.

protected void initializeWSDLOperationsForProvider() {
    Class<?> c = getProviderParameterType(getServiceClass());
    if (c == null) {
        throw new ServiceConstructionException(new Message("INVALID_PROVIDER_EXC", LOG));
    }
    if (getEndpointInfo() == null && isFromWsdl()) {
        // most likely, they specified a WSDL, but for some reason
        // did not give a valid ServiceName/PortName.  For provider,
        // we'll allow this since everything is bound directly to
        // the invoke method, however, this CAN cause other problems
        // such as addresses in the wsdl not getting updated and such
        // so we'll WARN about it.....
        List<QName> enames = new ArrayList<>();
        for (ServiceInfo si : getService().getServiceInfos()) {
            for (EndpointInfo ep : si.getEndpoints()) {
                enames.add(ep.getName());
            }
        }
        LOG.log(Level.WARNING, "COULD_NOT_FIND_ENDPOINT", new Object[] { getEndpointName(), enames });
    }
    try {
        Method invoke = getServiceClass().getMethod("invoke", c);
        QName catchAll = new QName("http://cxf.apache.org/jaxws/provider", "invoke");
        // Bind every operation to the invoke method.
        for (ServiceInfo si : getService().getServiceInfos()) {
            si.setProperty("soap.force.doclit.bare", Boolean.TRUE);
            if (!isFromWsdl()) {
                for (EndpointInfo ei : si.getEndpoints()) {
                    ei.setProperty("soap.no.validate.parts", Boolean.TRUE);
                }
            }
            for (BindingInfo bind : si.getBindings()) {
                for (BindingOperationInfo bop : bind.getOperations()) {
                    OperationInfo o = bop.getOperationInfo();
                    if (bop.isUnwrappedCapable()) {
                        // force to bare, no unwrapping
                        bop.getOperationInfo().setUnwrappedOperation(null);
                        bop.setUnwrappedOperation(null);
                    }
                    if (o.getInput() != null) {
                        final List<MessagePartInfo> messageParts;
                        if (o.getInput().getMessagePartsNumber() == 0) {
                            MessagePartInfo inf = o.getInput().addMessagePart(o.getName());
                            inf.setConcreteName(o.getName());
                            messageParts = o.getInput().getMessageParts();
                            bop.getInput().setMessageParts(messageParts);
                        } else {
                            messageParts = o.getInput().getMessageParts();
                        }
                        for (MessagePartInfo inf : messageParts) {
                            inf.setTypeClass(c);
                            break;
                        }
                    }
                    if (o.getOutput() != null) {
                        final List<MessagePartInfo> messageParts;
                        if (o.getOutput().getMessagePartsNumber() == 0) {
                            MessagePartInfo inf = o.getOutput().addMessagePart(o.getName());
                            inf.setConcreteName(new QName(o.getName().getNamespaceURI(), o.getName().getLocalPart() + "Response"));
                            messageParts = o.getOutput().getMessageParts();
                            bop.getOutput().setMessageParts(messageParts);
                        } else {
                            messageParts = o.getOutput().getMessageParts();
                        }
                        for (MessagePartInfo inf : messageParts) {
                            inf.setTypeClass(c);
                            break;
                        }
                    }
                    getMethodDispatcher().bind(o, invoke);
                }
                BindingOperationInfo bop = bind.getOperation(catchAll);
                if (bop == null) {
                    OperationInfo op = bind.getInterface().getOperation(catchAll);
                    if (op == null) {
                        op = bind.getInterface().addOperation(catchAll);
                        String name = catchAll.getLocalPart();
                        MessageInfo mInfo = op.createMessage(new QName(catchAll.getNamespaceURI(), name + "Request"), MessageInfo.Type.INPUT);
                        op.setInput(catchAll.getLocalPart() + "Request", mInfo);
                        MessagePartInfo mpi = mInfo.addMessagePart("parameters");
                        mpi.setElement(true);
                        mpi.setTypeClass(c);
                        mpi.setTypeQName(Constants.XSD_ANYTYPE);
                        mInfo = op.createMessage(new QName(catchAll.getNamespaceURI(), name + "Response"), MessageInfo.Type.OUTPUT);
                        op.setOutput(name + "Response", mInfo);
                        mpi = mInfo.addMessagePart("parameters");
                        mpi.setElement(true);
                        mpi.setTypeClass(c);
                        mpi.setTypeQName(Constants.XSD_ANYTYPE);
                        BindingOperationInfo bo = new BindingOperationInfo(bind, op);
                        op.setProperty("operation.is.synthetic", Boolean.TRUE);
                        bo.setProperty("operation.is.synthetic", Boolean.TRUE);
                        bind.addOperation(bo);
                    }
                }
            }
        }
    } catch (SecurityException | NoSuchMethodException e) {
        throw new ServiceConstructionException(e);
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) BindingInfo(org.apache.cxf.service.model.BindingInfo)

Example 24 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class WrapperClassOutInterceptor method createWrapperHelper.

private WrapperHelper createWrapperHelper(WrapperCapableDatabinding dataBinding, MessageInfo messageInfo, MessageInfo wrappedMessageInfo, Class<?> wrapperClass) {
    List<String> partNames = new ArrayList<>();
    List<String> elTypeNames = new ArrayList<>();
    List<Class<?>> partClasses = new ArrayList<>();
    QName wrapperName = null;
    for (MessagePartInfo p : wrappedMessageInfo.getMessageParts()) {
        if (p.getTypeClass() == wrapperClass) {
            wrapperName = p.getElementQName();
        }
    }
    for (MessagePartInfo p : messageInfo.getMessageParts()) {
        if (p.getTypeClass() == null) {
            // WSDL part wasn't mapped to a parameter
            continue;
        }
        ensureSize(partNames, p.getIndex());
        ensureSize(elTypeNames, p.getIndex());
        ensureSize(partClasses, p.getIndex());
        partNames.set(p.getIndex(), p.getName().getLocalPart());
        final String elementType;
        if (p.getTypeQName() == null) {
            // handling anonymous complex type
            elementType = null;
        } else {
            elementType = p.getTypeQName().getLocalPart();
        }
        elTypeNames.set(p.getIndex(), elementType);
        partClasses.set(p.getIndex(), p.getTypeClass());
    }
    return dataBinding.createWrapperHelper(wrapperClass, wrapperName, partNames, elTypeNames, partClasses);
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo)

Example 25 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class WrapperClassOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Exchange ex = message.getExchange();
    BindingOperationInfo bop = ex.getBindingOperationInfo();
    MessageInfo messageInfo = message.get(MessageInfo.class);
    if (messageInfo == null || bop == null || !bop.isUnwrapped()) {
        return;
    }
    BindingOperationInfo newbop = bop.getWrappedOperation();
    MessageInfo wrappedMsgInfo;
    if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
        wrappedMsgInfo = newbop.getInput().getMessageInfo();
    } else {
        wrappedMsgInfo = newbop.getOutput().getMessageInfo();
    }
    Class<?> wrapped = null;
    if (wrappedMsgInfo.getMessagePartsNumber() > 0) {
        wrapped = wrappedMsgInfo.getFirstMessagePart().getTypeClass();
    }
    if (wrapped != null) {
        MessagePartInfo firstMessagePart = wrappedMsgInfo.getFirstMessagePart();
        MessageContentsList objs = MessageContentsList.getContentsList(message);
        WrapperHelper helper = firstMessagePart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
        if (helper == null) {
            helper = getWrapperHelper(message, messageInfo, wrappedMsgInfo, wrapped, firstMessagePart);
        }
        if (helper == null) {
            return;
        }
        try {
            MessageContentsList newObjs = new MessageContentsList();
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && helper instanceof AbstractWrapperHelper) {
                ((AbstractWrapperHelper) helper).setValidate(true);
            }
            Object o2 = helper.createWrapperObject(objs);
            newObjs.put(firstMessagePart, o2);
            for (MessagePartInfo p : messageInfo.getMessageParts()) {
                if (Boolean.TRUE.equals(p.getProperty(ReflectionServiceFactoryBean.HEADER))) {
                    MessagePartInfo mpi = wrappedMsgInfo.getMessagePart(p.getName());
                    if (objs.hasValue(p)) {
                        newObjs.put(mpi, objs.get(p));
                    }
                }
            }
            message.setContent(List.class, newObjs);
        } catch (Fault f) {
            throw f;
        } catch (Exception e) {
            throw new Fault(e);
        }
        // we've now wrapped the object, so use the wrapped binding op
        ex.put(BindingOperationInfo.class, newbop);
        if (messageInfo == bop.getOperationInfo().getInput()) {
            message.put(MessageInfo.class, newbop.getOperationInfo().getInput());
            message.put(BindingMessageInfo.class, newbop.getInput());
        } else if (messageInfo == bop.getOperationInfo().getOutput()) {
            message.put(MessageInfo.class, newbop.getOperationInfo().getOutput());
            message.put(BindingMessageInfo.class, newbop.getOutput());
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) AbstractWrapperHelper(org.apache.cxf.databinding.AbstractWrapperHelper) AbstractWrapperHelper(org.apache.cxf.databinding.AbstractWrapperHelper) WrapperHelper(org.apache.cxf.databinding.WrapperHelper) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo)

Aggregations

MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)163 QName (javax.xml.namespace.QName)99 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)60 OperationInfo (org.apache.cxf.service.model.OperationInfo)46 MessageInfo (org.apache.cxf.service.model.MessageInfo)38 Test (org.junit.Test)38 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)29 Fault (org.apache.cxf.interceptor.Fault)21 Service (org.apache.cxf.service.Service)21 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)21 BindingInfo (org.apache.cxf.service.model.BindingInfo)20 ArrayList (java.util.ArrayList)19 Endpoint (org.apache.cxf.endpoint.Endpoint)18 MessageContentsList (org.apache.cxf.message.MessageContentsList)18 SoapOperationInfo (org.apache.cxf.binding.soap.model.SoapOperationInfo)16 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)15 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Exchange (org.apache.cxf.message.Exchange)13 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)13 Method (java.lang.reflect.Method)12