29
29
import java .io .Reader ;
30
30
import java .net .URL ;
31
31
import java .net .URLConnection ;
32
+ import java .util .HashMap ;
33
+ import java .util .Map ;
34
+ import java .util .regex .Pattern ;
32
35
33
36
/**
34
37
* Opens a {@link URLConnection} and passes a reader to the receiver.
42
45
@ FluxCommand ("open-http" )
43
46
public final class HttpOpener extends DefaultObjectPipe <String , ObjectReceiver <Reader >> {
44
47
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 <>();
47
53
48
54
/**
49
55
* Creates an instance of {@link HttpOpener}.
50
56
*/
51
57
public HttpOpener () {
58
+ setAccept ("UTF-8" );
59
+ setEncoding ("*/*" );
52
60
}
53
61
54
62
/**
@@ -59,37 +67,62 @@ public HttpOpener() {
59
67
* @param accept mime-type to use for the HTTP accept header
60
68
*/
61
69
public void setAccept (final String accept ) {
62
- this . accept = accept ;
70
+ setHeader ( " accept" , accept ) ;
63
71
}
64
72
65
73
/**
66
74
* Sets the preferred encoding of the HTTP response. This value is in the
67
75
* 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
69
77
* the encoding is UTF-8.
70
78
*
71
79
* @param encoding name of the encoding used for the accept-charset HTTP
72
80
* header
73
81
*/
74
82
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 );
76
109
}
77
110
78
111
@ Override
79
112
public void process (final String urlStr ) {
80
113
try {
81
114
final URL url = new URL (urlStr );
82
115
final URLConnection con = url .openConnection ();
83
- con .addRequestProperty ("Accept" , accept );
84
- con .addRequestProperty ("Accept-Charset" , encoding );
116
+ headers .forEach (con ::addRequestProperty );
85
117
String enc = con .getContentEncoding ();
86
118
if (enc == null ) {
87
- enc = encoding ;
119
+ enc = headers . get ( ENCODING_HEADER ) ;
88
120
}
89
121
getReceiver ().process (new InputStreamReader (con .getInputStream (), enc ));
90
122
}
91
123
catch (final IOException e ) {
92
124
throw new MetafactureException (e );
93
125
}
94
126
}
127
+
95
128
}
0 commit comments