|
33 | 33 | * Opens a {@link URLConnection} and passes a reader to the receiver.
|
34 | 34 | *
|
35 | 35 | * @author Christoph Böhme
|
| 36 | + * @author Jan Schnasse |
36 | 37 | *
|
37 | 38 | */
|
38 |
| -@Description("Opens a http resource.") |
| 39 | +@Description("Opens a http resource. Supports the setting of Accept and Accept-Charset as http header fields.") |
39 | 40 | @In(String.class)
|
40 | 41 | @Out(java.io.Reader.class)
|
41 | 42 | public final class HttpOpener
|
42 | 43 | extends DefaultObjectPipe<String, ObjectReceiver<Reader>> implements Opener {
|
| 44 | + private String encoding = "UTF-8"; |
| 45 | + private String accept = "*/*"; |
43 | 46 |
|
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. |
51 | 49 | */
|
52 |
| - public String getDefaultEncoding() { |
53 |
| - return defaultEncoding; |
| 50 | + public void setAccept(final String accept) { |
| 51 | + this.accept = accept; |
54 | 52 | }
|
55 | 53 |
|
56 | 54 | /**
|
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. |
61 | 57 | */
|
62 |
| - public void setDefaultEncoding(final String defaultEncoding) { |
63 |
| - this.defaultEncoding = defaultEncoding; |
| 58 | + public void setEncoding(final String encoding) { |
| 59 | + this.encoding = encoding; |
64 | 60 | }
|
65 | 61 |
|
66 | 62 | @Override
|
67 | 63 | public void process(final String urlStr) {
|
68 | 64 | try {
|
69 | 65 | final URL url = new URL(urlStr);
|
70 | 66 | final URLConnection con = url.openConnection();
|
| 67 | + con.addRequestProperty("Accept", accept); |
| 68 | + con.addRequestProperty("Accept-Charset", encoding); |
71 | 69 | String enc = con.getContentEncoding();
|
72 | 70 | if (enc == null) {
|
73 |
| - enc = defaultEncoding; |
| 71 | + enc = encoding; |
74 | 72 | }
|
75 | 73 | getReceiver().process(new InputStreamReader(con.getInputStream(), enc));
|
76 | 74 | } catch (IOException e) {
|
|
0 commit comments