001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.cxf;
017:
018: import java.io.OutputStream;
019: import java.io.PrintWriter;
020: import java.net.URI;
021: import java.net.URL;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.cxf.Bus;
028: import org.apache.cxf.BusFactory;
029: import org.apache.cxf.bus.extension.ExtensionManagerBus;
030: import org.apache.cxf.service.model.EndpointInfo;
031: import org.apache.cxf.transport.DestinationFactoryManager;
032: import org.apache.geronimo.webservices.WebServiceContainer;
033: import org.apache.geronimo.webservices.saaj.SAAJUniverse;
034:
035: public abstract class CXFWebServiceContainer implements
036: WebServiceContainer {
037:
038: private static final Log LOG = LogFactory
039: .getLog(CXFWebServiceContainer.class);
040:
041: protected final GeronimoDestination destination;
042:
043: protected final Bus bus;
044:
045: protected final CXFEndpoint endpoint;
046:
047: protected URL configurationBaseUrl;
048:
049: public CXFWebServiceContainer(Bus bus, URL configurationBaseUrl,
050: Object target) {
051: this .bus = bus;
052: this .configurationBaseUrl = configurationBaseUrl;
053:
054: List ids = new ArrayList();
055: ids.add("http://schemas.xmlsoap.org/wsdl/soap/");
056:
057: DestinationFactoryManager destinationFactoryManager = bus
058: .getExtension(DestinationFactoryManager.class);
059: GeronimoDestinationFactory factory = new GeronimoDestinationFactory(
060: bus);
061: factory.setTransportIds(ids);
062:
063: destinationFactoryManager.registerDestinationFactory(
064: "http://cxf.apache.org/transports/http/configuration",
065: factory);
066: destinationFactoryManager.registerDestinationFactory(
067: "http://cxf.apache.org/bindings/xformat", factory);
068: destinationFactoryManager.registerDestinationFactory(
069: "http://www.w3.org/2003/05/soap/bindings/HTTP/",
070: factory);
071: destinationFactoryManager.registerDestinationFactory(
072: "http://schemas.xmlsoap.org/soap/http", factory);
073: destinationFactoryManager.registerDestinationFactory(
074: "http://schemas.xmlsoap.org/wsdl/http/", factory);
075: destinationFactoryManager.registerDestinationFactory(
076: "http://schemas.xmlsoap.org/wsdl/soap/http", factory);
077:
078: endpoint = publishEndpoint(target);
079: destination = (GeronimoDestination) endpoint.getServer()
080: .getDestination();
081: }
082:
083: public void invoke(Request request, Response response)
084: throws Exception {
085: if (request.getMethod() == Request.GET) {
086: processGET(request, response);
087: } else {
088: processPOST(request, response);
089: }
090: }
091:
092: protected void processGET(Request request, Response response)
093: throws Exception {
094: if (request.getParameter("xsd") != null
095: || request.getParameter("XSD") != null) {
096: getWsdl(request, response);
097: } else if (endpoint.isSOAP11()) {
098: EndpointInfo ei = this .destination.getEndpointInfo();
099: response.setContentType("text/html");
100: PrintWriter pw = new PrintWriter(response.getOutputStream());
101: pw.write("<html><title>Web Service</title><body>");
102: pw.write("Hi, this is '"
103: + ei.getService().getName().getLocalPart()
104: + "' web service.");
105: pw.write("</body></html>");
106: pw.flush();
107: } else {
108: processPOST(request, response);
109: }
110: }
111:
112: protected void processPOST(Request request, Response response)
113: throws Exception {
114: SAAJUniverse universe = new SAAJUniverse();
115: universe.set(SAAJUniverse.DEFAULT);
116: try {
117: destination.invoke(request, response);
118: } finally {
119: universe.unset();
120: }
121: }
122:
123: public void getWsdl(Request request, Response response)
124: throws Exception {
125: GeronimoQueryHandler queryHandler = new GeronimoQueryHandler(
126: this .bus);
127: URI requestURI = request.getURI();
128: EndpointInfo ei = this .destination.getEndpointInfo();
129: OutputStream out = response.getOutputStream();
130: String baseUri = requestURI.toString();
131: response.setContentType("text/xml");
132: queryHandler.writeResponse(baseUri, null, ei, out);
133: }
134:
135: public void destroy() {
136: if (this .endpoint != null) {
137: this .endpoint.stop();
138: }
139: }
140:
141: abstract protected CXFEndpoint publishEndpoint(Object target);
142:
143: /*
144: * Ensure the bus created is unqiue and non-shared.
145: * The very first bus created is set as a default bus which then can
146: * be (re)used in other places.
147: */
148: public static Bus getBus() {
149: getDefaultBus();
150: return new ExtensionManagerBus();
151: }
152:
153: /*
154: * Ensure the Spring bus is initialized with the CXF module classloader
155: * instead of the application classloader.
156: */
157: public static Bus getDefaultBus() {
158: ClassLoader cl = Thread.currentThread().getContextClassLoader();
159: Thread.currentThread().setContextClassLoader(
160: CXFEndpoint.class.getClassLoader());
161: try {
162: return BusFactory.getDefaultBus();
163: } finally {
164: Thread.currentThread().setContextClassLoader(cl);
165: }
166: }
167:
168: }
|