50
50
@ FluxCommand ("open-http" )
51
51
public final class HttpOpener extends DefaultObjectPipe <String , ObjectReceiver <Reader >> {
52
52
53
- private static final Pattern HEADER_FIELD_SEPARATOR = Pattern .compile ("\n " );
54
- private static final Pattern HEADER_VALUE_SEPARATOR = Pattern .compile (":" );
53
+ public static final String ACCEPT_DEFAULT = "*/*" ;
54
+ public static final String ACCEPT_HEADER = "accept" ;
55
+ public static final String CONTENT_TYPE_HEADER = "content-type" ;
56
+ public static final String DEFAULT_PREFIX = "ERROR: " ;
57
+ public static final String ENCODING_DEFAULT = "UTF-8" ;
58
+ public static final String ENCODING_HEADER = "accept-charset" ;
59
+ public static final String INPUT_DESIGNATOR = "@-" ;
55
60
56
- private static final String ACCEPT_DEFAULT = "*/*" ;
57
- private static final String ACCEPT_HEADER = "accept" ;
58
- private static final String CONTENT_TYPE_HEADER = "content-type" ;
59
- private static final String DEFAULT_PREFIX = "ERROR: " ;
60
- private static final String ENCODING_DEFAULT = "UTF-8" ;
61
- private static final String ENCODING_HEADER = "accept-charset" ;
62
- private static final String INPUT_DESIGNATOR = "@-" ;
61
+ public static final String DEFAULT_METHOD_NAME = "GET" ;
62
+ public static final Method DEFAULT_METHOD = Method .valueOf (DEFAULT_METHOD_NAME );
63
63
64
- private static final Method DEFAULT_METHOD = Method .GET ;
64
+ public static final String HEADER_FIELD_SEPARATOR = "\n " ;
65
+ public static final String HEADER_VALUE_SEPARATOR = ":" ;
66
+
67
+ private static final Pattern HEADER_FIELD_SEPARATOR_PATTERN = Pattern .compile (HEADER_FIELD_SEPARATOR );
68
+ private static final Pattern HEADER_VALUE_SEPARATOR_PATTERN = Pattern .compile (HEADER_VALUE_SEPARATOR );
65
69
66
70
private static final int SUCCESS_CODE_MIN = 200 ;
67
71
private static final int SUCCESS_CODE_MAX = 399 ;
@@ -124,18 +128,27 @@ public HttpOpener() {
124
128
}
125
129
126
130
/**
127
- * Sets the HTTP accept header value. This is a mime-type such as text/plain
128
- * or text/html. The default value of the accept is */* which means
129
- * any mime-type.
131
+ * Sets the HTTP {@value ACCEPT_HEADER} header value. This is a MIME type
132
+ * such as {@code text/plain} or {@code application/json}. The default
133
+ * value for the accept header is {@value ACCEPT_DEFAULT} which means
134
+ * any MIME type.
130
135
*
131
- * @param accept mime- type to use for the HTTP accept header
136
+ * @param accept MIME type to use for the HTTP accept header
132
137
*/
133
138
public void setAccept (final String accept ) {
134
139
setHeader (ACCEPT_HEADER , accept );
135
140
}
136
141
137
142
/**
138
- * Sets the HTTP request body.
143
+ * Sets the HTTP request body. The default value for the request body is
144
+ * {@value INPUT_DESIGNATOR} <i>if the {@link #setMethod(Method) request
145
+ * method} accepts a request body</i>, which means it will use the {@link
146
+ * #process(String) input data} data as request body <i>if the input has
147
+ * not already been used</i>; otherwise, no request body will be set by
148
+ * default.
149
+ *
150
+ * <p>If a request body has been set, but the request method does not
151
+ * accept a body, the method <i>may</i> be changed to {@code POST}.
139
152
*
140
153
* @param body the request body
141
154
*/
@@ -144,20 +157,20 @@ public void setBody(final String body) {
144
157
}
145
158
146
159
/**
147
- * Sets the HTTP content type header. This is a mime-type such as text/plain,
148
- * text/html. The default is application/json.
160
+ * Sets the HTTP {@value CONTENT_TYPE_HEADER} header value . This is a
161
+ * MIME type such as {@code text/plain} or {@code application/json} .
149
162
*
150
- * @param contentType mime- type to use for the HTTP contentType header
163
+ * @param contentType MIME type to use for the HTTP content-type header
151
164
*/
152
165
public void setContentType (final String contentType ) {
153
166
setHeader (CONTENT_TYPE_HEADER , contentType );
154
167
}
155
168
156
169
/**
157
- * Sets the preferred encoding of the HTTP response . This value is in the
158
- * accept-charset header. Additonally, the encoding is used for reading the
159
- * HTTP resonse if it does not specify an encoding. The default value for
160
- * the encoding is UTF-8 .
170
+ * Sets the HTTP {@value ENCODING_HEADER} header value . This is the
171
+ * preferred encoding for the HTTP response. Additionally, the encoding
172
+ * is used for reading the HTTP response if it does not specify a content
173
+ * encoding. The default for the encoding is {@value ENCODING_DEFAULT} .
161
174
*
162
175
* @param encoding name of the encoding used for the accept-charset HTTP
163
176
* header
@@ -167,7 +180,8 @@ public void setEncoding(final String encoding) {
167
180
}
168
181
169
182
/**
170
- * Sets the error prefix.
183
+ * Sets the error prefix. The default error prefix is
184
+ * {@value DEFAULT_PREFIX}.
171
185
*
172
186
* @param errorPrefix the error prefix
173
187
*/
@@ -176,14 +190,18 @@ public void setErrorPrefix(final String errorPrefix) {
176
190
}
177
191
178
192
/**
179
- * Sets a request property, or multiple request properties separated by
180
- * {@code \n}.
193
+ * Sets a request property (header), or multiple request properties
194
+ * separated by {@value HEADER_FIELD_SEPARATOR}. Header name and value
195
+ * are separated by {@value HEADER_VALUE_SEPARATOR}. The header name is
196
+ * case-insensitive.
181
197
*
182
198
* @param header request property line
199
+ *
200
+ * @see #setHeader(String, String)
183
201
*/
184
202
public void setHeader (final String header ) {
185
- Arrays .stream (HEADER_FIELD_SEPARATOR .split (header )).forEach (h -> {
186
- final String [] parts = HEADER_VALUE_SEPARATOR .split (h , 2 );
203
+ Arrays .stream (HEADER_FIELD_SEPARATOR_PATTERN .split (header )).forEach (h -> {
204
+ final String [] parts = HEADER_VALUE_SEPARATOR_PATTERN .split (h , 2 );
187
205
if (parts .length == 2 ) {
188
206
setHeader (parts [0 ], parts [1 ].trim ());
189
207
}
@@ -194,7 +212,7 @@ public void setHeader(final String header) {
194
212
}
195
213
196
214
/**
197
- * Sets a request property.
215
+ * Sets a request property (header). The header name is case-insensitive .
198
216
*
199
217
* @param key request property key
200
218
* @param value request property value
@@ -204,7 +222,8 @@ public void setHeader(final String key, final String value) {
204
222
}
205
223
206
224
/**
207
- * Sets the HTTP request method.
225
+ * Sets the HTTP request method. The default request method is
226
+ * {@value DEFAULT_METHOD_NAME}.
208
227
*
209
228
* @param method the request method
210
229
*/
@@ -213,7 +232,9 @@ public void setMethod(final Method method) {
213
232
}
214
233
215
234
/**
216
- * Sets the HTTP request URL.
235
+ * Sets the HTTP request URL. The default value for the request URL is
236
+ * {@value INPUT_DESIGNATOR}, which means it will use the {@link
237
+ * #process(String) input data} as request URL.
217
238
*
218
239
* @param url the request URL
219
240
*/
0 commit comments