001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.systest.rest;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.xml.stream.XMLInputFactory;
024: import javax.xml.stream.XMLOutputFactory;
025:
026: import org.apache.cxf.binding.http.HttpBindingFactory;
027: import org.apache.cxf.customer.book.BookService;
028: import org.apache.cxf.customer.book.BookServiceImpl;
029: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
030: import org.apache.cxf.service.invoker.BeanInvoker;
031: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
032: import org.codehaus.jettison.mapped.MappedXMLInputFactory;
033: import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
034:
035: public class BookServer extends AbstractBusTestServerBase {
036:
037: protected void run() {
038: BookServiceImpl serviceObj = new BookServiceImpl();
039: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
040: sf.setServiceClass(BookService.class);
041: // Use the HTTP Binding which understands the Java Rest Annotations
042: sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
043: sf.setAddress("http://localhost:9080/xml/");
044: sf.getServiceFactory().setInvoker(new BeanInvoker(serviceObj));
045:
046: // Turn the "wrapped" style off. This means that CXF won't generate
047: // wrapper XML elements and we'll have prettier XML text. This
048: // means that we need to stick to one request and one response
049: // parameter though.
050: sf.getServiceFactory().setWrapped(false);
051:
052: sf.create();
053:
054: JaxWsServerFactoryBean sfJson = new JaxWsServerFactoryBean();
055: sfJson.setServiceClass(BookService.class);
056: // Use the HTTP Binding which understands the Java Rest Annotations
057: sfJson.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
058: sfJson.setAddress("http://localhost:9080/json");
059: sfJson.getServiceFactory().setInvoker(
060: new BeanInvoker(serviceObj));
061:
062: // Turn the "wrapped" style off. This means that CXF won't generate
063: // wrapper JSON elements and we'll have prettier JSON text. This
064: // means that we need to stick to one request and one response
065: // parameter though.
066: sfJson.getServiceFactory().setWrapped(false);
067:
068: // Tell CXF to use a different Content-Type for the JSON endpoint
069: // This should probably be application/json, but text/plain allows
070: // us to view easily in a web browser.
071: Map<String, Object> properties = new HashMap<String, Object>();
072: properties.put("Content-Type", "text/plain");
073:
074: // Set up the JSON StAX implementation
075: Map<String, String> nstojns = new HashMap<String, String>();
076: nstojns.put("http://book.acme.com", "acme");
077:
078: MappedXMLInputFactory xif = new MappedXMLInputFactory(nstojns);
079: properties.put(XMLInputFactory.class.getName(), xif);
080:
081: MappedXMLOutputFactory xof = new MappedXMLOutputFactory(nstojns);
082: properties.put(XMLOutputFactory.class.getName(), xof);
083:
084: sfJson.setProperties(properties);
085:
086: sfJson.create();
087: }
088:
089: public static void main(String[] args) {
090: try {
091: BookServer s = new BookServer();
092: s.start();
093: } catch (Exception ex) {
094: ex.printStackTrace();
095: System.exit(-1);
096: } finally {
097: System.out.println("done!");
098: }
099: }
100: }
|