Package org.apache.tuscany.sca.binding.rest

Examples of org.apache.tuscany.sca.binding.rest.RESTBinding


    public Class<RESTBinding> getModelType() {
        return RESTBinding.class;
    }

    public RESTBinding read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException {
        RESTBinding restBinding = httpBindingFactory.createRESTBinding();

        /**
         *    <tuscany:binding.rest uri="http://localhost:8085/Customer">
         *          <tuscany:wireFormat.xml />
         *          <tuscany:operationSelector.jaxrs />
         *          <tuscany:http-headers>
         *             <tuscany:header name="Cache-Control" value="no-cache"/>
         *             <tuscany:header name="Expires" value="-1"/>
         *          </tuscany:http-headers>
         *          <tuscany:response>
         *             <tuscany:wireFormat.json />
         *          </tuscany:response>
         *   </tuscany:binding.rest>
         *
         */
        while(reader.hasNext()) {
            QName elementName = null;
            int event = reader.getEventType();
            switch (event) {
                case START_ELEMENT:
                    elementName = reader.getName();

                    if(RESTBinding.TYPE.equals(elementName)) {

                        // binding attributes
                        String name = getString(reader, NAME);
                        if(name != null) {
                            restBinding.setName(name);
                        }

                        String uri = getURIString(reader, URI);
                        if (uri != null) {
                            restBinding.setURI(uri);
                        }
                        break;

                    } else if (HEADERS_QNAME.equals(elementName)) {

                        // ignore wrapper element
                        break;

                    } else if (HEADER_QNAME.equals(elementName)) {

                        // header name/value pair
                        String name = getString(reader, NAME);
                        String value = getURIString(reader, VALUE);

                        if(name != null) {
                            restBinding.getHttpHeaders().add(new HTTPHeader(name, value));
                        }
                        break;

                    } else if (RESPONSE_QNAME.equals(elementName)) {

                        // skip response
                        reader.next();
                        // and position to the next start_element event
                        while (reader.hasNext()) {
                            int sub_event = reader.getEventType();
                            switch (sub_event) {
                                case START_ELEMENT:
                                    elementName = reader.getName();
                                    break;
                                default: reader.next();
                            }
                            break;
                        }

                        // dispatch to read wire format for the response
                        Object extension = extensionProcessor.read(reader, context);
                        if (extension != null) {
                            if (extension instanceof WireFormat) {
                                restBinding.setResponseWireFormat((WireFormat)extension);
                            }
                        }
                        break;
                    } else {
                        // Read an extension element
                        Object extension = extensionProcessor.read(reader, context);
                        if (extension != null) {
                            if (extension instanceof WireFormat) {
                                restBinding.setRequestWireFormat((WireFormat)extension);
                                restBinding.setResponseWireFormat((WireFormat)extension);
                            } else if(extension instanceof OperationSelector) {
                                restBinding.setOperationSelector((OperationSelector)extension);
                            }
                        }
                        break;
                    }

View Full Code Here


     */
    @Test
    public void testWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_OPERATION_SELECTOR));
       
        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);       
        Assert.assertNotNull(binding);
       
        OperationSelector operationSelector = binding.getOperationSelector();
        Assert.assertEquals(JAXRSOperationSelector.class, operationSelector.getClass().getInterfaces()[0]);       
    }
View Full Code Here

   
    @Test
    public void testWriteWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_OPERATION_SELECTOR));
       
        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);
        Assert.assertNotNull(binding);
        reader.close();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        staxProcessor.write(binding, bos, context);
View Full Code Here

    @Test
    public void testLoadValidComposite() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(COMPOSITE));
       
        Composite composite = (Composite)staxProcessor.read(reader, context);
        RESTBinding binding = (RESTBinding)   composite.getComponents().get(0).getServices().get(0).getBindings().get(0);
       
        Assert.assertNotNull(binding);
        Assert.assertEquals(2, binding.getHttpHeaders().size());
        Assert.assertEquals("Cache-Control", binding.getHttpHeaders().get(0).getName());
        Assert.assertEquals("no-cache", binding.getHttpHeaders().get(0).getValue());
    }
View Full Code Here

    @Test
    public void testLoadValidComposite() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(COMPOSITE));
       
        Composite composite = (Composite)staxProcessor.read(reader, context);
        RESTBinding binding = (RESTBinding)   composite.getComponents().get(0).getServices().get(0).getBindings().get(0);
       
        Assert.assertNotNull(binding);
        Assert.assertNotNull(binding.getRequestWireFormat());
        Assert.assertNotNull(binding.getOperationSelector());
        Assert.assertEquals(30000, binding.getReadTimeout());
        Assert.assertEquals(true,binding.isCORS());
        Assert.assertEquals(2, binding.getHttpHeaders().size());
        Assert.assertEquals("Cache-Control", binding.getHttpHeaders().get(0).getName());
        Assert.assertEquals("no-cache", binding.getHttpHeaders().get(0).getValue());
       
    }
View Full Code Here

     */
    @Test
    public void testWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_WIRE_FORMAT));

        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);
        Assert.assertNotNull(binding);

        WireFormat requestWireFormat = binding.getRequestWireFormat();
        Assert.assertEquals(JSONWireFormat.class, requestWireFormat.getClass().getInterfaces()[0]);
    }
View Full Code Here

     */
    @Test
    public void testWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_OPERATION_SELECTOR));
       
        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);       
        Assert.assertNotNull(binding);
       
        OperationSelector operationSelector = binding.getOperationSelector();
        Assert.assertEquals(JAXRSOperationSelector.class, operationSelector.getClass().getInterfaces()[0]);       
    }
View Full Code Here

     */
    @Test
    public void testResponseWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_RESPONSE_WIRE_FORMAT));

        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);
        Assert.assertNotNull(binding);

        WireFormat responseWireFormat = binding.getResponseWireFormat();
        Assert.assertEquals(JSONWireFormat.class, responseWireFormat.getClass().getInterfaces()[0]);
    }
View Full Code Here

   
    @Test
    public void testWriteWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_OPERATION_SELECTOR));
       
        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);
        Assert.assertNotNull(binding);
        reader.close();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        staxProcessor.write(binding, bos, context);
View Full Code Here

    @Test
    public void testWriteWireFormat() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_WIRE_FORMAT));

        RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context);
        Assert.assertNotNull(binding);
        reader.close();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        staxProcessor.write(binding, bos, context);
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.binding.rest.RESTBinding

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.