ho fatto queste modifiche, e pare funzionare tutto (poi dal punto di vista design non so).
nell'enum imposto anche l'estensione del file da scaricare (in futuro magari aggiungo altre estensioni), e il tipo di operazione (r=lettura, quindi visualizzazione, r=scrittura quindi mi fa il download):
poi ho creato una classe che mi crea la Response.ResponseBuilder:codice:public enum MediaTypeAccepted { JSON("application/json", "", "r"), XLS("application/vnd.ms-excel", "xls", "w"); private String value; private String ext; private String operation; MediaTypeAccepted(String value, String ext, String operation) { this.value = value; this.ext = ext; this.operation = operation; } public String getValue() { return value; } public String getExt() { return ext; } public String getOperation() { return operation; } }
a seconda del tipo di operazione creo il file o meno (ovviamente la creazione prendendo i dati devo ancora impostarla).codice:public class CreateResponse { private Object entity; private String mediaType; private String ext; private String operation; public CreateResponse(Object entity, String mediaType, String ext, String operation) { this.entity = entity; this.mediaType = mediaType; this.ext = ext; this.operation = operation; } public Response.ResponseBuilder create() throws IOException { Response.ResponseBuilder response = null; if (operation.equals("w")) { File file = File.createTempFile("export", "." + ext); response = Response.ok((Object) file, mediaType); response.header("Content-Disposition", "attachment; filename=" + file); file.deleteOnExit(); } else { response = Response.ok(entity, mediaType); } return response; } }
infine nel service:
funzionare funziona.....codice:@Path("test") public class TestService { @GET @Path("{mediaType}") public Response test(@PathParam("mediaType") String mediaType) { CreateResponse response; Response.ResponseBuilder rb; if (EnumUtils.isValidEnum(MediaTypeAccepted.class, mediaType.toUpperCase())) { ZonaQueries query = new ZonaQueries(); ArrayList<Zona> list = null; String mt = MediaTypeAccepted.valueOf(mediaType.toUpperCase()).getValue(); String ext = MediaTypeAccepted.valueOf(mediaType.toUpperCase()).getExt(); String operation = MediaTypeAccepted.valueOf(mediaType.toUpperCase()).getOperation(); try { list = query.getAll(); response = new CreateResponse(list, mt, ext, operation); rb = response.create(); } catch (ClassNotFoundException | SQLException | IOException ex) { throw new NotFoundException(new JsonError("Errore", ex.getMessage())); } } else { throw new NotFoundException(new JsonError("Errore", "File non supportato")); } return rb.build(); } }

Rispondi quotando