You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/11/06 10:58:39 UTC
[camel] 05/12: Add connection manager for http requests and improve
yql config
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 709a67ce0d183f7a2b97433b207dacb78d7b1d55
Author: Carl-Philipp Harmant <cp...@gmail.com>
AuthorDate: Tue Oct 31 23:35:32 2017 -0500
Add connection manager for http requests and improve yql config
---
.../camel-yql/src/main/docs/yql-component.adoc | 22 +++++++---
.../apache/camel/component/yql/YqlComponent.java | 3 ++
.../apache/camel/component/yql/YqlProducer.java | 5 +++
.../yql/configuration/YqlConfiguration.java | 34 +++++++++++----
.../component/yql/exception/YqlHttpException.java | 50 ++++++++++++++++++++++
5 files changed, 100 insertions(+), 14 deletions(-)
diff --git a/components/camel-yql/src/main/docs/yql-component.adoc b/components/camel-yql/src/main/docs/yql-component.adoc
index e7b0951..efc13cb 100644
--- a/components/camel-yql/src/main/docs/yql-component.adoc
+++ b/components/camel-yql/src/main/docs/yql-component.adoc
@@ -29,7 +29,16 @@ Where *query* represents the YQL query.
### Options
// component options: START
-The Yahoo Query Language component has no options.
+The Yahoo Query Language component supports 2 options which are listed below.
+
+
+
+[width="100%",cols="2,5,^1,2",options="header"]
+|===
+| Name | Description | Default | Type
+| *connectionManager* (producer) | Set the connection manager. | | HttpClientConnection Manager
+| *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean
+|===
// component options: END
// endpoint options: START
@@ -46,17 +55,18 @@ with the following path and query parameters:
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *query* | *Required* Set the YQL query | | String
+| *query* | *Required* The YQL query to be sent. | | String
|===
-==== Query Parameters (4 parameters):
+==== Query Parameters (5 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *callback* (producer) | Set the callback function | | String
-| *diagnostics* (producer) | Set if diagnostics should be included in the query | false | boolean
-| *format* (producer) | Set the YQL format xml or json | | String
+| *callback* (producer) | If specified the option will be included in the HTTP request to YQL. If the format is json then the response will contain a JSONP callback method. If the format is xml then the response will contain a JSONP-X callback method. More information: https://developer.yahoo.com/yql/guide/response.html | | String
+| *diagnostics* (producer) | If true the option will be included in the HTTP request to YQL and the response will contain some diagnostics data. | false | boolean
+| *format* (producer) | The expected format. Can only be json or xml. | json | String
+| *throwExceptionOnFailure* (producer) | Option to disable throwing the YqlHttpException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code. | true | boolean
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
index 02b778f..fe02676 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
@@ -50,6 +50,9 @@ public class YqlComponent extends DefaultComponent {
}
}
+ /**
+ * Set the connection manager.
+ */
public void setConnectionManager(final HttpClientConnectionManager connectionManager){
this.localConnectionManager = connectionManager;
}
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
index c429c85..f32a7a7 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
@@ -20,7 +20,9 @@ import org.apache.camel.Exchange;
import org.apache.camel.component.yql.client.YqlClient;
import org.apache.camel.component.yql.client.YqlResponse;
import org.apache.camel.component.yql.configuration.YqlConfiguration;
+import org.apache.camel.component.yql.exception.YqlHttpException;
import org.apache.camel.impl.DefaultProducer;
+import org.apache.http.HttpStatus;
/**
* A Producer that send messages to YQL
@@ -48,6 +50,9 @@ public class YqlProducer extends DefaultProducer {
configuration.isDiagnostics(),
configuration.getCallback()
);
+ if (configuration.isThrowExceptionOnFailure() && yqlResponse.getStatus() != HttpStatus.SC_OK) {
+ throw YqlHttpException.failedWith(yqlResponse.getStatus(), yqlResponse.getBody(), yqlResponse.getHttpRequest());
+ }
exchange.getIn().setHeader(CAMEL_YQL_HTTP_STATUS, yqlResponse.getStatus());
exchange.getIn().setHeader(CAMEL_YQL_HTTP_REQUEST, yqlResponse.getHttpRequest());
exchange.getIn().setBody(yqlResponse.getBody());
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/configuration/YqlConfiguration.java b/components/camel-yql/src/main/java/org/apache/camel/component/yql/configuration/YqlConfiguration.java
index a3b1854..73ff453 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/configuration/YqlConfiguration.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/configuration/YqlConfiguration.java
@@ -24,25 +24,30 @@ import org.apache.camel.spi.UriPath;
@UriParams
public class YqlConfiguration {
- @UriPath
+ @UriPath(label="producer", description = "The YQL query to be sent.")
@Metadata(required = "true")
private String query;
- @UriParam
+ @UriParam(label="producer", enums = "json,xml", defaultValue = "json", description = "The expected format. Can only be json or xml.")
private String format = "json";
- @UriParam
+ @UriParam(label="producer", defaultValue = "false", description = "If true, the option will be included in the HTTP request to YQL and the response will contain some diagnostics data.")
private boolean diagnostics = false;
- @UriParam
+ @UriParam(label="producer", description = "If specified, the option will be included in the HTTP request to YQL. If the format is json, then the response will contain a JSONP callback method. "
+ + "If the format is xml, then the response will contain a JSONP-X callback method. More information: https://developer.yahoo.com/yql/guide/response.html")
private String callback = "";
+ @UriParam(label = "producer", defaultValue = "true", description = "Option to disable throwing the YqlHttpException in case of failed responses from the remote server. "
+ + "This allows you to get all responses regardless of the HTTP status code.")
+ private boolean throwExceptionOnFailure = true;
+
public String getQuery() {
return query;
}
/**
- * Set the YQL query
+ * The YQL query to be sent.
*/
public void setQuery(final String query) {
this.query = query;
@@ -53,7 +58,7 @@ public class YqlConfiguration {
}
/**
- * Set the YQL format, xml or json
+ * The expected format. Can only be json or xml.
*/
public void setFormat(final String format) {
this.format = format;
@@ -64,7 +69,7 @@ public class YqlConfiguration {
}
/**
- * Set if diagnostics should be included in the query
+ * If true, the option will be included in the HTTP request to YQL and the response will contain some diagnostics data.
*/
public void setDiagnostics(final boolean diagnostics) {
this.diagnostics = diagnostics;
@@ -75,9 +80,22 @@ public class YqlConfiguration {
}
/**
- * Set the callback function
+ * If specified, the option will be included in the HTTP request to YQL. If the format is json, then the response will contain a JSONP callback method.
+ * If the format is xml, then the response will contain a JSONP-X callback method. More information: https://developer.yahoo.com/yql/guide/response.html
*/
public void setCallback(final String callback) {
this.callback = callback;
}
+
+ public boolean isThrowExceptionOnFailure() {
+ return throwExceptionOnFailure;
+ }
+
+ /**
+ * Option to disable throwing the YqlHttpException in case of failed responses from the remote server.
+ * This allows you to get all responses regardless of the HTTP status code.
+ */
+ public void setThrowExceptionOnFailure(final boolean throwExceptionOnFailure) {
+ this.throwExceptionOnFailure = throwExceptionOnFailure;
+ }
}
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/exception/YqlHttpException.java b/components/camel-yql/src/main/java/org/apache/camel/component/yql/exception/YqlHttpException.java
new file mode 100644
index 0000000..3eea124
--- /dev/null
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/exception/YqlHttpException.java
@@ -0,0 +1,50 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.yql.exception;
+
+/**
+ * Signal a non 200 HTTP response
+ */
+public class YqlHttpException extends YqlException {
+
+ private final int httpStatus;
+ private final String body;
+ private final String originalRequest;
+
+ private YqlHttpException(final int httpStatus, final String body, final String originalRequest) {
+ super("HTTP request to YQL failed");
+ this.httpStatus = httpStatus;
+ this.body = body;
+ this.originalRequest = originalRequest;
+ }
+
+ public static YqlHttpException failedWith(final int httpStatus, final String body, final String originalRequest) {
+ return new YqlHttpException(httpStatus, body, originalRequest);
+ }
+
+ public int getHttpStatus() {
+ return httpStatus;
+ }
+
+ public String body() {
+ return body;
+ }
+
+ public String getOriginalRequest() {
+ return originalRequest;
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <co...@camel.apache.org>.