Source code: openfuture/editxml/servlet/EditorServletProxy.java
1 package openfuture.editxml.servlet;
2 /*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.<p>
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.<p>
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
16 * http://www.gnu.org/copyleft/lesser.html
17 */
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.util.Hashtable;
27 import java.util.LinkedList;
28 import openfuture.editxml.model.XmlDomain;
29 import openfuture.util.error.I18NException;
30
31 /**
32 * Configuration Management Information:
33 * -------------------------------------
34 * $Id: EditorServletProxy.java,v 1.2 2001/07/22 09:29:15 wreissen Exp $
35 *
36 * Version History:
37 * ----------------
38 * $Log: EditorServletProxy.java,v $
39 * Revision 1.2 2001/07/22 09:29:15 wreissen
40 * saveAs implemented.
41 *
42 * Revision 1.1.1.1 2001/07/08 18:29:31 wreissen
43 * initial version registered ad SourceForge
44 *
45 *
46 */
47
48 /**
49 * Proxy class to {@link openfuture.editxml.servlet.EditorServlet
50 * EditorServlet}. <p>
51 *
52 *
53 * Created: Mon Jul 02 06:36:54 2001
54 *
55 * @author <a href="mailto: wolfgang@openfuture.de">Wolfgang Reissenberger</a>
56 * @version $Revision: 1.2 $
57 */
58
59 public class EditorServletProxy {
60
61 private String servletURL;
62
63 /**
64 * Creates a new <code>EditorServletProxy</code> instance.
65 *
66 * @param servletURL URL of the
67 * {@link openfuture.editxml.servlet.EditorServlet
68 * EditorServlet} instance.
69 */
70 public EditorServletProxy (String servletURL) {
71 setServletURL(servletURL);
72 }
73
74
75 /**
76 * Retrieve the XML domain with the given name.
77 *
78 * @param domainname name of the XML domain
79 * @return the XML domain
80 * @exception I18NException if an error occurs
81 */
82 public XmlDomain getXmlDomain(String domainname) throws I18NException {
83
84 return ((XmlDomain) service("action=get.domain",
85 domainname));
86 }
87
88
89 /**
90 * Retrieve the XML domains.
91 *
92 * @return a {@java.util.LinkedList list} of
93 * {@link openfuture.editxml.model.XmlDomain domain}s.
94 * @exception I18NException if an error occurs
95 */
96 public Hashtable getDomainTable() throws I18NException {
97
98 return ((Hashtable) service("action=get.all.domains", null));
99 }
100
101
102 /**
103 * Retrieve the file list table.
104 *
105 * @return a table of
106 * {@link java.util.LinkedList list} of file names.
107 * @exception I18NException if an error occurs
108 */
109 public Hashtable getFileTable() throws I18NException {
110
111 return ((Hashtable) service("action=get.all.filenames", null));
112 }
113
114
115
116 /**
117 * Retrieve the content from the given <code>file</code> converted
118 * into an XML form.
119 *
120 * @param domain XML domain that holds the XML file
121 * @param file XML file name
122 * @return the XML file converted into an XML form
123 * @exception I18NException if an error occurs
124 * @see {@link openfuture.editxml.form.XmlForm#getEntries()
125 * XmlForm.getEntries()}
126 */
127 public Hashtable getEntries(String domain, String file)
128 throws I18NException {
129
130 Object result = service("action=get.form&domain=" + domain
131 + "&file=" + file, null);
132 return ((Hashtable) result);
133
134 }
135
136 /**
137 * Save the XML form as <code>XML file</code> to the server.
138 *
139 * @param domain XML domain that holds the XML file
140 * @param file XML file name
141 * @param entries the XML file converted into an XML form
142 * @exception I18NException if an error occurs
143 * @see {@link openfuture.editxml.form.XmlForm#getEntries()
144 * XmlForm.getEntries()}
145 */
146 public void saveEntries(Hashtable entries, String domain, String file)
147 throws I18NException {
148
149 Object result = service("action=save.form&domain=" + domain
150 + "&file=" + file, entries);
151 }
152
153 /**
154 * Save the XML form as a new <code>XML file</code> to the server.
155 *
156 * @param entries the XML file converted into an XML form
157 * @param domain XML domain that holds the XML file
158 * @param oldfile old file name (file that has been loaded)
159 * @param newfile name under which the file should be saved.
160 * @exception I18NException if an error occurs
161 * @see {@link openfuture.editxml.form.XmlForm#getEntries()
162 * XmlForm.getEntries()}
163 */
164 public void saveAsEntries(Hashtable entries, String domain,
165 String oldfile, String newfile)
166 throws I18NException {
167
168 Object result = service("action=save.as.form&domain=" + domain
169 + "&file=" + oldfile + "&newfile=" + newfile,
170 entries);
171 }
172
173 /**
174 * Contact the
175 * {@link openfuture.editxml.servlet.EditorServlet EditorServlet}
176 * defined by {@link #getServletURL(String)}. The
177 * <code>request</code> object is sent via the servlet's input
178 * stream. From the output stream, the result object is read and
179 * returned. The request parameter <code>serialize</code> is
180 * always set to <code>true</code>.
181 *
182 * @param queryString query string appended to the
183 * {@link #getServletURL(String) servlet URL}.
184 * @param request request object transmitted to the servlet
185 * @return result object received from the servlet
186 * @exception I18NException if an error occurs
187 */
188 protected Serializable service(String queryString,
189 Serializable request)
190 throws I18NException {
191 URLConnection urlc = null;
192
193 try {
194 String urlString = getServletURL() + "?serialize=true&" + queryString;
195 URL url = new URL(urlString);
196 urlc = url.openConnection();
197
198 urlc.setDoInput(true);
199
200 // write request to servlet, if the object is not null
201 if (request != null) {
202 urlc.setDoOutput(true);
203 ObjectOutputStream out =
204 new ObjectOutputStream(urlc.getOutputStream());
205
206 out.writeObject(request);
207 out.close();
208 }
209
210 // read the answer
211 ObjectInputStream in =
212 new ObjectInputStream(urlc.getInputStream());
213
214 Serializable result = (Serializable) in.readObject();
215
216 if (result instanceof I18NException)
217 throw ((I18NException) result);
218 return (result);
219
220 } catch (IOException exp) {
221 exp.printStackTrace();
222
223 String[] reasonArgs = new String[1];
224 reasonArgs[0] = exp.getMessage();
225
226 throw new I18NException("exception.error.communication.failed",
227 null,
228 "exception.reason.io", reasonArgs);
229 } catch (ClassNotFoundException exp) {
230 exp.printStackTrace();
231
232 String[] reasonArgs = new String[1];
233 reasonArgs[0] = exp.getMessage();
234
235 throw new I18NException("exception.error.communication.failed",
236 null,
237 "exception.reason.classnotfound",
238 reasonArgs);
239 }
240
241 }
242
243 /**
244 * Get the value of servletURL.
245 * @return value of servletURL.
246 */
247 public String getServletURL() {
248 return servletURL;
249 }
250
251 /**
252 * Set the value of servletURL.
253 * @param v Value to assign to servletURL.
254 */
255 public void setServletURL(String v) {
256 this.servletURL = v;
257 }
258
259
260
261 }// EditorServletProxy