Search in sources :

Example 6 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion 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 = null;
        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 e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
        } catch (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 = message.getExchange().getBindingOperationInfo().getOperationInfo();
            QName faultName = getFaultName(fault, cause.getClass(), op);
            MessagePartInfo part = getFaultMessagePart(faultName, op);
            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 (Exception nex) {
            if (nex instanceof Fault) {
                message.setContent(Exception.class, nex);
                super.handleMessage(message);
            } else {
                // 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 {
        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 7 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class SOAPHandlerInterceptorTest method prepareSOAPHeader.

private Object[] prepareSOAPHeader() throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    SoapVersion soapVersion = Soap11.getInstance();
    Element envElement = doc.createElementNS(soapVersion.getEnvelope().getNamespaceURI(), soapVersion.getEnvelope().getLocalPart());
    Element headerElement = doc.createElementNS(soapVersion.getNamespace(), soapVersion.getHeader().getLocalPart());
    Element bodyElement = doc.createElementNS(soapVersion.getBody().getNamespaceURI(), soapVersion.getBody().getLocalPart());
    Element childElement = doc.createElementNS("http://apache.org/hello_world_rpclit/types", "ns2:header1");
    Attr attr = childElement.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
    attr.setValue("true");
    childElement.setAttributeNodeNS(attr);
    headerElement.appendChild(childElement);
    envElement.appendChild(headerElement);
    envElement.appendChild(bodyElement);
    doc.appendChild(envElement);
    return new Object[] { doc, headerElement };
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Example 8 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class ServiceJavascriptBuilder method createInputSerializer.

private void createInputSerializer() {
    // If are working on a wrapped method, then we use the wrapper element.
    // If we are working on an unwrapped method, we will have to work from the unwrapped parts.
    MessageInfo message = currentOperation.getInput();
    String serializerFunctionGlobalName = getFunctionGlobalName(message.getName(), "serializeInput");
    String serializerFunctionPropertyName = getFunctionPropertyName(inputMessagesWithNameConflicts, message, message.getName()) + "_serializeInput";
    code.append("function " + serializerFunctionGlobalName + "(cxfjsutils, args) {\n");
    String wrapperXmlElementName = null;
    // we could use the wrapped part, or we could use a conventional name.
    if (isWrapped) {
        wrapperXmlElementName = prefixAccumulator.xmlElementString(inputWrapperPartInfo.getConcreteName());
        utils.appendLine("var wrapperObj = new " + inputWrapperClassName + "();");
        int px = 0;
        for (String param : inputParameterNames) {
            utils.appendLine("wrapperObj.set" + StringUtils.capitalize(param) + "(args[" + px + "]);");
            px++;
        }
    } else if (isRPC) {
        // make sure the rpc element has a valid prefix
        prefixAccumulator.xmlElementString(currentOperation.getName());
    }
    if (soapBindingInfo != null) {
        SoapVersion soapVersion = soapBindingInfo.getSoapVersion();
        assert soapVersion.getVersion() == 1.1;
        utils.appendLine("var xml;");
        utils.appendLine("xml = cxfjsutils.beginSoap11Message(\"" + prefixAccumulator.getAttributes() + "\");");
    } else {
        // other alternative is XML, which isn't really all here yet.
        unsupportedConstruct("XML_BINDING", currentInterfaceClassName, xmlBindingInfo.getName());
    }
    utils.setXmlStringAccumulator("xml");
    if (isWrapped) {
        ParticleInfo elementInfo = ParticleInfo.forPartElement(inputWrapperElement, xmlSchemaCollection, "wrapperObj", wrapperXmlElementName);
        elementInfo.setContainingType(null);
        utils.generateCodeToSerializeElement(elementInfo, "", xmlSchemaCollection);
    } else {
        String operationXmlElement = null;
        if (isRPC) {
            operationXmlElement = prefixAccumulator.xmlElementString(currentOperation.getName());
            // RPC has a level of element for the entire operation.
            // we might have some schema to model this, but the following seems
            // sufficient.
            utils.appendString("<" + operationXmlElement + ">");
        }
        int px = 0;
        // the JavaScript programmer is stuck with the situation).
        for (ParticleInfo ean : unwrappedElementsAndNames) {
            String savedjsName = ean.getJavascriptName();
            try {
                ean.setJavascriptName("args[" + px + "]");
                utils.generateCodeToSerializeElement(ean, "", xmlSchemaCollection);
                px++;
            } finally {
                ean.setJavascriptName(savedjsName);
            }
        }
        if (isRPC) {
            utils.appendString("</" + operationXmlElement + ">");
        }
    }
    utils.appendLine("xml = xml + cxfjsutils.endSoap11Message();");
    utils.appendLine("return xml;");
    code.append("}\n\n");
    code.append(currentInterfaceClassName + ".prototype." + serializerFunctionPropertyName + " = " + serializerFunctionGlobalName + ";\n\n");
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Example 9 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class SecurityTokenServiceProvider method createSOAPFault.

private SoapFault createSOAPFault(Throwable ex) {
    String faultString = "Internal STS error";
    QName faultCode = null;
    if (ex != null) {
        if (ex instanceof STSException && ((STSException) ex).getFaultCode() != null) {
            faultCode = ((STSException) ex).getFaultCode();
        }
        faultString = ex.getMessage();
    }
    MessageContext messageContext = context.getMessageContext();
    SoapVersion soapVersion = (SoapVersion) messageContext.get(SoapVersion.class.getName());
    SoapFault fault;
    if (soapVersion.getVersion() == 1.1 && faultCode != null) {
        fault = new SoapFault(faultString, faultCode);
    } else {
        fault = new SoapFault(faultString, soapVersion.getSender());
        if (soapVersion.getVersion() != 1.1 && faultCode != null) {
            fault.setSubCode(faultCode);
        }
    }
    return fault;
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SoapFault(org.apache.cxf.binding.soap.SoapFault) QName(javax.xml.namespace.QName) MessageContext(javax.xml.ws.handler.MessageContext)

Example 10 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class RMEndpointTest method testCreateEndpoint.

@Test
public void testCreateEndpoint() throws NoSuchMethodException, EndpointException {
    Method m = RMEndpoint.class.getDeclaredMethod("getUsingAddressing", new Class[] { EndpointInfo.class });
    Service as = control.createMock(Service.class);
    EndpointInfo aei = new EndpointInfo();
    ae = new EndpointImpl(null, as, aei);
    rme = EasyMock.createMockBuilder(RMEndpoint.class).withConstructor(manager, ae).addMockedMethod(m).createMock(control);
    rme.setAplicationEndpoint(ae);
    rme.setManager(manager);
    SoapBindingInfo bi = control.createMock(SoapBindingInfo.class);
    aei.setBinding(bi);
    SoapVersion sv = Soap11.getInstance();
    EasyMock.expect(bi.getSoapVersion()).andReturn(sv);
    String ns = "http://schemas.xmlsoap.org/wsdl/soap/";
    EasyMock.expect(bi.getBindingId()).andReturn(ns);
    aei.setTransportId(ns);
    String addr = "addr";
    aei.setAddress(addr);
    Object ua = new Object();
    EasyMock.expect(rme.getUsingAddressing(aei)).andReturn(ua);
    control.replay();
    rme.createServices();
    rme.createEndpoints(null);
    Endpoint e = rme.getEndpoint(ProtocolVariation.RM10WSA200408);
    WrappedEndpoint we = (WrappedEndpoint) e;
    assertSame(ae, we.getWrappedEndpoint());
    Service s = rme.getService(ProtocolVariation.RM10WSA200408);
    assertEquals(1, s.getEndpoints().size());
    assertSame(e, s.getEndpoints().get(RM10Constants.PORT_NAME));
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) EndpointImpl(org.apache.cxf.endpoint.EndpointImpl) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) Service(org.apache.cxf.service.Service) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

SoapVersion (org.apache.cxf.binding.soap.SoapVersion)26 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)15 QName (javax.xml.namespace.QName)12 Element (org.w3c.dom.Element)11 SoapFault (org.apache.cxf.binding.soap.SoapFault)10 Header (org.apache.cxf.headers.Header)7 Fault (org.apache.cxf.interceptor.Fault)7 SOAPMessage (javax.xml.soap.SOAPMessage)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)6 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)5 Message (org.apache.cxf.common.i18n.Message)5 Exchange (org.apache.cxf.message.Exchange)5 Node (org.w3c.dom.Node)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)4 SOAPElement (javax.xml.soap.SOAPElement)4 SOAPException (javax.xml.soap.SOAPException)4