gt:         final File data = new File(dataUrl);
context: package com.www.server;
  
  import java.io.*;
  import java.lang.reflect.*;
  import java.net.*;
  import java.nio.charset.StandardCharsets;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.*;
  import java.util.logging.*;
  import java.util.zip.*;
  import javax.ws.rs.*;
  import javax.ws.rs.core.*;
  import javax.ws.rs.core.Response.*;
  import org.apache.commons.io.*;
  import org.codehaus.jettison.json.JSONException;
  import org.codehaus.jettison.json.JSONObject;
  import org.jdom.*;
  import org.jdom.input.*;
  import org.jdom.output.*;
  
  @Path("tasks")
  public class TaskService {
  
      String app = "sensing";
  
      @Context
      private UriInfo context;
  
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String respondAsReady() {
          return "TaskService is ready.";
      }
  
      @GET
      @Path("test/{param}")
      @Produces(MediaType.TEXT_PLAIN)
      public String test(@PathParam("param") String id) {        
          return id;
      }
  
      @GET
      @Path("gettaskinfo_test/{deviceID}")
      @Produces(MediaType.APPLICATION_JSON)
      public Task getTaskInfo_test(@PathParam("deviceID") String deviceID) throws Exception {
          String taskID = getReadyTask();
          String taskDir = "";
          String xmlUrl = "";
          Task task = null;
          if (!taskID.isEmpty()) {
              System.out.println("TaskService/getTaskInfo: Returning task info with id " + taskID + " ...");
              taskDir = Utils.getDir(app, taskID);
              updateDownloaded(taskID);
              xmlUrl = taskDir + "/" + taskID + ".xml";
              if (Utils.getText(xmlUrl, "status").isEmpty()) {
                  Utils.setText(xmlUrl, "status", "start");
              }
              task = new Task(taskDir + "/" + taskID + ".xml");
          }
          Utils.updateDeviceActivity(deviceID);
          return task;
      }
  
      @GET
      @Path("gettaskinfo/{deviceID}")
      @Produces(MediaType.APPLICATION_JSON)
      public Task getTaskInfo(@PathParam("deviceID") String deviceID) throws Exception {
          String taskID = getReadyTask();
          String taskDir = "";
          String xmlUrl = "";
          Task task = null;
          if (!taskID.isEmpty()) {
              System.out.println("TaskService/getTaskInfo: Returning task info with id " + taskID + " ...");
              taskDir = Utils.getDir(app, taskID);
              updateDownloaded(taskID);
              xmlUrl = taskDir + "/" + taskID + ".xml";
              if (Utils.getText(xmlUrl, "status").isEmpty()) {
                  Utils.setText(xmlUrl, "status", "start");
              }
              task = new Task(taskDir + "/" + taskID + ".xml");
          }
          Utils.updateDeviceActivity(deviceID);
          return task;
      }
  
      @GET
      @Path("{taskID}/getbin/{deviceID}")
      public Response getBin(
              @PathParam("taskID") String taskID,
              @PathParam("deviceID") String deviceID) throws Exception {
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          ResponseBuilder response = Response.ok(null);
          if (clientDir != null && !clientDir.isEmpty()) {
              File bin = Utils.returnFileFrom(clientDir, ".zip");
              System.out.println("TaskService/getBin: Returning " + bin.getPath() + " ...");
              response = Response.ok((Object) bin);
          }
          Utils.updateDeviceActivity(deviceID);
          return response.build();
      }
  
      @GET
      @Path("{taskID}/getbin/{length}/{deviceID}")
      public Response getBin(
              @PathParam("taskID") String taskID,
              @PathParam("length") final Long length,
              @PathParam("deviceID") String deviceID) throws Exception {
          String result = "Oops!";
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          final String binUrl = clientDir + "/" + taskID + ".zip";
          Utils.updateDeviceActivity(deviceID);
          if (clientDir != null && !clientDir.isEmpty() && new File(binUrl).length() > length) {
              if (Utils.deviceExists(deviceID)) {
                  StreamingOutput stream = new StreamingOutput() {
                      @Override
                      public void write(OutputStream os) throws IOException, WebApplicationException {
                          Utils.returnPart(new RandomAccessFile(new File(binUrl), "r"), os, length);
                      }
                  };
                  System.out.println("TaskService/getBin: Returning part of " + binUrl + " from " + length + " to " + deviceID + "...");
                  return Response.ok("OK").entity(stream).build();
              } else {
                  return Response.ok("Device not found").build();
              }
          } else {
              return Response.ok(result).build();
          }
      }
  
      @POST
      @Path("{taskID}/putdata/{dataName}/{deviceID}")
      public Response putData(
              InputStream is,
              @PathParam("dataName") String dataName,
              @PathParam("taskID") String taskID,
              @PathParam("deviceID") String deviceID) throws Exception {
          String url = Utils.getDir(app, taskID) + "/client/" + dataName + "@" + deviceID + ".dat";
          System.out.println("TaskService/putData: Writing to " + url);
  //
  //        Utils.writeToFile(data, url);
  //        
  //        ObjectInputStream ois = new ObjectInputStream(is);
  //        List<Object> data = (List) ois.readObject();
  //        saveDataToFile(data, url);
  //        
          Utils.writeToFile(is, url);
  
          Utils.updateDeviceActivity(deviceID);
          return Response.ok().entity("OK").build();
      }
  
      @GET
      @Path("{taskID}/checkdata/{dataName}_{dataSize}/{deviceID}")
      @Produces(MediaType.APPLICATION_JSON)
      public Response checkData(
              @PathParam("taskID") String taskID,
              @PathParam("dataName") String dataName,
              @PathParam("dataSize") long dataSize,
              @PathParam("deviceID") String deviceID) throws JSONException {
          String TAG = getClass().getName() + "@checkData: ";
  
          JSONObject response = new JSONObject();
  
          response.append("response", "oops");
  
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          String serverDir = taskDir + "/server";
          String dataUrl = clientDir + "/" + taskID + ".dat";
          String newDataUrl = clientDir + "/" + dataName + "@" + deviceID + ".dat";
  
          System.out.println(TAG + "Checking data " + newDataUrl + " | " + new File(newDataUrl).length() + "/" + dataSize);
  
          if (Utils.seekFile(newDataUrl)) {
              String compareResult = Utils.compareFiles(newDataUrl, dataSize);
              if ("equal".equals(compareResult)) {
                  File file = Utils.returnFileFrom(taskDir, ".xml");
                  String fileName = FilenameUtils.removeExtension(file.getName());
                  String xmlUrl = taskDir + "/" + fileName + ".xml";
                  Utils.setText(xmlUrl, "new", "yes");
                  Utils.setText(xmlUrl, "counter", String.valueOf(Integer.valueOf(Utils.getText(xmlUrl, "counter")) + 1));
                  addLog(xmlUrl, dataName, deviceID);
  //
  //                mergeData(dataUrl, newDataUrl);
  //                
  
                  try {
                      FileInputStream fis = new FileInputStream(newDataUrl);
                      ObjectInputStream ois = new ObjectInputStream(fis);
                      List<Object> newData = (List) ois.readObject();
                      saveDataToFile(newData, dataUrl);
  
                      close(ois);
                      close(fis);
                      System.gc();
  
                      File newDataFile = new File(newDataUrl);
                      if (newDataFile.delete()) {
                          System.out.println(TAG + "Deleted " + newDataUrl);
                      } else {
                          System.out.println(TAG + "Error deleting " + newDataUrl);
                      }
                  } catch (IOException | ClassNotFoundException ex) {
                      System.out.println(TAG + ex.getMessage());
                  }
  
                  /*if (Utils.getText(xmlUrl, "server") != null) {
                   FileInputStream fis;
                   ObjectInputStream in;
                   fis = new FileInputStream(newDataUrl);
                   in = new ObjectInputStream(fis);
                   Object object = (Object) in.readObject();
                   String serverFileName = Utils.getText(xmlUrl, "server", "name");
                   String serverClassName = serverFileName.substring(0, serverFileName.indexOf("."));
                   loadClass(serverDir, serverClassName, object);
                   }/**/
                  response.put("response", "OK");
              } else if ("larger".equals(compareResult)) {
                  response.put("response", "put");
                  response.append("checked", String.valueOf(Utils.getFileSize(newDataUrl)));
              } else {
                  new File(newDataUrl).delete();
              }
          }
          Utils.updateDeviceActivity(deviceID);
  
          System.out.println(TAG + response.toString());
  
          return Response.ok().entity(response.toString()).build();
      }
  
      @GET
      @Path("{taskID}/getprop/{deviceID}")
      @Produces(MediaType.APPLICATION_JSON)
      public Task getProp(
              @PathParam("taskID") String taskID,
              @PathParam("deviceID") String deviceID) throws Exception {
          String taskDir = Utils.getDir(app, taskID);
          String xmlUrl = taskDir + "/" + taskID + ".xml";
          Task task = null;
          //System.out.println("TaskService/getProp: Returning task prop with id " + taskID + " ...");
          taskDir = Utils.getDir(app, taskID);
          xmlUrl = taskDir + "/" + taskID + ".xml";
          task = new Task(xmlUrl);
          Utils.updateDeviceActivity(deviceID);
          return task;
      }
  
      /*
       * Description: Return the source code of a task.
       * Parameters : - The task ID.
       * Returns    : - The task source code.
       * Changelog  : - .
       */
      @GET
      @Path("{taskID}/getsrc/")
      @Produces(MediaType.APPLICATION_JSON)
      public Object getSrc(@PathParam("taskID") String taskID) {
          String TAG = TaskService.class.getName() + "@getSrc: ";
          String taskDir = Utils.getDir(app, taskID);
          String xmlUrl = taskDir + "/" + taskID + ".xml";
          String taskName = Utils.getText(xmlUrl, "name");
          String srcUrl = taskDir + "/client/" + taskName;
          System.out.println(TAG + "Returning task source with id " + taskID + " from " + taskName + "...");
          FileInputStream fis = null;
          String src = "/*\n";
          src += " * " + Utils.getText(xmlUrl, "name") + "\n";
          src += " * " + Utils.getText(xmlUrl, "comment") + "\n";
          src += " * " + "assigned id " + Utils.getText(xmlUrl, "id") + "\n";
          src += " * " + "submitted by " + Utils.getText(xmlUrl, "username") + "\n";
          src += " * " + "on " + Utils.getText(xmlUrl, "date") + " at " + Utils.getText(xmlUrl, "time") + "\n";
          src += " */\n\n";
          try {
              fis = new FileInputStream(srcUrl);
              BufferedInputStream bis = new BufferedInputStream(fis);
              DataInputStream dis = new DataInputStream(bis);
              while (dis.available() != 0) {
                  src += (dis.readLine()) + "\n";
              }
              close(dis);
              close(bis);
              close(fis);
  
          } catch (IOException ex) {
              System.out.println(TAG + ex.getMessage());
          }
          return src;
      }
  
      /*
       * Description: Return the source code of a task.
       * Parameters : - The task ID.
       * Returns    : - The task source code.
       * Changelog  : - .
       */
      @GET
      @Path("getlist/")
      @Produces(MediaType.TEXT_HTML)
      public InputStream getList() {
          String TAG = TaskService.class.getName() + "@getList: ";
          String html = "<h1>List of submitted Sensing Tasks</h1>";
          try {
              Class.forName("com.mysql.jdbc.Driver");
              try (Connection c = DriverManager.getConnection(Globals.db_server, Globals.db_username, Globals.db_password)) {
                  ResultSet rs;
                  try (Statement s = c.createStatement()) {
                      rs = s.executeQuery("SELECT * FROM tasks WHERE ready='YES'");
                      html += "<ol type=\"1\">";
                      while (rs.next()) {
                          String id = rs.getString("id");
                          String dir = Utils.getDir(app, id);
                          String xml = dir + "/" + id + ".xml";
                          html += "<li value =\"" + Utils.getText(xml, "id") + "\"><a href=" + Globals.server_url + "/webresources/tasks/" + id + "/getsrc>"
                                  + Utils.getText(xml, "name") + ""
                                  + ": " + Utils.getText(xml, "comment") + ""
                                  + " submitted by " + Utils.getText(xml, "username") + ""
                                  + " on " + Utils.getText(xml, "date") + " at " + Utils.getText(xml, "time")
                                  + "</a></li>";
                      }
                      html += "</ol>";
                  }
                  rs.close();
                  c.close();
              }
          } catch (ClassNotFoundException | SQLException ex) {
              Logger.getLogger(TaskService.class.getName()).log(Level.SEVERE, null, ex);
          }
          InputStream is = new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));
          return is;
      }
  
      /* 
       * Data 
       */
      @GET
      @Path("{taskID}/getdata")
      @Produces(MediaType.MULTIPART_FORM_DATA)
      public Object getData(@PathParam("taskID") String taskID) throws Exception {
          String TAG = getClass().getName() + "@getData: ";
  
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          String dataUrl = clientDir + "/" + taskID + ".dat";
          File data = new File(dataUrl);
  //        File data = new File(Globals.db_dir + "/data");
          if (data.exists()) {
              System.out.println(TAG + "Returning data " + dataUrl);
              //data.delete();
              return Response.ok("OK").header("Content-Disposition", "attachment; filename=\"" + taskID + ".dat\"").entity(data).build();
          } else {
              System.out.println(TAG + "No data found " + dataUrl);
              return Response.ok("OK").header("Content-Disposition", "attachment; filename=\"" + taskID + ".dat\"").entity(null).build();
          }
      }
  
      @GET
      @Path("{taskID}/getdataOLD")
      @Produces(MediaType.MULTIPART_FORM_DATA)
      public Object getDataOLD(@PathParam("taskID") String taskID) throws Exception {
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          String dataUrl = clientDir + "/" + taskID + ".dat";
          final File data = new File(dataUrl);
          if (data.exists()) {
              String response = FileUtils.readFileToString(data);
              //data.delete();
              return Response.ok("OK").header("Content-Disposition", "attachment; filename=\"" + taskID + ".dat\"").entity(response).build();
          } else {
              return "Empty.";
          }
      }
  
      @GET
      @Path("{taskID}/deletedata")
      public void deleteData(@PathParam("taskID") String taskID) throws Exception {
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          String dataUrl = clientDir + "/" + taskID + ".dat";
          String xmlUrl = taskDir + "/" + taskID + ".xml";
          System.out.println("TaskService/deleteData: " + "Deleting data " + dataUrl);
          File data = new File(dataUrl);
          if (data.exists()) {
              File dir = new File(clientDir);
              for (File f : dir.listFiles()) {
                  if (f.getName().endsWith(".dat")) {
                      f.delete();
                  }
              }
  
              rmvLog(xmlUrl);
          }
      }
  
      @GET
      @Path("{taskID}/getdata/delete")
      @Produces(MediaType.MULTIPART_FORM_DATA)
      public Object downdelData(@PathParam("taskID") String taskID) throws Exception {
          String taskDir = Utils.getDir(app, taskID);
          String clientDir = taskDir + "/client";
          String dataUrl = clientDir + "/" + taskID + ".dat";
          String xmlUrl = taskDir + "/" + taskID + ".xml";
  