001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.server.cxf;
018:
019: import org.apache.cxf.Bus;
020: import org.apache.cxf.BusFactory;
021: import org.apache.cxf.bus.extension.ExtensionManagerBus;
022: import org.apache.cxf.service.model.EndpointInfo;
023: import org.apache.cxf.transport.DestinationFactoryManager;
024: import org.apache.openejb.core.webservices.PortData;
025: import org.apache.openejb.server.httpd.HttpRequest;
026: import org.apache.openejb.server.httpd.HttpResponse;
027: import org.apache.openejb.server.httpd.HttpListener;
028: import org.apache.openejb.server.webservices.saaj.SaajUniverse;
029:
030: import java.io.OutputStream;
031: import java.io.PrintWriter;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: public abstract class CxfWsContainer implements HttpListener {
036: protected final Bus bus;
037: protected final PortData port;
038: protected HttpDestination destination;
039: protected CxfEndpoint endpoint;
040:
041: public CxfWsContainer(Bus bus, PortData port) {
042: this .bus = bus;
043: this .port = port;
044:
045: List<String> ids = new ArrayList<String>();
046: ids.add("http://schemas.xmlsoap.org/wsdl/soap/");
047:
048: DestinationFactoryManager factoryManager = bus
049: .getExtension(DestinationFactoryManager.class);
050: HttpTransportFactory factory = new HttpTransportFactory(bus);
051: factory.setTransportIds(ids);
052:
053: factoryManager.registerDestinationFactory(
054: "http://cxf.apache.org/transports/http/configuration",
055: factory);
056: factoryManager.registerDestinationFactory(
057: "http://cxf.apache.org/bindings/xformat", factory);
058: factoryManager.registerDestinationFactory(
059: "http://www.w3.org/2003/05/soap/bindings/HTTP/",
060: factory);
061: factoryManager.registerDestinationFactory(
062: "http://schemas.xmlsoap.org/soap/http", factory);
063: factoryManager.registerDestinationFactory(
064: "http://schemas.xmlsoap.org/wsdl/http/", factory);
065: factoryManager.registerDestinationFactory(
066: "http://schemas.xmlsoap.org/wsdl/soap/http", factory);
067: }
068:
069: public void start() {
070: endpoint = createEndpoint();
071: endpoint.publish("http://nopath");
072: destination = (HttpDestination) endpoint.getServer()
073: .getDestination();
074: }
075:
076: protected abstract CxfEndpoint createEndpoint();
077:
078: public void destroy() {
079: if (endpoint != null) {
080: endpoint.stop();
081: endpoint = null;
082: }
083: // if (destination != null) {
084: // destination.shutdown();
085: // destination = null;
086: // }
087: }
088:
089: public void onMessage(HttpRequest request, HttpResponse response)
090: throws Exception {
091: if (request.getMethod() == HttpRequest.Method.GET) {
092: processGET(request, response);
093: } else {
094: processPOST(request, response);
095: }
096: }
097:
098: protected void processGET(HttpRequest request, HttpResponse response)
099: throws Exception {
100: boolean wsdlRequest = request.getParameter("wsdl") != null
101: || request.getParameter("WSDL") != null
102: || request.getParameter("xsd") != null
103: || request.getParameter("XSD") != null;
104:
105: if (wsdlRequest) {
106: getWsdl(request, response);
107: } else if (endpoint.isSOAP11()) {
108: EndpointInfo ei = this .destination.getEndpointInfo();
109: response.setContentType("text/html");
110: PrintWriter pw = new PrintWriter(response.getOutputStream());
111: pw.write("<html><title>Web Service</title><body>");
112: pw.write("Hi, this is '"
113: + ei.getService().getName().getLocalPart()
114: + "' web service.");
115: pw.write("</body></html>");
116: pw.flush();
117: } else {
118: processPOST(request, response);
119: }
120: }
121:
122: protected void processPOST(HttpRequest request,
123: HttpResponse response) throws Exception {
124: SaajUniverse universe = new SaajUniverse();
125: universe.set(SaajUniverse.DEFAULT);
126: try {
127: destination.invoke(request, response);
128: } finally {
129: universe.unset();
130: }
131: }
132:
133: private void getWsdl(HttpRequest request, HttpResponse response)
134: throws Exception {
135: WsdlQueryHandler queryHandler = new WsdlQueryHandler(this .bus);
136: String requestUri = request.getURI().toString();
137: EndpointInfo ei = this .destination.getEndpointInfo();
138: OutputStream out = response.getOutputStream();
139: response.setContentType("text/xml");
140: queryHandler.writeResponse(requestUri, null, ei, out);
141: }
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: }
|