001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.integration.mint;
019
020
021import org.glassfish.grizzly.PortRange;
022import org.glassfish.grizzly.http.server.HttpHandler;
023import org.glassfish.grizzly.http.server.HttpServer;
024import org.glassfish.grizzly.http.server.NetworkListener;
025import org.glassfish.grizzly.http.server.Request;
026import org.glassfish.grizzly.http.server.Response;
027import org.slf4j.Logger;
028import org.springframework.context.ApplicationContext;
029import org.springframework.context.ApplicationContextAware;
030
031import javax.annotation.PostConstruct;
032import javax.annotation.PreDestroy;
033
034import static org.slf4j.LoggerFactory.getLogger;
035
036/**
037 * <p>ContainerWrapper class.</p>
038 *
039 * @author osmandin
040 * @author ajs6f
041 */
042public class ContainerWrapper implements ApplicationContextAware {
043
044    private static final Logger logger = getLogger(ContainerWrapper.class);
045
046    private int port;
047
048    public void setPort(final int port) {
049        this.port = port;
050    }
051
052    private HttpServer server;
053
054    @PostConstruct
055    public void start() {
056        server = new HttpServer();
057        final NetworkListener listener = new NetworkListener("grizzly", "localhost", new PortRange(port));
058        server.addListener(listener);
059
060        try {
061            server.start();
062            logger.info("Test server running on {}", port);
063        } catch (final Throwable e) {
064            logger.error("Error with test server", e);
065        }
066    }
067
068    public void addHandler(final String data, final String path) {
069        final HttpHandler httpHandler = new HttpHandler() {
070
071            @Override
072            public void service(final Request request, final Response response)
073                    throws Exception {
074                response.getWriter().write(data);
075            }
076        };
077
078        server.getServerConfiguration().addHttpHandler(httpHandler, "/" + path);
079    }
080
081    @PreDestroy
082    public void stop() {
083        server.shutdownNow();
084    }
085
086    @Override
087    public void setApplicationContext(final ApplicationContext applicationContext) {
088    }
089
090}