Package org.apache.tuscany.sca.common.http.cors

Examples of org.apache.tuscany.sca.common.http.cors.CORSConfiguration


    @Test
    public void testLoadValidComposite() throws Exception {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(COMPOSITE));
       
        //Composite composite = (Composite)staxProcessor.read(reader, context);
        CORSConfiguration config = (CORSConfiguration) staxProcessor.read(reader, context);
       
        Assert.assertNotNull(config);
        Assert.assertTrue(config.isAllowCredentials());
        Assert.assertEquals(3600, config.getMaxAge());
        Assert.assertEquals(1,config.getAllowOrigins().size());
        Assert.assertEquals(2,config.getAllowMethods().size());
        Assert.assertEquals(2,config.getAllowHeaders().size());
        Assert.assertEquals(1,config.getExposeHeaders().size());       
    }
View Full Code Here


    }

    @Override
    public CORSConfiguration read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException {

        CORSConfiguration corsConfiguration = corsConfigurationFactory.createCORSConfiguration();
       
        /**
         *  <corsConfiguration>
         *       <allowCredentials>true</allowCredentials>
         *
         *       <maxAge>100</maxAge>
         *
         *       <allowOrigins>
         *          <origin>http://www.sfly.com</origin>
         *       </allowOrigins>
         *
         *       <allowMethods>
         *          <method>PUT</method>
         *          <method>POST</method>
         *      </allowMethods>
         *
         *       <allowHeaders>
         *          <header>X-custom-1</header>
         *          <header>X-custom-2</header>
         *       </allowHeaders>
         *
         *       <exposeHeaders>
         *          <header>X-custom-1</header>
         *          <header>X-custom-2</header>
         *       </exposeHeaders>
         *
         *    </corsConfiguration>
         **/
       
        HeaderElementType headerType = null;
       
        while(reader.hasNext()) {
            QName elementName = null;
            int event = reader.getEventType();
            switch (event) {
                case START_ELEMENT:
                    elementName = reader.getName();

                   if (ALLOW_CREDENTIALS_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.setAllowCredentials(Boolean.parseBoolean(value));
                    } else if (MAX_AGE_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.setMaxAge(Integer.parseInt(value));
                    } else if (ORIGIN_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.getAllowOrigins().add(value);
                    } else if (METHOD_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.getAllowMethods().add(value);
                    } else if (ALLOW_HEADERS_QNAME.equals(elementName)) {
                        headerType = HeaderElementType.ALLOWHEADERS;
                    } else if (EXPOSE_HEADERS_QNAME.equals(elementName)) {
                        headerType = HeaderElementType.EXPOSEHEADERS;
                    } else if (HEADER_QNAME.equals(elementName)) {
                        if(headerType != null) {
                            String value = reader.getElementText();
                            if(headerType == HeaderElementType.ALLOWHEADERS) {
                                corsConfiguration.getAllowHeaders().add(value);
                            } else {
                                corsConfiguration.getExposeHeaders().add(value);
                            }
                        }
                    }
                  
                   break;
View Full Code Here

    }

    @Override
    public CORSConfiguration read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException {

        CORSConfiguration corsConfiguration = corsConfigurationFactory.createCORSConfiguration();
       
        /**
         *  <corsConfiguration>
         *       <allowCredentials>true</allowCredentials>
         *
         *       <maxAge>100</maxAge>
         *
         *       <allowOrigins>
         *          <origin>http://www.sfly.com</origin>
         *       </allowOrigins>
         *
         *       <allowMethods>
         *          <method>PUT</method>
         *          <method>POST</method>
         *      </allowMethods>
         *
         *       <allowHeaders>
         *          <header>X-custom-1</header>
         *          <header>X-custom-2</header>
         *       </allowHeaders>
         *
         *       <exposeHeaders>
         *          <header>X-custom-1</header>
         *          <header>X-custom-2</header>
         *       </exposeHeaders>
         *
         *    </corsConfiguration>
         **/
       
        HeaderElementType headerType = null;
       
        while(reader.hasNext()) {
            QName elementName = null;
            int event = reader.getEventType();
            switch (event) {
                case START_ELEMENT:
                    elementName = reader.getName();

                   if (ALLOW_CREDENTIALS_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.setAllowCredentials(Boolean.parseBoolean(value));
                    } else if (MAX_AGE_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.setMaxAge(Integer.parseInt(value));
                    } else if (ORIGIN_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.getAllowOrigins().add(value);
                    } else if (METHOD_QNAME.equals(elementName)) {
                        String value = reader.getElementText();
                        corsConfiguration.getAllowMethods().add(value);
                    } else if (ALLOW_HEADERS_QNAME.equals(elementName)) {
                        headerType = HeaderElementType.ALLOWHEADERS;
                    } else if (EXPOSE_HEADERS_QNAME.equals(elementName)) {
                        headerType = HeaderElementType.EXPOSEHEADERS;
                    } else if (HEADER_QNAME.equals(elementName)) {
                        if(headerType != null) {
                            String value = reader.getElementText();
                            if(headerType == HeaderElementType.ALLOWHEADERS) {
                                corsConfiguration.getAllowHeaders().add(value);
                            } else {
                                corsConfiguration.getExposeHeaders().add(value);
                            }
                        }
                    }
                  
                   break;
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.common.http.cors.CORSConfiguration

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.