1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/chat/server/src/main/java/org/apidesign/demo/chat/server/Main.java Fri Apr 22 05:56:47 2016 +0200
1.3 @@ -0,0 +1,79 @@
1.4 +/**
1.5 + * The MIT License (MIT)
1.6 + *
1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 + *
1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 + * of this software and associated documentation files (the "Software"), to deal
1.11 + * in the Software without restriction, including without limitation the rights
1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 + * copies of the Software, and to permit persons to whom the Software is
1.14 + * furnished to do so, subject to the following conditions:
1.15 + *
1.16 + * The above copyright notice and this permission notice shall be included in
1.17 + * all copies or substantial portions of the Software.
1.18 + *
1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 + * THE SOFTWARE.
1.26 + */
1.27 +package org.apidesign.demo.chat.server;
1.28 +
1.29 +import java.io.IOException;
1.30 +import java.net.Inet4Address;
1.31 +import java.net.InterfaceAddress;
1.32 +import java.net.NetworkInterface;
1.33 +import java.net.URI;
1.34 +import java.util.Enumeration;
1.35 +import javax.ws.rs.container.ContainerRequestContext;
1.36 +import javax.ws.rs.container.ContainerResponseContext;
1.37 +import javax.ws.rs.container.ContainerResponseFilter;
1.38 +import org.glassfish.grizzly.http.server.HttpServer;
1.39 +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
1.40 +import org.glassfish.jersey.server.ResourceConfig;
1.41 +
1.42 +/** Starts REST server based on Jersey.
1.43 + */
1.44 +final class Main implements ContainerResponseFilter {
1.45 + public static void main(String... args) throws Exception {
1.46 + ResourceConfig rc = new ResourceConfig(
1.47 + ChatServerResource.class, Main.class
1.48 + );
1.49 + URI u = new URI("http://0.0.0.0:8080/");
1.50 + HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);
1.51 + System.err.println("Server running on following IP addresses:");
1.52 + dumpIPs();
1.53 + System.err.println("Press Enter to shutdown the server");
1.54 + System.in.read();
1.55 + server.stop();
1.56 + }
1.57 +
1.58 + @Override
1.59 + public void filter(
1.60 + ContainerRequestContext requestContext,
1.61 + ContainerResponseContext r
1.62 + ) throws IOException {
1.63 + r.getHeaders().add("Access-Control-Allow-Origin", "*");
1.64 + r.getHeaders().add("Access-Control-Allow-Credentials", "true");
1.65 + r.getHeaders().add("Access-Control-Allow-Headers", "Content-Type");
1.66 + r.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
1.67 + }
1.68 +
1.69 + private static void dumpIPs() throws Exception {
1.70 + Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
1.71 + while (en.hasMoreElements()) {
1.72 + NetworkInterface n = en.nextElement();
1.73 + if (n.isUp()) {
1.74 + for (InterfaceAddress i : n.getInterfaceAddresses()) {
1.75 + if (i.getAddress() instanceof Inet4Address) {
1.76 + System.err.println(n.getName() + ": " + i.getAddress());
1.77 + }
1.78 + }
1.79 + }
1.80 + }
1.81 + }
1.82 +}