com.image32.demo.simpleapi.SimpleApiDemo.java Source code

Java tutorial

Introduction

Here is the source code for com.image32.demo.simpleapi.SimpleApiDemo.java

Source

/*
 * The MIT License (MIT)
    
Copyright (c) 2014 Interconnect Medical, Inc.
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
    
 */

package com.image32.demo.simpleapi;

import java.awt.Desktop;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.URI;
import java.util.Properties;

import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.HandlerList;
import org.mortbay.jetty.servlet.HashSessionIdManager;
import org.mortbay.jetty.servlet.HashSessionManager;
import org.mortbay.jetty.servlet.SessionHandler;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.resource.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleApiDemo {

    private ContentHandler contentHandlers;
    private static Server server;
    private static int port = 8888;
    static Logger logger = LoggerFactory.getLogger(SimpleApiDemo.class.getName());

    SchemeRegistry schemeRegistry = null;
    PoolingClientConnectionManager cm = null;

    static SimpleApiDemo me = null;

    public static String image32ApiClientId;
    public static String image32ApiSecrect;
    public static String image32ApiAuthUrl;
    public static String image32ApiGetStudiesUrl;
    public static String demoDataFile;

    SimpleApiDemo() {
    }

    final public static void main(String[] args) {
        me = new SimpleApiDemo();
        me.contentHandlers = new ContentHandler();

        //load configurations
        try {
            Properties prop = new Properties();
            InputStream input = SimpleApiDemo.class.getResourceAsStream("/demoConfig.properties");

            // load a properties file
            prop.load(input);
            image32ApiClientId = prop.getProperty("image32ApiClientId");
            image32ApiSecrect = prop.getProperty("image32ApiSecrect");
            image32ApiAuthUrl = prop.getProperty("image32ApiAuthUrl");
            image32ApiGetStudiesUrl = prop.getProperty("image32ApiGetStudiesUrl");
            demoDataFile = prop.getProperty("demoDataFile");
            input.close();
        } catch (Exception ex) {
            logger.info("No configuration found.");
            System.exit(1);
        }

        // test port availability
        while (!checkPortAvailablity(port)) {
            if (port > 8090) {
                logger.info("Port is not available. Exiting...");
                System.exit(1);
            }
            port++;
        }
        ;

        // run server
        server = new Server(port);
        HashSessionIdManager hashSessionIdManager = new HashSessionIdManager();
        server.setSessionIdManager(hashSessionIdManager);

        WebAppContext homecontext = new WebAppContext();
        homecontext.setContextPath("/home");
        ResourceCollection resources = new ResourceCollection(new String[] { "site" });
        homecontext.setBaseResource(resources);

        HashSessionManager manager = new HashSessionManager();
        manager.setSecureCookies(true);
        SessionHandler sessionHandler = new SessionHandler(manager);

        sessionHandler.setHandler(me.contentHandlers);

        ContextHandler context = new ContextHandler();
        context.setContextPath("/app");
        context.setResourceBase(".");
        context.setClassLoader(Thread.currentThread().getContextClassLoader());
        context.setHandler(sessionHandler);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { homecontext, context });

        server.setHandler(handlers);

        try {
            server.start();
            logger.info("Server started on port " + port);
            logger.info("Please access this demo from browser with this url: http://localhost:" + port);

            if (Desktop.isDesktopSupported())
                Desktop.getDesktop().browse(new URI("http://localhost:" + port + "/home"));

            server.join();
        } catch (Exception exception) {
            logger.error(exception.getMessage());
        }
        System.exit(1);
    }

    public static boolean checkPortAvailablity(int port) {

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
        } finally {
            if (ds != null) {
                ds.close();
            }
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    return false;
                }
            }
        }
        return false;
    }

}