com.trendmicro.hdfs.webdav.tool.Get.java Source code

Java tutorial

Introduction

Here is the source code for com.trendmicro.hdfs.webdav.tool.Get.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

package com.trendmicro.hdfs.webdav.tool;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.hadoop.security.authentication.client.AuthenticatedURL;

public class Get {

    private static void printUsageAndExit(Options options, int exitCode) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Usage: Get [options] url", "", options, "", true);
        System.exit(exitCode);
    }

    public static void main(String[] args) throws Exception {
        // Process command line 

        Options options = new Options();
        options.addOption("d", "debug", false, "Enable debug logging");

        CommandLine cmd = null;
        try {
            cmd = new PosixParser().parse(options, args);
        } catch (ParseException e) {
            printUsageAndExit(options, -1);
        }
        boolean debug = cmd.hasOption('d');
        args = cmd.getArgs();
        if (args.length < 1) {
            printUsageAndExit(options, -1);
        }

        // Do the fetch

        AuthenticatedURL.Token token = new AuthenticatedURL.Token();
        AuthenticatedURL url = new AuthenticatedURL();
        HttpURLConnection conn = url.openConnection(new URL(args[0]), token);
        if (debug) {
            System.out.println("Token value: " + token);
            System.out.println("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage());
        }
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            reader.close();
        }
        System.out.println();
    }

}