/alaspatial/src/main/java/org/ala/spatial/web/services/DownloadController.java

http://alageospatialportal.googlecode.com/ · Java · 394 lines · 267 code · 77 blank · 50 comment · 61 complexity · ae78788a531032aeaafcabad0e8e3d21 MD5 · raw file

  1. /**
  2. * ************************************************************************
  3. * Copyright (C) 2010 Atlas of Living Australia All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. * the specific language governing rights and limitations under the License.
  12. * *************************************************************************
  13. */
  14. package org.ala.spatial.web.services;
  15. import java.io.*;
  16. import java.util.Iterator;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.swing.filechooser.FileNameExtensionFilter;
  19. import org.ala.spatial.util.AlaspatialProperties;
  20. import org.ala.spatial.util.Zipper;
  21. import org.apache.commons.io.FileUtils;
  22. import org.apache.commons.io.IOCase;
  23. import org.apache.commons.io.filefilter.DirectoryFileFilter;
  24. import org.apache.commons.io.filefilter.SuffixFileFilter;
  25. import org.springframework.stereotype.Controller;
  26. import org.springframework.util.FileCopyUtils;
  27. import org.springframework.web.bind.annotation.PathVariable;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RequestMethod;
  30. import org.springframework.web.bind.annotation.ResponseBody;
  31. /**
  32. *
  33. * @author ajay
  34. */
  35. @Controller
  36. @RequestMapping("/ws/download/")
  37. public class DownloadController {
  38. @RequestMapping(value = "/session/{sessionid}", method = RequestMethod.GET)
  39. @ResponseBody
  40. public String downloadSessionUrl(@PathVariable String sessionid, HttpServletResponse response) {
  41. try {
  42. String basedir = AlaspatialProperties.getBaseOutputDir() + File.separator + "output" + File.separator;
  43. String sessiondir = basedir + "session" + File.separator + sessionid;
  44. File sDir = new File(sessiondir);
  45. if (sDir.exists() && sDir.isDirectory()) {
  46. StringBuffer sbSession = new StringBuffer();
  47. sbSession.append("[InternetShortcut]").append("\n");
  48. sbSession.append("URL=").append(AlaspatialProperties.getBaseOutputURL());
  49. sbSession.append("?ss=").append(sessionid).append("\n");
  50. response.setContentType("application/internet-shortcut");
  51. response.setContentLength(safeLongToInt(sbSession.length()));
  52. response.setHeader("Content-Disposition", "attachment; filename=\"ALA_Spatial_Portal_Session_" + sessionid + ".url\"");
  53. FileCopyUtils.copy(sbSession.toString(), response.getWriter());
  54. return null;
  55. }
  56. } catch (Exception e) {
  57. System.out.println("Could not find session directory");
  58. }
  59. return "";
  60. }
  61. @RequestMapping(value = "/{pid}", method = RequestMethod.GET)
  62. @ResponseBody
  63. public String download(@PathVariable String pid, HttpServletResponse response) {
  64. try {
  65. File dir = findFile(pid);
  66. if (dir != null) {
  67. String parentName = "ALA_";
  68. String parentPath = dir.getParent().substring(dir.getParent().lastIndexOf("/") + 1);
  69. String zipfile = dir.getParent() + "/" + pid + ".zip";
  70. if ("maxent".equals(parentPath)) {
  71. fixMaxentFiles(pid, dir);
  72. Zipper.zipDirectory(dir.getParent() + "/temp/" + pid, zipfile);
  73. } else if ("layers".equals(parentPath) || "aloc".equals(parentPath)) {
  74. fixAlocFiles(pid, dir);
  75. Zipper.zipDirectory(dir.getParent() + "/temp/" + pid, zipfile);
  76. }else if ("gdm".equals(parentPath)) {
  77. fixGdmFiles(pid, dir);
  78. Zipper.zipDirectory(dir.getParent()+"/temp/"+pid, zipfile);
  79. } else {
  80. Zipper.zipDirectory(dir.getAbsolutePath(), zipfile);
  81. }
  82. System.out.println("Found " + dir.getName() + " in " + dir.getParent() + " and zipped at: " + zipfile);
  83. //return "Found " + dir.getName() + " in " + dir.getParent() + " and zipped at: " + zipfile;
  84. if ("maxent".equals(parentPath)) {
  85. parentName = "ALA_Prediction_";
  86. } else if ("sampling".equals(parentPath)) {
  87. parentName = "ALA_Species_Samples_";
  88. } else if ("layers".equals(parentPath) || "aloc".equals(parentPath)) {
  89. parentName = "ALA_Classification_";
  90. } else if ("gdm".equals(parentPath)) {
  91. parentName = "ALA_GDM_";
  92. } else if ("filtering".equals(parentPath)) {
  93. parentName = "ALA_EnvFilter_";
  94. } else if ("sitesbyspecies".equals(parentPath)) {
  95. parentName = "ALA_SitesBySpecies_";
  96. }
  97. File file = new File(zipfile);
  98. response.setContentType("application/zip");
  99. response.setContentLength(safeLongToInt(file.length()));
  100. response.setHeader("Content-Disposition", "attachment; filename=\"" + parentName + pid + ".zip\"");
  101. FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
  102. return null;
  103. } else {
  104. System.out.println("Could not find session data");
  105. return "Could not find session data";
  106. }
  107. } catch (Exception e) {
  108. System.out.println("Unable to download:");
  109. e.printStackTrace(System.out);
  110. }
  111. return "";
  112. }
  113. private File findFile(String pid) {
  114. try {
  115. System.out.println("Looking for: " + pid + " to downloadd");
  116. // the 'pid's are unique, so lets figure out
  117. // which directory they live under.
  118. String basedir = AlaspatialProperties.getBaseOutputDir() + File.separator + "output" + File.separator;
  119. File baseDir = new File(basedir);
  120. FilenameFilter ff = DirectoryFileFilter.DIRECTORY;
  121. File[] files = baseDir.listFiles(ff);
  122. for (int i = 0; i < files.length; i++) {
  123. File f = files[i];
  124. if (f.isDirectory() && !f.getName().equalsIgnoreCase("layers")) {
  125. if (f.getName().equalsIgnoreCase(pid)) {
  126. return f;
  127. } else {
  128. File[] files2 = f.listFiles(ff);
  129. for (int j = 0; j < files2.length; j++) {
  130. File f2 = files2[j];
  131. if (f2.getName().equalsIgnoreCase(pid)) {
  132. return f2;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. } catch (Exception e) {
  139. System.out.println("Error finding session data:");
  140. e.printStackTrace(System.out);
  141. }
  142. return null;
  143. }
  144. private static void fixMaxentFiles(String pid, File dir) {
  145. try {
  146. File tmpdir = new File(dir.getParent() + "/temp/" + pid);
  147. //FileCopyUtils.copy(new FileInputStream(dir), new FileOutputStream(tmpdir));
  148. FileUtils.copyDirectory(dir, tmpdir);
  149. //File grd = new File(tmpdir.getAbsolutePath() + "/" + pid + ".grd");
  150. //File grdnew = new File(tmpdir.getAbsolutePath() + "/prediction.grd");
  151. //File gri = new File(tmpdir.getAbsolutePath() + "/" + pid + ".gri");
  152. //File grinew = new File(tmpdir.getAbsolutePath() + "/prediction.gri");
  153. File rsp = new File(tmpdir.getAbsolutePath() + "/removedSpecies.txt");
  154. File rspnew = new File(tmpdir.getAbsolutePath() + "/prediction_removedSpecies.txt");
  155. File msp = new File(tmpdir.getAbsolutePath() + "/maskedOutSensitiveSpecies.txt");
  156. File mspnew = new File(tmpdir.getAbsolutePath() + "/prediction_maskedOutSensitiveSpecies.txt");
  157. //grd.renameTo(grdnew);
  158. //gri.renameTo(grinew);
  159. rsp.renameTo(rspnew);
  160. msp.renameTo(mspnew);
  161. (new File(tmpdir.getAbsolutePath() + "/species.zip")).delete();
  162. (new File(tmpdir.getAbsolutePath() + "/species.bat")).delete();
  163. (new File(tmpdir.getAbsolutePath() + "/" + pid + ".grd")).delete();
  164. (new File(tmpdir.getAbsolutePath() + "/" + pid + ".gri")).delete();
  165. } catch (Exception e) {
  166. System.out.println("Unable to fix Prediction files");
  167. e.printStackTrace(System.out);
  168. }
  169. }
  170. private static void fixAlocFiles(String pid, File dir) {
  171. try {
  172. File tmpdir = new File(dir.getParent() + "/temp/" + pid);
  173. //FileCopyUtils.copy(new FileInputStream(dir), new FileOutputStream(tmpdir));
  174. FileUtils.copyDirectory(dir, tmpdir);
  175. //File grd = new File(tmpdir.getAbsolutePath() + "/" + pid + ".grd");
  176. //File grdnew = new File(tmpdir.getAbsolutePath() + "/classfication.grd");
  177. //File gri = new File(tmpdir.getAbsolutePath() + "/" + pid + ".gri");
  178. //File grinew = new File(tmpdir.getAbsolutePath() + "/classfication.gri");
  179. File asc = new File(tmpdir.getAbsolutePath() + "/" + pid + ".asc");
  180. File ascnew = new File(tmpdir.getAbsolutePath() + "/classfication.asc");
  181. File prj = new File(tmpdir.getAbsolutePath() + "/" + pid + ".prj");
  182. File prjnew = new File(tmpdir.getAbsolutePath() + "/classfication.prj");
  183. File png = new File(tmpdir.getAbsolutePath() + "/aloc.png");
  184. File pngnew = new File(tmpdir.getAbsolutePath() + "/classfication.png");
  185. File pgw = new File(tmpdir.getAbsolutePath() + "/aloc.pgw");
  186. File pgwnew = new File(tmpdir.getAbsolutePath() + "/classfication.pgw");
  187. File log = new File(tmpdir.getAbsolutePath() + "/aloc.log");
  188. File lognew = new File(tmpdir.getAbsolutePath() + "/classfication.log");
  189. //grd.renameTo(grdnew);
  190. //gri.renameTo(grinew);
  191. asc.renameTo(ascnew);
  192. prj.renameTo(prjnew);
  193. png.renameTo(pngnew);
  194. pgw.renameTo(pgwnew);
  195. log.renameTo(lognew);
  196. (new File(tmpdir.getAbsolutePath() + "/" + pid + ".asc.zip")).delete();
  197. (new File(tmpdir.getAbsolutePath() + "/" + pid + ".sld")).delete();
  198. (new File(tmpdir.getAbsolutePath() + "/aloc.pngextents.txt")).delete();
  199. (new File(tmpdir.getAbsolutePath() + "/aloc.prj")).delete();
  200. (new File(tmpdir.getAbsolutePath() + "/t_aloc.tif")).delete();
  201. (new File(tmpdir.getAbsolutePath() + "/t_aloc.png")).delete();
  202. (new File(tmpdir.getAbsolutePath() + "/" + pid + ".grd")).delete();
  203. (new File(tmpdir.getAbsolutePath() + "/" + pid + ".gri")).delete();
  204. } catch (Exception e) {
  205. System.out.println("Unable to fix Classification files");
  206. e.printStackTrace(System.out);
  207. }
  208. }
  209. private static void fixGdmFiles(String pid, File dir) {
  210. try {
  211. File tmpdir = new File(dir.getParent() + "/temp/" + pid);
  212. //FileCopyUtils.copy(new FileInputStream(dir), new FileOutputStream(tmpdir));
  213. FileUtils.copyDirectory(dir, tmpdir);
  214. //File rsp = new File(tmpdir.getAbsolutePath() + "/removedSpecies.txt");
  215. //File rspnew = new File(tmpdir.getAbsolutePath() + "/prediction_removedSpecies.txt");
  216. //File msp = new File(tmpdir.getAbsolutePath() + "/maskedOutSensitiveSpecies.txt");
  217. //File mspnew = new File(tmpdir.getAbsolutePath() + "/prediction_maskedOutSensitiveSpecies.txt");
  218. //rsp.renameTo(rspnew);
  219. //msp.renameTo(mspnew);
  220. //(new File(tmpdir.getAbsolutePath() + "/species.zip")).delete();
  221. FilenameFilter ff = new SuffixFileFilter(".grd");
  222. File[] files = tmpdir.listFiles(ff);
  223. for (int i = 0; i < files.length; i++) {
  224. File f = files[i];
  225. if (!f.getName().startsWith("domain")) {
  226. f.delete();
  227. }
  228. }
  229. } catch (Exception e) {
  230. System.out.println("Unable to fix Prediction files");
  231. e.printStackTrace(System.out);
  232. }
  233. }
  234. private static File sfindFile(String pid) {
  235. try {
  236. System.out.println("Looking for: " + pid + " to download");
  237. // the 'pid's are unique, so lets figure out
  238. // which directory they live under.
  239. String basedir = "/data/ala/runtime/output" + File.separator;
  240. File baseDir = new File(basedir);
  241. FilenameFilter ff = DirectoryFileFilter.DIRECTORY;
  242. File[] files = baseDir.listFiles(ff);
  243. for (int i = 0; i < files.length; i++) {
  244. File f = files[i];
  245. if (f.isDirectory() && !f.getName().equalsIgnoreCase("layers")) {
  246. if (f.getName().equalsIgnoreCase(pid)) {
  247. return f;
  248. } else {
  249. File[] files2 = f.listFiles(ff);
  250. for (int j = 0; j < files2.length; j++) {
  251. File f2 = files2[j];
  252. if (f2.getName().equalsIgnoreCase(pid)) {
  253. return f2;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. } catch (Exception e) {
  260. System.out.println("Error finding session data:");
  261. e.printStackTrace(System.out);
  262. }
  263. return null;
  264. }
  265. private int safeLongToInt(long l) {
  266. if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
  267. throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
  268. }
  269. return (int) l;
  270. }
  271. public static void main(String[] args) {
  272. // maxent - 1323641423144
  273. // aloc - 1323844048457
  274. String pid = "1323844048457";
  275. try {
  276. File dir = sfindFile(pid);
  277. if (dir != null) {
  278. //System.out.println("Found session data: " + dir.getAbsolutePath());
  279. //return "Found session data: " + dir.getAbsolutePath();
  280. String parentName = "ALA_";
  281. String parentPath = dir.getParent().substring(dir.getParent().lastIndexOf("/") + 1);
  282. String zipfile = dir.getParent() + "/" + pid + ".zip";
  283. if ("maxent".equals(parentPath)) {
  284. fixMaxentFiles(pid, dir);
  285. Zipper.zipDirectory(dir.getParent() + "/temp/" + pid, zipfile);
  286. } else if ("layers".equals(parentPath) || "aloc".equals(parentPath)) {
  287. fixAlocFiles(pid, dir);
  288. Zipper.zipDirectory(dir.getParent() + "/temp/" + pid, zipfile);
  289. } else {
  290. Zipper.zipDirectory(dir.getAbsolutePath(), zipfile);
  291. }
  292. System.out.println("Found " + dir.getName() + " in " + dir.getParent() + " and zipped at: " + zipfile);
  293. //return "Found " + dir.getName() + " in " + dir.getParent() + " and zipped at: " + zipfile;
  294. if ("maxent".equals(parentPath)) {
  295. parentName = "ALA_Prediction_";
  296. } else if ("sampling".equals(parentPath)) {
  297. parentName = "ALA_Species_Samples_";
  298. } else if ("layers".equals(parentPath) || "aloc".equals(parentPath)) {
  299. parentName = "ALA_Classification_";
  300. } else if ("gdm".equals(parentPath)) {
  301. parentName = "ALA_GDM_";
  302. } else if ("filtering".equals(parentPath)) {
  303. parentName = "ALA_EnvFilter_";
  304. } else if ("sitesbyspecies".equals(parentPath)) {
  305. parentName = "ALA_SitesBySpecies_";
  306. }
  307. File file = new File(zipfile);
  308. System.out.println("File generated: " + file.getAbsolutePath());
  309. } else {
  310. System.out.println("Could not find session data");
  311. //return "Could not find session data";
  312. }
  313. } catch (Exception e) {
  314. e.printStackTrace(System.out);
  315. }
  316. }
  317. }