2 Replies Latest reply on Apr 12, 2010 10:18 PM by Craig Sherstan

    RESTeasy returning file as response

    Ilya Dyoshin Novice

      Good day community,


      I need to provide file download with resteasy. I.e. resteasy been is producing some file and returns it as the response on request.


      Any ideas? is this possible?


      best regards,
      Ilya Dyoshin

        • 1. Re: RESTeasy returning file as response
          Ilya Dyoshin Novice

          I've created some additional Producer for JAX-RS:



          @Name("fileContentWriterRESTEasy")
          @Scope(ScopeType.APPLICATION)
          @Provider
          @Produces("application/x-file-download")
          public class FileContentWriter implements MessageBodyWriter<byte[]>, Serializable {
          
                  @Override
                  public long getSize(byte[] file, Class<?> arg1, Type arg2, Annotation[] arg3,
                                  MediaType arg4) {
                          
                          return file.length;
                  }
          
                  @Override
                  public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
                                  MediaType arg3) {
                          return true;
                  }
          
                  @Override
                  public void writeTo(byte[] file, Class<?> arg1, Type arg2, Annotation[] arg3,
                                  MediaType arg4, MultivaluedMap<String, Object> arg5,
                                  OutputStream os) throws IOException, WebApplicationException {
                          os.write(file);
                  }
          
          }





          And use it like:


          @Name("synchroAccounting")
          @Path("/synchro")
          @Scope(ScopeType.APPLICATION)
          public class SynchroAccounting {
                  
                  @Logger
                  Log log;
                  
                  @In(create=true)
                  EntityManager entityManager;
          
                  @GET
                  @Path("/spr_fiz_lic")
                  public byte[] sprFizLic()  {
                          try {
                                  String fname =  UUID.randomUUID().toString();
                                  DBF dbf = new DBF(fname, true);
                                  
                                  CharField id = new CharField("id",36);
                                  CharField fio = new CharField("fio",200);
                                  DateField denRojd = new DateField("denRojd");
                                  
                                  dbf.addField(id);
                                  dbf.addField(fio);
                                  dbf.addField(denRojd);
                                  
                                  List<SprFizLic> res = entityManager.createQuery("SELECT o FROM SprFizLic o ").setMaxResults(100).getResultList();
                                  for (SprFizLic sprFizLic : res) {
                                          id.put(sprFizLic.getId());
                                          fio.put(sprFizLic.getDescription());
                                          denRojd.put(sprFizLic.getDenRojd());
                                          
                                          dbf.write();
                                  }
                                  
                                  dbf.close();
                                  
                                  
                                  return dbf.buffer.array();
                          }
                          catch (IOException ex ) {
                                  log.trace(ex);
                          } catch (xBaseJException e) {
                                  log.trace(e);
                          }
                          return null;
                  }
          }




          The system runs the query - and then it dies. and returns 403 reponse. Any Idea??


          • 2. Re: RESTeasy returning file as response
            Craig Sherstan Newbie

            Did you ever get this figured out?  I'm seeing the 403 response whenever I return a JSON response or when I return a collection with XML.