Search in sources :

Example 11 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class SoapHeaderInterceptor method handleMessage.

public void handleMessage(Message m) throws Fault {
    SoapMessage message = (SoapMessage) m;
    SoapVersion soapVersion = message.getVersion();
    Exchange exchange = message.getExchange();
    MessageContentsList parameters = MessageContentsList.getContentsList(message);
    if (null == parameters) {
        parameters = new MessageContentsList();
    }
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    if (null == bop) {
        return;
    }
    if (bop.isUnwrapped()) {
        bop = bop.getWrappedOperation();
    }
    boolean client = isRequestor(message);
    BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
    if (bmi == null) {
        // one way operation.
        return;
    }
    List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
    if (headers == null || headers.isEmpty()) {
        return;
    }
    boolean supportsNode = this.supportsDataReader(message, Node.class);
    Service service = ServiceModelUtil.getService(message.getExchange());
    for (SoapHeaderInfo header : headers) {
        MessagePartInfo mpi = header.getPart();
        try {
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
                Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
                validateHeader(message, mpi, schema);
            }
        } catch (Fault f) {
            if (!isRequestor(message)) {
                f.setFaultCode(Fault.FAULT_CODE_CLIENT);
            }
            throw f;
        }
        if (mpi.getTypeClass() != null) {
            Header param = findHeader(message, mpi);
            Object object = null;
            if (param != null) {
                message.getHeaders().remove(param);
                if (param.getDataBinding() == null) {
                    Node source = (Node) param.getObject();
                    if (source instanceof Element) {
                        // need to remove these attributes as they
                        // would cause validation failures
                        Element el = (Element) source;
                        el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
                        el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
                    }
                    if (supportsNode) {
                        object = getNodeDataReader(message).read(mpi, source);
                    } else {
                        W3CDOMStreamReader reader = new W3CDOMStreamReader((Element) source);
                        try {
                            // advance into the first tag
                            reader.nextTag();
                        } catch (XMLStreamException e) {
                        // ignore
                        }
                        object = getDataReader(message, XMLStreamReader.class).read(mpi, reader);
                    }
                } else {
                    object = param.getObject();
                }
            }
            parameters.put(mpi, object);
        }
    }
    if (!parameters.isEmpty()) {
        message.setContent(List.class, parameters);
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) SoapHeaderInfo(org.apache.cxf.binding.soap.model.SoapHeaderInfo) Schema(javax.xml.validation.Schema) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Header(org.apache.cxf.headers.Header) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 12 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class RPCOutInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    ServiceInfo si = getMockedServiceModel(this.getClass().getResource("/wsdl_soap/hello_world_rpc_lit.wsdl").toString());
    BindingInfo bi = si.getBinding(new QName(TNS, "Greeter_SOAPBinding_RPCLit"));
    BindingOperationInfo boi = bi.getOperation(new QName(TNS, OPNAME));
    boi.getOperationInfo().getOutput().getMessagePartByIndex(0).setIndex(0);
    soapMessage.getExchange().put(BindingOperationInfo.class, boi);
    control.reset();
    Service service = control.createMock(Service.class);
    EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();
    JAXBDataBinding dataBinding = new JAXBDataBinding(MyComplexStruct.class);
    service.getDataBinding();
    EasyMock.expectLastCall().andReturn(dataBinding).anyTimes();
    service.getServiceInfos();
    List<ServiceInfo> list = Arrays.asList(si);
    EasyMock.expectLastCall().andReturn(list).anyTimes();
    soapMessage.getExchange().put(Service.class, service);
    soapMessage.getExchange().put(Message.SCHEMA_VALIDATION_ENABLED, Boolean.FALSE);
    control.replay();
    MyComplexStruct mcs = new MyComplexStruct();
    mcs.setElem1("elem1");
    mcs.setElem2("elem2");
    mcs.setElem3(45);
    MessageContentsList param = new MessageContentsList();
    param.add(mcs);
    soapMessage.setContent(List.class, param);
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) QName(javax.xml.namespace.QName) BindingInfo(org.apache.cxf.service.model.BindingInfo) Service(org.apache.cxf.service.Service) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) Before(org.junit.Before)

Example 13 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class JAXRSInvoker method invoke.

@SuppressWarnings("unchecked")
public Object invoke(Exchange exchange, Object request, Object resourceObject) {
    final OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
    final ClassResourceInfo cri = ori.getClassResourceInfo();
    final Message inMessage = exchange.getInMessage();
    final ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(inMessage);
    if (cri.isRoot()) {
        cri.injectContexts(resourceObject, ori, inMessage);
        ProviderInfo<Application> appProvider = providerFactory.getApplicationProvider();
        if (appProvider != null) {
            InjectionUtils.injectContexts(appProvider.getProvider(), appProvider, inMessage);
        }
    }
    Method methodToInvoke = getMethodToInvoke(cri, ori, resourceObject);
    List<Object> params = null;
    if (request instanceof List) {
        params = CastUtils.cast((List<?>) request);
    } else if (request != null) {
        params = new MessageContentsList(request);
    }
    Object result = null;
    ClassLoaderHolder contextLoader = null;
    AsyncResponseImpl asyncResponse = null;
    try {
        if (setServiceLoaderAsContextLoader(inMessage)) {
            contextLoader = ClassLoaderUtils.setThreadContextClassloader(resourceObject.getClass().getClassLoader());
        }
        if (!ori.isSubResourceLocator()) {
            asyncResponse = (AsyncResponseImpl) inMessage.get(AsyncResponse.class);
        }
        result = invoke(exchange, resourceObject, methodToInvoke, params);
        if (asyncResponse == null && !ori.isSubResourceLocator()) {
            asyncResponse = checkFutureResponse(inMessage, checkResultObject(result));
        }
        if (asyncResponse != null) {
            if (!asyncResponse.suspendContinuationIfNeeded()) {
                result = handleAsyncResponse(exchange, asyncResponse);
            } else {
                providerFactory.clearThreadLocalProxies();
            }
        }
    } catch (Fault ex) {
        Object faultResponse;
        if (asyncResponse != null) {
            faultResponse = handleAsyncFault(exchange, asyncResponse, ex.getCause() == null ? ex : ex.getCause());
        } else {
            faultResponse = handleFault(ex, inMessage, cri, methodToInvoke);
        }
        return faultResponse;
    } finally {
        exchange.put(LAST_SERVICE_OBJECT, resourceObject);
        if (contextLoader != null) {
            contextLoader.reset();
        }
    }
    ClassResourceInfo subCri = null;
    if (ori.isSubResourceLocator()) {
        try {
            MultivaluedMap<String, String> values = getTemplateValues(inMessage);
            String subResourcePath = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
            String httpMethod = (String) inMessage.get(Message.HTTP_REQUEST_METHOD);
            String contentType = (String) inMessage.get(Message.CONTENT_TYPE);
            if (contentType == null) {
                contentType = "*/*";
            }
            List<MediaType> acceptContentType = (List<MediaType>) exchange.get(Message.ACCEPT_CONTENT_TYPE);
            result = checkSubResultObject(result, subResourcePath);
            Class<?> subResponseType = null;
            if (result.getClass() == Class.class) {
                ResourceContext rc = new ResourceContextImpl(inMessage, ori);
                result = rc.getResource((Class<?>) result);
                subResponseType = InjectionUtils.getActualType(methodToInvoke.getGenericReturnType());
            } else {
                subResponseType = methodToInvoke.getReturnType();
            }
            subCri = cri.getSubResource(subResponseType, ClassHelper.getRealClass(exchange.getBus(), result), result);
            if (subCri == null) {
                org.apache.cxf.common.i18n.Message errorM = new org.apache.cxf.common.i18n.Message("NO_SUBRESOURCE_FOUND", BUNDLE, subResourcePath);
                LOG.severe(errorM.toString());
                throw ExceptionUtils.toNotFoundException(null, null);
            }
            OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(Collections.singletonMap(subCri, values), inMessage, httpMethod, values, contentType, acceptContentType);
            exchange.put(OperationResourceInfo.class, subOri);
            inMessage.put(URITemplate.TEMPLATE_PARAMETERS, values);
            if (!subOri.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, inMessage, false, subOri.getNameBindings())) {
                return new MessageContentsList(exchange.get(Response.class));
            }
            // work out request parameters for the sub-resource class. Here we
            // presume InputStream has not been consumed yet by the root resource class.
            List<Object> newParams = JAXRSUtils.processParameters(subOri, values, inMessage);
            inMessage.setContent(List.class, newParams);
            return this.invoke(exchange, newParams, result);
        } catch (IOException ex) {
            Response resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
            if (resp == null) {
                resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
            }
            return new MessageContentsList(resp);
        } catch (WebApplicationException ex) {
            Response excResponse;
            if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), (String) inMessage.get(Message.HTTP_REQUEST_METHOD))) {
                excResponse = JAXRSUtils.createResponse(Collections.singletonList(subCri), null, null, 200, true);
            } else {
                excResponse = JAXRSUtils.convertFaultToResponse(ex, inMessage);
            }
            return new MessageContentsList(excResponse);
        }
    }
    setResponseContentTypeIfNeeded(inMessage, result);
    return result;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) WebApplicationException(javax.ws.rs.WebApplicationException) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) Fault(org.apache.cxf.interceptor.Fault) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) MediaType(javax.ws.rs.core.MediaType) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) IOException(java.io.IOException) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Application(javax.ws.rs.core.Application)

Example 14 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class JAXRSDefaultFaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (PropertyUtils.isTrue(message.getExchange().get(JAXRSUtils.SECOND_JAXRS_EXCEPTION))) {
        return;
    }
    final Fault f = (Fault) message.getContent(Exception.class);
    Response r = JAXRSUtils.convertFaultToResponse(f.getCause(), message);
    if (r != null) {
        JAXRSUtils.setMessageContentType(message, r);
        message.setContent(List.class, new MessageContentsList(r));
        if (message.getExchange().getOutMessage() == null && message.getExchange().getOutFaultMessage() != null) {
            message.getExchange().setOutMessage(message.getExchange().getOutFaultMessage());
        }
        new JAXRSOutInterceptor().handleMessage(message);
        return;
    }
    ServerProviderFactory.releaseRequestState(message);
    if (mustPropogateException(message)) {
        throw f;
    }
    new StaxOutInterceptor().handleMessage(message);
    message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
    NSStack nsStack = new NSStack();
    nsStack.push();
    XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
    try {
        nsStack.add("http://cxf.apache.org/bindings/xformat");
        String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
        StaxUtils.writeStartElement(writer, prefix, "XMLFault", "http://cxf.apache.org/bindings/xformat");
        StaxUtils.writeStartElement(writer, prefix, "faultstring", "http://cxf.apache.org/bindings/xformat");
        Throwable t = f.getCause();
        writer.writeCharacters(t == null ? f.getMessage() : t.toString());
        // fault string
        writer.writeEndElement();
        if (f.getDetail() != null) {
            StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
            StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE), writer, false);
            writer.writeEndElement();
        }
        // fault root
        writer.writeEndElement();
        writer.flush();
    } catch (XMLStreamException xe) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
    }
}
Also used : StaxOutInterceptor(org.apache.cxf.interceptor.StaxOutInterceptor) MessageContentsList(org.apache.cxf.message.MessageContentsList) Message(org.apache.cxf.message.Message) Fault(org.apache.cxf.interceptor.Fault) XMLStreamException(javax.xml.stream.XMLStreamException) Response(javax.ws.rs.core.Response) NSStack(org.apache.cxf.helpers.NSStack) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 15 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class JAXRSInInterceptor method createOutMessage.

private Message createOutMessage(Message inMessage, Response r) {
    Endpoint e = inMessage.getExchange().getEndpoint();
    Message mout = e.getBinding().createMessage();
    mout.setContent(List.class, new MessageContentsList(r));
    mout.setExchange(inMessage.getExchange());
    mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(inMessage.getExchange()));
    inMessage.getExchange().setOutMessage(mout);
    if (r.getStatus() >= Response.Status.BAD_REQUEST.getStatusCode()) {
        inMessage.getExchange().put("cxf.io.cacheinput", Boolean.FALSE);
    }
    return mout;
}
Also used : Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList)

Aggregations

MessageContentsList (org.apache.cxf.message.MessageContentsList)63 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)24 Exchange (org.apache.cxf.message.Exchange)22 Fault (org.apache.cxf.interceptor.Fault)19 Message (org.apache.cxf.message.Message)19 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)18 ArrayList (java.util.ArrayList)13 List (java.util.List)13 MessageImpl (org.apache.cxf.message.MessageImpl)11 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 Method (java.lang.reflect.Method)9 OperationInfo (org.apache.cxf.service.model.OperationInfo)9 Response (javax.ws.rs.core.Response)8 XMLStreamException (javax.xml.stream.XMLStreamException)8 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)8 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)8 MessageInfo (org.apache.cxf.service.model.MessageInfo)8 Test (org.junit.Test)8 Service (org.apache.cxf.service.Service)7