将数据发送到外部资源:为什么内容为空?

时间:2018-07-17 11:52:47

标签: apache-camel activemq alfresco jbossfuse camel-http

我有一条简单的骆驼路线。我想从队列中提取文件,并通过使用POST请求将其传递给外部资源。此路由有效,请求到达外部资源:

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("activemq:alfresco-queue")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                byte[] bytes = exchange.getIn().getBody(byte[].class);

                // All of that not working...
                // exchange.getIn().setHeader("content", bytes); gives "java.lang.IllegalAgrumentException: Request header is too large"
                // exchange.getIn().setBody(bytes, byte[].class); gives "size of content is -1"
                // exchange.getIn().setBody(bytes); gives "size of content is -1"
                // ???

                // ??? But I can print file content here
                for(int i=0; i < bytes.length; i++) {
                    System.out.print((char) bytes[i]);
                }
            }
        })

        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader(Exchange.CONTENT_TYPE, constant("multipart/form-data"))
        .to("http://vm-alfce52-31......com:8080/alfresco/s/someco/queuefileuploader?guest=true")

        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println("The response code is: " + exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE));
            }
        });
    }
}

问题是请求的有效负载丢失了:

// somewhere on an external resource
Content content = request.getContent();
long len = content.getSize() // is always == -1.

// the file name is passed successfully
String fileName = request.getHeader("fileName");

如何在此路由/处理器中设置和传递POST请求的有效负载?

我注意到以这种方式设置的任何数据也丢失了。仅标头发送到远程资源。

通过使用以<input type="file">编码的multipart/form-data的简单HTML格式,我可以成功地将所有数据发送到外部资源。

可能是什么原因?

我将非常感谢您提供的信息。

感谢所有人。


已更新。

以下代码还提供了null内容:

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

// this also gives null-content
//multipartEntityBuilder.addBinaryBody("file", exchange.getIn().getBody(byte[].class));

multipartEntityBuilder.addPart("file", new ByteArrayBody(exchange.getIn().getBody(byte[].class), exchange.getIn().getHeader("fileName", String.class)));
exchange.getOut().setBody(multipartEntityBuilder.build().getContent());

/********** This also gives null-content *********/
StringBody username = new StringBody("username", ContentType.MULTIPART_FORM_DATA); 
StringBody password = new StringBody("password", ContentType.MULTIPART_FORM_DATA); 

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); 
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
multipartEntityBuilder.addPart("username", username); 
multipartEntityBuilder.addPart("password", password); 

String filename = (String) exchange.getIn().getHeader("fileName");

File file = new File(filename);
try(RandomAccessFile accessFile = new RandomAccessFile(file, "rw")) {
    accessFile.write(bytes);
}

multipartEntityBuilder.addPart("upload", new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));
exchange.getIn().setBody(multipartEntityBuilder.build().getContent());

更多细节。如果我更改此内容:

exchange.getOut().setBody(multipartEntityBuilder.build().getContent());

对此:

exchange.getOut().setBody(multipartEntityBuilder.build());

我在FUSE端收到以下异常(我通过hawtio管理控制台看到了此异常):

Execution of JMS message listener failed.
Caused by: [org.apache.camel.RuntimeCamelException - org.apache.camel.InvalidPayloadException: 
No body available of type: java.io.InputStream but has value: org.apache.http.entity.mime.MultipartFormEntity@26ee73 of type: 
org.apache.http.entity.mime.MultipartFormEntity on: JmsMessage@0x1cb83b9. 
Caused by: No type converter available to convert from type: org.apache.http.entity.mime.MultipartFormEntity to the required type: 
java.io.InputStream with value org.apache.http.entity.mime.MultipartFormEntity@26ee73. Exchange[ID-63-DP-TAV-55652-1531889677177-5-1]. Caused by: 
[org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: 
org.apache.http.entity.mime.MultipartFormEntity to the required type: java.io.InputStream with value org.apache.http.entity.mime.MultipartFormEntity@26ee73]]

1 个答案:

答案 0 :(得分:1)

我编写了一个小型servlet应用程序,并从HttpServletRequest对象的doPost(...)方法中获取了内容。

问题出在外部系统(Alfresco)一侧的WebScriptRequest对象上。

@Bedla,谢谢您的建议!


在露天剧场方面,该问题可以通过以下方式解决:

public class QueueFileUploader extends DeclarativeWebScript {
    protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
        HttpServletRequest httpServletRequest = WebScriptServletRuntime.getHttpServletRequest(req);
        //  calling methods of httpServletRequest object and retrieving the content
        ...

路线:

public class MyRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("activemq:alfresco-queue")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
                multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                multipartEntityBuilder.addPart("file", new ByteArrayBody(exchange.getIn().getBody(byte[].class), 
                        exchange.getIn().getHeader("fileName", String.class)));
                exchange.getIn().setBody(multipartEntityBuilder.build().getContent());              
            }
        })

        .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.POST))
        .to("http4://localhost:8080/alfresco/s/someco/queuefileuploader?guest=true")
//      .to("http4://localhost:8080/ServletApp/hello")

        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println("The response code is: " + 
                        exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE));
            }
        });
    }
}