10000 MultiHttpRequestInitializer · sdecima/google-http-java-client@059de13 · GitHub
[go: up one dir, main page]

Skip to content

Commit 059de13

Browse files
author
Yaniv Inbar
committed
MultiHttpRequestInitializer
http://codereview.appspot.com/5434044/
1 parent 1a122f1 commit 059de13

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http;
16+
17+
import java.io.IOException;
18+
import java.util.Collection;
19+
20+
/**
21+
* HTTP request initializer that is compose of a sequence of initializers.
22+
*
23+
* <p>
24+
* Each of the HTTP request initializers are run in the order input initializers. Any {@code null}
25+
* initializer will be ignored.
26+
* </p>
27+
* <p>
28+
* A copy of the input initializers parameters will be made in the constructor so the order and
29+
* value of the input initializers cannot be changed. Thus, if all the initializers are thread-safe,
30+
* this implementation is thread-safe. Also, if all the initializers are immutable, this
31+
* implementation is immutable.
32+
* </p>
33+
*
34+
* @since 1.7
35+
* @author Yaniv Inbar
36+
*/
37+
public class MultiHttpRequestInitializer implements HttpRequestInitializer {
38+
39+
private final HttpRequestInitializer[] initializers;
40+
41+
/**
42+
* @param initializers HTTP request initializers
43+
*/
44+
public MultiHttpRequestInitializer(HttpRequestInitializer... initializers) {
45+
this.initializers = initializers.clone();
46+
}
47+
48+
/**
49+
* @param initializers HTTP request initializers
50+
*/
51+
public MultiHttpRequestInitializer(Collection<? extends HttpRequestInitializer> initializers) {
52+
this.initializers = initializers.toArray(new HttpRequestInitializer[initializers.size()]);
53+
}
54+
55+
public void initialize(HttpRequest request) throws IOException {
56+
for (HttpRequestInitializer initializer : initializers) {
57+
if (initializer != null) {
58+
initializer.initialize(request);
59+
}
60+
}
61+
}
62+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http;
16+
17+
import com.google.api.client.testing.http.HttpTesting;
18+
import com.google.api.client.testing.http.MockHttpTransport;
19+
20+
import junit.framework.TestCase;
21+
22+
import java.io.IOException;
23+
import java.util.Arrays;
24+
25+
/**
26+
* Tests {@link MultiHttpRequestInitializer}.
27+
*
28+
* @author Yaniv Inbar
29+
*/
30+
public class MultiHttpRequestInitializerTest extends TestCase {
31+
32+
static class StringHttpRequestInitializer implements HttpRequestInitializer {
33+
private final StringBuilder buf;
34+
private final char c;
35+
36+
StringHttpRequestInitializer(StringBuilder buf, char c) {
37+
this.buf = buf;
38+
this.c = c;
39+
}
40+
41+
public void initialize(HttpRequest request) throws IOException {
42+
buf.append(c);
43+
}
44+
}
45+
46+
public void testInitialize() throws IOException {
47+
HttpRequest request =
48+
new MockHttpTransport().createRequestFactory().buildGetRequest(
49+
HttpTesting.SIMPLE_GENERIC_URL);
50+
MultiHttpRequestInitializer initializer = new MultiHttpRequestInitializer();
51+
initializer.initialize(request);
52+
// one
53+
StringBuilder buf = new StringBuilder();
54+
initializer = new MultiHttpRequestInitializer(new StringHttpRequestInitializer(buf, 'a'));
55+
initializer.initialize(request);
56+
assertEquals("a", buf.toString());
57+
// two
58+
buf = new StringBuilder();
59+
initializer =
60+
new MultiHttpRequestInitializer(new StringHttpRequestInitializer(buf, 'a'),
61+
new StringHttpRequestInitializer(buf, 'b'));
62+
initializer.initialize(request);
63+
assertEquals("ab", buf.toString());
64+
// three with a null
65+
buf = new StringBuilder();
66+
initializer =
67+
new MultiHttpRequestInitializer(new StringHttpRequestInitializer(buf, 'a'),
68+
new StringHttpRequestInitializer(buf, 'b'), null, new StringHttpRequestInitializer(buf,
69+
'c'));
70+
initializer.initialize(request);
71+
assertEquals("abc", buf.toString());
72+
// three with a null, but using a collection
73+
buf = new StringBuilder();
74+
initializer =
75+
new MultiHttpRequestInitializer(Arrays.asList(
76+
new StringHttpRequestInitializer(buf, 'a'), new StringHttpRequestInitializer(buf, 'b'),
77+
null, new StringHttpRequestInitializer(buf, 'c')));
78+
initializer.initialize(request);
79+
assertEquals("abc", buf.toString());
80+
// just nulls
81+
buf = new StringBuilder();
82+
initializer = new MultiHttpRequestInitializer(null, null, null, null);
83+
initializer.initialize(request);
84+
assertEquals("", buf.toString());
85+
}
86+
}

0 commit comments

Comments
 (0)
0