1

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

Community
  • 1
  • 1
AKB
  • 5,918
  • 10
  • 53
  • 90
  • 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 Aug 16 '14 at 06:09
  • 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 Aug 16 '14 at 18:52
  • 1
    Also there's a typo: you have `` when it should be ``. – David Dossot Aug 16 '14 at 18:52

1 Answers1

1

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>
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • Set the Content-Disposition header inside postOperation. How do I set in my Rest class file? Do I set both mimetype and content type in Java is better? – AKB Aug 18 '14 at 18:31
  • And no: keep using `@Produces` for the response content type. – David Dossot Aug 18 '14 at 18:40
  • I have added return Response.ok(res).header("Content-Disposition", "attachment;filename=QuickbooksAccountsJava.iif").build(); made @Produces("text/csv") and having only rest call in mule. But file still doesn't download. – AKB Aug 18 '14 at 18:45
  • Can you pastebin a `curl -v` interaction please? I'd like to see the exact response, including headers. – David Dossot Aug 18 '14 at 18:54
  • updated above, I am using rest client from Chrome and in windows env. – AKB Aug 18 '14 at 19:05
  • File doesn't download to my local system. As I had informed earlier, If I dont have java component, A file called QuickbooksAccountsJava.iif use download locally to my system, I need this file to proceed further. – AKB Aug 18 '14 at 19:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59531/discussion-between-akb-and-david-dossot). – AKB Aug 18 '14 at 19:32
  • 1
    issue was Chrome Rest client doesnt show the download file and it's path, found it in CURL – AKB Aug 18 '14 at 21:29