http.ServerExample.java Source code

Java tutorial

Introduction

Here is the source code for http.ServerExample.java

Source

package http;

/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.commons.io.IOUtils;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.mongodb.connection.Connection;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.*;
import io.vertx.ext.web.sstore.LocalSessionStore;

import java.util.Map;

public class ServerExample extends AbstractVerticle {
    public static String proc = null;
    public static ArrayList<String> list = new ArrayList<String>();

    public static void main(String args[]) throws JSchException, InterruptedException {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(ServerExample.class.getName());

    }

    public static ArrayList<String> getProcess(String user, String password, String host, String cmd)
            throws IOException, JSchException, InterruptedException {
        System.out.println("From getProcess " + user + " " + password + " " + host + " ");
        ArrayList<String> processList = new ArrayList<String>();

        List<String> myList = new ArrayList<String>();

        JSch jsch = new JSch();
        String command = cmd; //"sh /opt/emms/emsam/bin/show_emsam_status.sh\n"; 

        Session session;
        session = jsch.getSession(user, host, 26);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        //      session.connect(30 * 1000);
        try {
            session.connect(0);
        } catch (Exception e) {
            System.out.println("Exception in connection ");
            e.printStackTrace();

            list = processList;
            return processList;
        }
        //      session.connect(0);
        Channel channel = session.openChannel("shell");
        ByteArrayInputStream is = new ByteArrayInputStream(command.getBytes());
        channel.setInputStream(is);

        InputStream inStream = channel.getInputStream();
        BufferedReader fromChannel = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
        OutputStream outStream = channel.getOutputStream();

        String result = new String();
        String[] entries = new String[40];

        try {

            channel.connect(15 * 1000);
            Thread.sleep(3 * 1000);

            byte[] tmp = new byte[2048];
            while (true) {
                while (inStream.available() > 0) {
                    int i = inStream.read(tmp, 0, 2048);
                    if (i < 0)
                        break;
                    result = new String(tmp, 0, i);

                    myList = (Arrays.asList(result.split("\n")));
                    //                                      myList = myList.subList(5, myList.size()-1);

                    for (int j = 1; j < myList.size(); j++) {

                        if (myList.get(j).toString().contains("S")) {
                            processList.add(myList.get(j));
                        }

                    }

                }

                if (inStream.available() == 0) {
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            channel.disconnect();
            session.disconnect();
            list = processList;
            return processList;

        }

    }

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        HttpServer server = vertx.createHttpServer();
        Router router = Router.router(vertx);

        router.route().handler(StaticHandler.create("webroot"));

        server.requestHandler(router::accept).listen(9095);
        System.out.println("Thread Router Start: " + Thread.currentThread().getId());
        System.out.println("STARTED ROUTER");
        startFuture.complete();

        //      router.post("/services/reset").handler(new Reset());
        router.post("/services/getstatus").handler(new GetStatus());
        router.post("/service/getexpectedprocess").handler(new GetExpectedProcess());
        router.post("/service/getdiff").handler(new GetDiff());
        router.post("/service/GetMemInfo").handler(new GetMemInfo());
        router.post("/service/GetCPUInfo").handler(new GetCPUInfo());
    }

}