001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.remoting.caucho;
018:
019: import java.io.IOException;
020: import java.lang.reflect.Constructor;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import com.caucho.burlap.io.BurlapInput;
027: import com.caucho.burlap.io.BurlapOutput;
028: import com.caucho.burlap.server.BurlapSkeleton;
029:
030: import org.springframework.beans.factory.BeanInitializationException;
031: import org.springframework.beans.factory.InitializingBean;
032: import org.springframework.remoting.support.RemoteExporter;
033: import org.springframework.util.Assert;
034: import org.springframework.web.HttpRequestHandler;
035: import org.springframework.web.HttpRequestMethodNotSupportedException;
036: import org.springframework.web.util.NestedServletException;
037:
038: /**
039: * HTTP request handler that exports the specified service bean as
040: * Burlap service endpoint, accessible via a Burlap proxy.
041: *
042: * <p>Burlap is a slim, XML-based RPC protocol.
043: * For information on Burlap, see the
044: * <a href="http://www.caucho.com/burlap">Burlap website</a>
045: *
046: * <p>This exporter will work with both Burlap 2.x and 3.x (respectively
047: * Resin 2.x and 3.x), autodetecting the corresponding skeleton class.
048: *
049: * <p>Note: Burlap services exported with this class can be accessed by
050: * any Burlap client, as there isn't any special handling involved.
051: *
052: * @author Juergen Hoeller
053: * @since 13.05.2003
054: * @see BurlapClientInterceptor
055: * @see BurlapProxyFactoryBean
056: * @see org.springframework.remoting.caucho.HessianServiceExporter
057: * @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
058: * @see org.springframework.remoting.rmi.RmiServiceExporter
059: */
060: public class BurlapServiceExporter extends RemoteExporter implements
061: HttpRequestHandler, InitializingBean {
062:
063: private BurlapSkeleton skeleton;
064:
065: public void afterPropertiesSet() {
066: prepare();
067: }
068:
069: /**
070: * Initialize this service exporter.
071: */
072: public void prepare() {
073: try {
074: try {
075: // Try Burlap 3.x (with service interface argument).
076: Constructor ctor = BurlapSkeleton.class
077: .getConstructor(new Class[] { Object.class,
078: Class.class });
079: checkService();
080: checkServiceInterface();
081: this .skeleton = (BurlapSkeleton) ctor
082: .newInstance(new Object[] {
083: getProxyForService(),
084: getServiceInterface() });
085: } catch (NoSuchMethodException ex) {
086: // Fall back to Burlap 2.x (without service interface argument).
087: Constructor ctor = BurlapSkeleton.class
088: .getConstructor(new Class[] { Object.class });
089: this .skeleton = (BurlapSkeleton) ctor
090: .newInstance(new Object[] { getProxyForService() });
091: }
092: } catch (Exception ex) {
093: throw new BeanInitializationException(
094: "Burlap skeleton initialization failed", ex);
095: }
096: }
097:
098: /**
099: * Processes the incoming Burlap request and creates a Burlap response.
100: */
101: public void handleRequest(HttpServletRequest request,
102: HttpServletResponse response) throws ServletException,
103: IOException {
104:
105: Assert.notNull(this .skeleton,
106: "BurlapServiceExporter has not been initialized");
107:
108: if (!"POST".equals(request.getMethod())) {
109: throw new HttpRequestMethodNotSupportedException("POST",
110: "BurlapServiceExporter only supports POST requests");
111: }
112:
113: BurlapInput in = new BurlapInput(request.getInputStream());
114: BurlapOutput out = new BurlapOutput(response.getOutputStream());
115: try {
116: this .skeleton.invoke(in, out);
117: } catch (Throwable ex) {
118: throw new NestedServletException(
119: "Burlap skeleton invocation failed", ex);
120: }
121: }
122:
123: }
|