I built a API-REST service to obtain specific PDF documents stored in a FTP directory by passing different arguments to the URL.
I'm using APACHE-CAMEL and JAVA
Example:
My URL looks like this:
These parameters are completed to look for a file, in the given example i set:
companyNumber: 45
accountNumber: 000333
documentType: FC
transactionNumber: BG534530SF
The PDF file has this name:
45000333FCBG534530SF
The problem is that it takes 20 or 25 seconds or even higher to get a document and i wonder how can i improve search time while searching for a file
This is my code:
package ar.com.project.esb.route;
import ar.com.project.camel.common.route.RestRouteBuilder;
import ar.com.project.esb.Constants;
import org.apache.camel.LoggingLevel;
import org.apache.camel.model.rest.RestParamType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.ws.rs.core.MediaType;
@Component
public class GetReceiptsPDFRoute extends RestRouteBuilder {
@Value("${timeout.FTP}")
private Long timeoutFTP;
@Override
public void configure() {
super.configure();
rest(restApiPath)
.get("/receipts/pdf?companyNumber={companyNumber}&accountNumber={accountNumber}&documentType={documentType}&transactionNumber={transactionNumber}").id("receipts PDF")
.param().name("companyNumber").type(RestParamType.query).dataType("String").required(true).endParam()
.param().name("accountNumber").type(RestParamType.query).dataType("String").required(true).endParam()
.param().name("documentType").type(RestParamType.query).dataType("String").required(true).endParam()
.param().name("transactionNumber").type(RestParamType.query).dataType("String").required(true).endParam()
.consumes(MediaType.APPLICATION_JSON)
.produces("application/pdf")
.to("direct:receiptsPDF");
/**
* set name and location to find PDF documents in FTP directory
*/
from("direct:receiptsPDF").routeId("ReceiptsPDF")
.setProperty("currentOperation", constant("ReceiptsPDF"))
.log(LoggingLevel.INFO,"getClaimStatus", "[Inicia ${property.currentOperation}]")
.log(LoggingLevel.INFO, "company Number: ${header.companyNumber}")
.log(LoggingLevel.INFO, "account Number: ${header.accountNumber}")
.log(LoggingLevel.INFO, "document Type: ${header.documentType}")
.log(LoggingLevel.INFO, "transaction Type: ${header.transactionNumber}")
.doTry()
.choice()
.when(header("documentType").isEqualTo(Constants.RECEIPT))
.log(LoggingLevel.INFO, "[${property.currentOperation}] Find RECEIPT")
.setHeader("TARGET_DIR", constant("{{source.erp.RECEIPTs}}"))
.endChoice()
.otherwise()
.log(LoggingLevel.INFO, "[${property.currentOperation}] Find DOCUMENTS")
.setHeader("TARGET_DIR", constant("{{source.erp.DOCUMENTS}}"))
.end()
.setProperty("fileName", simple("${header.companyNumber}${header.accountNumber}${header.documentType}${header.transactionNumber}.pdf"))
.to("direct:findPdfInFTP")
.log(LoggingLevel.INFO,"getClaimStatus", "[Ending ${property.currentOperation}]")
.endDoTry()
.doCatch(Exception.class)
.log(LoggingLevel.ERROR, "getReceipts", "[ERROR: ${exception.message}")
.end();
/**
* Search in FTP directory
*/
from("direct:findPdfInFTP").routeId("find file in ftp")
.doTry()
.log(LoggingLevel.INFO, "Search: ${header.TARGET_DIR}/${property.fileName}")
.pollEnrich().simple("{{erp.sftp}}/${header.TARGET_DIR}?password={{erp.sftp.password}}&fileName=${property.fileName}&disconnect=true&noop=true&recursive=true&useUserKnownHostsFile=false&idempotent=false&include=.*.pdf").timeout(timeoutFTP)
.choice()
.when(simple("${body} == null"))
.log(LoggingLevel.ERROR, "getClaimStatus", "[ERROR: file does not exist...]")
.otherwise()
.log(LoggingLevel.INFO,"FILE OBTAINED: ${headers.CamelFileName}")
.endDoTry()
.doCatch(Exception.class)
.log(LoggingLevel.ERROR, "getReceipts", "[ERROR: ${exception.message}")
.end();
}
}
A guy told me about making an attempt to download it instead of searching, using a fixed name and gave me this link: https://camel.apache.org/components/3.18.x/ftp-component.html#_consuming_a_single_file_using_a_fixed_name
Don't know how to make it work on my code, could you please help me?