001/*
002 * Copyright 2002-2018 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.util.FileCopyUtils;
026
027/**
028 * HTTP request handler that exports the specified service bean as
029 * Hessian service endpoint, accessible via a Hessian proxy.
030 * Designed for Sun's JRE 1.6 HTTP server, implementing the
031 * {@link com.sun.net.httpserver.HttpHandler} interface.
032 *
033 * <p>Hessian is a slim, binary RPC protocol.
034 * For information on Hessian, see the
035 * <a href="http://hessian.caucho.com">Hessian website</a>.
036 * <b>Note: As of Spring 4.0, this exporter requires Hessian 4.0 or above.</b>
037 *
038 * <p>Hessian services exported with this class can be accessed by
039 * any Hessian client, as there isn't any special handling involved.
040 *
041 * @author Juergen Hoeller
042 * @since 2.5.1
043 * @see org.springframework.remoting.caucho.HessianClientInterceptor
044 * @see org.springframework.remoting.caucho.HessianProxyFactoryBean
045 * @deprecated as of Spring Framework 5.1, in favor of {@link HessianServiceExporter}
046 */
047@Deprecated
048@org.springframework.lang.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}