Skip to content

Commit bdab7d3

Browse files
authored
Merge pull request #459 from metafacture/456-genericHeaderSupport
Enable generic header support in `HttpOpener`.
2 parents 45da349 + 8674b87 commit bdab7d3

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

metafacture-io/src/main/java/org/metafacture/io/HttpOpener.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,40 @@
2929
import java.io.Reader;
3030
import java.net.URL;
3131
import java.net.URLConnection;
32+
import java.util.Arrays;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import java.util.regex.Pattern;
3236

3337
/**
3438
* Opens a {@link URLConnection} and passes a reader to the receiver.
3539
*
3640
* @author Christoph Böhme
3741
* @author Jan Schnasse
3842
*/
39-
@Description("Opens a http resource. Supports the setting of Accept and Accept-Charset as http header fields.")
43+
@Description("Opens an HTTP resource. Supports the setting of `Accept` and `Accept-Charset` as HTTP header fields, as well as generic headers (separated by `\\n`).")
4044
@In(String.class)
41-
@Out(java.io.Reader.class)
45+
@Out(Reader.class)
4246
@FluxCommand("open-http")
4347
public final class HttpOpener extends DefaultObjectPipe<String, ObjectReceiver<Reader>> {
4448

45-
private String encoding = "UTF-8";
46-
private String accept = "*/*";
49+
private static final Pattern HEADER_FIELD_SEPARATOR = Pattern.compile("\n");
50+
private static final Pattern HEADER_VALUE_SEPARATOR = Pattern.compile(":");
51+
52+
private static final String ACCEPT_HEADER = "accept";
53+
private static final String ENCODING_HEADER = "accept-charset";
54+
55+
private static final String ACCEPT_DEFAULT = "*/*";
56+
private static final String ENCODING_DEFAULT = "UTF-8";
57+
58+
private final Map<String, String> headers = new HashMap<>();
4759

4860
/**
4961
* Creates an instance of {@link HttpOpener}.
5062
*/
5163
public HttpOpener() {
64+
setAccept(ACCEPT_DEFAULT);
65+
setEncoding(ENCODING_DEFAULT);
5266
}
5367

5468
/**
@@ -59,37 +73,65 @@ public HttpOpener() {
5973
* @param accept mime-type to use for the HTTP accept header
6074
*/
6175
public void setAccept(final String accept) {
62-
this.accept = accept;
76+
setHeader(ACCEPT_HEADER, accept);
6377
}
6478

6579
/**
6680
* Sets the preferred encoding of the HTTP response. This value is in the
6781
* accept-charset header. Additonally, the encoding is used for reading the
68-
* HTTP resonse if it does not specify an encoding. The default value for
82+
* HTTP resonse if it does not specify an encoding. The default value for
6983
* the encoding is UTF-8.
7084
*
7185
* @param encoding name of the encoding used for the accept-charset HTTP
7286
* header
7387
*/
7488
public void setEncoding(final String encoding) {
75-
this.encoding = encoding;
89+
setHeader(ENCODING_HEADER, encoding);
90+
}
91+
92+
/**
93+
* Sets a request property, or multiple request properties separated by
94+
* {@code \n}.
95+
*
96+
* @param header request property line
97+
*/
98+
public void setHeader(final String header) {
99+
Arrays.stream(HEADER_FIELD_SEPARATOR.split(header)).forEach(h -> {
100+
final String[] parts = HEADER_VALUE_SEPARATOR.split(h, 2);
101+
if (parts.length == 2) {
102+
setHeader(parts[0], parts[1].trim());
103+
}
104+
else {
105+
throw new IllegalArgumentException("Invalid header: " + h);
106+
}
107+
});
108+
}
109+
110+
/**
111+
* Sets a request property.
112+
*
113+
* @param key request property key
114+
* @param value request property value
115+
*/
116+
public void setHeader(final String key, final String value) {
117+
headers.put(key.toLowerCase(), value);
76118
}
77119

78120
@Override
79121
public void process(final String urlStr) {
80122
try {
81123
final URL url = new URL(urlStr);
82124
final URLConnection con = url.openConnection();
83-
con.addRequestProperty("Accept", accept);
84-
con.addRequestProperty("Accept-Charset", encoding);
125+
headers.forEach(con::addRequestProperty);
85126
String enc = con.getContentEncoding();
86127
if (enc == null) {
87-
enc = encoding;
128+
enc = headers.get(ENCODING_HEADER);
88129
}
89130
getReceiver().process(new InputStreamReader(con.getInputStream(), enc));
90131
}
91132
catch (final IOException e) {
92133
throw new MetafactureException(e);
93134
}
94135
}
136+
95137
}

0 commit comments

Comments
 (0)