org.echocat.nodoodle.server.WebService.java Source code

Java tutorial

Introduction

Here is the source code for org.echocat.nodoodle.server.WebService.java

Source

/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****
 *
 * Version: MPL 2.0
 *
 * echocat NoDoodle, Copyright (c) 2010-2012 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

package org.echocat.nodoodle.server;

import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

import static org.apache.commons.collections.IteratorUtils.*;

public class WebService implements HttpService, ApplicationContextAware {

    private final CXFServlet _servlet = new CXFServlet();

    private ApplicationContext _applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        _applicationContext = applicationContext;
    }

    @Override
    public void init(ServletContext servletContext) throws ServletException {
        if (servletContext == null) {
            throw new NullPointerException("No servletContext provided.");
        }
        if (_applicationContext == null) {
            throw new IllegalStateException("The applicationContext is not set.");
        }
        final ServletConfigImpl servletConfig = new ServletConfigImpl(servletContext);
        _servlet.init(servletConfig);
    }

    @Override
    public void destroy() {
        _servlet.destroy();
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        _servlet.service(request, response);
    }

    private static class ServletConfigImpl implements ServletConfig {

        private final ServletContext _servletContext;

        private ServletConfigImpl(ServletContext servletContext) {
            _servletContext = servletContext;
        }

        @Override
        public String getServletName() {
            return "webService";
        }

        @Override
        public ServletContext getServletContext() {
            return _servletContext;
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<?> getInitParameterNames() {
            return asEnumeration(emptyIterator());
        }
    }
}