001/**
002 * Copyright 2015 DuraSpace, Inc.
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 */
016package org.fcrepo.http.commons.test.util;
017
018import static org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer;
019import static org.slf4j.LoggerFactory.getLogger;
020
021import java.net.URI;
022import java.util.Collection;
023
024import javax.annotation.PostConstruct;
025import javax.annotation.PreDestroy;
026import javax.xml.bind.JAXBContext;
027import javax.xml.bind.Unmarshaller;
028
029import org.fcrepo.http.commons.webxml.WebAppConfig;
030import org.fcrepo.http.commons.webxml.bind.ContextParam;
031import org.fcrepo.http.commons.webxml.bind.Filter;
032import org.fcrepo.http.commons.webxml.bind.FilterMapping;
033import org.fcrepo.http.commons.webxml.bind.InitParam;
034import org.fcrepo.http.commons.webxml.bind.Listener;
035import org.fcrepo.http.commons.webxml.bind.Servlet;
036import org.fcrepo.http.commons.webxml.bind.ServletMapping;
037import org.glassfish.grizzly.http.server.HttpServer;
038import org.glassfish.grizzly.servlet.FilterRegistration;
039import org.glassfish.grizzly.servlet.ServletRegistration;
040import org.glassfish.grizzly.servlet.WebappContext;
041import org.slf4j.Logger;
042import org.springframework.beans.BeansException;
043import org.springframework.context.ApplicationContext;
044import org.springframework.context.ApplicationContextAware;
045
046/**
047 * <p>ContainerWrapper class.</p>
048 *
049 * @author awoods
050 */
051public class ContainerWrapper implements ApplicationContextAware {
052
053    private static final Logger logger = getLogger(ContainerWrapper.class);
054
055    private int port;
056
057    private HttpServer server;
058
059    private WebappContext appContext;
060
061    private String configLocation;
062
063    public void setConfigLocation(final String configLocation) {
064        this.configLocation = configLocation.replaceFirst("^classpath:", "/");
065    }
066
067    public void setPort(final int port) {
068        this.port = port;
069    }
070
071    @PostConstruct
072    public void start() throws Exception {
073
074        final JAXBContext context = JAXBContext.newInstance(WebAppConfig.class);
075        final Unmarshaller u = context.createUnmarshaller();
076        final WebAppConfig o =
077                (WebAppConfig) u.unmarshal(getClass().getResource(
078                        this.configLocation));
079
080        final URI uri = URI.create("http://localhost:" + port);
081
082        server = createHttpServer(uri);
083
084        // create a "root" web application
085        appContext = new WebappContext(o.displayName(), "/");
086
087        for (final ContextParam p : o.contextParams()) {
088            appContext.addContextInitParameter(p.name(), p.value());
089        }
090
091        for (final Listener l : o.listeners()) {
092            appContext.addListener(l.className());
093        }
094
095        for (final Servlet s : o.servlets()) {
096            final ServletRegistration servlet =
097                    appContext.addServlet(s.servletName(), s.servletClass());
098
099            final Collection<ServletMapping> mappings =
100                    o.servletMappings(s.servletName());
101            for (final ServletMapping sm : mappings) {
102                servlet.addMapping(sm.urlPattern());
103            }
104            for (final InitParam p : s.initParams()) {
105                servlet.setInitParameter(p.name(), p.value());
106            }
107        }
108
109        for (final Filter f : o.filters()) {
110            final FilterRegistration filter =
111                    appContext.addFilter(f.filterName(), f.filterClass());
112
113            final Collection<FilterMapping> mappings =
114                    o.filterMappings(f.filterName());
115            for (final FilterMapping sm : mappings) {
116                final String urlPattern = sm.urlPattern();
117                final String servletName = sm.servletName();
118                if (urlPattern != null) {
119                    filter.addMappingForUrlPatterns(null, urlPattern);
120                } else {
121                    filter.addMappingForServletNames(null, servletName);
122                }
123
124            }
125            for (final InitParam p : f.initParams()) {
126                filter.setInitParameter(p.name(), p.value());
127            }
128        }
129
130        appContext.deploy(server);
131
132        logger.debug("started grizzly webserver endpoint at " +
133                server.getHttpHandler().getName());
134    }
135
136    @PreDestroy
137    public void stop() {
138        try {
139            appContext.undeploy();
140        } catch (final Exception e) {
141            logger.warn(e.getMessage(), e);
142        } finally {
143            server.shutdownNow();
144        }
145    }
146
147    @Override
148    public void setApplicationContext(final ApplicationContext applicationContext)
149            throws BeansException {
150        // this.applicationContext = applicationContext;
151
152    }
153
154}