PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/webswing-app-toolkit/src/main/java/org/webswing/toolkit/extra/WebShellFolderManager.java

https://bitbucket.org/isharasamantha/webswing
Java | 217 lines | 200 code | 17 blank | 0 comment | 33 complexity | 9d6d11f0d4214860ea505ca793a47641 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package org.webswing.toolkit.extra;
  2. import org.webswing.Constants;
  3. import org.webswing.toolkit.util.Logger;
  4. import sun.awt.shell.ShellFolder;
  5. import sun.awt.shell.ShellFolder.Invoker;
  6. import sun.awt.shell.Win32ShellFolderManager2;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10. import java.lang.reflect.Constructor;
  11. import java.lang.reflect.Method;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Collections;
  15. import java.util.List;
  16. @SuppressWarnings("restriction")
  17. public class WebShellFolderManager extends Win32ShellFolderManager2 {
  18. private boolean windows;
  19. private Object defaultManager;
  20. private File root;
  21. private List<File> roots = new ArrayList<File>();
  22. public WebShellFolderManager() {
  23. String path = System.getProperty(Constants.SWING_START_SYS_PROP_TRANSFER_DIR, System.getProperty("user.dir") + "/upload");
  24. String[] paths = path.split(File.pathSeparator);
  25. for (int i = 0; i < paths.length; i++) {
  26. File root = new IsolatedRootFile(paths[i]);
  27. if (!root.getAbsoluteFile().exists()) {
  28. root.mkdirs();
  29. }
  30. roots.add(root);
  31. if (i == 0) {
  32. this.root = root;
  33. System.setProperty("user.home", root.getAbsolutePath());
  34. }
  35. }
  36. windows = System.getProperty("os.name", "").startsWith("Windows");
  37. try {
  38. Class<?> managerClass = ClassLoader.getSystemClassLoader().loadClass("sun.awt.shell.ShellFolderManager");
  39. Constructor<?> c = managerClass.getDeclaredConstructor();
  40. c.setAccessible(true);
  41. defaultManager = c.newInstance();
  42. } catch (Exception e) {
  43. System.err.println("Error while instantiating default shell folder manager. " + e.getMessage());
  44. e.printStackTrace();
  45. }
  46. }
  47. @Override
  48. public Object get(String paramString) {
  49. if (paramString.equals("fileChooserDefaultFolder")) {
  50. return ensureExists(root);
  51. }
  52. if (paramString.equals("roots")) {
  53. return ensureExists(roots.toArray(new File[roots.size()]));
  54. }
  55. if (paramString.equals("fileChooserComboBoxFolders")) {
  56. return ensureExists(roots.toArray(new File[roots.size()]));
  57. }
  58. if (paramString.equals("fileChooserShortcutPanelFolders")) {
  59. return ensureExists(roots.toArray(new File[roots.size()]));
  60. }
  61. if (paramString.startsWith("fileChooserIcon ") || paramString.startsWith("optionPaneIcon ") || paramString.startsWith("shell32Icon ")) {
  62. return super.get(paramString);
  63. }
  64. return null;
  65. }
  66. private File[] ensureExists(File[] roots) {
  67. for (File f : roots) {
  68. ensureExists(f);
  69. }
  70. return roots;
  71. }
  72. private File ensureExists(File f) {
  73. if (!f.getAbsoluteFile().exists()) {
  74. boolean done = f.mkdirs();
  75. if (done) {
  76. Logger.error("Isolated filesystem folder " + f.getAbsolutePath() + "not found. Make sure the folder is unique for each session (use ${user} variable) or disable the 'Clear Upload Folder' option in configuration.");
  77. } else {
  78. Logger.error("Isolated filesystem folder " + f.getAbsolutePath() + " could not be created. Make sure the path is valid and the process has write access.");
  79. }
  80. }
  81. return f;
  82. }
  83. @Override
  84. public ShellFolder createShellFolder(File paramFile) throws FileNotFoundException {
  85. try {
  86. if (isSubfolderOfRoots(paramFile)) {
  87. if (windows) {
  88. return super.createShellFolder(paramFile);
  89. } else {
  90. try {
  91. Method m = defaultManager.getClass().getDeclaredMethod("createShellFolder", File.class);
  92. m.setAccessible(true);
  93. return (ShellFolder) m.invoke(defaultManager, paramFile);
  94. } catch (Exception e) {
  95. System.err.println("Failed to invoke createShellFolder method on default shell folder manager: " + e.getMessage());
  96. e.printStackTrace();
  97. return null;
  98. }
  99. }
  100. } else {
  101. throw new FileNotFoundException("Path is outside the allowed Webswing Filesystem isolation folder. (" + paramFile.getCanonicalPath() + ")");
  102. }
  103. } catch (IOException e) {
  104. System.err.println("Error while creating ShellFolder. " + e.getMessage());
  105. if (e instanceof FileNotFoundException) {
  106. throw (FileNotFoundException) e;
  107. } else {
  108. throw new FileNotFoundException("Error while creating ShellFolder. " + e.getMessage());
  109. }
  110. }
  111. }
  112. private boolean isSubfolderOfRoots(File paramFile) throws IOException {
  113. String cp = paramFile.getCanonicalPath();
  114. for (File root : roots) {
  115. if (cp.startsWith(root.getCanonicalPath())) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. @Override
  122. protected Invoker createInvoker() {
  123. if (windows) {
  124. return super.createInvoker();
  125. } else {
  126. try {
  127. Method m = defaultManager.getClass().getDeclaredMethod("createInvoker");
  128. m.setAccessible(true);
  129. return (Invoker) m.invoke(defaultManager);
  130. } catch (Exception e) {
  131. System.err.println("Failed to invoke createInvoker method on default shell folder manager: " + e.getMessage());
  132. e.printStackTrace();
  133. return null;
  134. }
  135. }
  136. }
  137. @Override
  138. public boolean isComputerNode(File paramFile) {
  139. if (windows) {
  140. return super.isComputerNode(paramFile);
  141. } else {
  142. try {
  143. Method m = defaultManager.getClass().getDeclaredMethod("isComputerNode", File.class);
  144. m.setAccessible(true);
  145. return (Boolean) m.invoke(defaultManager, paramFile);
  146. } catch (Exception e) {
  147. System.err.println("Failed to invoke isComputerNode method on default shell folder manager: " + e.getMessage());
  148. e.printStackTrace();
  149. return false;
  150. }
  151. }
  152. }
  153. @Override
  154. public boolean isFileSystemRoot(File paramFile) {
  155. try {
  156. for (File root : roots) {
  157. if (root.getCanonicalPath().equals(paramFile.getCanonicalPath())) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. } catch (IOException e1) {
  163. if (windows) {
  164. return super.isFileSystemRoot(paramFile);
  165. } else {
  166. try {
  167. Method m = defaultManager.getClass().getDeclaredMethod("isFileSystemRoot", File.class);
  168. m.setAccessible(true);
  169. return (Boolean) m.invoke(defaultManager, paramFile);
  170. } catch (Exception e) {
  171. System.err.println("Failed to invoke isFileSystemRoot method on default shell folder manager: " + e.getMessage());
  172. e.printStackTrace();
  173. return false;
  174. }
  175. }
  176. }
  177. }
  178. @SuppressWarnings("rawtypes")
  179. public void sortFiles(List paramList) {
  180. if (windows) {
  181. try {
  182. Method m = super.getClass().getDeclaredMethod("sortFiles", List.class);
  183. m.setAccessible(true);
  184. m.invoke(defaultManager, paramList);
  185. } catch (Exception e) {
  186. System.err.println("Failed to invoke sortFiles method on default shell folder manager: " + e.getMessage());
  187. e.printStackTrace();
  188. }
  189. } else {
  190. try {
  191. Method m = defaultManager.getClass().getDeclaredMethod("sortFiles", List.class);
  192. m.setAccessible(true);
  193. m.invoke(defaultManager, paramList);
  194. } catch (Exception e) {
  195. System.err.println("Failed to invoke sortFiles method on default shell folder manager: " + e.getMessage());
  196. e.printStackTrace();
  197. }
  198. }
  199. }
  200. }