You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2022/07/22 13:51:05 UTC

[sling-org-apache-sling-pipes] branch master updated (8c9dd0b -> f75979e)

This is an automated email from the ASF dual-hosted git repository.

npeltier pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git


    from 8c9dd0b  SLING-11284 reshuffle public & private packages
     new 3b6ebb1  @releng downgrade ca deps
     new f75979e  SLING-11484 enhance o.a.s.p.AbstractInputStreamPipe remote

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pom.xml                                            |  6 +-
 .../sling/pipes/AbstractInputStreamPipe.java       | 93 +++++++++++++++++++---
 .../pipes/internal/inputstream/JsonPipeTest.java   | 30 +++++++
 src/test/resources/simplelogger.properties         |  3 +-
 4 files changed, 115 insertions(+), 17 deletions(-)


[sling-org-apache-sling-pipes] 02/02: SLING-11484 enhance o.a.s.p.AbstractInputStreamPipe remote

Posted by np...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

npeltier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git

commit f75979ef43b483762bb3962b1dfe1ad1ff0c6765
Author: Nicolas Peltier <np...@apache.org>
AuthorDate: Fri Jul 22 14:34:09 2022 +0200

    SLING-11484 enhance o.a.s.p.AbstractInputStreamPipe remote
    
    - add form url encode post
    - add raw body post
    - add ability to add whatever headers
    - testing through json pipe tests
---
 .../sling/pipes/AbstractInputStreamPipe.java       | 93 +++++++++++++++++++---
 .../pipes/internal/inputstream/JsonPipeTest.java   | 30 +++++++
 src/test/resources/simplelogger.properties         |  3 +-
 3 files changed, 112 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/AbstractInputStreamPipe.java b/src/main/java/org/apache/sling/pipes/AbstractInputStreamPipe.java
index ca9c3da..c01c723 100644
--- a/src/main/java/org/apache/sling/pipes/AbstractInputStreamPipe.java
+++ b/src/main/java/org/apache/sling/pipes/AbstractInputStreamPipe.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.pipes;
 
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.Resource;
@@ -23,6 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.ByteArrayInputStream;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
@@ -30,8 +33,10 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 /**
  * Input Stream based pipe, coming from web, from request, resource tree, web
@@ -48,13 +53,21 @@ public abstract class AbstractInputStreamPipe extends BasePipe {
 
     private static final String BASIC_AUTH_BINDINGS = "basicAuth";
 
+    private static final String HEADER_PREFIX = "header_";
     private static final String AUTH_HEADER = "Authentication";
-
     private static final String BASIC_PREFIX = "Basic ";
     private static final String PN_URL_MODE = "url_mode";
     private static final String URL_MODE_FETCH = "FETCH";
     private static final String URL_MODE_AS_IS = "AS_IS";
 
+    private static final String METHOD_POST = "POST";
+
+    private static final String HEADER_TYPE ="Content-Type";
+
+    private static final String HEADER_LENGTH ="Content-Length";
+
+    private static final String QUERY_CHAR = "?";
+
     protected Object binding;
 
     InputStream is;
@@ -72,22 +85,76 @@ public abstract class AbstractInputStreamPipe extends BasePipe {
         return null;
     }
 
+    void addHeaders(URLConnection connection) {
+        connection.setRequestProperty( "charset", "utf-8");
+        Collection<String> headers = properties.keySet().stream()
+                .filter(k -> k.startsWith(HEADER_PREFIX))
+                .map(s -> StringUtils.substringAfter(s, HEADER_PREFIX))
+                .collect(Collectors.toList());
+        if (!headers.isEmpty()) {
+            for (String k : headers) {
+                String value = getBindings().instantiateExpression(properties.get(HEADER_PREFIX + k, String.class));
+                connection.setRequestProperty(k, value);
+            }
+            String basicAuth = (String)getBindings().getBindings().get(BASIC_AUTH_BINDINGS);
+            if (StringUtils.isNotBlank(basicAuth)) {
+                if (headers.contains(AUTH_HEADER)) {
+                    LOGGER.warn("both authentication header & basic auth are set, ignoring basic auth");
+                } else {
+                    String encoded = Base64.getEncoder().encodeToString(basicAuth.getBytes(StandardCharsets.UTF_8));
+                    connection.setRequestProperty(AUTH_HEADER, BASIC_PREFIX + encoded);
+                }
+            }
+        }
+    }
+
+    private URLConnection preparePost(String expr) throws IOException {
+        HttpURLConnection connection;
+        String url = expr;
+        String data = EMPTY;
+        byte[] postData;
+        if (expr.contains(QUERY_CHAR)) {
+            data = StringUtils.substringAfter(expr, QUERY_CHAR);
+            url = StringUtils.substringBefore(expr, QUERY_CHAR);
+        } else {
+            //we consider POST property value to be the request body
+            data = getBindings().instantiateExpression(properties.get(METHOD_POST, String.class));
+        }
+        connection = (HttpURLConnection) new URL(url).openConnection();
+        connection.setRequestMethod(METHOD_POST);
+        postData = data.getBytes(StandardCharsets.UTF_8);
+        int postDataLength = postData.length;
+        if (expr.contains(QUERY_CHAR)) {
+            connection.setRequestProperty(HEADER_TYPE, "application/x-www-form-urlencoded");
+        }
+        connection.setRequestProperty( HEADER_LENGTH, Integer.toString( postDataLength ));
+        connection.setDoOutput( true );
+        connection.setInstanceFollowRedirects( false );
+        addHeaders(connection);
+        try( DataOutputStream wr = new DataOutputStream( connection.getOutputStream())) {
+            wr.write( postData );
+        }
+        return connection;
+    }
+
     InputStream getInputStream() throws IOException {
         String expr = getExpr();
         if (expr.startsWith(REMOTE_START) && !properties.get(PN_URL_MODE, URL_MODE_FETCH).equalsIgnoreCase(URL_MODE_AS_IS)) {
-            //first look at
-            String urlExpression = getExpr();
-            if (StringUtils.isNotBlank(urlExpression)) {
-                URL url = new URL(urlExpression);
-                URLConnection urlConnection = url.openConnection();
-                String basicAuth = (String)getBindings().getBindings().get(BASIC_AUTH_BINDINGS);
-                if (StringUtils.isNotBlank(basicAuth)) {
-                    LOGGER.debug("Configuring basic authentication for {}", urlConnection);
-                    HttpURLConnection connection = (HttpURLConnection) urlConnection;
-                    String encoded = Base64.getEncoder().encodeToString(basicAuth.getBytes(StandardCharsets.UTF_8));
-                    connection.setRequestProperty(AUTH_HEADER, BASIC_PREFIX + encoded);
+            if (StringUtils.isNotBlank(expr)) {
+                boolean usePost = properties.containsKey(METHOD_POST);
+                LOGGER.debug("Accessing {} (POST={})", expr, usePost);
+                URLConnection urlConnection;
+                if (usePost) {
+                    if (isDryRun()) {
+                        LOGGER.debug("we won't execute a POST request in a dry run");
+                        return null;
+                    }
+                    urlConnection = preparePost(expr);
+                } else {
+                    URL url = new URL(expr);
+                    urlConnection = url.openConnection();
+                    addHeaders(urlConnection);
                 }
-                LOGGER.debug("Executing GET {}", url);
                 return urlConnection.getInputStream();
             }
         }
diff --git a/src/test/java/org/apache/sling/pipes/internal/inputstream/JsonPipeTest.java b/src/test/java/org/apache/sling/pipes/internal/inputstream/JsonPipeTest.java
index b935439..687a14e 100644
--- a/src/test/java/org/apache/sling/pipes/internal/inputstream/JsonPipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/inputstream/JsonPipeTest.java
@@ -32,7 +32,11 @@ import java.lang.reflect.InvocationTargetException;
 import java.util.Iterator;
 import java.util.List;
 
+import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart;
 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.matching;
+import static com.github.tomakehurst.wiremock.client.WireMock.post;
 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
 import static com.github.tomakehurst.wiremock.client.WireMock.get;
 import static org.junit.Assert.assertArrayEquals;
@@ -118,6 +122,32 @@ public class JsonPipeTest extends AbstractPipeTest {
         assertEquals("/content/bar", results.getCurrentPathSet().iterator().next());
     }
 
+    @Test
+    public void testPostJsonFormUrlEncoded() throws InvocationTargetException, IllegalAccessException {
+        http.givenThat(post(urlEqualTo("/post/foo.json"))
+                .withHeader("x-header", equalTo("bar"))
+                .withRequestBody(matching("(.*&|^)foo=1($|&.*)"))
+                .willReturn(aResponse().withStatus(201).withBody("{\"created\":\"true\"}")));
+        ExecutionResult results = execute("echo /content " +
+                "| json "  + baseUrl + "/post/foo.json?foo=1 @ name data @ with raw=true POST=true header_x-header=bar" +
+                "| write created=${data.created}");
+        assertEquals(1, results.size());
+        assertEquals("true", context.resourceResolver().getResource("/content").getValueMap().get("created"));
+    }
+
+    @Test
+    public void testPostJsonRawBody() throws InvocationTargetException, IllegalAccessException {
+        http.givenThat(post(urlEqualTo("/post/foo.json"))
+                .withHeader("x-header", equalTo("bar"))
+                .withRequestBody(matching("(.*&|^)\\{foo:1\\}($|&.*)"))
+                .willReturn(aResponse().withStatus(201).withBody("{\"created\":\"true\"}")));
+        ExecutionResult results = execute("echo /content " +
+                "| json "  + baseUrl + "/post/foo.json @ name data @ with raw=true POST={foo:1} header_x-header=bar" +
+                "| write created=${data.created}");
+        assertEquals(1, results.size());
+        assertEquals("true", context.resourceResolver().getResource("/content").getValueMap().get("created"));
+    }
+
     @Test
     public void testLoopOverObject() throws InvocationTargetException, IllegalAccessException {
         ExecutionResult results = execute("echo /content " +
diff --git a/src/test/resources/simplelogger.properties b/src/test/resources/simplelogger.properties
index 16018c4..909cb31 100644
--- a/src/test/resources/simplelogger.properties
+++ b/src/test/resources/simplelogger.properties
@@ -15,7 +15,8 @@
 # limitations under the License.
 
 org.slf4j.simpleLogger.logFile=System.out
-org.slf4j.simpleLogger.defaultLogLevel=off
+org.slf4j.simpleLogger.defaultLogLevel=info
+com.github.tomakehurst.wiremock=error
 org.slf4j.simpleLogger.log.org.apache.jackrabbit.oak.plugins.index.IndexUpdate=warn
 org.slf4j.simpleLogger.log.org.apache.sling.scripting.core.impl.ScriptEngineManagerFactory=warn
 org.slf4j.simpleLogger.log.org.apache.sling.resourceresolver.impl.mapping.MapEntries=warn


[sling-org-apache-sling-pipes] 01/02: @releng downgrade ca deps

Posted by np...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

npeltier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git

commit 3b6ebb13ef5a8e61ba44d030c3cc9176b5aad48e
Author: Nicolas Peltier <np...@apache.org>
AuthorDate: Fri Jul 22 14:33:53 2022 +0200

    @releng downgrade ca deps
    
    looks like they make it incompatible with fairly recent AEM versions
---
 pom.xml | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index dc9ef3b..6c86c7c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -272,13 +272,13 @@
     </dependency>
     <dependency>
       <artifactId>org.apache.sling.caconfig.api</artifactId>
-      <version>1.2.0</version>
+      <version>1.1.2</version>
       <groupId>org.apache.sling</groupId>
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <artifactId>org.apache.sling.caconfig.impl</artifactId>
-      <version>1.5.0</version>
+      <artifactId>org.apache.sling.caconfig.spi</artifactId>
+      <version>1.3.4</version>
       <groupId>org.apache.sling</groupId>
       <scope>provided</scope>
     </dependency>