You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/06/10 06:39:17 UTC

[camel] 01/02: Fix Jira Attach file conversion

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

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a64f10abdcbab618756570aeaf6b4d581c54b942
Author: Claudio Miranda <cl...@claudius.com.br>
AuthorDate: Fri Jun 7 19:33:52 2019 -0300

    Fix Jira Attach file conversion
---
 .../apache/camel/component/jira/FileConverter.java | 49 ++++++++++++++++++++++
 .../oauth/OAuthAsynchronousHttpClientFactory.java  |  3 +-
 .../jira/producer/AttachFileProducer.java          | 22 ++++------
 .../jira/producer/AttachFileProducerTest.java      | 26 ++++--------
 4 files changed, 67 insertions(+), 33 deletions(-)

diff --git a/components/camel-jira/src/main/java/org/apache/camel/component/jira/FileConverter.java b/components/camel-jira/src/main/java/org/apache/camel/component/jira/FileConverter.java
new file mode 100644
index 0000000..0d36d7c
--- /dev/null
+++ b/components/camel-jira/src/main/java/org/apache/camel/component/jira/FileConverter.java
@@ -0,0 +1,49 @@
+/**
+ * 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.jira;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.file.GenericFile;
+
+@Converter(loader = true)
+public class FileConverter {
+
+    private FileConverter() {
+    }
+
+    @Converter
+    public static File genericToFile(GenericFile<File> genericFile, Exchange exchange) throws IOException {
+        Object body = genericFile.getBody();
+        File file;
+        if (body instanceof byte[]) {
+            byte[] bos = (byte[]) body;
+            file = new File(System.getProperty("java.io.tmpdir"), genericFile.getFileName());
+            Files.write(file.toPath(), bos, StandardOpenOption.CREATE);
+            // delete the temporary file on exit, as other routing may need the file for post processing
+            file.deleteOnExit();
+        } else {
+            file = (File) body;
+        }
+        return file;
+    }
+}
diff --git a/components/camel-jira/src/main/java/org/apache/camel/component/jira/oauth/OAuthAsynchronousHttpClientFactory.java b/components/camel-jira/src/main/java/org/apache/camel/component/jira/oauth/OAuthAsynchronousHttpClientFactory.java
index a2ac19c..90c66b0 100644
--- a/components/camel-jira/src/main/java/org/apache/camel/component/jira/oauth/OAuthAsynchronousHttpClientFactory.java
+++ b/components/camel-jira/src/main/java/org/apache/camel/component/jira/oauth/OAuthAsynchronousHttpClientFactory.java
@@ -171,8 +171,7 @@ public class OAuthAsynchronousHttpClientFactory {
                 props.load(resourceAsStream);
                 return props.getProperty("version", UNKNOWN_VERSION);
             } catch (Exception e) {
-                LOG.debug("Could not find version for maven artifact {}:{}", groupId, artifactId);
-                LOG.debug("Got the following exception", e);
+                LOG.debug("Could not find version for Jira Rest Java Client maven artifact {}:{}. Error: {}", groupId, artifactId, e.getMessage());
                 return UNKNOWN_VERSION;
             }
         }
diff --git a/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java b/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java
index b2a422c..5b0dc2f 100644
--- a/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java
+++ b/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java
@@ -23,7 +23,7 @@ import com.atlassian.jira.rest.client.api.IssueRestClient;
 import com.atlassian.jira.rest.client.api.JiraRestClient;
 import com.atlassian.jira.rest.client.api.domain.Issue;
 import org.apache.camel.Exchange;
-import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.component.jira.JiraEndpoint;
 import org.apache.camel.support.DefaultProducer;
 import org.slf4j.Logger;
@@ -40,22 +40,16 @@ public class AttachFileProducer extends DefaultProducer {
     }
 
     @Override
-    public void process(Exchange exchange) {
+    public void process(Exchange exchange) throws InvalidPayloadException {
         String issueKey = exchange.getIn().getHeader(ISSUE_KEY, String.class);
         if (issueKey == null) {
             throw new IllegalArgumentException("Missing exchange input header named \'IssueKey\', it should specify the issue key to attach a file.");
         }
-        Object body = exchange.getIn().getBody();
-        if (body instanceof GenericFile) {
-            JiraRestClient client = ((JiraEndpoint) getEndpoint()).getClient();
-            IssueRestClient issueClient = client.getIssueClient();
-            Issue issue = issueClient.getIssue(issueKey).claim();
-            URI attachmentsUri = issue.getAttachmentsUri();
-            GenericFile<File> file = (GenericFile<File>) body;
-            issueClient.addAttachments(attachmentsUri, file.getFile());
-        } else {
-            LOG.info("Jira AttachFileProducer can attach only one file on each invocation. The body instance is " + body.getClass().getName() + " but it accepts only GenericFile<File>. You can "
-                + "write a rooute processor to transform any incoming file to a Generic<File>");
-        }
+        File file = exchange.getIn().getMandatoryBody(File.class);
+        JiraRestClient client = ((JiraEndpoint) getEndpoint()).getClient();
+        IssueRestClient issueClient = client.getIssueClient();
+        Issue issue = issueClient.getIssue(issueKey).claim();
+        URI attachmentsUri = issue.getAttachmentsUri();
+        issueClient.addAttachments(attachmentsUri, file);
     }
 }
diff --git a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
index 3771e0b..a4434e7 100644
--- a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
+++ b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -35,7 +36,6 @@ import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.component.jira.JiraComponent;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.spi.Registry;
@@ -92,7 +92,11 @@ public class AttachFileProducerTest extends CamelTestSupport {
             return Promises.promise(issue);
         });
         when(issueRestClient.addAttachments(any(URI.class), any(File.class))).then(inv -> {
-            attachedFile = inv.getArgument(1);
+            File attachedFileTmp = inv.getArgument(1);
+            // create a temp destiny file as the attached file is marked for removal on AttachFileProducer
+            attachedFile = File.createTempFile("camel-jira-test-", null);
+            Files.copy(attachedFileTmp.toPath(), attachedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+            attachedFile.deleteOnExit();
             Collection<Attachment> attachments = new ArrayList<>();
             attachments.add(new Attachment(issue.getAttachmentsUri(), attachedFile.getName(), null, null,
                     new Long(attachedFile.length()).intValue(), null, null, null));
@@ -112,12 +116,6 @@ public class AttachFileProducerTest extends CamelTestSupport {
     }
 
     @Override
-    protected void stopCamelContext() throws Exception {
-        super.stopCamelContext();
-        attachedFile.deleteOnExit();
-    }
-
-    @Override
     protected CamelContext createCamelContext() throws Exception {
         setMocks();
         CamelContext camelContext = super.createCamelContext();
@@ -131,14 +129,8 @@ public class AttachFileProducerTest extends CamelTestSupport {
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             @Override
-            public void configure() throws IOException {
-                File sampleFile = generateSampleFile();
+            public void configure() {
                 from("direct:start")
-                        .process(processor -> {
-                            GenericFile<File> genFile = new GenericFile<>();
-                            genFile.setFile(sampleFile);
-                            processor.getOut().setBody(genFile);
-                        })
                         .setHeader(ISSUE_KEY, () -> KEY + "-1")
                         .to("jira://attach?jiraUrl=" + JIRA_CREDENTIALS)
                         .to(mockResult);
@@ -147,8 +139,8 @@ public class AttachFileProducerTest extends CamelTestSupport {
     }
 
     @Test
-    public void verifyAttachment() throws InterruptedException {
-        template.sendBody(null);
+    public void verifyAttachment() throws InterruptedException, IOException {
+        template.sendBody(generateSampleFile());
         Issue retrievedIssue = issueRestClient.getIssue(issue.getKey()).claim();
         assertEquals(issue, retrievedIssue);
         // there is only one attachment