001/*
002 * Copyright 2002-2014 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 *      https://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
017package org.springframework.remoting.caucho;
018
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021
022import com.sun.net.httpserver.HttpExchange;
023import com.sun.net.httpserver.HttpHandler;
024
025import org.springframework.lang.UsesSunHttpServer;
026import org.springframework.util.FileCopyUtils;
027
028/**
029 * HTTP request handler that exports the specified service bean as
030 * Hessian service endpoint, accessible via a Hessian proxy.
031 * Designed for Sun's JRE 1.6 HTTP server, implementing the
032 * {@link com.sun.net.httpserver.HttpHandler} interface.
033 *
034 * <p>Hessian is a slim, binary RPC protocol.
035 * For information on Hessian, see the
036 * <a href="http://hessian.caucho.com">Hessian website</a>.
037 * <b>Note: As of Spring 4.0, this exporter requires Hessian 4.0 or above.</b>
038 *
039 * <p>Hessian services exported with this class can be accessed by
040 * any Hessian client, as there isn't any special handling involved.
041 *
042 * @author Juergen Hoeller
043 * @since 2.5.1
044 * @see org.springframework.remoting.caucho.HessianClientInterceptor
045 * @see org.springframework.remoting.caucho.HessianProxyFactoryBean
046 * @see org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter
047 */
048@UsesSunHttpServer
049public class SimpleHessianServiceExporter extends HessianExporter implements HttpHandler {
050
051        /**
052         * Processes the incoming Hessian request and creates a Hessian response.
053         */
054        @Override
055        public void handle(HttpExchange exchange) throws IOException {
056                if (!"POST".equals(exchange.getRequestMethod())) {
057                        exchange.getResponseHeaders().set("Allow", "POST");
058                        exchange.sendResponseHeaders(405, -1);
059                        return;
060                }
061
062                ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
063                try {
064                        invoke(exchange.getRequestBody(), output);
065                }
066                catch (Throwable ex) {
067                        exchange.sendResponseHeaders(500, -1);
068                        logger.error("Hessian skeleton invocation failed", ex);
069                        return;
070                }
071
072                exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_HESSIAN);
073                exchange.sendResponseHeaders(200, output.size());
074                FileCopyUtils.copy(output.toByteArray(), exchange.getResponseBody());
075        }
076
077}