Example usage for java.io InputStream close

List of usage examples for java.io InputStream close

Introduction

In this page you can find the example usage for java.io InputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:de.gbv.ole.Marc21ToOleBulk.java

/**
 * Convertiert eine MARC21-Datei in eine MarcXML-Bulk-Import-SQL-Datei.
 * @param args      Pfad mit auf .mrc endendem Dateinamen der MARC21-Datei.
 * @throws IOException      Fehler beim Dateilesen oder -schreiben
 *//* w w w  . j av a  2s .  c om*/
public static void main(final String[] args) throws IOException {
    if (args.length < 1 || args.length > 2) {
        usageExit();
    }

    boolean ppn5 = false;
    if (args.length == 2) {
        if ("-5".equals(args[0])) {
            ppn5 = true;
        } else {
            usageExit();
        }
    }
    String infile = args[args.length - 1];

    if (!infile.endsWith(MRCSUFFIX)) {
        System.err.println("Filename ending in .mrc expected, " + "but found filename: " + infile);
        usageExit();
    }

    InputStream in = new FileInputStream(infile);
    MarcStreamReader reader = new MarcStreamReader(in);

    String filename = infile.substring(0, infile.length() - MRCSUFFIX.length());
    Writer bib = utf8Writer(filename + "-bib.sql");
    Writer holdings = utf8Writer(filename + "-holdings.sql");
    Writer item = utf8Writer(filename + "-item.sql");

    while (reader.hasNext()) {
        MarcWriter writer = new Marc21ToOleBulk(bib, holdings, item, ppn5);
        writer.write(reader.next());
        writer.close();
    }

    in.close();
    item.close();
    holdings.close();
    bib.close();
}

From source file:com.momab.dstool.DSTool.java

public static void main(String[] args) throws ParseException, IOException, ClassNotFoundException {

    final CommandLineParser parser = new BasicParser();
    final Options options = commandLineOptions();
    final CommandLine line = parser.parse(options, args);

    if (args.length == 0) {
        printUsage(options, System.out);
        System.exit(0);/*from  w  w w.  ja v  a2 s .  com*/
    }

    if (args.length == 1 && line.hasOption("h")) {
        printHelp(options, System.out);
        System.exit(0);
    }

    if (!line.hasOption("r") && !line.hasOption("w") && !line.hasOption("d")) {
        printErrorMessage("One of the options -r -w or -d must be specified.", System.err);
        System.exit(-1);
    }

    if (line.getArgs().length != 2) {
        printErrorMessage("To few arguments.", System.err);
        System.exit(-1);
    }

    DSOperation op = null;

    // Required operands/arguments
    DSOperationBuilder opBuilder = new DSOperationBuilder(line.getArgs()[0], line.getArgs()[1]);

    // Username and password
    if (line.hasOption("u") || line.hasOption("p")) {
        if (!line.hasOption("u") || !line.hasOption("p")) {
            printErrorMessage("Missing username or password", System.err);
            System.exit(-1);
        }

        opBuilder.asUser(line.getOptionValue("u"), line.getOptionValue("p"));
    }

    // Port
    if (line.hasOption("P")) {
        opBuilder = opBuilder.usingPort(Integer.valueOf(line.getOptionValue("P")));
    }

    // File
    File file = null;
    OutputStream out = null;
    InputStream in = null;
    if (line.hasOption("f")) {
        if (line.hasOption("d")) {
            printErrorMessage("Option -f is invalid for a -d delete operation", System.err);
            System.exit(-1);
        }

        file = new File(line.getOptionValue("f"));
    }

    // Read (download) operation
    if (line.hasOption("r")) {

        if (file != null) {
            out = new FileOutputStream(file);
        } else {
            out = System.out;
        }

        opBuilder = opBuilder.writeTo(out);

        op = opBuilder.buildDownload();
    }

    // Write (upload) operation
    if (line.hasOption("w")) {

        if (file != null) {
            in = new FileInputStream(file);
        } else {
            in = System.in;
        }

        opBuilder = opBuilder.readFrom(in);

        op = opBuilder.buildUpload();
    }

    // Delete (upload) operation
    if (line.hasOption("d")) {
        op = opBuilder.buildDelete();
    }

    if (file != null || line.hasOption("d")) {
        boolean result = op.Run(new ProgressPrinter() {
        });

        if (in != null)
            in.close();
        if (out != null)
            out.close();

        System.exit(result ? 0 : -1);

    } else {
        System.exit(op.Run(null) ? 0 : -1);
    }

}

From source file:name.livitski.databag.cli.Launcher.java

public static void main(String[] args) {
    Launcher tool = new Launcher();
    LogManager logging = LogManager.getLogManager();
    Class<?> beanClass = tool.getClass();
    InputStream cfg = beanClass.getResourceAsStream("/logging.properties");
    try {// w  w w. j av a 2  s . c o m
        logging.readConfiguration(cfg);
    } catch (Exception e) {
        System.err.println("WARNING: could not initialize logging. Detailed diagnostics may not be available. "
                + e.getMessage());
    } finally {
        try {
            cfg.close();
            cfg = null;
        } catch (Exception ignored) {
        }
    }
    try {
        tool.setOptions(SYNTAX.parseCommandLine(args));
    } catch (ParseException e) {
        tool.log().severe(e.getMessage());
        showHelpHint();
        System.exit(Status.SYNTAX.getCode());
    }
    if (null != tool.getOptions())
        tool.run();
    if (Status.OK != tool.getStatus())
        System.exit(tool.getStatus().getCode());
}

From source file:ImageInfo.java

/**
 * To use this class as a command line application, give it either
 * some file names as parameters (information on them will be
 * printed to standard output, one line per file) or call
 * it with no parameters. It will then check data given to it
 * via standard input.// w  w w  .jav  a2  s  .  c o m
 *
 * @param args the program arguments which must be file names
 */
public static void main(String[] args) {
    ImageInfo imageInfo = new ImageInfo();
    imageInfo.setDetermineImageNumber(true);
    boolean verbose = determineVerbosity(args);
    if (args.length == 0) {
        run(null, System.in, imageInfo, verbose);
    } else {
        int index = 0;
        while (index < args.length) {
            InputStream in = null;
            try {
                String name = args[index++];
                System.out.print(name + ";");
                if (name.startsWith("http://")) {
                    in = new URL(name).openConnection().getInputStream();
                } else {
                    in = new FileInputStream(name);
                }
                run(name, in, imageInfo, verbose);
                in.close();
            } catch (Exception e) {
                System.out.println(e);
                try {
                    in.close();
                } catch (Exception ee) {
                }
            }
        }
    }
}

From source file:azkaban.webapp.AzkabanWebServer.java

/**
 * Azkaban using Jetty/*  www.  j  a  va  2s.c  o  m*/
 *
 * @param args
 */
public static void main(String[] args) throws Exception {
    logger.info("Starting Jetty Azkaban Web Server...");
    Props azkabanSettings = AzkabanServer.loadProps(args);

    if (azkabanSettings == null) {
        logger.error("Azkaban Properties not loaded.");
        logger.error("Exiting Azkaban...");
        return;
    }

    int maxThreads = azkabanSettings.getInt("jetty.maxThreads", DEFAULT_THREAD_NUMBER);
    boolean isStatsOn = azkabanSettings.getBoolean("jetty.connector.stats", true);
    logger.info("Setting up connector with stats on: " + isStatsOn);

    boolean ssl;
    int port;
    final Server server = new Server();
    if (azkabanSettings.getBoolean("jetty.use.ssl", true)) {
        int sslPortNumber = azkabanSettings.getInt("jetty.ssl.port", DEFAULT_SSL_PORT_NUMBER);
        port = sslPortNumber;
        ssl = true;
        logger.info(
                "Setting up Jetty Https Server with port:" + sslPortNumber + " and numThreads:" + maxThreads);

        SslSocketConnector secureConnector = new SslSocketConnector();
        secureConnector.setPort(sslPortNumber);
        secureConnector.setKeystore(azkabanSettings.getString("jetty.keystore"));
        secureConnector.setPassword(azkabanSettings.getString("jetty.password"));
        secureConnector.setKeyPassword(azkabanSettings.getString("jetty.keypassword"));
        secureConnector.setTruststore(azkabanSettings.getString("jetty.truststore"));
        secureConnector.setTrustPassword(azkabanSettings.getString("jetty.trustpassword"));
        secureConnector.setHeaderBufferSize(MAX_HEADER_BUFFER_SIZE);

        // set up vulnerable cipher suites to exclude
        List<String> cipherSuitesToExclude = azkabanSettings.getStringList("jetty.excludeCipherSuites");
        logger.info("Excluded Cipher Suites: " + String.valueOf(cipherSuitesToExclude));
        if (cipherSuitesToExclude != null && !cipherSuitesToExclude.isEmpty()) {
            secureConnector.setExcludeCipherSuites(cipherSuitesToExclude.toArray(new String[0]));
        }

        server.addConnector(secureConnector);
    } else {
        ssl = false;
        port = azkabanSettings.getInt("jetty.port", DEFAULT_PORT_NUMBER);
        SocketConnector connector = new SocketConnector();
        connector.setPort(port);
        connector.setHeaderBufferSize(MAX_HEADER_BUFFER_SIZE);
        server.addConnector(connector);
    }

    // setting stats configuration for connectors
    for (Connector connector : server.getConnectors()) {
        connector.setStatsOn(isStatsOn);
    }

    String hostname = azkabanSettings.getString("jetty.hostname", "localhost");
    azkabanSettings.put("server.hostname", hostname);
    azkabanSettings.put("server.port", port);
    azkabanSettings.put("server.useSSL", String.valueOf(ssl));

    app = new AzkabanWebServer(server, azkabanSettings);

    boolean checkDB = azkabanSettings.getBoolean(AzkabanDatabaseSetup.DATABASE_CHECK_VERSION, false);
    if (checkDB) {
        AzkabanDatabaseSetup setup = new AzkabanDatabaseSetup(azkabanSettings);
        setup.loadTableInfo();
        if (setup.needsUpdating()) {
            logger.error("Database is out of date.");
            setup.printUpgradePlan();

            logger.error("Exiting with error.");
            System.exit(-1);
        }
    }

    try {
        JdbcExecutorLoader jdbcExecutorLoader = new JdbcExecutorLoader(azkabanSettings);
        jdbcExecutorLoader.updateExecutableJobsOnStartUp();
    } catch (Exception e) {
        logger.warn("There could be some jobs KILLED by this server restart event,"
                + "but their status still erroneously being shown as RUNNING." + "Please run the SQL "
                + "[update execution_jobs join execution_flows"
                + " on execution_jobs.exec_id = execution_flows.exec_id"
                + " set execution_jobs.status = 70 where"
                + " execution_jobs.status = 30 and execution_flows.status = 70]"
                + "on the database to rectify their status", e);
    }

    QueuedThreadPool httpThreadPool = new QueuedThreadPool(maxThreads);
    server.setThreadPool(httpThreadPool);

    String staticDir = azkabanSettings.getString("web.resource.dir", DEFAULT_STATIC_DIR);
    logger.info("Setting up web resource dir " + staticDir);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.setMaxFormContentSize(MAX_FORM_CONTENT_SIZE);

    String defaultServletPath = azkabanSettings.getString("azkaban.default.servlet.path", "/index");
    root.setResourceBase(staticDir);
    ServletHolder indexRedirect = new ServletHolder(new IndexRedirectServlet(defaultServletPath));
    root.addServlet(indexRedirect, "/");
    ServletHolder index = new ServletHolder(new ProjectServlet());
    root.addServlet(index, "/index");

    ServletHolder staticServlet = new ServletHolder(new DefaultServlet());
    root.addServlet(staticServlet, "/css/*");
    root.addServlet(staticServlet, "/js/*");
    root.addServlet(staticServlet, "/images/*");
    root.addServlet(staticServlet, "/fonts/*");
    root.addServlet(staticServlet, "/favicon.ico");

    root.addServlet(new ServletHolder(new ProjectManagerServlet()), "/manager");
    root.addServlet(new ServletHolder(new ExecutorServlet()), "/executor");
    root.addServlet(new ServletHolder(new HistoryServlet()), "/history");
    root.addServlet(new ServletHolder(new ScheduleServlet()), "/schedule");
    root.addServlet(new ServletHolder(new JMXHttpServlet()), "/jmx");
    root.addServlet(new ServletHolder(new TriggerManagerServlet()), "/triggers");
    root.addServlet(new ServletHolder(new StatsServlet()), "/stats");
    root.addServlet(new ServletHolder(new AboutServlet()), "/about");
    root.addServlet(new ServletHolder(new FileEditorServlet()), "/fileeditor");

    ServletHolder restliHolder = new ServletHolder(new RestliServlet());
    restliHolder.setInitParameter("resourcePackages", "azkaban.restli");
    root.addServlet(restliHolder, "/restli/*");

    String viewerPluginDir = azkabanSettings.getString("viewer.plugin.dir", "plugins/viewer");
    loadViewerPlugins(root, viewerPluginDir, app.getVelocityEngine());

    // triggerplugin
    String triggerPluginDir = azkabanSettings.getString("trigger.plugin.dir", "plugins/triggers");
    Map<String, TriggerPlugin> triggerPlugins = loadTriggerPlugins(root, triggerPluginDir, app);
    app.setTriggerPlugins(triggerPlugins);
    // always have basic time trigger
    // TODO: find something else to do the job
    app.getTriggerManager().start();

    root.setAttribute(ServerConstants.AZKABAN_SERVLET_CONTEXT_KEY, app);
    try {
        server.start();
    } catch (Exception e) {
        logger.warn(e);
        Utils.croak(e.getMessage(), 1);
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            try {
                logTopMemoryConsumers();
            } catch (Exception e) {
                logger.info(("Exception when logging top memory consumers"), e);
            }

            logger.info("Shutting down http server...");
            try {
                app.close();
                server.stop();
                server.destroy();
            } catch (Exception e) {
                logger.error("Error while shutting down http server.", e);
            }
            logger.info("kk thx bye.");
        }

        public void logTopMemoryConsumers() throws Exception, IOException {
            if (new File("/bin/bash").exists() && new File("/bin/ps").exists()
                    && new File("/usr/bin/head").exists()) {
                logger.info("logging top memeory consumer");

                java.lang.ProcessBuilder processBuilder = new java.lang.ProcessBuilder("/bin/bash", "-c",
                        "/bin/ps aux --sort -rss | /usr/bin/head");
                Process p = processBuilder.start();
                p.waitFor();

                InputStream is = p.getInputStream();
                java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    logger.info(line);
                }
                is.close();
            }
        }
    });
    logger.info("Server running on " + (ssl ? "ssl" : "") + " port " + port + ".");
}

From source file:CourserankConnector.java

public static void main(String[] args) throws Exception {
    ///////////////////////////////////////
    //Tagger init

    //MaxentTagger tagger = new MaxentTagger("models/english-left3words-distsim.tagger");
    /////from   w w w .j  a va  2  s .  c  o m
    //CLIENT INITIALIZATION
    ImportData importCourse = new ImportData();
    HttpClient httpclient = new DefaultHttpClient();
    httpclient = WebClientDevWrapper.wrapClient(httpclient);
    try {
        /*
         httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(null, -1),
            new UsernamePasswordCredentials("eadrian", "eactresp1"));
        */
        //////////////////////////////////////////////////
        //Get Course Bulletin Departments page
        List<Course> courses = new ArrayList<Course>();

        HttpGet httpget = new HttpGet("http://explorecourses.stanford.edu");

        System.out.println("executing request" + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        String bulletinpage = "";

        //STORE RETURNED HTML TO BULLETINPAGE

        if (entity != null) {
            //System.out.println("Response content length: " + entity.getContentLength());
            InputStream i = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                bulletinpage += line;
                //System.out.println(line);
            }
            br.close();
            i.close();
        }
        EntityUtils.consume(entity);

        ///////////////////////////////////////////////////////////////////////////////
        //Login to Courserank

        httpget = new HttpGet("https://courserank.com/stanford/main");

        System.out.println("executing request" + httpget.getRequestLine());
        response = httpclient.execute(httpget);
        entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        String page = "";
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            InputStream i = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                page += line;
                //System.out.println(line);
            }
            br.close();
            i.close();
        }
        EntityUtils.consume(entity);
        ////////////////////////////////////////////////////
        //POST REQUEST LOGIN

        HttpPost post = new HttpPost("https://www.courserank.com/stanford/main");

        List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);

        pairs.add(new BasicNameValuePair("RT", ""));
        pairs.add(new BasicNameValuePair("action", "login"));
        pairs.add(new BasicNameValuePair("password", "trespass"));
        pairs.add(new BasicNameValuePair("username", "eaconte@stanford.edu"));
        post.setEntity(new UrlEncodedFormEntity(pairs));
        System.out.println("executing request" + post.getRequestLine());
        HttpResponse resp = httpclient.execute(post);
        HttpEntity ent = resp.getEntity();

        System.out.println("----------------------------------------");
        if (ent != null) {
            System.out.println("Response content length: " + ent.getContentLength());
            InputStream i = ent.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                //System.out.println(line);
            }
            br.close();
            i.close();
        }
        EntityUtils.consume(ent);
        ///////////////////////////////////////////////////
        //THIS STEP MAY NOT BE NEEDED BUT GETS MAIN PROFILE PAGE

        HttpGet gethome = new HttpGet("https://www.courserank.com/stanford/home");

        System.out.println("executing request" + gethome.getRequestLine());
        HttpResponse gresp = httpclient.execute(gethome);
        HttpEntity gent = gresp.getEntity();

        System.out.println("----------------------------------------");
        if (ent != null) {
            System.out.println("Response content length: " + gent.getContentLength());
            InputStream i = gent.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                //System.out.println(line);
            }
            br.close();
            i.close();
        }

        /////////////////////////////////////////////////////////////////////////////////
        //Parse Bulletin

        String results = getToken(bulletinpage, "RESULTS HEADER", "Additional Searches");
        String[] depts = results.split("href");

        //SPLIT FOR EACH DEPARTMENT LINK, ITERATE
        boolean ready = false;
        for (int i = 1; i < depts.length; i++) {
            //EXTRACT LINK, DEPARTMENT NAME AND ABBREVIATION
            String dept = new String(depts[i]);
            String abbr = getToken(dept, "(", ")");
            String name = getToken(dept, ">", "(");
            name.trim();
            //System.out.println(tagger.tagString(name));
            String link = getToken(dept, "=\"", "\">");
            System.out.println(name + " : " + abbr + " : " + link);

            System.out.println("======================================================================");

            if (i <= 10 || i >= 127) //values to keep it to undergraduate courses. Excludes law, med, business, overseas
                continue;
            /*if (i<=46)
               continue; */ //Start at BIOHOP
            /*if (abbr.equals("INTNLREL"))
               ready = true;
            if (!ready)
               continue;*/
            //Construct department course search URL
            //Then request page
            String URL = "http://explorecourses.stanford.edu/" + link
                    + "&filter-term-Autumn=on&filter-term-Winter=on&filter-term-Spring=on";
            httpget = new HttpGet(URL);

            //System.out.println("executing request" + httpget.getRequestLine());
            response = httpclient.execute(httpget);
            entity = response.getEntity();

            //ystem.out.println("----------------------------------------");
            //System.out.println(response.getStatusLine());
            String rpage = "";
            if (entity != null) {
                //System.out.println("Response content length: " + entity.getContentLength());
                InputStream in = entity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String line;
                while ((line = br.readLine()) != null) {
                    rpage += line;
                    //System.out.println(line);
                }
                br.close();
                in.close();
            }
            EntityUtils.consume(entity);

            //Process results page
            List<Course> deptCourses = new ArrayList<Course>();
            List<Course> result = processResultPage(rpage);
            deptCourses.addAll(result);

            //While there are more result pages, keep going
            boolean more = (!(result.size() == 0) && (result.get((result.size() - 1)).courseNumber < 299));
            boolean morepages = anotherPage(rpage);
            while (morepages && more) {
                URL = nextURL(URL);
                httpget = new HttpGet(URL);

                //System.out.println("executing request" + httpget.getRequestLine());
                response = httpclient.execute(httpget);
                entity = response.getEntity();

                //System.out.println("----------------------------------------");
                //System.out.println(response.getStatusLine());
                rpage = "";
                if (entity != null) {
                    //System.out.println("Response content length: " + entity.getContentLength());
                    InputStream in = entity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String line;
                    while ((line = br.readLine()) != null) {
                        rpage += line;
                        //System.out.println(line);
                    }
                    br.close();
                    in.close();
                }
                EntityUtils.consume(entity);
                morepages = anotherPage(rpage);
                result = processResultPage(rpage);
                deptCourses.addAll(result);
                more = (!(result.size() == 0) && (result.get((result.size() - 1)).courseNumber < 299));
                /*String mores = more? "yes": "no";
                String pagess = morepages?"yes":"no";
                System.out.println("more: "+mores+" morepages: "+pagess);
                System.out.println("more");*/
            }

            //Get course ratings for all department courses via courserank
            deptCourses = getRatings(httpclient, abbr, deptCourses);
            for (int j = 0; j < deptCourses.size(); j++) {
                Course c = deptCourses.get(j);
                System.out.println("" + c.title + " : " + c.rating);
                c.tags = name;
                c.code = c.code.trim();
                c.department = name;
                c.deptAB = abbr;
                c.writeToDatabase();
                //System.out.println(tagger.tagString(c.title));
            }

        }

        if (!page.equals(""))
            return;

        ///////////////////////////////////////////////////
        //Get Course Bulletin Department courses 

        /*
                
         httpget = new HttpGet("https://courserank.com/stanford/main");
                
         System.out.println("executing request" + httpget.getRequestLine());
         response = httpclient.execute(httpget);
         entity = response.getEntity();
                
         System.out.println("----------------------------------------");
         System.out.println(response.getStatusLine());
         page = "";
         if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
        InputStream i = entity.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           page +=line;
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(entity);
         ////////////////////////////////////////////////////
         //POST REQUEST LOGIN
                 
                 
         HttpPost post = new HttpPost("https://www.courserank.com/stanford/main");
                 
         List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);
                 
                 
         pairs.add(new BasicNameValuePair("RT", ""));
         pairs.add(new BasicNameValuePair("action", "login"));
         pairs.add(new BasicNameValuePair("password", "trespass"));
         pairs.add(new BasicNameValuePair("username", "eaconte@stanford.edu"));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         System.out.println("executing request" + post.getRequestLine());
         HttpResponse resp = httpclient.execute(post);
         HttpEntity ent = resp.getEntity();
                 
         System.out.println("----------------------------------------");
         if (ent != null) {
        System.out.println("Response content length: " + ent.getContentLength());
        InputStream i = ent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(ent);
         ///////////////////////////////////////////////////
         //THIS STEP MAY NOT BE NEEDED BUT GETS MAIN PROFILE PAGE
                 
         HttpGet gethome = new HttpGet("https://www.courserank.com/stanford/home");
                 
                 
         System.out.println("executing request" + gethome.getRequestLine());
         HttpResponse gresp = httpclient.execute(gethome);
         HttpEntity gent = gresp.getEntity();
                 
         System.out.println("----------------------------------------");
         if (ent != null) {
        System.out.println("Response content length: " + gent.getContentLength());
        InputStream i = gent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
                 
                 
         ////////////////////////////////////////
         //GETS FIRST PAGE OF RESULTS
         EntityUtils.consume(gent);
                 
         post = new HttpPost("https://www.courserank.com/stanford/search_results");
                 
         pairs = new ArrayList<NameValuePair>(2);
                 
                 
         pairs.add(new BasicNameValuePair("filter_term_currentYear", "on"));
         pairs.add(new BasicNameValuePair("query", ""));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         System.out.println("executing request" + post.getRequestLine());
         resp = httpclient.execute(post);
         ent = resp.getEntity();
                 
         System.out.println("----------------------------------------");
                 
         String rpage = "";
         if (ent != null) {
        System.out.println("Response content length: " + ent.getContentLength());
        InputStream i = ent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           rpage += line;
           System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(ent);
                 
         ////////////////////////////////////////////////////
         //PARSE FIRST PAGE OF RESULTS
                 
         //int index = rpage.indexOf("div class=\"searchItem");
         String []classSplit = rpage.split("div class=\"searchItem");
         for (int i=1; i<classSplit.length; i++) {
            String str = classSplit[i];
                    
            //ID
            String CID = getToken(str, "course?id=","\">");
                    
            // CODE 
            String CODE = getToken(str,"class=\"code\">" ,":</");
                    
            //TITLE 
            String NAME = getToken(str, "class=\"title\">","</");
                    
            //DESCRIP
            String DES = getToken(str, "class=\"description\">","</");
                    
            //TERM
            String TERM = getToken(str, "Terms:", "|");
                    
            //UNITS
            String UNITS = getToken(str, "Units:", "<br/>");
                
            //WORKLOAD
                    
            String WLOAD = getToken(str, "Workload:", "|");
                    
            //GER
            String GER = getToken(str, "GERs:", "</d");
                    
            //RATING
            int searchIndex = 0;
            float rating = 0;
            while (true) {
          int ratingIndex = str.indexOf("large_Full", searchIndex);
          if (ratingIndex ==-1) {
             int halfratingIndex = str.indexOf("large_Half", searchIndex);
             if (halfratingIndex == -1)
                break;
             else
                rating += .5;
             break;
          }
          searchIndex = ratingIndex+1;
          rating++;
                     
            }
            String RATING = ""+rating;
                    
            //GRADE
            String GRADE = getToken(str, "div class=\"unofficialGrade\">", "</");
            if (GRADE.equals("NOT FOUND")) {
          GRADE = getToken(str, "div class=\"officialGrade\">", "</");
            }
                    
            //REVIEWS
            String REVIEWS = getToken(str, "class=\"ratings\">", " ratings");
                    
                    
            System.out.println(""+CODE+" : "+NAME + " : "+CID);
            System.out.println("----------------------------------------");
            System.out.println("Term: "+TERM+" Units: "+UNITS+ " Workload: "+WLOAD + " Grade: "+ GRADE);
            System.out.println("Rating: "+RATING+ " Reviews: "+REVIEWS);
            System.out.println("==========================================");
            System.out.println(DES);
            System.out.println("==========================================");
                    
                    
                    
         }
                 
                 
         ///////////////////////////////////////////////////
         //GETS SECOND PAGE OF RESULTS
         post = new HttpPost("https://www.courserank.com/stanford/search_results");
                 
         pairs = new ArrayList<NameValuePair>(2);
                 
                 
         pairs.add(new BasicNameValuePair("filter_term_currentYear", "on"));
         pairs.add(new BasicNameValuePair("page", "2"));
         pairs.add(new BasicNameValuePair("query", ""));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         System.out.println("executing request" + post.getRequestLine());
         resp = httpclient.execute(post);
         ent = resp.getEntity();
                 
         System.out.println("----------------------------------------");
         if (ent != null) {
        System.out.println("Response content length: " + ent.getContentLength());
        InputStream i = ent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(ent);
                 
         /*
         httpget = new HttpGet("https://github.com/");
                
         System.out.println("executing request" + httpget.getRequestLine());
         response = httpclient.execute(httpget);
         entity = response.getEntity();
                
         System.out.println("----------------------------------------");
         System.out.println(response.getStatusLine());
         page = "";
         if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
        InputStream i = entity.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           page +=line;
           System.out.println(line);
        }
        br.close();
        i.close();
         }*/
        EntityUtils.consume(entity);

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.heliosapm.benchmarks.json.JSONUnmarshalling.java

/**
 * @param args/*  w  ww  . ja va 2 s .c o m*/
 */
public static void main(String[] args) {
    final Map<String, ChannelBuffer> bufferMap = DIRECT_DATA_BUFFERS;
    log("JSON Test");
    InputStream is = null;
    for (String sample : DATA) {
        try {
            final String jsonText = bufferMap.get(sample).duplicate().toString(UTF8);
            Person[] p = parseToObject(jsonText, Person[].class);
            log("Parsed STRING [%s] to objects: %s", sample, p.length);
            p = parseToObject(bufferMap.get(sample).duplicate(), Person[].class);
            log("Parsed BUFFER [%s] to objects: %s", sample, p.length);
            String s = serializeToString(p);
            log("Serialized to STRING [%s], size: %s", sample, s.length());
            ChannelBuffer c = serializeToBuffer(heapFactory, p);
            log("Serialized to Heap Buffer [%s], size: %s", sample, c.readableBytes());
            c = serializeToBuffer(directFactory, p);
            log("Serialized to Direct Buffer [%s], size: %s", sample, c.readableBytes());
        } catch (Exception ex) {
            throw new RuntimeException("Failed to process string sample [" + sample + "]", ex);
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (Exception x) {
                    /* No Op */}
        }
    }
}

From source file:test1.ApacheHttpRestClient2.java

public final static void main(String[] args) {

    HttpClient httpClient = new DefaultHttpClient();
    try {/*from w w  w.j  a  va 2  s .  c  om*/
        // this ona api call returns results in a JSON format
        HttpGet httpGetRequest = new HttpGet("https://api.ona.io/api/v1/users");

        // Execute HTTP request
        HttpResponse httpResponse = httpClient.execute(httpGetRequest);

        System.out.println("------------------HTTP RESPONSE----------------------");
        System.out.println(httpResponse.getStatusLine());
        System.out.println("------------------HTTP RESPONSE----------------------");

        // Get hold of the response entity
        HttpEntity entity = httpResponse.getEntity();

        // If the response does not enclose an entity, there is no need
        // to bother about connection release
        byte[] buffer = new byte[1024];
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
                int bytesRead = 0;
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                while ((bytesRead = bis.read(buffer)) != -1) {
                    String chunk = new String(buffer, 0, bytesRead);
                    FileWriter file = new FileWriter("C:\\Users\\fred\\Desktop\\webicons\\output.txt");
                    //file.write(chunk.toJSONString());
                    file.write(chunk.toCharArray());
                    file.flush();
                    file.close();

                    System.out.print(chunk);
                    System.out.println(chunk);
                }
            } catch (IOException ioException) {
                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                ioException.printStackTrace();
            } catch (RuntimeException runtimeException) {
                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection immediately.
                httpGetRequest.abort();
                runtimeException.printStackTrace();
            }
            //          try {
            //              FileWriter file = new FileWriter("C:\\Users\\fred\\Desktop\\webicons\\output.json");
            //                file.write(bis.toJSONString());
            //                file.flush();
            //                file.close();
            //
            //                System.out.print(bis);
            //          } catch (Exception e) {
            //          }

            finally {
                // Closing the input stream will trigger connection release
                try {
                    inputStream.close();
                } catch (Exception ignore) {
                }
            }
        }
    } catch (ClientProtocolException e) {
        // thrown by httpClient.execute(httpGetRequest)
        e.printStackTrace();
    } catch (IOException e) {
        // thrown by entity.getContent();
        e.printStackTrace();
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.lxf.spider.client.QuickStart.java

public static void main(String[] args) throws Exception {
    InputStream input = null;
    OutputStream output = null;//  w w w .ja v a2s. c om
    //httpclient
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        //get
        HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/pdqx.jc.web/cen/center.html");
        //
        CloseableHttpResponse response1 = httpclient.execute(httpGet);

        // The underlying HTTP connection is still held by the response object
        // to allow the response content to be streamed directly from the network socket.
        // In order to ensure correct deallocation of system resources
        // the user MUST call CloseableHttpResponse#close() from a finally clause.
        // Please note that if response content is not fully consumed the underlying
        // connection cannot be safely re-used and will be shut down and discarded
        // by the connection manager.
        try {
            //???
            int statusCode = response1.getStatusLine().getStatusCode();
            //??200?
            if (statusCode == HttpStatus.SC_OK) {
                //                  input = httpGet.getRequestLine();
                System.out.println(response1.getStatusLine());
                //
                HttpEntity entity1 = response1.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                input = entity1.getContent();
                output = new FileOutputStream("d:/lxf.data");
                //?
                int tempByte = -1;
                while ((tempByte = input.read()) > 0) {
                    output.write(tempByte);
                }
                EntityUtils.consume(entity1);
                //?
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            }

        } finally {
            response1.close();
        }

        /*HttpPost httpPost = new HttpPost("http://targethost/login");
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "vip"));
        nvps.add(new BasicNameValuePair("password", "secret"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response2 = httpclient.execute(httpPost);
                
        try {
        System.out.println(response2.getStatusLine());
        HttpEntity entity2 = response2.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity2);
        } finally {
        response2.close();
        }*/
    } finally {
        httpclient.close();
    }
}

From source file:com.vmware.photon.controller.core.Main.java

public static void main(String[] args) throws Throwable {
    try {/*from www  .  j a  v a 2 s.  c om*/
        LoggingFactory.bootstrap();

        logger.info("args: " + Arrays.toString(args));

        ArgumentParser parser = ArgumentParsers.newArgumentParser("PhotonControllerCore").defaultHelp(true)
                .description("Photon Controller Core");
        parser.addArgument("config-file").help("photon controller configuration file");
        parser.addArgument("--manual").type(Boolean.class).setDefault(false)
                .help("If true, create default deployment.");

        Namespace namespace = parser.parseArgsOrFail(args);

        PhotonControllerConfig photonControllerConfig = getPhotonControllerConfig(namespace);
        DeployerConfig deployerConfig = photonControllerConfig.getDeployerConfig();

        new LoggingFactory(photonControllerConfig.getLogging(), "photon-controller-core").configure();

        SSLContext sslContext;
        if (deployerConfig.getDeployerContext().isAuthEnabled()) {
            sslContext = SSLContext.getInstance(KeyStoreUtils.THRIFT_PROTOCOL);
            TrustManagerFactory tmf = null;

            tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            KeyStore keyStore = KeyStore.getInstance("JKS");
            InputStream in = FileUtils
                    .openInputStream(new File(deployerConfig.getDeployerContext().getKeyStorePath()));
            keyStore.load(in, deployerConfig.getDeployerContext().getKeyStorePassword().toCharArray());
            tmf.init(keyStore);
            sslContext.init(null, tmf.getTrustManagers(), null);
        } else {
            KeyStoreUtils.generateKeys("/thrift/");
            sslContext = KeyStoreUtils.acceptAllCerts(KeyStoreUtils.THRIFT_PROTOCOL);
        }

        ThriftModule thriftModule = new ThriftModule(sslContext);
        PhotonControllerXenonHost xenonHost = startXenonHost(photonControllerConfig, thriftModule,
                deployerConfig, sslContext);

        if ((Boolean) namespace.get("manual")) {
            DefaultDeployment.createDefaultDeployment(photonControllerConfig.getXenonConfig().getPeerNodes(),
                    deployerConfig, xenonHost);
        }

        // Creating a temp configuration file for apife with modification to some named sections in photon-controller-config
        // so that it can match the Configuration class of dropwizard.
        File apiFeTempConfig = File.createTempFile("apiFeTempConfig", ".tmp");
        File source = new File(args[0]);
        FileInputStream fis = new FileInputStream(source);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        FileWriter fstream = new FileWriter(apiFeTempConfig, true);
        BufferedWriter out = new BufferedWriter(fstream);

        String aLine = null;
        while ((aLine = in.readLine()) != null) {
            if (aLine.equals("apife:")) {
                aLine = aLine.replace("apife:", "server:");
            }
            out.write(aLine);
            out.newLine();
        }
        in.close();
        out.close();

        // This approach can be simplified once the apife container is gone, but for the time being
        // it expects the first arg to be the string "server".
        String[] apiFeArgs = new String[2];
        apiFeArgs[0] = "server";
        apiFeArgs[1] = apiFeTempConfig.getAbsolutePath();
        ApiFeService.setupApiFeConfigurationForServerCommand(apiFeArgs);
        ApiFeService.addServiceHost(xenonHost);
        ApiFeService.setSSLContext(sslContext);

        ApiFeService apiFeService = new ApiFeService();
        apiFeService.run(apiFeArgs);
        apiFeTempConfig.deleteOnExit();

        LocalApiClient localApiClient = apiFeService.getInjector().getInstance(LocalApiClient.class);
        xenonHost.setApiClient(localApiClient);

        // in the non-auth enabled scenario we need to be able to accept any self-signed certificate
        if (!deployerConfig.getDeployerContext().isAuthEnabled()) {
            KeyStoreUtils.acceptAllCerts(KeyStoreUtils.THRIFT_PROTOCOL);
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                logger.info("Shutting down");
                xenonHost.stop();
                logger.info("Done");
                LoggingFactory.detachAndStop();
            }
        });
    } catch (Exception e) {
        logger.error("Failed to start photon controller ", e);
        throw e;
    }
}