Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Problem with error handling of custom DataSource executeDownload.

    I am using SmartGWTPower 2.4 and Firefox 4.0.1, I have to implement to download a file related to a record in a ListGrid.
    I set a field type to "binary" but there was no download icon in the ListGrid, so I added an IButton to the Layout.
    The download works fine, but there is a problem with the error handling. I used the same error handling in the catch clause as in core DS operations (executeFetch, executeAdd, executeUpdate, executeRemove) and a popup appeared with the error message. In case of executeDownload, the former screen disappeared and the following page appeared:

    Code:
    <HTML>
    <BODY ONLOAD='var results = document.formResults.results.value;parent.isc.Comm.hiddenFrameReply(2,results)'><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><FORM name='formResults'><TEXTAREA readonly name='results'>
    //isc_RPCResponseStart-->[{queueStatus:-1,isDSResponse:true,invalidateCache:false,status:-1,data:"My Error Message"}]//isc_RPCResponseEnd</TEXTAREA></FORM>
    </BODY></HTML>
    ds.xml:
    Code:
    <DataSource ID="myCustomDS" serverConstructor="my.app.server.ds.MyCustomDataSource">
     <fields>
       <field name="docid" hidden="true" primaryKey="true" type="text"/>
        <field name="filename" type="text"/>
        <field name="fileData" type="binary"/>
    client:
    Code:
    downloadButton.addClickHandler(new ClickHandler() {
     public void onClick(ClickEvent event) {
       DSRequest requestProperties = new DSRequest();
       String filename = myListGrid.getSelectedRecord().getAttributeAsString("filename");
       requestProperties.setOperationId(filename);
       myCustomDS.downloadFile(myListGrid.getSelectedRecord(), "fileData", requestProperties);
     }
    });
    server:
    Code:
    public DSResponse executeDownload(DSRequest req) throws Exception {
      String attachedFile = req.getDownloadFieldName();
      String attachedFileName = req.getOperationId();
      DSResponse resp = new DSResponse();
      resp.setDropExtraFields(true);
      try {
        ...
        req.setDownloadFileName(attachedFileName);
        Map responseMap = new HashMap();
        responseMap.put(attachedFile + "_filename", attachedFileName);
        responseMap.put(attachedFile + "_filesize", fileData.length);
        responseMap.put(attachedFile, new ByteArrayInputStream(fileData));
        resp.setData(responseMap);
        resp.setDataSource(req.getDataSource());
      } catch (Exception e) {
        req.setDownloadFieldName(null);
        resp = new DSResponse("My error message", DSResponse.STATUS_FAILURE);
      }
      return resp;
    }
    server log:
    Code:
    INFO: ===  INFO 2011.06.24 14:36:10 (Logger.java:388) - URL: '/MyProject/myproject/sc/IDACall', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1': Moz (Gecko) with Accept-Encoding header
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - Parsed XML from (in memory stream): 2ms
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - Processing 1 requests.
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - Request #1 (DSRequest) payload: {
       criteria:{
           docid:"12345",
           download_fieldname:"fileData"
       },
       operationConfig:{
           dataSource:"myCustomDS",
           operationType:"downloadFile"
       },
       appID:"builtinApplication",
       operation:"mydoc.pdf",
       oldValues:{
           docid:"12345",
           download_fieldname:"fileData"
       }
    }
    
    INFO: ===  INFO 2011.06.24 14:36:10 (Logger.java:388) - Performing 1 operation(s)
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - [builtinApplication.mydoc.pdf] No userTypes defined, allowing anyone access to all operations for this application
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - [builtinApplication.mydoc.pdf] No public zero-argument method named '_mydoc.pdf' found, performing generic datasource operation
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - Content type for RPC transaction: text/html; charset=UTF-8
    
    INFO: === DEBUG 2011.06.24 14:36:10 (Logger.java:406) - non-DMI response, dropExtraFields: true
    
    INFO: ===  INFO 2011.06.24 14:36:10 (Logger.java:388) - /MyProject/myproject/sc/IDACall: 417 -> 266 bytes
    Please help me, to resolve this problem.

    #2
    Error handling for downloads is not done the normal way (due to browser limitations). If there's a possibility of a recoverable error during a download, do a request *before* the download to check for errors, then start the download.

    Comment


      #3
      I have managed to implement this download task with error handling with RPC. I would like to ask, as it can be implemented with RPCResponse why not with DSResponse?

      client:
      Code:
      downloadButtonRPC.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          String filename = myListGrid.getSelectedRecord().getAttributeAsString("filename");
          String docid = myListGrid.getSelectedRecord().getAttributeAsString("docid");
          RPCRequest req = new RPCRequest();
          req.setDownloadResult(true);
          req.setActionURL("documentServlet?filename=" + filename + "&docid=" + docid);
          RPCManager.sendRequest(req);
        }
      });
      server:
      Code:
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getParameter("filename");
        String docid = request.getParameter("docid");
        String errMsg = null;
        byte[] fileData = null;
        ServletOutputStream os = null;
        try {
          .. 
          response.setContentType("application/pdf");
          response.setHeader("Content-Disposition", "attachment;filename=" + filename);
          os = response.getOutputStream();
          os.write(fileData);
          os.flush();
        } catch (Exception e) {
          errMsg = "My error message";
        } finally {
          try {
            if (os != null) os.close();
          } catch (IOException e) {}
        }
        if (errMsg != null) {
          response.setHeader("Content-Disposition", "");
          try {
            RPCManager rpc = new RPCManager(request, response);
            rpc.sendFailure((RPCRequest) rpc.getRequests().get(0), errMsg);
          } catch (Exception e) {}
        }
      }

      Comment

      Working...
      X