extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/grid/OpenGridServlet.java

Code
Comments
Other
Rev Date Author Line
2298 18 Mar 14 nicklas 1 package net.sf.basedb.reggie.grid;
2295 17 Mar 14 nicklas 2
2295 17 Mar 14 nicklas 3 import java.io.IOException;
7116 17 Apr 23 nicklas 4 import java.io.InputStream;
7116 17 Apr 23 nicklas 5 import java.io.InputStreamReader;
7116 17 Apr 23 nicklas 6 import java.io.PrintWriter;
2378 25 Apr 14 nicklas 7 import java.util.List;
7117 18 Apr 23 nicklas 8 import java.util.regex.Pattern;
2295 17 Mar 14 nicklas 9
2295 17 Mar 14 nicklas 10 import javax.servlet.ServletException;
2295 17 Mar 14 nicklas 11 import javax.servlet.http.HttpServlet;
2295 17 Mar 14 nicklas 12 import javax.servlet.http.HttpServletRequest;
2295 17 Mar 14 nicklas 13 import javax.servlet.http.HttpServletResponse;
2295 17 Mar 14 nicklas 14
3009 04 Dec 14 nicklas 15 import org.jdom2.Element;
2295 17 Mar 14 nicklas 16 import org.json.simple.JSONArray;
2295 17 Mar 14 nicklas 17 import org.json.simple.JSONObject;
2295 17 Mar 14 nicklas 18
2417 12 May 14 nicklas 19 import net.sf.basedb.core.AnyToAny;
2417 12 May 14 nicklas 20 import net.sf.basedb.core.BasicItem;
2295 17 Mar 14 nicklas 21 import net.sf.basedb.core.DbControl;
3837 12 Apr 16 nicklas 22 import net.sf.basedb.core.FileServer;
4306 17 Jan 17 nicklas 23 import net.sf.basedb.core.Include;
2417 12 May 14 nicklas 24 import net.sf.basedb.core.Item;
2297 18 Mar 14 nicklas 25 import net.sf.basedb.core.ItemNotFoundException;
2406 07 May 14 nicklas 26 import net.sf.basedb.core.MimeType;
3605 16 Nov 15 nicklas 27 import net.sf.basedb.core.Nameable;
2295 17 Mar 14 nicklas 28 import net.sf.basedb.core.SessionControl;
4306 17 Jan 17 nicklas 29 import net.sf.basedb.opengrid.AbstractHost;
4306 17 Jan 17 nicklas 30 import net.sf.basedb.opengrid.AbstractSession;
4306 17 Jan 17 nicklas 31 import net.sf.basedb.opengrid.CmdResult;
4306 17 Jan 17 nicklas 32 import net.sf.basedb.opengrid.OpenGrid;
4306 17 Jan 17 nicklas 33 import net.sf.basedb.opengrid.OpenGridCluster;
4306 17 Jan 17 nicklas 34 import net.sf.basedb.opengrid.RemoteHost;
4306 17 Jan 17 nicklas 35 import net.sf.basedb.opengrid.config.ConnectionInfo;
4306 17 Jan 17 nicklas 36 import net.sf.basedb.opengrid.filetransfer.ServletResponseDownloadTarget;
4306 17 Jan 17 nicklas 37 import net.sf.basedb.opengrid.json.JSONOption;
4306 17 Jan 17 nicklas 38 import net.sf.basedb.opengrid.json.JSONOptions;
4306 17 Jan 17 nicklas 39 import net.sf.basedb.opengrid.service.OpenGridService;
2598 22 Aug 14 nicklas 40 import net.sf.basedb.reggie.JsonUtil;
3605 16 Nov 15 nicklas 41 import net.sf.basedb.reggie.Reggie;
4306 17 Jan 17 nicklas 42 import net.sf.basedb.reggie.XmlConfig;
4306 17 Jan 17 nicklas 43 import net.sf.basedb.reggie.dao.ReggieRole;
7116 17 Apr 23 nicklas 44 import net.sf.basedb.reggie.extensions.TabularViewActionFactory;
2297 18 Mar 14 nicklas 45 import net.sf.basedb.util.Values;
2295 17 Mar 14 nicklas 46 import net.sf.basedb.util.error.ThrowableUtil;
2295 17 Mar 14 nicklas 47
2295 17 Mar 14 nicklas 48 /**
2295 17 Mar 14 nicklas 49   Get information about Open Grid Scheduler hosts.
2295 17 Mar 14 nicklas 50   
2295 17 Mar 14 nicklas 51   @author nicklas
2295 17 Mar 14 nicklas 52    @since 2.16
2295 17 Mar 14 nicklas 53  */
2298 18 Mar 14 nicklas 54 public class OpenGridServlet 
2295 17 Mar 14 nicklas 55   extends HttpServlet 
2295 17 Mar 14 nicklas 56 {
2295 17 Mar 14 nicklas 57
2295 17 Mar 14 nicklas 58
2295 17 Mar 14 nicklas 59   private static final long serialVersionUID = 7976046697638443809L;
2295 17 Mar 14 nicklas 60
2298 18 Mar 14 nicklas 61   public OpenGridServlet()
2295 17 Mar 14 nicklas 62   {}
2295 17 Mar 14 nicklas 63
2295 17 Mar 14 nicklas 64   @Override
2295 17 Mar 14 nicklas 65   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2295 17 Mar 14 nicklas 66     throws ServletException, IOException 
2295 17 Mar 14 nicklas 67   {
2295 17 Mar 14 nicklas 68     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 69     JsonUtil.setJsonResponseHeaders(resp);
2598 22 Aug 14 nicklas 70
2295 17 Mar 14 nicklas 71     JSONObject json = new JSONObject();
2295 17 Mar 14 nicklas 72     json.put("status", "ok");
2295 17 Mar 14 nicklas 73   
3975 26 May 16 nicklas 74     final SessionControl sc = Reggie.getSessionControl(req);
2295 17 Mar 14 nicklas 75     DbControl dc = null;
4306 17 Jan 17 nicklas 76     AbstractSession<?> session = null;
2295 17 Mar 14 nicklas 77     try
2295 17 Mar 14 nicklas 78     {
2295 17 Mar 14 nicklas 79       if ("GetHostInfo".equals(cmd))
2295 17 Mar 14 nicklas 80       {
6682 20 Apr 22 nicklas 81         dc = sc.newDbControl(":Job scheduler hosts");
4306 17 Jan 17 nicklas 82         
4306 17 Jan 17 nicklas 83         if (Values.getBoolean(req.getParameter("reload")))
4306 17 Jan 17 nicklas 84         {
4306 17 Jan 17 nicklas 85           ReggieRole.checkPermission(dc, "'" + cmd + "[reload]'", ReggieRole.ADMINISTRATOR);
4306 17 Jan 17 nicklas 86           Reggie.reloadConfig();
4306 17 Jan 17 nicklas 87         }
4306 17 Jan 17 nicklas 88         boolean includeUnconfigured = Values.getBoolean(req.getParameter("includeUnconfigured"));
6632 08 Mar 22 nicklas 89         boolean includeNotConnected = Values.getBoolean(req.getParameter("includeNotConnected"));
4306 17 Jan 17 nicklas 90         
4306 17 Jan 17 nicklas 91         String hostId = req.getParameter("hostId");
2960 19 Nov 14 nicklas 92         String config = req.getParameter("config");
4306 17 Jan 17 nicklas 93         JSONOptions includeClusterInfo = new JSONOptions();
4306 17 Jan 17 nicklas 94         includeClusterInfo.enable(JSONOption.CLUSTER_INFO);
4306 17 Jan 17 nicklas 95         includeClusterInfo.enable(JSONOption.NODE_INFO);
2860 27 Oct 14 nicklas 96         if (hostId == null)
2297 18 Mar 14 nicklas 97         {
4306 17 Jan 17 nicklas 98           JSONArray jsonHosts = new JSONArray();
4306 17 Jan 17 nicklas 99           for (OpenGridCluster host : OpenGridService.getInstance().getClusters(dc, Include.ALL))
2297 18 Mar 14 nicklas 100           {
6632 08 Mar 22 nicklas 101             boolean isConnected = host.getClusterInfo().isConnected();
6632 08 Mar 22 nicklas 102             XmlConfig cfg = Reggie.getConfig(host.getId());
6632 08 Mar 22 nicklas 103             
6632 08 Mar 22 nicklas 104             if (!isConnected && !includeNotConnected) continue;
6632 08 Mar 22 nicklas 105             if (cfg == null && !includeUnconfigured) continue;
6632 08 Mar 22 nicklas 106             
4306 17 Jan 17 nicklas 107             JSONObject jsonHost = host.asJSONObject(includeClusterInfo);
4306 17 Jan 17 nicklas 108             jsonHost.put("reggeConfigExists", cfg != null);
4306 17 Jan 17 nicklas 109             if (cfg != null)
4306 17 Jan 17 nicklas 110             {
4306 17 Jan 17 nicklas 111               jsonHost.put("priorities", getPrioritiesConfig(cfg));
6977 16 Jan 23 nicklas 112               jsonHost.put("partitions", getPartitionsConfig(cfg));
6632 08 Mar 22 nicklas 113               if (config != null) 
6632 08 Mar 22 nicklas 114               {
6632 08 Mar 22 nicklas 115                 JSONObject jsonConfig = getExtraConfig(cfg, config);
6632 08 Mar 22 nicklas 116                 if (jsonConfig.isEmpty()) continue;
6661 01 Apr 22 nicklas 117                 ((JSONObject)jsonHost.get("config")).putAll(jsonConfig);
6632 08 Mar 22 nicklas 118               }
4306 17 Jan 17 nicklas 119             }
6632 08 Mar 22 nicklas 120             jsonHosts.add(jsonHost);
2297 18 Mar 14 nicklas 121           }
4306 17 Jan 17 nicklas 122           json.put("hosts", jsonHosts);
2297 18 Mar 14 nicklas 123         }
2297 18 Mar 14 nicklas 124         else
2297 18 Mar 14 nicklas 125         {
4306 17 Jan 17 nicklas 126           OpenGridCluster host = OpenGridService.getInstance().getClusterById(dc, hostId);
4306 17 Jan 17 nicklas 127           if (host != null)
2297 18 Mar 14 nicklas 128           {
4306 17 Jan 17 nicklas 129             JSONObject jsonHost = host.asJSONObject(includeClusterInfo);
4306 17 Jan 17 nicklas 130             XmlConfig cfg = Reggie.getConfig(host.getId());
4306 17 Jan 17 nicklas 131             jsonHost.put("reggeConfigExists", cfg != null);
4306 17 Jan 17 nicklas 132             if (cfg != null)
4306 17 Jan 17 nicklas 133             {
4306 17 Jan 17 nicklas 134               jsonHost.put("priorities", getPrioritiesConfig(cfg));
6977 16 Jan 23 nicklas 135               jsonHost.put("partitions", getPartitionsConfig(cfg));
4306 17 Jan 17 nicklas 136               if (config != null) jsonHost.put("config", getExtraConfig(cfg, config));
4306 17 Jan 17 nicklas 137             }
4306 17 Jan 17 nicklas 138             json.put("host", jsonHost);
2297 18 Mar 14 nicklas 139           }
2297 18 Mar 14 nicklas 140         }
2295 17 Mar 14 nicklas 141       }
2295 17 Mar 14 nicklas 142       
2406 07 May 14 nicklas 143       else if ("ListProjectFiles".equals(cmd))
2406 07 May 14 nicklas 144       {
6337 16 Jun 21 nicklas 145         dc = sc.newDbControl(":View project archive files");
2406 07 May 14 nicklas 146         
3837 12 Apr 16 nicklas 147         String clusterId = Values.getStringOrNull(req.getParameter("clusterId"));
3837 12 Apr 16 nicklas 148         int fileServerId = Values.getInt(req.getParameter("fileserver"));
3837 12 Apr 16 nicklas 149         String archiveRoot = null;
2406 07 May 14 nicklas 150         String path = req.getParameter("path");
3605 16 Nov 15 nicklas 151         String archive = Values.getStringOrNull(req.getParameter("archive"));
2417 12 May 14 nicklas 152         String itemType = Values.getStringOrNull(req.getParameter("itemType"));
2417 12 May 14 nicklas 153         int itemId = Values.getInt(req.getParameter("itemId"));
3837 12 Apr 16 nicklas 154         
2417 12 May 14 nicklas 155         BasicItem item = null;
2417 12 May 14 nicklas 156         if (itemType != null)
2417 12 May 14 nicklas 157         {
2417 12 May 14 nicklas 158           item = Item.valueOf(itemType).getById(dc, itemId);
5553 12 Aug 19 nicklas 159           if (archive == null && Reggie.isExternalItem(((Nameable)item).getName()))
3605 16 Nov 15 nicklas 160           {
3605 16 Nov 15 nicklas 161             archive = "external-archive";
3605 16 Nov 15 nicklas 162           }
2417 12 May 14 nicklas 163         }
2417 12 May 14 nicklas 164         
4306 17 Jan 17 nicklas 165         AbstractHost<?> host = null;
3837 12 Apr 16 nicklas 166         if (clusterId != null)
3837 12 Apr 16 nicklas 167         {
4306 17 Jan 17 nicklas 168           net.sf.basedb.opengrid.OpenGridCluster cluster = net.sf.basedb.opengrid.service.OpenGridService.getInstance().getClusterById(dc, clusterId);
3837 12 Apr 16 nicklas 169           if (cluster == null)
3837 12 Apr 16 nicklas 170           {
3837 12 Apr 16 nicklas 171             throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
3837 12 Apr 16 nicklas 172           }
3837 12 Apr 16 nicklas 173           host = cluster;
4306 17 Jan 17 nicklas 174           XmlConfig cfg = Reggie.getConfig(clusterId);
4306 17 Jan 17 nicklas 175           
4306 17 Jan 17 nicklas 176           String projectRoot = cfg.getRequiredConfig("project-archive", null);
4306 17 Jan 17 nicklas 177           archiveRoot = cfg.getConfig(archive, null, projectRoot);
3837 12 Apr 16 nicklas 178         }
3837 12 Apr 16 nicklas 179         else
3837 12 Apr 16 nicklas 180         {
3837 12 Apr 16 nicklas 181           FileServer server = FileServer.getById(dc, fileServerId);
4306 17 Jan 17 nicklas 182           host = new RemoteHost(new ConnectionInfo(server));
3837 12 Apr 16 nicklas 183           archiveRoot = server.getRootPath();
3837 12 Apr 16 nicklas 184         }
4306 17 Jan 17 nicklas 185
4306 17 Jan 17 nicklas 186         ScriptUtil.checkValidPath(path, true, false);
4306 17 Jan 17 nicklas 187         session = host.connect(10);
4306 17 Jan 17 nicklas 188
3839 13 Apr 16 nicklas 189         // The 'find' command generate tab-separated output:
3839 13 Apr 16 nicklas 190         // 0: File depth in directory structure
3839 13 Apr 16 nicklas 191         // 1: Path to file relative the root
3839 13 Apr 16 nicklas 192         // 2: Size in bytes
3839 13 Apr 16 nicklas 193         // 3: date+time in format 'yyyyMMdd HHmm'
3839 13 Apr 16 nicklas 194         // 4: Owner of the file
3839 13 Apr 16 nicklas 195         String f = "find \"" + archiveRoot+ path + "\" -name \"*\" -type f -printf \"%d\\t%P\\t%s\\t%TY%Tm%Td %TH%TM\\t%u\\n\" | sort";
4306 17 Jan 17 nicklas 196         CmdResult<String> find = session.executeCmd(f, 10);
4311 17 Jan 17 nicklas 197         find.throwExceptionIfNonZeroExitStatus();
4311 17 Jan 17 nicklas 198         
4311 17 Jan 17 nicklas 199         JSONArray jsonFiles = new JSONArray();
4311 17 Jan 17 nicklas 200         for (String line : find.getStdout().split("\n"))
2406 07 May 14 nicklas 201         {
4311 17 Jan 17 nicklas 202           String[] l = line.split("\t", 5);
4311 17 Jan 17 nicklas 203           if (l.length == 5)
2406 07 May 14 nicklas 204           {
4311 17 Jan 17 nicklas 205             String filePath = l[1];
4311 17 Jan 17 nicklas 206             JSONObject jsonFile = new JSONObject();
4311 17 Jan 17 nicklas 207             jsonFile.put("size", Values.getLong(l[2]));
4311 17 Jan 17 nicklas 208             jsonFile.put("lastModified", l[3]);
4311 17 Jan 17 nicklas 209             jsonFile.put("owner", l[4]);
4311 17 Jan 17 nicklas 210             jsonFile.put("path", filePath);
4311 17 Jan 17 nicklas 211             jsonFile.put("archive", archive);
4311 17 Jan 17 nicklas 212             if (item != null)
2406 07 May 14 nicklas 213             {
4311 17 Jan 17 nicklas 214               int lastFolder = filePath.lastIndexOf('/');
4311 17 Jan 17 nicklas 215               // Is file linked directly?
4311 17 Jan 17 nicklas 216               boolean isLinked = AnyToAny.exists(dc, item, filePath.substring(lastFolder+1));
4311 17 Jan 17 nicklas 217               if (!isLinked)
2417 12 May 14 nicklas 218               {
4311 17 Jan 17 nicklas 219                 // Check if folder is linked
4311 17 Jan 17 nicklas 220                 int firstFolder = filePath.indexOf('/');
4311 17 Jan 17 nicklas 221                 if (firstFolder > 0)
2417 12 May 14 nicklas 222                 {
4311 17 Jan 17 nicklas 223                   isLinked = AnyToAny.exists(dc, item, filePath.substring(0, firstFolder));
2417 12 May 14 nicklas 224                 }
2417 12 May 14 nicklas 225               }
4311 17 Jan 17 nicklas 226               jsonFile.put("isLinked", isLinked);
2406 07 May 14 nicklas 227             }
4311 17 Jan 17 nicklas 228             jsonFiles.add(jsonFile);
2406 07 May 14 nicklas 229           }
2406 07 May 14 nicklas 230         }
4311 17 Jan 17 nicklas 231         json.put("projectArchive", archiveRoot);
4311 17 Jan 17 nicklas 232         json.put("files", jsonFiles);
4311 17 Jan 17 nicklas 233
2406 07 May 14 nicklas 234       }
7312 29 Aug 23 nicklas 235       else if ("DownloadProjectFile".equals(cmd) || "ViewProjectFile".equals(cmd))
2406 07 May 14 nicklas 236       {
4306 17 Jan 17 nicklas 237         
2406 07 May 14 nicklas 238         json = null; // No JSON output
6337 16 Jun 21 nicklas 239         dc = sc.newDbControl(":Download project archive file");
2406 07 May 14 nicklas 240         
3837 12 Apr 16 nicklas 241         String clusterId = Values.getStringOrNull(req.getParameter("clusterId"));
3837 12 Apr 16 nicklas 242         int fileServerId = Values.getInt(req.getParameter("fileserver"));
3837 12 Apr 16 nicklas 243         String archive = Values.getStringOrNull(req.getParameter("archive"));
3837 12 Apr 16 nicklas 244         String archiveRoot = null;
3837 12 Apr 16 nicklas 245         
4306 17 Jan 17 nicklas 246         AbstractHost<?> host = null;
3837 12 Apr 16 nicklas 247         if (clusterId != null)
2406 07 May 14 nicklas 248         {
4306 17 Jan 17 nicklas 249           OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
3837 12 Apr 16 nicklas 250           if (cluster == null)
3837 12 Apr 16 nicklas 251           {
3837 12 Apr 16 nicklas 252             throw new ItemNotFoundException("OpenGridScheduler[" + clusterId + "]");
3837 12 Apr 16 nicklas 253           }
3837 12 Apr 16 nicklas 254           host = cluster;
4306 17 Jan 17 nicklas 255           XmlConfig cfg = Reggie.getConfig(clusterId);
4306 17 Jan 17 nicklas 256           String projectRoot = cfg.getRequiredConfig("project-archive", null);
4306 17 Jan 17 nicklas 257           archiveRoot = cfg.getConfig(archive, null, projectRoot);
2406 07 May 14 nicklas 258         }
3837 12 Apr 16 nicklas 259         else
3837 12 Apr 16 nicklas 260         {
3837 12 Apr 16 nicklas 261           FileServer server = FileServer.getById(dc, fileServerId);
4306 17 Jan 17 nicklas 262           host = new RemoteHost(new ConnectionInfo(server));
3837 12 Apr 16 nicklas 263           archiveRoot = server.getRootPath();
3837 12 Apr 16 nicklas 264         }
2406 07 May 14 nicklas 265         
2406 07 May 14 nicklas 266         String path = req.getParameter("path");
4306 17 Jan 17 nicklas 267         ScriptUtil.checkValidPath(path, false, true);
2406 07 May 14 nicklas 268         
2406 07 May 14 nicklas 269         int size = Values.getInt(req.getParameter("size"));
2406 07 May 14 nicklas 270         String[] parts = path.split("/");
2406 07 May 14 nicklas 271         String fileName = parts[parts.length-1];
2406 07 May 14 nicklas 272         String mimeType = "application/octet-stream";
2406 07 May 14 nicklas 273         int i = fileName.lastIndexOf('.');
2406 07 May 14 nicklas 274         if (i > 0)
2406 07 May 14 nicklas 275         {
2406 07 May 14 nicklas 276           try
2406 07 May 14 nicklas 277           {
2406 07 May 14 nicklas 278             MimeType mt = MimeType.getByExtension(dc, fileName.substring(i+1));
2406 07 May 14 nicklas 279             mimeType = mt.getName();
2406 07 May 14 nicklas 280           }
2406 07 May 14 nicklas 281           catch (Exception ex)
2406 07 May 14 nicklas 282           {}
2406 07 May 14 nicklas 283         }
7312 29 Aug 23 nicklas 284         if ("DownloadProjectFile".equals(cmd))
7312 29 Aug 23 nicklas 285         {
7312 29 Aug 23 nicklas 286           resp.setHeader("Content-Disposition", "attachment; filename=" + fileName);
7312 29 Aug 23 nicklas 287         }
2406 07 May 14 nicklas 288         resp.setContentType(mimeType);
2406 07 May 14 nicklas 289         if (size > 0) resp.setContentLength(size);
4306 17 Jan 17 nicklas 290
4306 17 Jan 17 nicklas 291         session = host.connect(5);
4306 17 Jan 17 nicklas 292         session.downloadFile(archiveRoot + path, new ServletResponseDownloadTarget(resp, fileName));
2406 07 May 14 nicklas 293       }
7116 17 Apr 23 nicklas 294       else if ("ViewTabularFile".equals(cmd))
7116 17 Apr 23 nicklas 295       {
7116 17 Apr 23 nicklas 296         json = null; // No JSON output
7116 17 Apr 23 nicklas 297         dc = sc.newDbControl(":View tabular project archive file");
7116 17 Apr 23 nicklas 298         
7116 17 Apr 23 nicklas 299         String clusterId = Values.getStringOrNull(req.getParameter("clusterId"));
7116 17 Apr 23 nicklas 300         int fileServerId = Values.getInt(req.getParameter("fileserver"));
7116 17 Apr 23 nicklas 301         String archive = Values.getStringOrNull(req.getParameter("archive"));
7116 17 Apr 23 nicklas 302         String archiveRoot = null;
7116 17 Apr 23 nicklas 303         
7116 17 Apr 23 nicklas 304         AbstractHost<?> host = null;
7116 17 Apr 23 nicklas 305         if (clusterId != null)
7116 17 Apr 23 nicklas 306         {
7116 17 Apr 23 nicklas 307           OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
7116 17 Apr 23 nicklas 308           if (cluster == null)
7116 17 Apr 23 nicklas 309           {
7116 17 Apr 23 nicklas 310             throw new ItemNotFoundException("OpenGridScheduler[" + clusterId + "]");
7116 17 Apr 23 nicklas 311           }
7116 17 Apr 23 nicklas 312           host = cluster;
7116 17 Apr 23 nicklas 313           XmlConfig cfg = Reggie.getConfig(clusterId);
7116 17 Apr 23 nicklas 314           String projectRoot = cfg.getRequiredConfig("project-archive", null);
7116 17 Apr 23 nicklas 315           archiveRoot = cfg.getConfig(archive, null, projectRoot);
7116 17 Apr 23 nicklas 316         }
7116 17 Apr 23 nicklas 317         else
7116 17 Apr 23 nicklas 318         {
7116 17 Apr 23 nicklas 319           FileServer server = FileServer.getById(dc, fileServerId);
7116 17 Apr 23 nicklas 320           host = new RemoteHost(new ConnectionInfo(server));
7116 17 Apr 23 nicklas 321           archiveRoot = server.getRootPath();
7116 17 Apr 23 nicklas 322         }
7116 17 Apr 23 nicklas 323
7116 17 Apr 23 nicklas 324         String path = req.getParameter("path");
7116 17 Apr 23 nicklas 325         ScriptUtil.checkValidPath(path, false, true);
7116 17 Apr 23 nicklas 326         session = host.connect(5);
7116 17 Apr 23 nicklas 327         InputStream download = session.readFile(archiveRoot + path, null);
7117 18 Apr 23 nicklas 328         Pattern columnSeparator = null;
7117 18 Apr 23 nicklas 329         // For VCF files we force tab since auto-detction will most likely find semicolon in the INFO field
7117 18 Apr 23 nicklas 330         if (path.endsWith(".vcf")) columnSeparator = Pattern.compile("\\t");
7117 18 Apr 23 nicklas 331
7116 17 Apr 23 nicklas 332         resp.setContentType("text/html");
7116 17 Apr 23 nicklas 333         resp.setCharacterEncoding("UTF-8");
7116 17 Apr 23 nicklas 334         PrintWriter out = resp.getWriter();
7117 18 Apr 23 nicklas 335         TabularViewActionFactory.tabularFormat(new InputStreamReader(download, "UTF-8"), out, path, columnSeparator);
7116 17 Apr 23 nicklas 336         out.flush();
7116 17 Apr 23 nicklas 337         out.close();
7116 17 Apr 23 nicklas 338         download.close();
7116 17 Apr 23 nicklas 339       }
7116 17 Apr 23 nicklas 340
2295 17 Mar 14 nicklas 341     }
2295 17 Mar 14 nicklas 342     catch (Throwable t)
2295 17 Mar 14 nicklas 343     {
2295 17 Mar 14 nicklas 344       t.printStackTrace();
2406 07 May 14 nicklas 345       if (json != null)
2406 07 May 14 nicklas 346       {
2406 07 May 14 nicklas 347         json.clear();
2406 07 May 14 nicklas 348         json.put("status", "error");
2406 07 May 14 nicklas 349         json.put("message", t.getMessage());
2406 07 May 14 nicklas 350         json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2406 07 May 14 nicklas 351       }
2406 07 May 14 nicklas 352       else
2406 07 May 14 nicklas 353       {
2406 07 May 14 nicklas 354         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
2406 07 May 14 nicklas 355       }
2295 17 Mar 14 nicklas 356     }
2295 17 Mar 14 nicklas 357     finally
2295 17 Mar 14 nicklas 358     {
4306 17 Jan 17 nicklas 359       OpenGrid.close(session);
2295 17 Mar 14 nicklas 360       if (dc != null) dc.close();
2406 07 May 14 nicklas 361       if (json != null) json.writeJSONString(resp.getWriter());
2295 17 Mar 14 nicklas 362     }
2295 17 Mar 14 nicklas 363     
2295 17 Mar 14 nicklas 364   }
2297 18 Mar 14 nicklas 365   
4306 17 Jan 17 nicklas 366   /**
4306 17 Jan 17 nicklas 367     Get pre-defined job priority values.
4306 17 Jan 17 nicklas 368   */
4306 17 Jan 17 nicklas 369   private JSONArray getPrioritiesConfig(XmlConfig cfg)
2378 25 Apr 14 nicklas 370   {
4306 17 Jan 17 nicklas 371     Element config = cfg.getRoot();
2861 27 Oct 14 nicklas 372     // Load priorities configuration
2861 27 Oct 14 nicklas 373     Element priorities = config.getChild("priorities");
2861 27 Oct 14 nicklas 374     JSONArray jsonPriorities = new JSONArray();
2861 27 Oct 14 nicklas 375     if (priorities != null)
2378 25 Apr 14 nicklas 376     {
5415 09 May 19 nicklas 377       List<Element> list = priorities.getChildren("priority");
2861 27 Oct 14 nicklas 378       for (Element p : list)
2378 25 Apr 14 nicklas 379       {
2861 27 Oct 14 nicklas 380         JSONObject jsonPriority = new JSONObject();
2861 27 Oct 14 nicklas 381         jsonPriority.put("name", p.getAttributeValue("name"));
2861 27 Oct 14 nicklas 382         jsonPriority.put("value", Values.getInteger(p.getAttributeValue("value"), null));
2861 27 Oct 14 nicklas 383         jsonPriority.put("default", Values.getBoolean(p.getAttributeValue("default"), false));
2861 27 Oct 14 nicklas 384         jsonPriorities.add(jsonPriority);
2378 25 Apr 14 nicklas 385       }
2378 25 Apr 14 nicklas 386     }
4306 17 Jan 17 nicklas 387     return jsonPriorities;
4306 17 Jan 17 nicklas 388   }
4306 17 Jan 17 nicklas 389   
4306 17 Jan 17 nicklas 390   /**
6977 16 Jan 23 nicklas 391     Get pre-defined partitions (queues) for submitting jobs.
6977 16 Jan 23 nicklas 392     @since 4.42
6977 16 Jan 23 nicklas 393   */
6977 16 Jan 23 nicklas 394   private JSONArray getPartitionsConfig(XmlConfig cfg)
6977 16 Jan 23 nicklas 395   {
6977 16 Jan 23 nicklas 396     Element config = cfg.getRoot();
6977 16 Jan 23 nicklas 397     // Load partitions configuration
6977 16 Jan 23 nicklas 398     Element partitions = config.getChild("partitions");
6977 16 Jan 23 nicklas 399     JSONArray jsonPartitions = new JSONArray();
6977 16 Jan 23 nicklas 400     if (partitions != null)
6977 16 Jan 23 nicklas 401     {
6977 16 Jan 23 nicklas 402       List<Element> list = partitions.getChildren("partition");
6977 16 Jan 23 nicklas 403       for (Element p : list)
6977 16 Jan 23 nicklas 404       {
6977 16 Jan 23 nicklas 405         JSONObject jsonPartition = new JSONObject();
6977 16 Jan 23 nicklas 406         jsonPartition.put("name", p.getAttributeValue("name"));
6977 16 Jan 23 nicklas 407         jsonPartition.put("value", p.getAttributeValue("value"));
6979 16 Jan 23 nicklas 408         jsonPartition.put("description", p.getAttributeValue("description"));
6977 16 Jan 23 nicklas 409         jsonPartition.put("default", Values.getBoolean(p.getAttributeValue("default"), false));
6977 16 Jan 23 nicklas 410         jsonPartitions.add(jsonPartition);
6977 16 Jan 23 nicklas 411       }
6977 16 Jan 23 nicklas 412     }
6977 16 Jan 23 nicklas 413     return jsonPartitions;
6977 16 Jan 23 nicklas 414   }
6977 16 Jan 23 nicklas 415
6977 16 Jan 23 nicklas 416   
6977 16 Jan 23 nicklas 417   /**
4306 17 Jan 17 nicklas 418     Extract configuration settings (the text within tags) from reggie-config.xml 
4306 17 Jan 17 nicklas 419     and return as a JSON object. Subtags are not included.
2890 03 Nov 14 nicklas 420     
4306 17 Jan 17 nicklas 421     @param cfg The main configuration (for a host)
4306 17 Jan 17 nicklas 422     @param config The tag name in the config that should be included
4306 17 Jan 17 nicklas 423   */
4306 17 Jan 17 nicklas 424   private JSONObject getExtraConfig(XmlConfig cfg, String config)
4306 17 Jan 17 nicklas 425   {
2960 19 Nov 14 nicklas 426     // Extra config options
2960 19 Nov 14 nicklas 427     JSONObject jsonConfig = new JSONObject();
6693 22 Apr 22 nicklas 428     Element xc = cfg.getElement(config);
4306 17 Jan 17 nicklas 429     if (xc != null)
2960 19 Nov 14 nicklas 430     {
4306 17 Jan 17 nicklas 431       JSONObject jsonXc = new JSONObject();
4306 17 Jan 17 nicklas 432       jsonConfig.put(config, jsonXc);
5415 09 May 19 nicklas 433       for (Element e : xc.getChildren())
2960 19 Nov 14 nicklas 434       {
7372 06 Oct 23 nicklas 435         String name = e.getName();
7372 06 Oct 23 nicklas 436         String value = e.getTextTrim();
7372 06 Oct 23 nicklas 437         String ps = e.getAttributeValue("parameter-set");
7372 06 Oct 23 nicklas 438         if (ps != null)
7372 06 Oct 23 nicklas 439         {
7372 06 Oct 23 nicklas 440           boolean extendsDefault = Values.getBoolean(e.getAttributeValue("extends-default"));
7372 06 Oct 23 nicklas 441           String defaultValue = (String)jsonXc.get(name);
7372 06 Oct 23 nicklas 442           for (String pset : ps.split(","))
7372 06 Oct 23 nicklas 443           {
7372 06 Oct 23 nicklas 444             jsonXc.put(name+"."+pset, (extendsDefault && defaultValue != null ? defaultValue+"\n" : "")+value);
7372 06 Oct 23 nicklas 445           }
7372 06 Oct 23 nicklas 446         }
7372 06 Oct 23 nicklas 447         else
7372 06 Oct 23 nicklas 448         {
7372 06 Oct 23 nicklas 449           jsonXc.put(name, value);
7372 06 Oct 23 nicklas 450         }
2960 19 Nov 14 nicklas 451       }
2960 19 Nov 14 nicklas 452     }
4306 17 Jan 17 nicklas 453     return jsonConfig;
2378 25 Apr 14 nicklas 454   }
2295 17 Mar 14 nicklas 455 }