|
| 1 | +/* |
| 2 | + * Copyright 2011 Splunk, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 5 | + * not use this file except in compliance with the License. You may obtain |
| 6 | + * a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import com.splunk.http.*; |
| 18 | + |
| 19 | +import org.apache.commons.cli.CommandLine; |
| 20 | +import org.apache.commons.cli.CommandLineParser; |
| 21 | +import org.apache.commons.cli.HelpFormatter; |
| 22 | +import org.apache.commons.cli.Options; |
| 23 | +import org.apache.commons.cli.ParseException; |
| 24 | +import org.apache.commons.cli.PosixParser; |
| 25 | + |
| 26 | +import java.io.BufferedReader; |
| 27 | +import java.io.InputStreamReader; |
| 28 | +import java.util.Properties; |
| 29 | + |
| 30 | +public class Program { |
| 31 | + public static void main(String[] args) { |
| 32 | + Options options = new Options(); |
| 33 | + options.addOption("h", "help", false, "Display this help message"); |
| 34 | + options.addOption(null, "host", true, "Host name (default localhost)"); |
| 35 | + options.addOption(null, "port", true, "Port number (default 8089)"); |
| 36 | + options.addOption(null, "scheme", true, "Scheme (default https)"); |
| 37 | + options.addOption(null, "username", true, "Username to login with"); |
| 38 | + options.addOption(null, "password", true, "Password to login with"); |
| 39 | + options.addOption(null, "namespace", true, null); |
| 40 | + |
| 41 | + CommandLineParser parser = new PosixParser(); |
| 42 | + |
| 43 | + CommandLine cmdline = null; |
| 44 | + try { |
| 45 | + cmdline = parser.parse(options, args); |
| 46 | + } |
| 47 | + catch (ParseException e) { |
| 48 | + System.err.println(e.getMessage()); |
| 49 | + System.exit(1); |
| 50 | + } |
| 51 | + |
| 52 | + if (cmdline.hasOption('h')) { |
| 53 | + help("spurl [options] {path}", options); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + run(cmdline); |
| 59 | + } |
| 60 | + catch (Exception e) { |
| 61 | + e.printStackTrace(); |
| 62 | + System.exit(1); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static void run(CommandLine cmdline) throws Exception { |
| 67 | + String host = cmdline.getOptionValue("host", "localhost"); |
| 68 | + String port = cmdline.getOptionValue("port", "8089"); |
| 69 | + String scheme = cmdline.getOptionValue("scheme", "https"); |
| 70 | + String username = cmdline.getOptionValue("username"); |
| 71 | + String password = cmdline.getOptionValue("password"); |
| 72 | + |
| 73 | + Service service = new Service(host, Integer.parseInt(port), scheme); |
| 74 | + |
| 75 | + String[] args = cmdline.getArgs(); |
| 76 | + String path = args.length > 0 ? args[0] : "/"; |
| 77 | + ResponseMessage response = |
| 78 | + service.send(new RequestMessage("GET", path)); |
| 79 | + |
| 80 | + int status = response.getStatus(); |
| 81 | + System.out.println(String.format("=> %d", status)); |
| 82 | + if (status != 200) return; |
| 83 | + BufferedReader reader = new BufferedReader( |
| 84 | + new InputStreamReader(response.getContent())); |
| 85 | + while (true) { |
| 86 | + String line = reader.readLine(); |
| 87 | + if (line == null) break; |
| 88 | + System.out.println(line); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + static void help(String app, Options options) { |
| 93 | + HelpFormatter formatter = new HelpFormatter(); |
| 94 | + formatter.printHelp(app, options); |
| 95 | + } |
| 96 | +} |
0 commit comments