Skip to content

Commit fe7277c

Browse files
committed
Enable generic header support in HttpOpener. (#456)
1 parent 45da349 commit fe7277c

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.io.Reader;
3030
import java.net.URL;
3131
import java.net.URLConnection;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.regex.Pattern;
3235

3336
/**
3437
* Opens a {@link URLConnection} and passes a reader to the receiver.
@@ -42,13 +45,18 @@
4245
@FluxCommand("open-http")
4346
public final class HttpOpener extends DefaultObjectPipe<String, ObjectReceiver<Reader>> {
4447

45-
private String encoding = "UTF-8";
46-
private String accept = "*/*";
48+
private static final Pattern HEADER_PATTERN = Pattern.compile(":\\s*");
49+
50+
private static final String ENCODING_HEADER = "accept-charset";
51+
52+
private final Map<String, String> headers = new HashMap<>();
4753

4854
/**
4955
* Creates an instance of {@link HttpOpener}.
5056
*/
5157
public HttpOpener() {
58+
setAccept("UTF-8");
59+
setEncoding("*/*");
5260
}
5361

5462
/**
@@ -59,37 +67,62 @@ public HttpOpener() {
5967
* @param accept mime-type to use for the HTTP accept header
6068
*/
6169
public void setAccept(final String accept) {
62-
this.accept = accept;
70+
setHeader("accept", accept);
6371
}
6472

6573
/**
6674
* Sets the preferred encoding of the HTTP response. This value is in the
6775
* 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
76+
* HTTP resonse if it does not specify an encoding. The default value for
6977
* the encoding is UTF-8.
7078
*
7179
* @param encoding name of the encoding used for the accept-charset HTTP
7280
* header
7381
*/
7482
public void setEncoding(final String encoding) {
75-
this.encoding = encoding;
83+
setHeader(ENCODING_HEADER, encoding);
84+
}
85+
86+
/**
87+
* Sets a request property.
88+
*
89+
* @param header request property line
90+
*/
91+
public void setHeader(final String header) {
92+
final String[] parts = HEADER_PATTERN.split(header, 2);
93+
if (parts.length == 2) {
94+
setHeader(parts[0], parts[1]);
95+
}
96+
else {
97+
throw new IllegalArgumentException("Invalid header: " + header);
98+
}
99+
}
100+
101+
/**
102+
* Sets a request property.
103+
*
104+
* @param key request property key
105+
* @param value request property value
106+
*/
107+
public void setHeader(final String key, final String value) {
108+
headers.put(key.toLowerCase(), value);
76109
}
77110

78111
@Override
79112
public void process(final String urlStr) {
80113
try {
81114
final URL url = new URL(urlStr);
82115
final URLConnection con = url.openConnection();
83-
con.addRequestProperty("Accept", accept);
84-
con.addRequestProperty("Accept-Charset", encoding);
116+
headers.forEach(con::addRequestProperty);
85117
String enc = con.getContentEncoding();
86118
if (enc == null) {
87-
enc = encoding;
119+
enc = headers.get(ENCODING_HEADER);
88120
}
89121
getReceiver().process(new InputStreamReader(con.getInputStream(), enc));
90122
}
91123
catch (final IOException e) {
92124
throw new MetafactureException(e);
93125
}
94126
}
127+
95128
}

0 commit comments

Comments
 (0)