You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dh...@apache.org on 2015/03/02 20:05:00 UTC

camel git commit: CAMEL-8426: fixed invalid client id handling in Box component, added integration test for same

Repository: camel
Updated Branches:
  refs/heads/master 69fd8434b -> ad35c0bbd


CAMEL-8426: fixed invalid client id handling in Box component, added integration test for same


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ad35c0bb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ad35c0bb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ad35c0bb

Branch: refs/heads/master
Commit: ad35c0bbd1a8851663d151187f35f5f7f4ce56b8
Parents: 69fd843
Author: Dhiraj Bokde <dh...@yahoo.com>
Authored: Mon Mar 2 11:04:54 2015 -0800
Committer: Dhiraj Bokde <dh...@yahoo.com>
Committed: Mon Mar 2 11:04:54 2015 -0800

----------------------------------------------------------------------
 .../component/box/internal/LoginAuthFlowUI.java | 25 ++++++
 .../box/InvalidClientIdIntegrationTest.java     | 83 ++++++++++++++++++++
 2 files changed, 108 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ad35c0bb/components/camel-box/src/main/java/org/apache/camel/component/box/internal/LoginAuthFlowUI.java
----------------------------------------------------------------------
diff --git a/components/camel-box/src/main/java/org/apache/camel/component/box/internal/LoginAuthFlowUI.java b/components/camel-box/src/main/java/org/apache/camel/component/box/internal/LoginAuthFlowUI.java
index babda28..d5d2d33 100644
--- a/components/camel-box/src/main/java/org/apache/camel/component/box/internal/LoginAuthFlowUI.java
+++ b/components/camel-box/src/main/java/org/apache/camel/component/box/internal/LoginAuthFlowUI.java
@@ -40,15 +40,21 @@ import com.gargoylesoftware.htmlunit.Page;
 import com.gargoylesoftware.htmlunit.ProxyConfig;
 import com.gargoylesoftware.htmlunit.WebClient;
 import com.gargoylesoftware.htmlunit.WebClientOptions;
+import com.gargoylesoftware.htmlunit.WebRequest;
+import com.gargoylesoftware.htmlunit.WebResponse;
 import com.gargoylesoftware.htmlunit.html.HtmlButton;
+import com.gargoylesoftware.htmlunit.html.HtmlDivision;
 import com.gargoylesoftware.htmlunit.html.HtmlForm;
 import com.gargoylesoftware.htmlunit.html.HtmlPage;
 import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
 import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
+import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper;
+
 import org.apache.camel.component.box.BoxConfiguration;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.jsse.SSLContextParameters;
+import org.apache.http.HttpHeaders;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpStatus;
 import org.apache.http.conn.params.ConnRoutePNames;
@@ -95,6 +101,15 @@ public final class LoginAuthFlowUI implements IAuthFlowUI {
             throw ObjectHelper.wrapRuntimeCamelException(e);
         }
 
+        // disable default gzip compression, as htmlunit does not negotiate pages sent with no compression
+        new WebConnectionWrapper(webClient) {
+            @Override
+            public WebResponse getResponse(WebRequest request) throws IOException {
+                request.setAdditionalHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
+                return super.getResponse(request);
+            }
+        };
+
         // add HTTP proxy if set
         final Map<String, Object> httpParams = configuration.getHttpParams();
         if (httpParams != null && httpParams.get(ConnRoutePNames.DEFAULT_PROXY) != null) {
@@ -113,6 +128,16 @@ public final class LoginAuthFlowUI implements IAuthFlowUI {
             viewData.setOptionalState(String.valueOf(csrfId));
             final HtmlPage authPage = webClient.getPage(viewData.buildUrl().toString());
 
+            // look for <div role="error_message">
+            final HtmlDivision div = authPage.getFirstByXPath("//div[contains(concat(' ', @class, ' '), ' error_message ')]");
+            if (div != null) {
+                final String errorMessage = div.getTextContent()
+                    .replaceAll("\\s+", " ")
+                    .replaceAll(" Show Error Details", ":")
+                    .trim();
+                throw new IllegalArgumentException("Error authorizing application: " + errorMessage);
+            }
+
             // submit login credentials
             final HtmlForm loginForm = authPage.getFormByName("login_form");
             final HtmlTextInput login = loginForm.getInputByName("login");

http://git-wip-us.apache.org/repos/asf/camel/blob/ad35c0bb/components/camel-box/src/test/java/org/apache/camel/component/box/InvalidClientIdIntegrationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-box/src/test/java/org/apache/camel/component/box/InvalidClientIdIntegrationTest.java b/components/camel-box/src/test/java/org/apache/camel/component/box/InvalidClientIdIntegrationTest.java
new file mode 100644
index 0000000..8216b5a
--- /dev/null
+++ b/components/camel-box/src/test/java/org/apache/camel/component/box/InvalidClientIdIntegrationTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.box;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.box.internal.BoxApiCollection;
+import org.apache.camel.component.box.internal.IBoxSharedItemsManagerApiMethod;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test Box component configuration.
+ */
+public class InvalidClientIdIntegrationTest extends AbstractBoxTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(InvalidClientIdIntegrationTest.class);
+    private static final String PATH_PREFIX = BoxApiCollection.getCollection().getApiName(IBoxSharedItemsManagerApiMethod.class).getName();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        final CamelContext camelContext = super.createCamelContext();
+
+        // set client_id to an invalid value
+        final BoxConfiguration configuration = camelContext.getComponent("box", BoxComponent.class).getConfiguration();
+        configuration.setClientId("bad_client_id");
+
+        // also remove auth secure storage to avoid loading a stored token
+        configuration.setAuthSecureStorage(null);
+
+        return camelContext;
+    }
+
+    @Override
+    protected void startCamelContext() throws Exception {
+        // should throw an exception on start
+        try {
+            super.startCamelContext();
+            fail("Invalid client id MUST cause an IllegalArgumentException on startup");
+        } catch (FailedToCreateRouteException e) {
+            Throwable t = e;
+            while (t.getCause() != null && t.getCause() != e) {
+                t = t.getCause();
+            }
+            assertNotNull("root cause exception", t);
+            assertEquals("illegal argument exception", IllegalArgumentException.class, t.getClass());
+            LOG.debug("Caught expected exception {}", t.getMessage());
+        }
+    }
+
+    @Test
+    public void testInvalidClientId() throws Exception {
+        // do nothing
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // dummy route to force box component startup
+                from("direct://GETSHAREDITEM")
+                    .to("box://" + PATH_PREFIX + "/getSharedItem?inBody=defaultRequest");
+            }
+        };
+    }
+}