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.server;
24  
25  import java.io.IOException;
26  import java.io.PrintWriter;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import de.bea.domingo.DNotesException;
31  import de.bea.domingo.DSession;
32  
33  /***
34   * Domingo server.
35   * TODO.
36   *
37   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
38   */
39  public final class DomingoServer extends BaseCommand implements Command {
40  
41      private final Map commands = new HashMap();
42  
43      /***
44       * Constructor.
45       */
46      public DomingoServer() {
47          super();
48          commands.put("error", new ErrorCommand());
49          commands.put("readdatabase", new ReadDatabaseCommand());
50          commands.put("readdocument", new ReadDocumentCommand());
51          commands.put("savedocument", new SaveDocumentCommand());
52          commands.put("createdatabase", new CreateDatabaseCommand());
53          commands.put("createdatabasefromtemplate", new CreateDatabaseFromTemplateCommand());
54      }
55  
56      /***
57       * Executes a domingo request with the specified parameters.
58       * Depending on the <code>cmd</code> parameter, the request is delegated to the
59       * corresponsing implementation of the {@link Command} interface.
60       *
61       * @see de.bea.domingo.server.Command#execute(de.bea.domingo.DSession, java.util.Map, java.io.PrintWriter)
62       *
63       * @param session domingo session for execution
64       * @param parameters request parameters
65       * @param printWriter response stream for output
66       * @throws DNotesException if the command cannot be executed
67       * @throws IOException if the response could not be created or completed
68       * @throws UnsupportedOperationException if the specified command doesn't exist
69       */
70      public void execute(final DSession session, final Map parameters, final PrintWriter printWriter)
71              throws UnsupportedOperationException, DNotesException, IOException {
72          final String commandString = getParameterString(parameters, "cmd");
73          final Command command = (Command) commands.get(commandString.toLowerCase());
74          Exception exception = null;
75          if (command == null) {
76              throw new UnsupportedOperationException("Cannot execute command " + commandString);
77          }
78          try {
79              command.execute(session, parameters, printWriter);
80          } catch (UnsupportedOperationException e) {
81              parameters.put("error_id", "7001");
82              exception = e;
83          } catch (DNotesException e) {
84              parameters.put("error_id", "7002");
85              exception = e;
86          } catch (IOException e) {
87              parameters.put("error_id", "7003");
88              exception = e;
89          }
90          if (exception != null) {
91              parameters.put("exception", exception);
92              Command errorCommand = (Command) commands.get("error");
93              errorCommand.execute(session, parameters, printWriter);
94          }
95      }
96  }