0

我使用 Camel-http4 是因为我只需要一个生产者,而不是公开一个网络服务。我只需要将文件推送到 HTTP 服务器,所以这是一个客户端实现。我面临的问题是尝试发布返回错误。

CamelHttpResponseCode=403,CamelHttpResponseText=禁止

{"timestamp":"2018-05-03T20:36:20.571","traceId":"","path":"[POST] https://apm-query-svc-prod.apm-api.com/ v2/t_series/upload%2520?throwExceptionOnFailure=false ","errors":[{"httpStatusCode":"FORBIDDEN","code":"FORBIDDEN","message":"解析多部分 servlet 请求失败;嵌套异常是java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: 请求被拒绝,因为没有找到多部分边界","detail":null,"requestId":null}]}

问题,有没有办法用 Camel-HTTP4 做到这一点?如何/可以做到这一点?谢谢!

这是路线

<route 
    id="core.pr.upload.route"
    autoStartup="false" >
    <from uri="{{uploadEntranceEndpoint}}" />
    <process ref="customerEntitesProcessor" />
    <process ref="customerTokenProcessor" />
    <process ref="uploadProcessor" />
    <setHeader headerName="CamelHttpUri">
        <simple>${header.UPLOADURL}?throwExceptionOnFailure=false</simple>
    </setHeader>
    <setHeader headerName="CamelHttpMethod">
        <constant>POST</constant>
    </setHeader>
    <to uri="http4://apm-query-svc-prod.app-api.aws-usw02-pr.io:443/v2/t_series/upload?throwExceptionOnFailure=false" />
    <log message="After POSTING FILE: ${body}" loggingLevel="INFO"/>    
    <to uri="{{afteruploadLocation}}" />
</route>

这是类,我只是设置了标头,然后 URI 被 Exchange.HTTP_URI 提供的覆盖。

公共类 UploadProcessor 实现处理器 {

private static final Logger LOG = LoggerFactory.getLogger(UploadProcessor.class);

@Override
public void process(Exchange exchange) throws Exception {

    LOG.info("Entering Upload Processor ..." );

        final String tenant = (String)exchange.getIn().getHeader("TENANT");
        final String token = (String)exchange.getIn().getHeader("TOKEN");
        final String uploadUrl = (String)exchange.getIn().getHeader("UPLOADURL");
        final String custKey = (String)exchange.getIn().getHeader("CUSTKEY");

        LOG.info("TENANT: " + tenant);
        LOG.info("TOKEN: " + token);
        LOG.info("UPLOADURL: " + uploadUrl);
        LOG.info("CUSTKEY: " + custKey);

        LOG.info("Uploading File for CustKey: " + custKey + " and Tenant: " + tenant);          

        StringBuilder authHeader = new StringBuilder("Bearer "); 
        authHeader.append(token);           
        LOG.info("Authorization: " + authHeader.toString());

        exchange.setProperty("CamelCharsetName", "UTF-8");  //"CamelCharsetName" Exchange.CHARSET_NAME
        exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8");   //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
        exchange.getIn().setHeader("CamelAcceptContentType", "application/json");  //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
        exchange.getIn().setHeader("CamelHttpUri", uploadUrl);  //"CamelHttpUri" Exchange.HTTP_URI
        exchange.getIn().setHeader("CamelHttpMethod", "POST");  //"CamelHttpMethod" Exchange.HTTP_METHOD
        exchange.getIn().setHeader("x-ge-csvformat", "ODB");
        exchange.getIn().setHeader("Tenant", tenant);
        exchange.getIn().setHeader("Content-Type", "multipart/form-data");  //"Content-Type" ; boundary=
        exchange.getIn().setHeader("Authorization", authHeader.toString());
        LOG.info("Exiting Upload Processor: Finish " );
    }

}

这是我的第一次 HTTP 介绍,不熟悉多部分边界,我希望有一种简单的解决方案。:) 谢谢你 !!!

解决方案一点也不难,这是基于拉尔夫回答的最终结果。谢谢大家!困难的部分是找到导入包并且它具有依赖关系。添加到 POM

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.1</version>
</dependency>


import java.lang.StringBuilder;
import java.net.URLEncoder;
import java.util.Base64;
import java.io.File;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.camel.LoggingLevel;
import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
public class UploadProcessor implements Processor {

    private static final Logger LOG = LoggerFactory.getLogger(UploadProcessor.class);

    @Override
    public void process(Exchange exchange) throws Exception {

        LOG.info("Entering Upload Processor ..." );

            final String tenant = (String)exchange.getIn().getHeader("TENANT");
            final String token = (String)exchange.getIn().getHeader("TOKEN");
            final String uploadUrl = (String)exchange.getIn().getHeader("UPLOADURL");
            final String custKey = (String)exchange.getIn().getHeader("CUSTKEY");

            LOG.info("TENANT: " + tenant);
            LOG.info("TOKEN: " + token);
            LOG.info("UPLOADURL: " + uploadUrl);
            LOG.info("CUSTKEY: " + custKey);

            LOG.info("Uploading File for CustKey: " + custKey + " and Tenant: " + tenant);          

            StringBuilder authHeader = new StringBuilder("Bearer "); 
            authHeader.append(token);           
            LOG.info("Authorization: " + authHeader.toString());

            exchange.setProperty("CamelCharsetName", "UTF-8");  //"CamelCharsetName" Exchange.CHARSET_NAME
            exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8");   //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
            exchange.getIn().setHeader("CamelAcceptContentType", "application/json");  //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
            exchange.getIn().setHeader("CamelHttpUri", uploadUrl);  //"CamelHttpUri" Exchange.HTTP_URI
            exchange.getIn().setHeader("CamelHttpMethod", "POST");  //"CamelHttpMethod" Exchange.HTTP_METHOD
            exchange.getIn().setHeader("x-ge-csvformat", "ODB");
            exchange.getIn().setHeader("Tenant", tenant);
            exchange.getIn().setHeader("Authorization", authHeader.toString());

            // Process the file in the exchange body
            File file = exchange.getIn().getBody(File.class);
            String fileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
            LOG.info("fileName: " + fileName);

            MultipartEntityBuilder entity = MultipartEntityBuilder.create();
            entity.addBinaryBody("file", file);
            entity.addTextBody("name", fileName);

            exchange.getIn().setBody(entity.build());
            LOG.info("Exiting Upload Processor: Finish " );

        }
}
4

0 回答 0