vayacondios-server 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,8 +25,8 @@ class Vayacondios
25
25
  @@legacy_switch = nil
26
26
 
27
27
  def self.legacy_switch
28
- legacy_mode_on = Settings[:vayacondios][:legacy]
29
28
  if @@legacy_switch.nil?
29
+ legacy_mode_on = Settings[:vayacondios][:legacy]
30
30
  @@legacy_switch = get_legacy_switch(legacy_mode_on)
31
31
  Log.info("using #{legacy_mode_on ? 'legacy' : 'standard'} mode")
32
32
  end
@@ -34,6 +34,7 @@ class Vayacondios
34
34
  end
35
35
 
36
36
  def self.force_legacy_mode on
37
+ Log.info("forcing #{on ? 'legacy' : 'standard'} mode")
37
38
  @@legacy_switch = get_legacy_switch on
38
39
  end
39
40
 
@@ -25,8 +25,8 @@ class Vayacondios
25
25
  @@legacy_switch = nil
26
26
 
27
27
  def self.legacy_switch
28
- legacy_mode_on = Settings[:vayacondios][:legacy]
29
28
  if @@legacy_switch.nil?
29
+ legacy_mode_on = Settings[:vayacondios][:legacy]
30
30
  @@legacy_switch = get_legacy_switch(legacy_mode_on)
31
31
  Log.info("using #{legacy_mode_on ? 'legacy' : 'standard'} mode")
32
32
  end
@@ -34,6 +34,7 @@ class Vayacondios
34
34
  end
35
35
 
36
36
  def self.force_legacy_mode on
37
+ Log.info("forcing #{on ? 'legacy' : 'standard'} mode")
37
38
  @@legacy_switch = get_legacy_switch on
38
39
  end
39
40
 
@@ -25,8 +25,8 @@ class Vayacondios
25
25
  @@legacy_switch = nil
26
26
 
27
27
  def self.legacy_switch
28
- legacy_mode_on = Settings[:vayacondios][:legacy]
29
28
  if @@legacy_switch.nil?
29
+ legacy_mode_on = Settings[:vayacondios][:legacy]
30
30
  @@legacy_switch = get_legacy_switch(legacy_mode_on)
31
31
  Log.info("using #{legacy_mode_on ? 'legacy' : 'standard'} mode")
32
32
  end
@@ -34,6 +34,7 @@ class Vayacondios
34
34
  end
35
35
 
36
36
  def self.force_legacy_mode on
37
+ Log.info("forcing #{on ? 'legacy' : 'standard'} mode")
37
38
  @@legacy_switch = get_legacy_switch on
38
39
  end
39
40
 
@@ -1,3 +1,3 @@
1
1
  class Vayacondios
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
data/pom.xml CHANGED
@@ -4,7 +4,7 @@
4
4
  <groupId>com.infochimps</groupId>
5
5
  <artifactId>vayacondios</artifactId>
6
6
  <packaging>jar</packaging>
7
- <version>1.0.2-SNAPSHOT</version>
7
+ <version>1.0.3-SNAPSHOT</version>
8
8
  <name>java-common</name>
9
9
  <url>http://maven.apache.org</url>
10
10
 
@@ -62,6 +62,8 @@
62
62
  <arguments>
63
63
  <argument>-cp</argument>
64
64
  <classpath/>
65
+ <argument>-Dlog4j.debug</argument>
66
+ <argument>-Dlog4j.configuration=file://${project.basedir}/config/log4j.properties</argument>
65
67
  <argument>-jar</argument>
66
68
  <argument>target/${project.artifactId}-${project.version}.jar</argument>
67
69
  </arguments>
@@ -112,13 +114,12 @@
112
114
  <dependency>
113
115
  <groupId>org.slf4j</groupId>
114
116
  <artifactId>slf4j-api</artifactId>
115
- <version>1.7.2</version>
117
+ <version>1.7.5</version>
116
118
  </dependency>
117
119
  <dependency>
118
120
  <groupId>org.slf4j</groupId>
119
- <artifactId>slf4j-simple</artifactId>
120
- <version>1.7.2</version>
121
- <scope>test</scope>
121
+ <artifactId>slf4j-log4j12</artifactId>
122
+ <version>1.7.5</version>
122
123
  </dependency>
123
124
  <dependency>
124
125
  <groupId>junit</groupId>
@@ -24,44 +24,97 @@ public class HttpHelper {
24
24
  private static final boolean USE_CHARLES = false;
25
25
 
26
26
  // opens or returns a null reader
27
- public static BufferedReader openOrNull(Logger log, String urlString, Charset inputCharset) {
28
- try { return open(log, urlString, inputCharset); }
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); }
29
39
  catch (IOException e) {
30
40
  log.warn("Got an exception trying to open {}: {}", urlString, e);
31
41
  return null;
32
42
  }
33
43
  }
34
44
 
45
+ //----------------------------------------------------------------------------
46
+
35
47
  public static BufferedReader open(Logger log,
36
48
  String urlString,
37
49
  Charset inputCharset) throws IOException {
38
- return getReader(openStream(log, urlString), inputCharset);
50
+ return open(log, urlString, inputCharset, 0);
39
51
  }
40
52
 
41
53
  public static BufferedReader open(Logger log,
42
54
  String urlString,
43
55
  HashMap<String,String> extraHeaders,
44
56
  Charset inputCharset) throws IOException {
45
- return getReader(openStream(log, urlString, extraHeaders), inputCharset);
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
+ }
46
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);
47
78
  }
48
79
 
80
+ //----------------------------------------------------------------------------
81
+
49
82
  public static InputStream openStream(Logger log,
50
83
  String urlString) throws IOException {
51
- HttpURLConnection con = getConnection(urlString, log);
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);
52
98
  return getStream(con, log);
53
99
  }
54
100
 
55
101
  public static InputStream openStream(Logger log,
56
102
  String urlString,
57
- HashMap<String,String> extraHeaders) throws IOException {
58
- HttpURLConnection con = getConnection(urlString, log);
103
+ HashMap<String,String> extraHeaders,
104
+ int timeout) throws IOException {
105
+
106
+ HttpURLConnection con = getConnection(urlString, log, timeout);
59
107
  for (Entry<String,String> header : extraHeaders.entrySet())
60
108
  con.setRequestProperty(header.getKey(), header.getValue());
61
109
  return getStream(con, log);
62
110
  }
63
111
 
64
- private static HttpURLConnection getConnection(String urlString, Logger log) throws IOException {
112
+ //----------------------------------------------------------------------------
113
+
114
+ private static HttpURLConnection getConnection(String urlString,
115
+ Logger log,
116
+ int timeout)
117
+ throws IOException {
65
118
  URL url = null;
66
119
  try { url = new URL(urlString); }
67
120
  catch (MalformedURLException e) {
@@ -79,13 +132,27 @@ public class HttpHelper {
79
132
  con.setRequestProperty("Authorization", "Basic " + new String(BASE64.encodeBase64(userInfo.getBytes())));
80
133
  }
81
134
  con.setRequestProperty("Accept-Encoding", "gzip,deflate");
135
+ if (timeout != 0) con.setReadTimeout(timeout);
82
136
  return con;
83
137
  }
84
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.info("successfully opened connection to {} with character encoding {}",
147
+ con.getURL().toString(),
148
+ inputCharset);
149
+
150
+ return reader;
151
+ }
152
+
85
153
  private static InputStream getStream(HttpURLConnection con,
86
154
  Logger log) throws IOException {
87
- InputStream in = null;
88
-
155
+ InputStream in;
89
156
  try { in = con.getInputStream(); }
90
157
  catch (IOException e) {
91
158
  // Some HTTP responses will raise an exception, but the
@@ -96,7 +163,7 @@ public class HttpHelper {
96
163
  InputStream errorStream = con.getErrorStream();
97
164
  if (errorStream != null) {
98
165
  BufferedReader r = new BufferedReader(new InputStreamReader(errorStream));
99
- try { for (String line; (line = r.readLine()) != null; log.warn(line)); }
166
+ try { for (String line; (line = r.readLine()) != null; log.error(line)); }
100
167
  catch (IOException nested_exc) {
101
168
  log.error("Got an exception in the exception handler: {}", nested_exc);
102
169
  throw e;
@@ -105,17 +172,10 @@ public class HttpHelper {
105
172
  throw e;
106
173
  }
107
174
 
108
- log.debug("successfully opened connection to " + con.getURL().toString());
109
175
  String encoding = con.getContentEncoding();
110
176
  log.debug("Got HTTP stream with content encoding type '" + encoding + "'");
111
177
 
112
- return (encoding != null && encoding.equals("gzip")) ? new GZIPInputStream(in) : in;
113
- }
114
-
115
- private static BufferedReader getReader(InputStream in, Charset inputCharset) {
116
- InputStreamReader istream_reader = new InputStreamReader(in, inputCharset);
117
- BufferedReader reader = new BufferedReader(istream_reader);
118
-
119
- return reader;
178
+ return (encoding != null && encoding.equals("gzip")) ?
179
+ new GZIPInputStream(in) : in;
120
180
  }
121
181
  }
@@ -301,7 +301,7 @@ public class ItemSets<LinkType extends LinkToVCD> extends Organization {
301
301
  * A Vayacondios item can be a boolean, a number, or a string.
302
302
  */
303
303
  public static class Item {
304
- static class Serializer implements JsonSerializer {
304
+ public static class Serializer implements JsonSerializer {
305
305
  public JsonElement serialize(Object item,
306
306
  Type typeOfId,
307
307
  JsonSerializationContext context) {
@@ -1,7 +1,7 @@
1
1
  package com.infochimps.vayacondios;
2
-
3
2
  import com.google.gson.Gson;
4
3
  import com.google.gson.GsonBuilder;
4
+ import com.google.gson.JsonArray;
5
5
  import com.google.gson.JsonElement;
6
6
  import com.google.gson.JsonIOException;
7
7
  import com.google.gson.JsonObject;
@@ -23,6 +23,7 @@ import java.nio.charset.Charset;
23
23
  import java.util.ArrayList;
24
24
  import java.util.HashMap;
25
25
  import java.util.List;
26
+ import java.util.Map;
26
27
 
27
28
  import java.net.URL;
28
29
 
@@ -50,8 +51,7 @@ public class StandardVCDLink extends LinkToVCD {
50
51
 
51
52
  // assume Vayacondios response comes in a single line
52
53
  if (line != null &&
53
- (response = PARSER.parse(line)).isJsonObject() &&
54
- (itemSet = (response.getAsJsonObject().get("contents"))).isJsonArray()) {
54
+ (itemSet = VCD_HANDLER.extractContents(PARSER.parse(line))) != null) {
55
55
  for (JsonElement elem : itemSet.getAsJsonArray()) {
56
56
  if (!elem.isJsonPrimitive()) {
57
57
  LOG.warn("ignoring non-primitive in itemset: " + elem);
@@ -81,9 +81,7 @@ public class StandardVCDLink extends LinkToVCD {
81
81
  List<Item> items) throws IOException {
82
82
 
83
83
  // serialize the items
84
- HashMap content = new HashMap();
85
- content.put("contents", items);
86
- String body = GSON.toJson(content);
84
+ String body = VCD_HANDLER.wrapContents(items);
87
85
  // connect to our standard path
88
86
  URL url = new URL(getParent().urlString(PATH_COMPONENT, topic, id));
89
87
  HttpURLConnection connection = (HttpURLConnection)
@@ -137,6 +135,17 @@ public class StandardVCDLink extends LinkToVCD {
137
135
  connection.disconnect();
138
136
  }
139
137
 
138
+ //----------------------------------------------------------------------------
139
+
140
+ public static void forceLegacy(boolean vcdLegacy) {
141
+ LOG.info("forcing vayacondios {} mode", vcdLegacy ? "legacy" : "standard");
142
+
143
+ VCD_HANDLER = vcdLegacy ?
144
+ new LegacyContentsHandler() : new StandardContentsHandler();
145
+ }
146
+
147
+ //----------------------------------------------------------------------------
148
+
140
149
  private BufferedReader openUrl(String urlString) throws IOException {
141
150
  HashMap headers = new HashMap();
142
151
  headers.put("Accept", "*/*");
@@ -149,4 +158,61 @@ public class StandardVCDLink extends LinkToVCD {
149
158
  private static final JsonParser PARSER = new JsonParser();
150
159
  private static final Logger LOG = getLogger();
151
160
  private static final String PATH_COMPONENT = "itemset";
161
+
162
+ private static LegacySwitch VCD_HANDLER = new StandardContentsHandler();
163
+
164
+ //----------------------------------------------------------------------------
165
+
166
+ private static interface LegacySwitch {
167
+ String wrapContents(List<Item> items);
168
+ JsonArray extractContents(JsonElement response);
169
+ }
170
+
171
+ private static class LegacyContentsHandler implements LegacySwitch {
172
+ @Override
173
+ public String wrapContents(List<Item> items) {
174
+ String json = GSON.toJson(items);
175
+ LOG.trace("results of wrapping with legacy handler: {}", json);
176
+ return json;
177
+ }
178
+ @Override
179
+ public JsonArray extractContents(JsonElement response) {
180
+ if (response.isJsonArray()) {
181
+ return response.getAsJsonArray();
182
+ } else { return null; }
183
+ }
184
+ }
185
+
186
+ private static class StandardContentsHandler implements LegacySwitch {
187
+ @Override
188
+ public String wrapContents(final List<Item> items) {
189
+ // not at all sure why GSON is returning null given a
190
+ // Map<String,List<Item>> all of the sudden, but it seems to
191
+ // work on a Map<String,List<String>>, so here we go.
192
+ Map<String,List<String>> contents =
193
+ new HashMap<String,List<String>>();
194
+ List<String> strItems = new ArrayList<String>();
195
+ for (Item item : items) {
196
+ strItems.add(item.getObject().toString());
197
+ }
198
+ contents.put("contents", strItems);
199
+ LOG.trace("contents: {}", contents);
200
+ String json = GSON.toJson(contents);
201
+ LOG.trace("results of wrapping with standard handler: {}", json);
202
+ return json;
203
+ }
204
+ @Override
205
+ public JsonArray extractContents(JsonElement response) {
206
+ JsonElement itemSet;
207
+
208
+ if (response.isJsonObject()) {
209
+ if ((itemSet = response.getAsJsonObject().get("contents"))
210
+ .isJsonArray()) {
211
+ return itemSet.getAsJsonArray();
212
+ }
213
+ }
214
+
215
+ return null;
216
+ }
217
+ }
152
218
  }
@@ -1,6 +1,7 @@
1
1
  package com.infochimps.vayacondios;
2
2
 
3
3
  import com.infochimps.vayacondios.VayacondiosClient;
4
+ import com.infochimps.vayacondios.StandardVCDLink;
4
5
 
5
6
  import static com.infochimps.util.CurrentClass.getLogger;
6
7
 
@@ -20,6 +21,10 @@ public class VCDIntegrationTest {
20
21
  private static final int VCD_PORT = 8000;
21
22
  private static final Logger LOG = getLogger();
22
23
 
24
+ public static void setUp() {
25
+ StandardVCDLink.forceLegacy(false);
26
+ }
27
+
23
28
  private static ItemSets itemSets() {
24
29
  return new VayacondiosClient("localhost", VCD_PORT).
25
30
  organization("org").
@@ -79,6 +84,8 @@ public class VCDIntegrationTest {
79
84
  "this will fail. ***");
80
85
  System.out.println("Running Vayacondios integration test...");
81
86
 
87
+ setUp();
88
+
82
89
  try {
83
90
  create("foo", "baz", "bar", "bing");
84
91
  assertEquals("foo", "baz", "bar", "bing");
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vayacondios-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-06-03 00:00:00.000000000 Z
15
+ date: 2013-06-06 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: configliere
@@ -279,7 +279,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
279
279
  version: '0'
280
280
  segments:
281
281
  - 0
282
- hash: -3470064849041831815
282
+ hash: 530761123720877382
283
283
  required_rubygems_version: !ruby/object:Gem::Requirement
284
284
  none: false
285
285
  requirements:
@@ -288,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
288
  version: '0'
289
289
  segments:
290
290
  - 0
291
- hash: -3470064849041831815
291
+ hash: 530761123720877382
292
292
  requirements: []
293
293
  rubyforge_project:
294
294
  rubygems_version: 1.8.25