Skip to content

Commit 7c29aa2

Browse files
committed
Add outline of proper sample with initial cmdline handling goo, plus some more build stuff including support for 3rd party libs.
1 parent 9508157 commit 7c29aa2

6 files changed

Lines changed: 169 additions & 25 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
*.iml
33
*.DS_Store*
44
build
5+
build.log
56
dist
7+
out

build.xml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,28 @@
2020

2121
<property name="dir.build" location="build"/>
2222
<property name="dir.build.examples" location="${dir.build}/examples"/>
23-
<property name="dir.build.examples.test" location="${dir.build.examples}/test"/>
23+
<property
24+
name="dir.build.examples.spurl"
25+
location="${dir.build.examples}/spurl"/>
26+
<property
27+
name="dir.build.examples.test"
28+
location="${dir.build.examples}/test"/>
2429
<property name="dir.build.splunk" location="${dir.build}/splunk"/>
2530
<property name="dir.build.tests" location="${dir.build}/tests"/>
2631
<property name="dir.dist" location="dist"/>
2732
<property name="dir.dist.examples" location="${dir.dist}/examples"/>
2833
<property name="dir.examples" location="examples"/>
34+
<property name="dir.examples.spurl" location="${dir.examples}/spurl"/>
2935
<property name="dir.examples.test" location="${dir.examples}/test"/>
36+
<property name="dir.lib" location="lib"/>
3037
<property name="dir.splunk" location="splunk"/>
3138
<property name="dir.tests" location="tests"/>
3239

40+
<path id="classpath.examples">
41+
<pathelement location="${dir.dist}/splunk.jar"/>
42+
<pathelement location="${dir.lib}/commons-cli-1.2.jar"/>
43+
</path>
44+
3345
<target name="build" depends="dist"/>
3446

3547
<target name="clean">
@@ -44,7 +56,20 @@
4456
destdir="${dir.build.splunk}"/>
4557
</target>
4658

47-
<target name="compile-examples" depends="compile-splunk">
59+
<target
60+
name="compile-examples"
61+
depends="compile-examples-spurl,compile-examples-test"/>
62+
63+
<target name="compile-examples-spurl" depends="compile-splunk">
64+
<mkdir dir="${dir.build.examples.spurl}"/>
65+
<javac includeantruntime="false"
66+
srcdir="${dir.examples.spurl}"
67+
destdir="${dir.build.examples.spurl}">
68+
<classpath refid="classpath.examples"/>
69+
</javac>
70+
</target>
71+
72+
<target name="compile-examples-test" depends="compile-splunk">
4873
<mkdir dir="${dir.build.examples.test}"/>
4974
<javac includeantruntime="false"
5075
classpath="${dir.build.splunk}"
@@ -64,6 +89,19 @@
6489

6590
<target name="dist-examples" depends="compile-examples">
6691
<mkdir dir="${dir.dist.examples}"/>
92+
93+
<jar
94+
basedir="${dir.build.examples.spurl}"
95+
destfile="${dir.dist.examples}/spurl.jar"
96+
includes="**/*.class">
97+
<manifest>
98+
<attribute name="Main-Class" value="Program"/>
99+
<attribute
100+
name="Class-Path"
101+
value="../splunk.jar ../../lib/commons-cli-1.2.jar"/>
102+
</manifest>
103+
</jar>
104+
67105
<jar
68106
basedir="${dir.build.examples.test}"
69107
destfile="${dir.dist.examples}/test.jar"

examples/spurl/Program.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

lib/commons-cli-1.2.jar

40.2 KB
Binary file not shown.

run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ if [ $? -ne 0 ] ; then
1313
exit $?
1414
fi
1515
echo Running $1.jar ..
16-
java -jar dist/examples/$1.jar
16+
java -jar dist/examples/$1.jar $2 $3 $4 $5 $6 $7 $8 $9

splunk/com/splunk/http/Service.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,25 @@ public class Service {
3737
int port = 8089;
3838

3939
public Service() {
40-
TrustManager[] trustAll = new TrustManager[] {
41-
new X509TrustManager() {
42-
public X509Certificate[] getAcceptedIssuers() { return null; }
43-
public void checkClientTrusted(
44-
X509Certificate[] certs, String authType) { }
45-
public void checkServerTrusted(
46-
X509Certificate[] certs, String authType) { }
47-
}
48-
};
49-
try {
50-
SSLContext context = SSLContext.getInstance("SSL");
51-
context.init(null, trustAll, new java.security.SecureRandom());
52-
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
53-
HttpsURLConnection.setDefaultHostnameVerifier(
54-
new HostnameVerifier() {
55-
public boolean verify(
56-
String urlHostName, SSLSession session) { return true; }
57-
});
58-
}
59-
catch (Exception e) {
60-
throw new RuntimeException("Error installing trust manager.");
61-
}
40+
setTrustPolicy();
6241
}
6342

6443
public Service(String host) {
6544
this.host = host;
45+
setTrustPolicy();
6646
}
6747

6848
public Service(String host, int port) {
6949
this.host = host;
7050
this.port = port;
51+
setTrustPolicy();
7152
}
7253

7354
public Service(String host, int port, String scheme) {
7455
this.host = host;
7556
this.port = port;
7657
this.scheme = scheme;
58+
setTrustPolicy();
7759
}
7860

7961
public String getHost() {
@@ -88,6 +70,32 @@ public int getPort() {
8870
return this.port;
8971
}
9072

73+
// Set trust policy to be used by this instance.
74+
void setTrustPolicy() {
75+
TrustManager[] trustAll = new TrustManager[] {
76+
new X509TrustManager() {
77+
public X509Certificate[] getAcceptedIssuers() { return null; }
78+
public void checkClientTrusted(
79+
X509Certificate[] certs, String authType) { }
80+
public void checkServerTrusted(
81+
X509Certificate[] certs, String authType) { }
82+
}
83+
};
84+
try {
85+
SSLContext context = SSLContext.getInstance("SSL");
86+
context.init(null, trustAll, new java.security.SecureRandom());
87+
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
88+
HttpsURLConnection.setDefaultHostnameVerifier(
89+
new HostnameVerifier() {
90+
public boolean verify(
91+
String urlHostName, SSLSession session) { return true; }
92+
});
93+
}
94+
catch (Exception e) {
95+
throw new RuntimeException("Error installing trust manager.");
96+
}
97+
}
98+
9199
public void setPort(int value) {
92100
this.port = value;
93101
}

0 commit comments

Comments
 (0)