Home » geronimo-2.2-source-release » org.apache.geronimo.cxf » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.cxf;
   18   
   19   import java.io.OutputStream;
   20   import java.io.PrintWriter;
   21   import java.net.URI;
   22   import java.net.URL;
   23   import java.util.ArrayList;
   24   import java.util.List;
   25   
   26   import org.slf4j.Logger;
   27   import org.slf4j.LoggerFactory;
   28   import org.apache.cxf.Bus;
   29   import org.apache.cxf.BusFactory;
   30   import org.apache.cxf.bus.extension.ExtensionManagerBus;
   31   import org.apache.cxf.service.model.EndpointInfo;
   32   import org.apache.cxf.tools.common.extensions.soap.SoapAddress;
   33   import org.apache.cxf.transport.DestinationFactoryManager;
   34   import org.apache.geronimo.webservices.WebServiceContainer;
   35   import org.apache.geronimo.webservices.saaj.SAAJUniverse;
   36   
   37   public abstract class CXFWebServiceContainer implements WebServiceContainer {
   38   
   39       private static final Logger LOG = LoggerFactory.getLogger(CXFWebServiceContainer.class);
   40       
   41       protected final GeronimoDestination destination;
   42   
   43       protected final Bus bus;
   44   
   45       protected final CXFEndpoint endpoint;
   46   
   47       protected URL configurationBaseUrl;
   48   
   49       public CXFWebServiceContainer(Bus bus,
   50                                     URL configurationBaseUrl,
   51                                     Object target) {
   52           this.bus = bus;
   53           this.configurationBaseUrl = configurationBaseUrl;
   54               
   55           List ids = new ArrayList();
   56           ids.add("http://schemas.xmlsoap.org/wsdl/soap/");
   57                  
   58           DestinationFactoryManager destinationFactoryManager = bus
   59                   .getExtension(DestinationFactoryManager.class);
   60           GeronimoDestinationFactory factory = new GeronimoDestinationFactory(bus);
   61           factory.setTransportIds(ids);
   62                   
   63           destinationFactoryManager.registerDestinationFactory(
   64                   "http://cxf.apache.org/transports/http/configuration", factory);
   65           destinationFactoryManager.registerDestinationFactory(
   66                   "http://cxf.apache.org/bindings/xformat", factory);
   67           destinationFactoryManager.registerDestinationFactory(
   68                   "http://www.w3.org/2003/05/soap/bindings/HTTP/", factory);
   69           destinationFactoryManager.registerDestinationFactory(
   70                   "http://schemas.xmlsoap.org/soap/http", factory);
   71           destinationFactoryManager.registerDestinationFactory(
   72                   "http://schemas.xmlsoap.org/wsdl/http/", factory);
   73           destinationFactoryManager.registerDestinationFactory(
   74                   "http://schemas.xmlsoap.org/wsdl/soap/http", factory);
   75   
   76           endpoint = publishEndpoint(target);
   77           destination = (GeronimoDestination) endpoint.getServer().getDestination();
   78       }
   79   
   80       static String getBaseUri(URI request) {
   81           return request.getScheme() + "://" + request.getHost() + ":" + request.getPort() + request.getPath();
   82       }
   83       
   84       public void invoke(Request request, Response response) throws Exception {
   85           this.endpoint.updateAddress(request.getURI());
   86           if (request.getMethod() == Request.GET) {
   87               processGET(request, response);
   88           } else {
   89               processPOST(request, response);
   90           }
   91       }
   92       
   93       protected void processGET(Request request, Response response) throws Exception {
   94           if (request.getParameter("wsdl") != null || request.getParameter("WSDL") != null ||
   95                      request.getParameter("xsd") != null || request.getParameter("XSD") != null) {
   96               GeronimoQueryHandler queryHandler = new GeronimoQueryHandler(this.bus);
   97               URI requestURI = request.getURI();
   98               EndpointInfo ei = this.destination.getEndpointInfo();
   99               // update service port location on each request
  100               SoapAddress address = ei.getExtensor(SoapAddress.class);
  101               address.setLocationURI(getBaseUri(requestURI));
  102               OutputStream out = response.getOutputStream();
  103               String baseUri = requestURI.toString();
  104               response.setContentType("text/xml");
  105               queryHandler.writeResponse(baseUri, null, ei, out);
  106           } else if (endpoint.isSOAP11()) {
  107               EndpointInfo ei = this.destination.getEndpointInfo();
  108               response.setContentType("text/html");
  109               PrintWriter pw = new PrintWriter(response.getOutputStream());
  110               pw.write("<html><title>Web Service</title><body>");
  111               pw.write("Hi, this is '" + ei.getService().getName().getLocalPart() + "' web service.");
  112               pw.write("</body></html>");
  113               pw.flush();
  114           } else {
  115               processPOST(request, response);
  116           }
  117       }
  118       
  119       protected void processPOST(Request request, Response response) throws Exception {
  120           SAAJUniverse universe = new SAAJUniverse();
  121           universe.set(SAAJUniverse.DEFAULT);
  122           try {
  123               destination.invoke(request, response);
  124           } finally {
  125               universe.unset();
  126           }
  127       }
  128      
  129       public void getWsdl(Request request, Response response) throws Exception {
  130           invoke(request, response);
  131       }    
  132           
  133       public void destroy() {
  134           if (this.endpoint != null) {
  135               this.endpoint.stop();
  136           }
  137       }
  138   
  139       abstract protected CXFEndpoint publishEndpoint(Object target);
  140           
  141       /*
  142        * Ensure the bus created is unqiue and non-shared. 
  143        * The very first bus created is set as a default bus which then can
  144        * be (re)used in other places.
  145        */
  146       public static Bus getBus() {        
  147           getDefaultBus();
  148           return new ExtensionManagerBus();
  149       }
  150       
  151       /*
  152        * Ensure the Spring bus is initialized with the CXF module classloader
  153        * instead of the application classloader. 
  154        */
  155       public static Bus getDefaultBus() {
  156           ClassLoader cl = Thread.currentThread().getContextClassLoader();
  157           Thread.currentThread().setContextClassLoader(CXFEndpoint.class.getClassLoader());
  158           try {
  159               return BusFactory.getDefaultBus();
  160           } finally {
  161               Thread.currentThread().setContextClassLoader(cl);
  162           }
  163       }
  164       
  165   }

Home » geronimo-2.2-source-release » org.apache.geronimo.cxf » [javadoc | source]