Skip to content

Commit 9be4ec0

Browse files
committed
Refinement of HttpOpener
- Support of http headers Accept and Accept-Charset - Example in Flux: http-opener(accept="text/rdf+xml", encoding="utf-8") - Defaults to accept="*/*" and encoding="utf-8" - Encoding is not only used for stream encoding, but also in the http request
1 parent d78aca1 commit 9be4ec0

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/main/java/org/culturegraph/mf/stream/source/HttpOpener.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,42 @@
3333
* Opens a {@link URLConnection} and passes a reader to the receiver.
3434
*
3535
* @author Christoph Böhme
36+
* @author Jan Schnasse
3637
*
3738
*/
38-
@Description("Opens a http resource.")
39+
@Description("Opens a http resource. Supports the setting of Accept and Accept-Charset as http header fields.")
3940
@In(String.class)
4041
@Out(java.io.Reader.class)
4142
public final class HttpOpener
4243
extends DefaultObjectPipe<String, ObjectReceiver<Reader>> implements Opener {
44+
private String encoding = "UTF-8";
45+
private String accept = "*/*";
4346

44-
private String defaultEncoding = "UTF-8";
45-
46-
/**
47-
* Returns the default encoding used when no encoding is
48-
* provided by the server. The default setting is UTF-8.
49-
*
50-
* @return current default setting
47+
/**
48+
* @param accept The accept header in the form type/subtype, e.g. text/plain.
5149
*/
52-
public String getDefaultEncoding() {
53-
return defaultEncoding;
50+
public void setAccept(final String accept) {
51+
this.accept = accept;
5452
}
5553

5654
/**
57-
* Sets the default encoding to use when no encoding is
58-
* provided by the server. The default setting is UTF-8.
59-
*
60-
* @param defaultEncoding new default encoding
55+
* @param encoding The encoding is used to encode the output and is passed
56+
* as Accept-Charset to the http connection.
6157
*/
62-
public void setDefaultEncoding(final String defaultEncoding) {
63-
this.defaultEncoding = defaultEncoding;
58+
public void setEncoding(final String encoding) {
59+
this.encoding = encoding;
6460
}
6561

6662
@Override
6763
public void process(final String urlStr) {
6864
try {
6965
final URL url = new URL(urlStr);
7066
final URLConnection con = url.openConnection();
67+
con.addRequestProperty("Accept", accept);
68+
con.addRequestProperty("Accept-Charset", encoding);
7169
String enc = con.getContentEncoding();
7270
if (enc == null) {
73-
enc = defaultEncoding;
71+
enc = encoding;
7472
}
7573
getReceiver().process(new InputStreamReader(con.getInputStream(), enc));
7674
} catch (IOException e) {

0 commit comments

Comments
 (0)