vayacondios-server 0.2.11 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -1
- data/.travis.yml +2 -0
- data/Gemfile +15 -9
- data/LICENSE.md +2 -6
- data/Procfile +1 -1
- data/README.md +656 -111
- data/Rakefile +89 -6
- data/bin/vcd +10 -0
- data/bin/vcd-server +8 -0
- data/config/database.yml +6 -0
- data/config/spec.example.yml +18 -0
- data/config/vayacondios.example.yml +15 -0
- data/config/vcd-server.rb +37 -0
- data/examples/configuration.rb +56 -0
- data/examples/event_stream.rb +19 -0
- data/examples/simple.rb +61 -0
- data/features/event.feature +319 -0
- data/features/events.feature +208 -0
- data/features/stash.feature +840 -0
- data/features/stashes.feature +492 -0
- data/features/step_definitions/stash_steps.rb +113 -0
- data/features/stream.feature +30 -0
- data/features/support/em.rb +14 -0
- data/features/support/env.rb +13 -0
- data/lib/vayacondios/configuration.rb +63 -0
- data/lib/vayacondios/server/api.rb +126 -0
- data/lib/vayacondios/server/api_options.rb +56 -0
- data/lib/vayacondios/server/configuration.rb +23 -0
- data/lib/vayacondios/server/driver.rb +71 -0
- data/lib/vayacondios/server/drivers/mongo.rb +126 -0
- data/lib/vayacondios/server/handlers/document_handler.rb +81 -0
- data/lib/vayacondios/server/handlers/event_handler.rb +31 -26
- data/lib/vayacondios/server/handlers/events_handler.rb +31 -0
- data/lib/vayacondios/server/handlers/stash_handler.rb +69 -0
- data/lib/vayacondios/server/handlers/stashes_handler.rb +49 -0
- data/lib/vayacondios/server/handlers/stream_handler.rb +39 -0
- data/lib/vayacondios/server/models/document.rb +87 -0
- data/lib/vayacondios/server/models/event.rb +198 -0
- data/lib/vayacondios/server/models/stash.rb +100 -0
- data/lib/vayacondios/server.rb +35 -0
- data/lib/vayacondios-server.rb +19 -13
- data/lib/vayacondios.rb +22 -0
- data/pom.xml +124 -4
- data/spec/configuration_spec.rb +41 -0
- data/spec/server/api_options_spec.rb +32 -0
- data/spec/server/api_spec.rb +279 -0
- data/spec/server/configuration_spec.rb +27 -0
- data/spec/server/drivers/mongo_spec.rb +107 -0
- data/spec/server/handlers/event_handler_spec.rb +62 -0
- data/spec/server/handlers/events_handler_spec.rb +51 -0
- data/spec/server/handlers/stash_handler_spec.rb +68 -0
- data/spec/server/handlers/stashes_handler_spec.rb +50 -0
- data/spec/server/handlers/stream_handler_spec.rb +5 -0
- data/spec/server/models/document_spec.rb +9 -0
- data/spec/server/models/event_spec.rb +185 -0
- data/spec/server/models/stash_spec.rb +95 -0
- data/spec/spec_helper.rb +23 -3
- data/spec/support/database_helper.rb +42 -0
- data/spec/support/log_helper.rb +19 -0
- data/spec/support/shared_context_for_events.rb +22 -0
- data/spec/support/shared_context_for_stashes.rb +24 -0
- data/spec/support/shared_examples_for_handlers.rb +32 -0
- data/src/main/java/com/infochimps/vayacondios/BaseClient.java +342 -0
- data/src/main/java/com/infochimps/vayacondios/HTTPClient.java +426 -0
- data/src/main/java/com/infochimps/vayacondios/VayacondiosClient.java +487 -65
- data/src/main/java/com/infochimps/vayacondios/test/IntegrationTest.java +3 -0
- data/src/test/java/com/infochimps/vayacondios/BaseClientTest.java +50 -0
- data/src/test/java/com/infochimps/vayacondios/HTTPClientIT.java +267 -0
- data/vayacondios-server.gemspec +9 -9
- metadata +127 -122
- checksums.yaml +0 -15
- data/.rspec +0 -2
- data/.yardopts +0 -10
- data/Guardfile +0 -41
- data/app/http_shim.rb +0 -71
- data/bin/vcd.sh +0 -27
- data/config/http_shim.rb +0 -43
- data/config/vayacondios.example.yaml +0 -7
- data/config/vayacondios.yaml +0 -7
- data/examples/java/ItemSetTest.java +0 -76
- data/lib/tasks/publish.rake +0 -23
- data/lib/tasks/spec.rake +0 -11
- data/lib/tasks/yard.rake +0 -2
- data/lib/vayacondios/client/config.rb +0 -7
- data/lib/vayacondios/client/configliere.rb +0 -38
- data/lib/vayacondios/client/cube_client.rb +0 -39
- data/lib/vayacondios/client/http_client.rb +0 -49
- data/lib/vayacondios/client/itemset.rb +0 -130
- data/lib/vayacondios/client/legacy_switch.rb +0 -43
- data/lib/vayacondios/client/notifier.rb +0 -123
- data/lib/vayacondios/client/zabbix_client.rb +0 -148
- data/lib/vayacondios/legacy_switch.rb +0 -43
- data/lib/vayacondios/server/errors/bad_request.rb +0 -6
- data/lib/vayacondios/server/errors/not_found.rb +0 -6
- data/lib/vayacondios/server/handlers/config_handler.rb +0 -32
- data/lib/vayacondios/server/handlers/itemset_handler.rb +0 -60
- data/lib/vayacondios/server/legacy_switch.rb +0 -43
- data/lib/vayacondios/server/model/config_document.rb +0 -89
- data/lib/vayacondios/server/model/document.rb +0 -25
- data/lib/vayacondios/server/model/event_document.rb +0 -94
- data/lib/vayacondios/server/model/itemset_document.rb +0 -126
- data/lib/vayacondios/server/rack/extract_methods.rb +0 -35
- data/lib/vayacondios/server/rack/jsonize.rb +0 -43
- data/lib/vayacondios/server/rack/params.rb +0 -50
- data/lib/vayacondios/server/rack/path.rb +0 -23
- data/lib/vayacondios/server/rack/path_validation.rb +0 -22
- data/lib/vayacondios/version.rb +0 -3
- data/lib/vayacondios-client.rb +0 -22
- data/scripts/hadoop_monitor/configurable.rb +0 -66
- data/scripts/hadoop_monitor/hadoop_attempt_scraper.rb +0 -45
- data/scripts/hadoop_monitor/hadoop_client.rb +0 -273
- data/scripts/hadoop_monitor/hadoop_monitor.rb +0 -101
- data/scripts/hadoop_monitor/hadoopable.rb +0 -65
- data/scripts/hadoop_monitor/machine_monitor.rb +0 -115
- data/scripts/s3_cataloger/buckets +0 -33
- data/scripts/s3_cataloger/foreach_bucket +0 -88
- data/scripts/s3_cataloger/parse_ls.py +0 -391
- data/spec/client/itemset_legacy_spec.rb +0 -55
- data/spec/client/itemset_spec.rb +0 -60
- data/spec/client/notifier_spec.rb +0 -120
- data/spec/server/config_spec.rb +0 -113
- data/spec/server/event_spec.rb +0 -103
- data/spec/server/itemset_legacy_spec.rb +0 -320
- data/spec/server/itemset_spec.rb +0 -317
- data/spec/server/rack/extract_methods_spec.rb +0 -60
- data/spec/server/rack/path_spec.rb +0 -36
- data/spec/server/rack/path_validation_spec.rb +0 -22
- data/spec/server/server_spec.rb +0 -20
- data/spec/support/mongo_cleaner.rb +0 -32
- data/src/main/java/ItemSetTest.java +0 -76
- data/src/main/java/com/infochimps/util/CurrentClass.java +0 -26
- data/src/main/java/com/infochimps/util/DebugUtil.java +0 -38
- data/src/main/java/com/infochimps/util/HttpHelper.java +0 -181
- data/src/main/java/com/infochimps/vayacondios/ItemSets.java +0 -373
- data/src/main/java/com/infochimps/vayacondios/LinkToVCD.java +0 -18
- data/src/main/java/com/infochimps/vayacondios/MemoryVCDShim.java +0 -84
- data/src/main/java/com/infochimps/vayacondios/Organization.java +0 -62
- data/src/main/java/com/infochimps/vayacondios/PathBuilder.java +0 -13
- data/src/main/java/com/infochimps/vayacondios/StandardVCDLink.java +0 -218
- data/src/main/java/com/infochimps/vayacondios/VCDIntegrationTest.java +0 -108
- data/src/test/java/com/infochimps/vayacondios/TestVayacondiosInMemory.java +0 -78
- data/vayacondios-client.gemspec +0 -25
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'vayacondios/server/rack/path'
|
3
|
-
|
4
|
-
describe Vayacondios::Rack::Path do
|
5
|
-
let(:env) { { 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=utf-8' } }
|
6
|
-
let(:app) { mock('app').as_null_object }
|
7
|
-
subject { described_class.new(app) }
|
8
|
-
|
9
|
-
it 'adds a key in env for :vayacondios_path' do
|
10
|
-
app.should_receive(:call).with do |app_env|
|
11
|
-
app_env.keys.should include(:vayacondios_path)
|
12
|
-
end
|
13
|
-
subject.call(env)
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'parse_path' do
|
17
|
-
it 'returns nil if the path is unparseable' do
|
18
|
-
subject.parse_path('/what/the:f/happened&here?').should be nil
|
19
|
-
end
|
20
|
-
it 'parses organizations and types correctly' do
|
21
|
-
subject.parse_path('/v1/infochimps/event').should include(organization: 'infochimps', type: 'event')
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'parses topics correctly' do
|
25
|
-
subject.parse_path('/v1/infochimps/config/foo').should include(topic: 'foo')
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'parses ids correctly' do
|
29
|
-
subject.parse_path('/v1/infochimps/itemset/bar/1').should include(id: '1')
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'parses formats correctly' do
|
33
|
-
subject.parse_path('/v1/infochimps/event/baz/1.json').should include(format: 'json')
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'vayacondios/server/rack/path_validation'
|
3
|
-
|
4
|
-
describe Vayacondios::Rack::PathValidation do
|
5
|
-
let(:env){ { 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=utf-8' } }
|
6
|
-
let(:app){ mock('app').as_null_object }
|
7
|
-
subject { described_class.new(app) }
|
8
|
-
|
9
|
-
it 'sets @opts when created' do
|
10
|
-
subject.instance_variable_get('@opts').should == {}
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'returns a bad request when :vayacondios_path does not exist' do
|
14
|
-
subject.call(env).should == [400, {}, '{"error":"Bad Request. Format path is <host>/v1/<org>/event/<topic>"}']
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'valid_paths?' do
|
18
|
-
it 'validates the :vayacondios_path' do
|
19
|
-
subject.valid_paths?({}).should be_true
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/spec/server/server_spec.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
require 'multi_json'
|
4
|
-
|
5
|
-
require File.join(File.dirname(__FILE__), '../../', 'app/http_shim')
|
6
|
-
|
7
|
-
describe HttpShim do
|
8
|
-
include Goliath::TestHelper
|
9
|
-
|
10
|
-
let(:err) { Proc.new{ |c| fail "HTTP Request Failed #{c.response}" } }
|
11
|
-
|
12
|
-
it "responds to requests" do
|
13
|
-
with_api(HttpShim) do |api|
|
14
|
-
get_request({}, err) do |c|
|
15
|
-
c.response_header.status.should == 400
|
16
|
-
MultiJson.load(c.response).should eql({"error" => "Bad Request. Format path is <host>/v1/<org>/event/<topic>"})
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'mongo'
|
2
|
-
require 'configliere'
|
3
|
-
|
4
|
-
Settings.define :app_name, :default => 'vayacondios', :description => 'Name to key on for tracer stats, statsd metrics, etc.'
|
5
|
-
Settings.define 'mongo.host', :default => 'localhost', :description => 'Mongo hostname'
|
6
|
-
Settings.define 'mongo.port', :default => '27017', :description => 'Mongo port'
|
7
|
-
|
8
|
-
Settings.read(File.join File.dirname(__FILE__), '..', '..', 'config', 'vayacondios.yaml')
|
9
|
-
Settings.resolve!
|
10
|
-
|
11
|
-
def get_mongo_db &block
|
12
|
-
fail unless block
|
13
|
-
conn = Mongo::Connection.new(Settings[:mongo][:host],Settings[:mongo][:port])
|
14
|
-
yield conn.db(Settings[:mongo][:database])
|
15
|
-
conn.close
|
16
|
-
end
|
17
|
-
|
18
|
-
def clean_mongo
|
19
|
-
get_mongo_db do |db|
|
20
|
-
db.collections.select {|c| c.name !~ /system/ }.each { |c| c.drop }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
RSpec.configure do |config|
|
25
|
-
config.before :each do
|
26
|
-
clean_mongo
|
27
|
-
end
|
28
|
-
|
29
|
-
config.after :all do
|
30
|
-
clean_mongo
|
31
|
-
end
|
32
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
import com.infochimps.vayacondios.Organization;
|
2
|
-
import com.infochimps.vayacondios.VayacondiosClient;
|
3
|
-
import com.infochimps.vayacondios.ItemSets;
|
4
|
-
import static com.infochimps.vayacondios.ItemSets.Item;
|
5
|
-
import static com.infochimps.vayacondios.ItemSets.ItemSet;
|
6
|
-
|
7
|
-
import java.io.*;
|
8
|
-
import java.util.*;
|
9
|
-
|
10
|
-
public class ItemSetTest {
|
11
|
-
private static final int VCD_PORT = 8000;
|
12
|
-
|
13
|
-
public static void main(String argv[]) throws IOException {
|
14
|
-
// Instantiate a new Vayacondios client.
|
15
|
-
VayacondiosClient client = new VayacondiosClient("localhost", VCD_PORT);
|
16
|
-
|
17
|
-
// Organizations can allow for testing and multiple environments
|
18
|
-
// (staging, development).
|
19
|
-
Organization org = client.organization("test");
|
20
|
-
|
21
|
-
// At the moment, only the itemsets protocol is supported via a
|
22
|
-
// Java API.
|
23
|
-
ItemSets isets = org.itemsets();
|
24
|
-
|
25
|
-
List<Item> foobar = Arrays.asList(new Item("foo"), new Item("bar"));
|
26
|
-
|
27
|
-
// All of the following are equivalent ways to clobber the itemset
|
28
|
-
// at topic my_topic and id my_id, replacing it with the items
|
29
|
-
// "foo" and "bar." The first and second exist to avoid
|
30
|
-
// instantiating new objects repeatedly when a code path must
|
31
|
-
// access many different topics and ids.
|
32
|
-
isets .create("my_topic", "my_id", foobar);
|
33
|
-
isets.topic("my_topic") .create( "my_id", foobar);
|
34
|
-
isets.topic("my_topic").itemSet("my_id").create( foobar);
|
35
|
-
|
36
|
-
List<Item> fetched;
|
37
|
-
|
38
|
-
// Similarly, these are all equivalent ways to fetch items.
|
39
|
-
fetched = isets .fetch("my_topic", "my_id");
|
40
|
-
fetched = isets.topic("my_topic") .fetch( "my_id");
|
41
|
-
fetched = isets.topic("my_topic").itemSet("my_id").fetch( );
|
42
|
-
|
43
|
-
ItemSet iset = isets.topic("my_topic").itemSet("my_id");
|
44
|
-
|
45
|
-
// foo and bar will be printed, along with their types: STRING
|
46
|
-
System.out.println("fetched items: " + fetched);
|
47
|
-
|
48
|
-
List<Item> bazqux = Arrays.asList(new Item("baz"), new Item("qux"));
|
49
|
-
|
50
|
-
// Updating an itemset with a list of items ensures the presence
|
51
|
-
// in the itemset of all of the items in the specified list.
|
52
|
-
isets .update("my_topic", "my_id", bazqux);
|
53
|
-
isets.topic("my_topic") .update( "my_id", bazqux);
|
54
|
-
isets.topic("my_topic").itemSet("my_id").update( bazqux);
|
55
|
-
|
56
|
-
// foo, bar, baz, and qux will all be printed.
|
57
|
-
System.out.println("after update: " + iset.fetch());
|
58
|
-
|
59
|
-
List<Item> barbaz = Arrays.asList(new Item("bar"), new Item("baz"));
|
60
|
-
|
61
|
-
// Updating an itemset with a list of items ensures the presence
|
62
|
-
// in the itemset of all of the items in the specified list.
|
63
|
-
isets .remove("my_topic", "my_id", barbaz);
|
64
|
-
isets.topic("my_topic") .remove( "my_id", barbaz);
|
65
|
-
isets.topic("my_topic").itemSet("my_id").remove( barbaz);
|
66
|
-
|
67
|
-
// foo and qux will now be printed
|
68
|
-
System.out.println("after removing: " + iset.fetch());
|
69
|
-
|
70
|
-
// Create an empty itemset to effectively delete it.
|
71
|
-
iset.create(Collections.EMPTY_LIST);
|
72
|
-
|
73
|
-
// foo and qux will now be printed
|
74
|
-
System.out.println("after deletion: " + iset.fetch());
|
75
|
-
}
|
76
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
package com.infochimps.util;
|
2
|
-
|
3
|
-
import org.slf4j.Logger;
|
4
|
-
import org.slf4j.LoggerFactory;
|
5
|
-
|
6
|
-
public class CurrentClass extends SecurityManager {
|
7
|
-
private static CurrentClass SINGLETON = new CurrentClass();
|
8
|
-
|
9
|
-
// must call this directly. behavior is dependent on call stack
|
10
|
-
public static Class get() {
|
11
|
-
return SINGLETON.getCurrentClass();
|
12
|
-
}
|
13
|
-
|
14
|
-
// must call this directly. behavior is dependent on call stack
|
15
|
-
public static Logger getLogger() {
|
16
|
-
return LoggerFactory.getLogger(SINGLETON.getCurrentClass(2));
|
17
|
-
}
|
18
|
-
|
19
|
-
private Class getCurrentClass(int i) {
|
20
|
-
return getClassContext()[i];
|
21
|
-
}
|
22
|
-
|
23
|
-
private Class getCurrentClass() {
|
24
|
-
return getCurrentClass(3);
|
25
|
-
}
|
26
|
-
}
|
@@ -1,38 +0,0 @@
|
|
1
|
-
package com.infochimps.util;
|
2
|
-
|
3
|
-
import java.net.InetSocketAddress;
|
4
|
-
|
5
|
-
import javax.net.ssl.HostnameVerifier;
|
6
|
-
import javax.net.ssl.HttpsURLConnection;
|
7
|
-
import javax.net.ssl.SSLContext;
|
8
|
-
import javax.net.ssl.SSLSession;
|
9
|
-
import javax.net.ssl.TrustManager;
|
10
|
-
import javax.net.ssl.X509TrustManager;
|
11
|
-
|
12
|
-
import java.security.cert.X509Certificate;
|
13
|
-
import java.security.SecureRandom;
|
14
|
-
import java.security.GeneralSecurityException;
|
15
|
-
|
16
|
-
import java.net.Proxy;
|
17
|
-
|
18
|
-
public class DebugUtil {
|
19
|
-
public static Proxy useCharles() {
|
20
|
-
trustAllCerts();
|
21
|
-
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888));
|
22
|
-
}
|
23
|
-
|
24
|
-
public static void trustAllCerts() {
|
25
|
-
try {
|
26
|
-
SSLContext sc = SSLContext.getInstance("SSL");
|
27
|
-
sc.init(null,
|
28
|
-
new TrustManager[] {
|
29
|
-
new X509TrustManager() {
|
30
|
-
public X509Certificate[] getAcceptedIssuers() { return null;}
|
31
|
-
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
|
32
|
-
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
|
33
|
-
}
|
34
|
-
}, new SecureRandom());
|
35
|
-
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
36
|
-
} catch (GeneralSecurityException e) {}
|
37
|
-
}
|
38
|
-
}
|
@@ -1,181 +0,0 @@
|
|
1
|
-
package com.infochimps.util;
|
2
|
-
|
3
|
-
import java.io.BufferedReader;
|
4
|
-
import java.io.InputStream;
|
5
|
-
import java.io.InputStreamReader;
|
6
|
-
import java.io.IOException;
|
7
|
-
import java.net.HttpURLConnection;
|
8
|
-
import java.net.MalformedURLException;
|
9
|
-
import java.net.URL;
|
10
|
-
import java.net.URLDecoder;
|
11
|
-
import java.nio.charset.Charset;
|
12
|
-
import java.util.HashMap;
|
13
|
-
import java.util.zip.GZIPInputStream;
|
14
|
-
|
15
|
-
import org.apache.commons.codec.binary.Base64;
|
16
|
-
|
17
|
-
import org.slf4j.Logger;
|
18
|
-
import org.slf4j.LoggerFactory;
|
19
|
-
|
20
|
-
import static java.util.Map.Entry;
|
21
|
-
|
22
|
-
public class HttpHelper {
|
23
|
-
private static final Base64 BASE64 = new Base64();
|
24
|
-
private static final boolean USE_CHARLES = false;
|
25
|
-
|
26
|
-
// opens or returns a null reader
|
27
|
-
public static BufferedReader openOrNull(Logger log,
|
28
|
-
String urlString,
|
29
|
-
Charset inputCharset) {
|
30
|
-
return openOrNull(log, urlString, inputCharset, 0);
|
31
|
-
}
|
32
|
-
|
33
|
-
// opens or returns a null reader
|
34
|
-
public static BufferedReader openOrNull(Logger log,
|
35
|
-
String urlString,
|
36
|
-
Charset inputCharset,
|
37
|
-
int timeout) {
|
38
|
-
try { return open(log, urlString, inputCharset, timeout); }
|
39
|
-
catch (IOException e) {
|
40
|
-
log.warn("Got an exception trying to open {}: {}", urlString, e);
|
41
|
-
return null;
|
42
|
-
}
|
43
|
-
}
|
44
|
-
|
45
|
-
//----------------------------------------------------------------------------
|
46
|
-
|
47
|
-
public static BufferedReader open(Logger log,
|
48
|
-
String urlString,
|
49
|
-
Charset inputCharset) throws IOException {
|
50
|
-
return open(log, urlString, inputCharset, 0);
|
51
|
-
}
|
52
|
-
|
53
|
-
public static BufferedReader open(Logger log,
|
54
|
-
String urlString,
|
55
|
-
HashMap<String,String> extraHeaders,
|
56
|
-
Charset inputCharset) throws IOException {
|
57
|
-
return open(log, urlString, extraHeaders, inputCharset, 0);
|
58
|
-
}
|
59
|
-
|
60
|
-
public static BufferedReader open(Logger log,
|
61
|
-
String urlString,
|
62
|
-
Charset inputCharset,
|
63
|
-
int timeout) throws IOException {
|
64
|
-
HttpURLConnection con = getConnection(urlString, log, timeout);
|
65
|
-
return getReader(con, log, inputCharset);
|
66
|
-
}
|
67
|
-
|
68
|
-
public static BufferedReader open(Logger log,
|
69
|
-
String urlString,
|
70
|
-
HashMap<String,String> extraHeaders,
|
71
|
-
Charset inputCharset,
|
72
|
-
int timeout) throws IOException {
|
73
|
-
|
74
|
-
HttpURLConnection con = getConnection(urlString, log, timeout);
|
75
|
-
for (Entry<String,String> header : extraHeaders.entrySet())
|
76
|
-
con.setRequestProperty(header.getKey(), header.getValue());
|
77
|
-
return getReader(con, log, inputCharset);
|
78
|
-
}
|
79
|
-
|
80
|
-
//----------------------------------------------------------------------------
|
81
|
-
|
82
|
-
public static InputStream openStream(Logger log,
|
83
|
-
String urlString) throws IOException {
|
84
|
-
return openStream(log, urlString, 0);
|
85
|
-
}
|
86
|
-
|
87
|
-
public static InputStream openStream(Logger log,
|
88
|
-
String urlString,
|
89
|
-
HashMap<String,String> extraHeaders)
|
90
|
-
throws IOException {
|
91
|
-
return openStream(log, urlString, extraHeaders, 0);
|
92
|
-
}
|
93
|
-
|
94
|
-
public static InputStream openStream(Logger log,
|
95
|
-
String urlString,
|
96
|
-
int timeout) throws IOException {
|
97
|
-
HttpURLConnection con = getConnection(urlString, log, timeout);
|
98
|
-
return getStream(con, log);
|
99
|
-
}
|
100
|
-
|
101
|
-
public static InputStream openStream(Logger log,
|
102
|
-
String urlString,
|
103
|
-
HashMap<String,String> extraHeaders,
|
104
|
-
int timeout) throws IOException {
|
105
|
-
|
106
|
-
HttpURLConnection con = getConnection(urlString, log, timeout);
|
107
|
-
for (Entry<String,String> header : extraHeaders.entrySet())
|
108
|
-
con.setRequestProperty(header.getKey(), header.getValue());
|
109
|
-
return getStream(con, log);
|
110
|
-
}
|
111
|
-
|
112
|
-
//----------------------------------------------------------------------------
|
113
|
-
|
114
|
-
private static HttpURLConnection getConnection(String urlString,
|
115
|
-
Logger log,
|
116
|
-
int timeout)
|
117
|
-
throws IOException {
|
118
|
-
URL url = null;
|
119
|
-
try { url = new URL(urlString); }
|
120
|
-
catch (MalformedURLException e) {
|
121
|
-
log.warn("malformed URL: {}", url);
|
122
|
-
throw new IOException(e);
|
123
|
-
}
|
124
|
-
|
125
|
-
HttpURLConnection con = (HttpURLConnection)(USE_CHARLES ?
|
126
|
-
url.openConnection(DebugUtil.useCharles()) :
|
127
|
-
url.openConnection());
|
128
|
-
|
129
|
-
String userInfo = url.getUserInfo();
|
130
|
-
if (userInfo != null) {
|
131
|
-
userInfo = URLDecoder.decode(userInfo, "US-ASCII");
|
132
|
-
con.setRequestProperty("Authorization", "Basic " + new String(BASE64.encodeBase64(userInfo.getBytes())));
|
133
|
-
}
|
134
|
-
con.setRequestProperty("Accept-Encoding", "gzip,deflate");
|
135
|
-
if (timeout != 0) con.setReadTimeout(timeout);
|
136
|
-
return con;
|
137
|
-
}
|
138
|
-
|
139
|
-
private static BufferedReader getReader(HttpURLConnection con,
|
140
|
-
Logger log,
|
141
|
-
Charset inputCharset) throws IOException {
|
142
|
-
BufferedReader reader =
|
143
|
-
new BufferedReader(
|
144
|
-
new InputStreamReader(getStream(con, log), inputCharset));
|
145
|
-
|
146
|
-
log.debug("successfully opened connection to {} with character encoding {}",
|
147
|
-
con.getURL().toString(),
|
148
|
-
inputCharset);
|
149
|
-
|
150
|
-
return reader;
|
151
|
-
}
|
152
|
-
|
153
|
-
private static InputStream getStream(HttpURLConnection con,
|
154
|
-
Logger log) throws IOException {
|
155
|
-
InputStream in;
|
156
|
-
try { in = con.getInputStream(); }
|
157
|
-
catch (IOException e) {
|
158
|
-
// Some HTTP responses will raise an exception, but the
|
159
|
-
// useful information is in the error stream.
|
160
|
-
|
161
|
-
log.warn("Exception opening {}", con.getURL().toString());
|
162
|
-
|
163
|
-
InputStream errorStream = con.getErrorStream();
|
164
|
-
if (errorStream != null) {
|
165
|
-
BufferedReader r = new BufferedReader(new InputStreamReader(errorStream));
|
166
|
-
try { for (String line; (line = r.readLine()) != null; log.error(line)); }
|
167
|
-
catch (IOException nested_exc) {
|
168
|
-
log.error("Got an exception in the exception handler: {}", nested_exc);
|
169
|
-
throw e;
|
170
|
-
}
|
171
|
-
}
|
172
|
-
throw e;
|
173
|
-
}
|
174
|
-
|
175
|
-
String encoding = con.getContentEncoding();
|
176
|
-
log.debug("Got HTTP stream with content encoding type '" + encoding + "'");
|
177
|
-
|
178
|
-
return (encoding != null && encoding.equals("gzip")) ?
|
179
|
-
new GZIPInputStream(in) : in;
|
180
|
-
}
|
181
|
-
}
|