Permalink
Browse files

allow configuration options for NekoHtml for all HTTP client actions

  • Loading branch information...
1 parent 7c860f9 commit 879c7e0ec0699662b78fdcc3a2bc6488ac5df464 @opax opax committed May 26, 2014
@@ -111,6 +111,7 @@
final static String NAMESPACE_URI = HTTPClientModule.NAMESPACE_URI;
final static String PREFIX = HTTPClientModule.PREFIX;
final static String HTTP_MODULE_PERSISTENT_STATE = HTTPClientModule.HTTP_MODULE_PERSISTENT_STATE;
+ final static String HTTP_MODULE_PERSISTENT_OPTIONS = HTTPClientModule.HTTP_MODULE_PERSISTENT_OPTIONS;
final static String HTTP_EXCEPTION_STATUS_CODE = "500";
@@ -153,21 +154,29 @@ protected void setHeaders(final HttpMethod method, final Node headers) throws XP
/**
* Performs a HTTP Request.
*
- * @param context The context of the calling XQuery
- * @param method The HTTP method for the request
- * @param persistState If true existing HTTP state (cookies, credentials, etc) are re-used and athe state is persisted for future HTTP Requests
+ * @param context The context of the calling XQuery
+ * @param method The HTTP method for the request
+ * @param persistState If true existing HTTP state (cookies, credentials, etc) are re-used and athe state is persisted for future HTTP Requests
+ * @param parserFeatures Map of NekoHtml parser features to be used for the HTML parser. If null, the session-wide options will be used.
+ * @param parserProperties Map of NekoHtml parser properties to be used for the HTML parser. If null, the session-wide options will be used.
*
* @return DOCUMENT ME!
*
* @throws IOException
* @throws XPathException
*/
- protected Sequence doRequest(final XQueryContext context, final HttpMethod method, final boolean persistState, final Map<String, Boolean> parserFeatures, final Map<String, String> parserProperties) throws IOException, XPathException {
+ protected Sequence doRequest(final XQueryContext context, final HttpMethod method, boolean persistState, Map<String, Boolean> parserFeatures, Map<String, String> parserProperties) throws IOException, XPathException {
Sequence encodedResponse = null;
final HttpClient http = new HttpClient();
+ FeaturesAndProperties defaultFeaturesAndProperties = (FeaturesAndProperties) context.getXQueryContextVar(HTTP_MODULE_PERSISTENT_OPTIONS);
+ if (defaultFeaturesAndProperties != null) {
+ if (parserFeatures == null) parserFeatures = defaultFeaturesAndProperties.getFeatures();
+ if (parserProperties == null) parserProperties = defaultFeaturesAndProperties.getProperties();
+ }
+
//execute the request
try {
@@ -44,7 +44,8 @@
public final static String INCLUSION_DATE = "2007-09-06";
public final static String RELEASED_IN_VERSION = "eXist-1.2";
- public final static String HTTP_MODULE_PERSISTENT_STATE = "_eXist_httpclient_module_persistent_state";
+ public final static String HTTP_MODULE_PERSISTENT_STATE = "_eXist_httpclient_module_persistent_state";
+ public final static String HTTP_MODULE_PERSISTENT_OPTIONS = "_eXist_httpclient_module_persistent_options";
private final static FunctionDef[] functions = {
@@ -57,7 +58,8 @@
new FunctionDef( POSTFunction.signatures[1], POSTFunction.class ),
new FunctionDef( HEADFunction.signature, HEADFunction.class ),
new FunctionDef( OPTIONSFunction.signature, OPTIONSFunction.class ),
- new FunctionDef( ClearFunction.signatures[0], ClearFunction.class )
+ new FunctionDef( ClearFunction.signatures[0], ClearFunction.class ),
+ new FunctionDef( SetOptionsFunction.signatures[0], SetOptionsFunction.class)
};
@@ -0,0 +1,66 @@
+package org.exist.xquery.modules.httpclient;
+
+import org.apache.log4j.Logger;
+
+import org.exist.dom.QName;
+
+import org.exist.xquery.Cardinality;
+import org.exist.xquery.FunctionSignature;
+import org.exist.xquery.XPathException;
+import org.exist.xquery.XQueryContext;
+import org.exist.xquery.value.NodeValue;
+import org.exist.xquery.value.Sequence;
+import org.exist.xquery.value.SequenceType;
+import org.exist.xquery.value.Type;
+
+import org.apache.commons.httpclient.HttpState;
+
+/**
+ * Set default options for the NekoHtml parser for all subsequent requests in
+ * the same XQuery context
+ *
+ * @see <a href='http://nekohtml.sourceforge.net/settings.html'>NekoHtml Parser Settings</a>
+ *
+ * @author O.Pax <o.pax@web.de>
+ * @version 2.1
+ * @serial 20140526
+ */
+public class SetOptionsFunction extends BaseHTTPClientFunction {
+
+ protected static final Logger logger = Logger.getLogger( SetOptionsFunction.class );
+
+ public final static FunctionSignature[] signatures = {
+ new FunctionSignature(
+ new QName( "set-parser-options", NAMESPACE_URI, PREFIX ),
+ "Sets default options for the HTML parser for all subsequent requests in this session",
+ new SequenceType[] {
+ OPTIONS_PARAM
+ },
+ new SequenceType( Type.ITEM, Cardinality.EMPTY )
+ )
+ };
+
+
+ public SetOptionsFunction(XQueryContext context, FunctionSignature signature)
+ {
+ super( context, signature );
+ }
+
+
+ public Sequence eval( Sequence[] args, Sequence contextSequence ) throws XPathException
+ {
+ if( isCalledAs( "set-parser-options" ) ) {
+
+ FeaturesAndProperties featuresAndProperties = null;
+
+ if (args.length > 0 && !args[0].isEmpty()) {
+ featuresAndProperties = getParserFeaturesAndProperties(((NodeValue)args[0].itemAt(0)).getNode());
+ }
+
+ context.setXQueryContextVar( HTTP_MODULE_PERSISTENT_OPTIONS, featuresAndProperties );
+ }
+
+ return( Sequence.EMPTY_SEQUENCE );
+ }
+
+}

0 comments on commit 879c7e0

Please sign in to comment.