1
votes

If I have some java or rest component in mule flows, mule doesn't create and download file.

     <flow name="qbiif" doc:name="qbiif">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
            <jersey:resources doc:name="REST">
                <component class="com.trinet.rest.RestIntegration"/>
            </jersey:resources>
            <component doc:name="Java">
                <singleton-object class="com.mycompany.iif.java.AppendData"/>
            </component>
            <set-payload value="Hello" doc:name="Set Payload"/>
            <set-property propertyName="mimeType" value="text/csv" doc:name="Property"/>
            <set-property propertyName="Content-Disposition" value="attachment;filename=QuickbooksAccountsJava.iif" doc:name="Property"/>
            <logger message="final output==#[payload]" level="INFO" doc:name="Logger"/>  
</flow>

If I dont have any java/rest component then it downloads the file as shown below code.

<flow name="qbiif" doc:name="qbiif">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>      
        <set-payload value="Hello" doc:name="Set Payload"/>
        <set-property propertyName="mimeType" value="text/csv" doc:name="Property"/>
        <set-property propertyName="Content-Disposition" value="attachment;filename=QuickbooksAccountsJava.iif" doc:name="Property"/>
</flow>

I need file to be downloaded with content from java payload datas.

I have followed this link , but no changes.

EDIT

Rest component:

@Path("/")
public class RestIntegration {

    @POST
    @Path("/post/{id1}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response postOperation(@PathParam("id1")String id, String cont) throws Exception{
                return Response.status(200).entity(cont).build();
    }
    }



Java Component




 public class AppendData implements Serializable {
        public String appendData(@Payload String str) throws IOException, ParseException{
            JSONParser jsonParser = new JSONParser();
            Object jsonObjectInstance = jsonParser.parse(new StringReader(str));
            StringBuilder stringBuilder = new StringBuilder();
            buildHeaders(stringBuilder);
            if (jsonObjectInstance instanceof JSONObject) {
                JSONObject jObject = (JSONObject) jsonObjectInstance;
                buildContent(stringBuilder,jObject);
            }else{
                JSONArray jsonArray = (JSONArray) jsonObjectInstance;
                for(int i=0; i<jsonArray.size(); i++ ){
                    JSONObject jsonObject= (JSONObject) jsonArray.get(i);
                    buildContent(stringBuilder,jsonObject);
                }
            }
            buildFooter(stringBuilder);
            return (stringBuilder.toString());
        }
    private void buildContent(StringBuilder stringBuilder, JSONObject jsonObject) {
            stringBuilder.append("ANS"+"\t");
    }
    private void buildHeaders(StringBuilder stringBuilder) {
            stringBuilder.append("!TRNS \t Account \t Date \t\t Amount \t Description \t Memo \n");
        }
        private void buildFooter(StringBuilder stringBuilder) {
            stringBuilder.append("ENDANNS");
        }
    }

If there is no Java/Rest component I am able to download file. At the end logger prints proper error message.

EDIT-2

<flow name="qbiif" doc:name="qbiif">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
    <jersey:resources doc:name="REST">
        <component class="com.trinet.rest.RestIntegration"/>
    </jersey:resources>
</flow>

RestIntegration.java:

@Consumes(MediaType.APPLICATION_JSON)
@Produces("text/csv")
public Response postOperation(@PathParam("id1")String id, String cont) throws Exception{
        String res = appendData(cont); // become private method
        return Response.ok(res).header("Content-Disposition", "attachment;filename=QuickbooksAccountsJava.iif").header("mimeType", "text/csv").build();

I am using Chrome Rest client in windows. I am getting response as:

enter image description here

1
I have tried to replicate your scenario and I was successful to create and download file .. issue may be in your Java classes .. Would you able to share it so the issue can be detected - Anirban Sen Chowdhary
Producing a proper response should be the job of com.trinet.rest.RestIntegration. Really there should be no reason to add extra components or message processors after the jersey:resources. - David Dossot
Also there's a typo: you have </jersey:resource> when it should be </jersey:resources>. - David Dossot
@DavidDossot updated my code - AKB

1 Answers

1
votes

There's a design flaw in your approach.

com.trinet.rest.RestIntegration should be the place where the expected response is created:

  • Fix @Produces so it returns text/csv,
  • Move the logic of appendData into postOperation,
  • Set the Content-Disposition header inside postOperation.

Thus, there should be not need at all for any extra message processor besides the Jersey resources, ie your flow should be like this:

<flow name="qbiif" doc:name="qbiif">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
    <jersey:resources doc:name="REST">
        <component class="com.trinet.rest.RestIntegration"/>
    </jersey:resources>
</flow>