View Javadoc

1   /***
2    * $Id: Browser.java,v 1.1 2004/11/08 23:15:38 mch Exp $
3    */
4   package org.astrogrid.store.browser;
5   
6   import java.io.IOException;
7   import java.net.URISyntaxException;
8   import java.text.DateFormat;
9   import java.text.SimpleDateFormat;
10  import java.util.Enumeration;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import org.astrogrid.community.Account;
14  import org.astrogrid.store.Agsl;
15  import org.astrogrid.store.Ivorn;
16  import org.astrogrid.store.Msrl;
17  import org.astrogrid.store.VoSpaceClient;
18  import org.astrogrid.store.delegate.StoreClient;
19  import org.astrogrid.store.delegate.StoreDelegateFactory;
20  import org.astrogrid.store.delegate.StoreFile;
21  
22  /***
23   * An object representing a view onto a store, such as myspace, suitable for
24   * display via HTML
25   */
26  public class Browser  {
27     /*** Holds the file tree on the server */
28     private StoreFile root = null;
29     
30     public final static String STORE_KEY = "store";
31     public final static String STORE_NAME_KEY = "storeName";
32     public final static String SELECTED_PATH_KEY = "path";
33     public final static String CANCEL_ACTION_KEY = "cancel";
34     public final static String OK_ACTION_KEY     = "ok";
35     public final static String OK_NAME_KEY = "okName";
36     
37     private DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
38     
39     public final static String refRoot = "StoreBrowser?";
40     
41     private Account user = Account.ANONYMOUS;
42     
43     StoresView storesView = null;
44     FilesView filesView = null;
45     StoreClient store = null;
46     
47     public Browser() {
48  //    setUser(request);
49        storesView = new StoresView(this);
50        filesView = new FilesView(this);
51     }
52     
53     public Account getUser() {
54        return user;
55     }
56  
57     /*** Sets the user from the given request properties. Currently just looks for
58      * 'userIvorn' and uses that *
59     public void setUser(HttpServletRequest request) throws URISyntaxException {
60        Ivorn ivorn = new Ivorn(getParameter(request, "userIvorn"));
61        user = new Account(ivorn.getPath());
62     }
63     
64     /**
65      * Returns the currently selected path
66      */
67     private String getSelectedPath(HttpServletRequest request) {
68        return getParameter(request, SELECTED_PATH_KEY);
69     }
70  
71     public String getStoreURI(HttpServletRequest request) {
72        return getParameter(request, STORE_KEY);
73     }
74     
75     public String getStoreName(HttpServletRequest request) {
76        String name = getParameter(request, STORE_NAME_KEY);
77        if (name == null) {
78           name = "(No Selected Store)";
79        }
80        return name;
81     }
82     
83     /***
84      * Returns the currently selected StoreFile
85      *
86      private String getSelectedFile(HttpServletRequest request) {
87      String path = getSelectedPath(request);
88      //rather poor search, anyway...
89      
90      }
91     
92      /**
93      * Returns the relevent storeclient for the store
94      */
95     public synchronized StoreClient getStoreClient(HttpServletRequest request) throws IOException {
96  
97        if (store != null) {
98           return store;
99        }
100       
101       String storeUri = getParameter(request, STORE_KEY);
102       
103       if (storeUri == null) {
104          store = null;
105       }
106       else if (Msrl.isMsrl(storeUri)) {
107          store = StoreDelegateFactory.createDelegate(getUser().toUser(), new Agsl(new Msrl(storeUri)));
108          return store;
109       }
110       else if (Ivorn.isIvorn(storeUri)) {
111          try {
112             VoSpaceClient voClient = new VoSpaceClient(getUser().toUser());
113             store = voClient.getDelegate(new Ivorn(storeUri));
114             return store;
115          }
116          catch (URISyntaxException e) {
117             throw new IllegalArgumentException("Malformed IVORN:"+e);
118          }
119       }
120       else {
121          store = StoreDelegateFactory.createDelegate(getUser().toUser(), storeUri);
122          return store;
123       }
124       return null;
125    }
126    
127    /*** Gets parameter - looks in request first, if it's not there looks in session */
128    public String getParameter(HttpServletRequest request, String key) {
129       String value = request.getParameter(key);
130       if (value == null) {
131          value = (String) request.getSession().getValue(key);
132          
133       }
134       return value;
135    }
136    
137    /*** Gets multiple parameter - looks in request first, if it's not there looks in session */
138    public String[] getParameterValues(HttpServletRequest request, String key) {
139       String[] values = request.getParameterValues(key);
140       if (values == null) {
141          values = (String[]) request.getSession().getValue(key);
142       }
143       return values;
144    }
145    
146 
147    /***
148     * One day I'll work out how to do this as a popup
149     */
150    public void writeMessageBox(HttpServletResponse response, String title, String header, String message) throws IOException {
151       response.getWriter().print(
152          "<html>"+
153          "<head><title>"+title+"</title></head>"+
154          "<body>"+
155          "<h1>"+header+"</h1>"+
156          message+
157          "</body>"+
158          "</html>"
159       );
160    }
161    
162 
163    /***
164     * Asks the user to enter a new file and some text
165     */
166    public void newFileForm(HttpServletRequest request, HttpServletResponse response) throws IOException {
167 
168       StoreFile file = getStoreClient(request).getFile(getSelectedPath(request));
169       //      if ((getSelectedPath(request) == null) ||
170 //          (getSelectedPath(request).endsWith("/"))) {
171       if (true) {
172          //selected path is a folder
173          response.getWriter().write(
174             "<html>"+
175             "<head><title>Browser - New File Form</title></head>\n"+
176             "<body>"+
177             "<h1>New File</h1>\n"+
178             "<form action='Browser?newFile=true' method='post'>Filename: <pre>");
179          if (getSelectedPath(request)!=null) {
180             response.getWriter().write(getSelectedPath(request));
181          } else {
182             response.getWriter().write("/");
183          }
184          response.getWriter().write(
185             "</pre>"+
186             "<input type='text' name='Filename'><p>\n"+
187             "<textarea name='contents' cols=60 rows=20>Type or cut and paste the contents of the new file in here</textarea><p>\n"+
188             "<input type='submit' name='Browser?NewFile=true' value='NewFile'>"+
189             "</form>"+
190             "</body></html>"
191          );
192       }
193       else {
194          writeMessageBox(response, "New File", "New File Form",
195                            "The file '"+getSelectedPath(request)+"' is currently selected<p>"+
196                            "Please go back and select the directory you want the new file to be created in");
197       }
198    }
199 
200    /***
201     * Creates new file on the client given inputs from above
202     */
203    public void newFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
204       String parentPath = getSelectedPath(request);
205       if (parentPath == null) {
206          parentPath="";
207       }
208       
209       getStoreClient(request).putString(request.getParameter("contents"),
210                                         parentPath+"/"+request.getParameter("Filename"),
211                                         false);
212 
213       writeBrowser(request, response);
214    }
215    
216 
217    /***
218     * Asks the user for a target ready to copy a file to
219     */
220    public void newFileFolder(HttpServletRequest request, HttpServletResponse response) throws IOException {
221       throw new UnsupportedOperationException();
222    }
223 
224    
225    
226    /*** Writes the full HTML page for the whole browser page */
227    public void writeBrowser(HttpServletRequest request, HttpServletResponse response) throws IOException  {
228       
229       String path = getParameter(request, SELECTED_PATH_KEY);
230       if (path == null) {
231          path = "";
232       }
233       
234       response.setContentType("text/html");
235       
236       response.getWriter().print(
237          "<html>"+
238             "<head>"+
239             "<title>"+getStoreName(request)+" Browser</title>"+
240             "</head>"+
241             "<body border='0'>"+
242 //            "<div id='address'>"+
243             "<table width='100%'>\n"+
244             "<tr><td><h2>"+getStoreName(request)+"</h2></td><td align='right'>");
245       writeToolBar(request, response);
246       response.getWriter().print(
247             "</td></tr>\n"+
248             "<tr><td align='right'>Current Path:</td><td>"+path+"</td></tr>\n");
249       response.getWriter().print("<tr><td rowspan='2' valign='top'>");
250       storesView.writeView(request, response);
251       response.getWriter().print("</td><td valign='top'>");
252       filesView.writeView(request, response);
253       response.getWriter().print("</td></tr>");
254       response.getWriter().print("<tr><td>Buttons</td></tr>");
255       response.getWriter().print("</table>");
256       
257       response.getWriter().print("<div id='footer' style='bottom'>"+
258                                     //"<hr>"+
259                                     "<small>(C) AstroGrid 2002-2004</small>"+
260                                     "</div>"+
261                                     "</body>"+
262                                     "</html>");
263       
264       response.getWriter().println("<hr><h2>Debug</h2><pre>"
265                           //            STORE_KEY+": "+request.getParameter(STORE_KEY)+" ("+request.getSession().getValue(STORE_KEY)+")\n"+
266                           //            SELECTED_PATH_KEY+": "+request.getParameter(SELECTED_PATH_KEY)+" ("+request.getSession().getValue(SELECTED_PATH_KEY)+")\n"
267                                   );
268       /*
269       String[] openPaths = (String[]) request.getSession().getValue("openPaths");
270       
271       if (openPaths != null) {
272          for (int i = 0; i < openPaths.length; i++) {
273             response.getWriter().println("open: "+openPaths[i]);
274          }
275       }
276        */
277       
278       //parameters
279       Enumeration e = request.getParameterNames();
280       while (e.hasMoreElements()) {
281          String key = (String)e.nextElement();
282          String[] values = request.getParameterValues(key);
283          response.getWriter().print(" " + key + " = ");
284          for(int i = 0; i < values.length; i++) {
285             response.getWriter().print(values[i] + " ");
286          }
287          response.getWriter().println();
288       }
289 
290       response.getWriter().println();
291       response.getWriter().println("Session:");
292       //session parameters
293       if (request.getSession() != null) {
294          String[] keys = request.getSession().getValueNames();
295          for (int i = 0; i < keys.length; i++) {
296             String value = request.getSession().getValue(keys[i]).toString();
297             response.getWriter().println(" " + keys[i] + " = "+value);
298          }
299       }
300       
301       response.getWriter().println("</pre>");
302       
303    }
304    
305    public String toolButtonHtml(String href, String img, String alt) {
306       return "<a href='"+href+"'>"+
307          "<img src='../"+img+"' alt='"+alt+"' border='0'/>"+
308          "</a>";
309    }
310    
311    public void writeToolBar(HttpServletRequest request, HttpServletResponse response) throws IOException {
312       String bg="#FFFFFF";
313       response.getWriter().print(
314          "<div id='toolbar' style='top'>"+
315             "<table bgcolor='"+bg+"'><tr><td>"+
316             "<table bgcolor='"+bg+"'><tr>"+
317             "  <td>"+toolButtonHtml("", "images/Back.gif", "Back")+"</td>"+
318             "  <td>"+toolButtonHtml("", "images/Forward.gif", "Forward")+"</td>"+
319             "  <td>"+toolButtonHtml(refRoot+"closeCurrent=true", "images/Up.gif", "Up")+"</td>"+
320             "</tr></table>  "+
321             "</td><td>"+
322             "<table bgcolor='"+bg+"'><tr>"+
323             "  <td>"+toolButtonHtml(refRoot+"copy='"+getParameter(request, SELECTED_PATH_KEY)+"'", "images/Copy.gif", "Copy")+"</td>"+
324             "  <td>"+toolButtonHtml(refRoot+"move='"+getParameter(request, SELECTED_PATH_KEY)+"'", "images/Move.gif", "Move")+"</td>"+
325             "  <td>"+toolButtonHtml(refRoot+"delete='"+getParameter(request, SELECTED_PATH_KEY)+"'", "images/Delete.gif", "Delete")+"</td>"+
326             "  <td>"+toolButtonHtml(refRoot+"newFileForm=true'", "images/NewFile.gif", "New File")+"</td>"+
327             "  <td>"+toolButtonHtml(refRoot+"newFolderForm=true'", "images/NewFolder.gif", "New Folder")+"</td>"+
328             "</tr></table>  "+
329             "</td><td>"+
330             "<table bgcolor='"+bg+"'><tr>"+
331             "  <td>"+toolButtonHtml(refRoot+"refresh=true'", "images/Refresh.gif", "Refresh")+"</td>"+
332             "</tr></table>  "+
333             "</td></tr></table>"+
334             "</div>");
335    }
336    
337 }