View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.http;
24  
25  import java.io.IOException;
26  import java.util.Calendar;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.TimeZone;
30  
31  import de.bea.domingo.DAgentContext;
32  import de.bea.domingo.DBase;
33  import de.bea.domingo.DBaseDocument;
34  import de.bea.domingo.DDatabase;
35  import de.bea.domingo.DDxlExporter;
36  import de.bea.domingo.DLog;
37  import de.bea.domingo.DNotesException;
38  import de.bea.domingo.DNotesMonitor;
39  import de.bea.domingo.DNotesRuntimeException;
40  import de.bea.domingo.DSession;
41  import de.bea.domingo.DView;
42  import de.bea.domingo.DViewEntry;
43  
44  /***
45   * Notes session.
46   *
47   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
48   */
49  public final class SessionHttp extends BaseHttp implements DSession {
50  
51      /*** Column number of <tt>MailFile</tt> column in view <tt>($Users)</tt>. */
52      private static final int MAIL_FILE_COLUMN = 6;
53  
54      /*** File extension of Lotus Notes databases. */
55      private static final String DATABASE_EXT = ".nsf";
56  
57      /*** serial version ID for serialization. */
58      private static final long serialVersionUID = -490910110376926306L;
59  
60      private DominoHttpClient fHttpClient;
61  
62      /***
63       * Constructor.
64       *
65       * @throws IOException if the session to the server cannot be created
66       */
67      private SessionHttp(final NotesHttpFactory theFactory, final String host, final String user, final String passwd,
68              final DNotesMonitor monitor) throws IOException {
69          super(theFactory, null, monitor);
70          fHttpClient = new DominoHttpClient(getMonitor(), host, user, passwd);
71          fHttpClient.login();
72      }
73  
74      /***
75       * Creates an Http session object.
76       *
77       * @param factory the controling factory
78       * @param host the host for the session to connect
79       * @param user the username for login
80       * @param passwd the password for login
81       * @param monitor the monitor
82       * @return a session object
83       * @throws IOException if the session to the server cannot be created
84       */
85      static DSession getInstance(final NotesHttpFactory factory, final String host, final String user, final String passwd,
86              final DNotesMonitor monitor) throws IOException {
87          return new SessionHttp(factory, host, user, passwd, monitor);
88      }
89  
90      /***
91       * {@inheritDoc}
92       *
93       * @see de.bea.domingo.DSession#isOnServer()
94       */
95      public boolean isOnServer() {
96          return true;
97      }
98  
99      /***
100      * {@inheritDoc}
101      *
102      * @see de.bea.domingo.DSession#getDatabase(java.lang.String,
103      *      java.lang.String)
104      */
105     public DDatabase getDatabase(final String serverName, final String databaseName) throws DNotesRuntimeException {
106         return DatabaseHttp.getInstance(getFactory(), this, databaseName, getMonitor());
107     }
108 
109     /***
110      * {@inheritDoc}
111      *
112      * @see de.bea.domingo.proxy.BaseProxy#toString()
113      */
114     public String toString() {
115         return BaseHttp.toStringIntern(this);
116     }
117 
118     /***
119      * {@inheritDoc}
120      *
121      * @see de.bea.domingo.DSession#getUserName()
122      */
123     public String getUserName() {
124         return fHttpClient.getUserName();
125     }
126 
127     /***
128      * {@inheritDoc}
129      *
130      * @see de.bea.domingo.DSession#createDatabase(java.lang.String,
131      *      java.lang.String)
132      */
133     public DDatabase createDatabase(final String serverName, final String databaseName) {
134         final String path = databaseName.replace('//', '/');
135         final String bs;
136         try {
137             bs = execute("cmd=CreateDatabase&file=" + path);
138         } catch (IOException e) {
139             throw new NotesHttpRuntimeException("Cannot create database: " + path, e);
140         }
141         if (bs.indexOf("Database created") >= 0) {
142             return DatabaseHttp.getInstance(getFactory(), this, databaseName, getMonitor());
143         }
144         return null;
145     }
146 
147     /***
148      * {@inheritDoc}
149      *
150      * @see de.bea.domingo.DSession#getCommonUserName()
151      */
152     public String getCommonUserName() {
153         return fHttpClient.getCommonUserName();
154     }
155 
156     /***
157      * {@inheritDoc}
158      *
159      * @see de.bea.domingo.DSession#getCanonicalUserName()
160      */
161     public String getCanonicalUserName() {
162         return fHttpClient.getCanonicalUserName();
163     }
164 
165     /***
166      * {@inheritDoc}
167      *
168      * @see de.bea.domingo.DSession#evaluate(java.lang.String)
169      */
170     public List evaluate(final String formula) throws DNotesRuntimeException {
171         // TODO
172         return null;
173     }
174 
175     /***
176      * {@inheritDoc}
177      *
178      * @throws DNotesRuntimException
179      * @see de.bea.domingo.DSession#evaluate(java.lang.String,
180      *      de.bea.domingo.DBaseDocument)
181      */
182     public List evaluate(final String formula, final DBaseDocument doc) throws DNotesRuntimeException {
183         // TODO
184         return null;
185     }
186 
187     /***
188      * {@inheritDoc}
189      *
190      * @see de.bea.domingo.DSession#getEnvironmentString(java.lang.String)
191      */
192     public String getEnvironmentString(final String name) {
193         return getEnvironmentString(name, false);
194     }
195 
196     /***
197      * {@inheritDoc}
198      *
199      * @see de.bea.domingo.DSession#getEnvironmentValue(java.lang.String)
200      */
201     public Object getEnvironmentValue(final String name) {
202         return getEnvironmentValue(name, false);
203     }
204 
205     /***
206      * {@inheritDoc}
207      *
208      * @see de.bea.domingo.DSession#getEnvironmentString(java.lang.String,
209      *      boolean)
210      */
211     public String getEnvironmentString(final String name, final boolean isSystem) {
212         // TODO
213         return null;
214     }
215 
216     /***
217      * {@inheritDoc}
218      *
219      * @see de.bea.domingo.DSession#getEnvironmentValue(java.lang.String,
220      *      boolean)
221      */
222     public Object getEnvironmentValue(final String name, final boolean isSystem) {
223         // TODO
224         return null;
225     }
226 
227     /***
228      * {@inheritDoc}
229      *
230      * @see de.bea.domingo.DSession#setEnvironmentString(java.lang.String,
231      *      java.lang.String)
232      */
233     public void setEnvironmentString(final String name, final String value) {
234         // TODO
235     }
236 
237     /***
238      * {@inheritDoc}
239      *
240      * @see de.bea.domingo.DSession#setEnvironmentString(java.lang.String,
241      *      java.lang.String, boolean)
242      */
243     public void setEnvironmentString(final String name, final String value, final boolean isSystem) {
244         // TODO
245     }
246 
247     /***
248      * {@inheritDoc}
249      *
250      * @see de.bea.domingo.DSession#createLog(java.lang.String)
251      */
252     public DLog createLog(final String name) {
253         // TODO
254         return null;
255     }
256 
257     /***
258      * {@inheritDoc}
259      *
260      * @see de.bea.domingo.DSession#getAddressBooks()
261      */
262     public List getAddressBooks() {
263         // TODO
264         return null;
265     }
266 
267     /***
268      * {@inheritDoc}
269      *
270      * @see de.bea.domingo.DSession#getAbbreviatedName(java.lang.String)
271      */
272     public String getAbbreviatedName(final String canonicalName) {
273         // TODO
274         return null;
275     }
276 
277     /***
278      * {@inheritDoc}
279      *
280      * @see de.bea.domingo.DSession#getCanonicalName(java.lang.String)
281      */
282     public String getCanonicalName(final String abreviatedName) {
283         // TODO
284         return null;
285     }
286 
287     /***
288      * {@inheritDoc}
289      *
290      * @see de.bea.domingo.DSession#getAgentContext()
291      */
292     public DAgentContext getAgentContext() {
293         // TODO
294         return null;
295     }
296 
297     /***
298      * {@inheritDoc}
299      *
300      * @see de.bea.domingo.DSession#getCurrentTime()
301      */
302     public Calendar getCurrentTime() {
303         // TODO
304         return null;
305     }
306 
307     /***
308      * {@inheritDoc}
309      *
310      * @see de.bea.domingo.DSession#isValid()
311      */
312     public boolean isValid() {
313         // TODO
314         return false;
315     }
316 
317     /***
318      * {@inheritDoc}
319      *
320      * @see de.bea.domingo.DSession#getNotesVersion()
321      */
322     public String getNotesVersion() {
323         return "unknown"; // TODO getNotesVersion()
324     }
325 
326     /***
327      * {@inheritDoc}
328      *
329      * @see de.bea.domingo.DSession#getPlatform()
330      */
331     public String getPlatform() {
332         return "unknown"; // TODO getPlatform()
333     }
334 
335     /***
336      * {@inheritDoc}
337      *
338      * @see de.bea.domingo.DSession#createDxlExporter()
339      */
340     public DDxlExporter createDxlExporter() throws DNotesException {
341         // TODO Auto-generated method stub
342         return null;
343     }
344 
345     /***
346      * {@inheritDoc}
347      *
348      * @see de.bea.domingo.DSession#getMailServer()
349      */
350     public String getMailServer() {
351         // TODO Auto-generated method stub
352         return null;
353     }
354 
355     /***
356      * {@inheritDoc}
357      *
358      * @see de.bea.domingo.DSession#getMailDatabaseName()
359      */
360     public String getMailDatabaseName() {
361         return getMailDatabaseName(fHttpClient.getUserName());
362     }
363 
364     /***
365      * {@inheritDoc}
366      *
367      * @see de.bea.domingo.DSession#getMailDatabaseName()
368      */
369     private String getMailDatabaseName(final String username) {
370         DViewEntry entry = getUserEntry(username);
371         String mailFile = getColumnValueString(entry, MAIL_FILE_COLUMN);
372         if (mailFile == null || mailFile.length() == 0) {
373             return null;
374         }
375         mailFile = mailFile.replace('//', '/');
376         if (!mailFile.endsWith(DATABASE_EXT)) {
377             return mailFile + DATABASE_EXT;
378         }
379         return mailFile;
380     }
381 
382     /***
383      * {@inheritDoc}
384      *
385      * @see de.bea.domingo.DSession#getMailDatabase()
386      */
387     public DDatabase getMailDatabase() throws DNotesException {
388         return getMailDatabase(getMailDatabaseName());
389     }
390 
391     /***
392      * {@inheritDoc}
393      *
394      * @see de.bea.domingo.DSession#getMailDatabase(java.lang.String)
395      */
396     public DDatabase getMailDatabase(final String username) throws DNotesException {
397       String databaseName = getMailDatabaseName(username);
398       return DatabaseHttp.getInstance(getFactory(), this, databaseName, getMonitor());
399     }
400 
401     /***
402      * Returns the column value of a view entry at a given index as a single string.
403      * If the column value is a list of values, only the first value of the lust is returned.
404      * If the column value is not a string, the value is converted to a string with the
405      * <tt>toString()</tt> method of the value.
406      *
407      * @param entry the view entry
408      * @param index the column index
409      * @return column value as string
410      */
411     private String getColumnValueString(final DViewEntry entry, final int index) {
412         Object object = entry.getColumnValues().get(index);
413         if (object instanceof List) {
414             return ((List) object).get(0).toString();
415         } else if (object instanceof String) {
416             return (String) object;
417         } else {
418             return object.toString();
419         }
420     }
421 
422     /***
423      * Returns the person document of the current user from the servers
424      * names.nsf.
425      *
426      * @param username a username
427      * @return person document of the user
428      */
429     private DViewEntry getUserEntry(final String username) {
430         DDatabase database = getDatabase("", "names.nsf");
431         DView view = database.getView("($Users)");
432         Iterator iterator = view.getAllEntriesByKey(username);
433         if (iterator.hasNext()) {
434             return (DViewEntry) iterator.next();
435         }
436         return null;
437     }
438 
439     /***
440      * {@inheritDoc}
441      *
442      * @see de.bea.domingo.DSession#getMailDomain()
443      */
444     public String getMailDomain() {
445         // TODO Auto-generated method stub
446         return null;
447     }
448 
449     /***
450      * {@inheritDoc}
451      *
452      * @see de.bea.domingo.DSession#resolve(java.lang.String)
453      */
454     public DBase resolve(final String url) {
455         // TODO Auto-generated method stub
456         return null;
457     }
458 
459     /***
460      * {@inheritDoc}
461      * @see de.bea.domingo.DSession#getServerName()
462      */
463     public String getServerName() {
464         // TODO Auto-generated method stub
465         return null;
466     }
467 
468     /***
469      * Factory method for GET methods.
470      * @param pathInfo path_info of the method
471      * @return Http GET method
472      */
473     public DominoGetMethod createGetMethod(final String pathInfo) {
474         return fHttpClient.createGetMethod(pathInfo);
475     }
476 
477     /***
478      * Factory method for POST methods.
479      * @param pathInfo path_info of the method
480      * @return Http POST method
481      */
482     public DominoPostMethod createPostMethod(final String pathInfo) {
483         return fHttpClient.createPost(pathInfo);
484     }
485 
486     /***
487      * Executes the given {@link DominoHttpMethod HTTP method}.
488      *
489      * @param method the {@link DominoHttpMethod HTTP method} to execute
490      * @return the method's response code
491      *
492      * @throws IOException If an I/O (transport) error occurs. Some transport
493      *             exceptions can be recovered from.
494      */
495     protected int executeMethod(final DominoHttpMethod method) throws IOException {
496         return fHttpClient.executeMethod(method);
497     }
498 
499     /***
500      * Checks if the Domingo database is available on the server or not.
501      *
502      * @return <code>true</code> if the Domingo database is available, else
503      *         <code>false</code>
504      */
505     public boolean isDomingoAvailable() {
506         // TODO implement isDomingoAvailable()
507         return false;
508     }
509 
510     /***
511      * {@inheritDoc}
512      * @see de.bea.domingo.DSession#getTimeZone()
513      */
514     public TimeZone getTimeZone() {
515         // TODO Auto-generated method stub
516         return null;
517     }
518 
519     /***
520      * {@inheritDoc}
521      * @see de.bea.domingo.DSession#setTimeZone(java.util.TimeZone)
522      */
523     public void setTimeZone(final TimeZone zone) {
524         // TODO Auto-generated method stub
525     }
526 }