1    package com.instantbank.lettertemplate.editor.applet;
2    
3    import java.net.URL;
4    import java.net.URLConnection;
5    
6    import java.io.ObjectInputStream;
7    import java.io.Serializable;
8    import java.io.ObjectOutputStream;
9    
10   /**
11    *  This class provides a simple method of posting multiple Serialized objects
12    *  to a Java servlet and getting objects in return. This code was inspired by
13    *  code samples from the book 'Java Servlet Programming' by Jason Hunter and
14    *  William Crawford (O'Reilly & Associates, 1998).
15    *
16    * @author InstantBank (Rodrigo Lopez, Ma. Consuelo Franky)
17    * @created September 2002
18    */
19   public class TemplateEditorSender {
20   
21     /**
22      *  Sends a stream of objects to a servlet and waits a response.
23      *
24      * @param servlet The servlet to whom the objects are posted.
25      * @param objs The actual posted objects.
26      * @return An ObjectInputStream containing the response. The
27      *      Objects inside this stream will be read (and properly casted) by the
28      *      corresponding "asking" method in the {@link TemplateEditorProxy}
29      *      class.
30      * @exception Exception
31      */
32     public static ObjectInputStream postObjects
33       (URL servlet, Serializable objs[]) throws Exception {
34       URLConnection con = servlet.openConnection();
35       con.setDoInput(true);
36       con.setDoOutput(true);
37       con.setUseCaches(false);
38       con.setRequestProperty
39         ("Content-Type", "application/x-www-form-urlencoded");
40   
41       // Write the arguments as post data
42       ObjectOutputStream out
43          = new ObjectOutputStream(con.getOutputStream());
44       int numObjects = objs.length;
45       for(int x = 0; x < numObjects; x++) {
46         out.writeObject(objs[x]);
47       }
48   
49       out.flush();
50       out.close();
51   
52       return new ObjectInputStream(con.getInputStream());
53     }
54   
55   
56     /**
57      *  Same as {@link #postObjects(URL, Serializable[]) postObjects} method but
58      *  there is no expected answer.
59      *
60      * @param servlet
61      * @param objs
62      * @exception Exception
63      */
64     public static void postObjectsNoAnswer
65       (URL servlet, Serializable objs[]) throws Exception {
66       URLConnection con = servlet.openConnection();
67       con.setDoInput(false);
68       con.setDoOutput(true);
69       con.setUseCaches(false);
70   
71       con.setRequestProperty
72         ("Content-Type", "application/x-www-form-urlencoded");
73   
74       // Write the arguments as post data
75       ObjectOutputStream out
76          = new ObjectOutputStream(con.getOutputStream());
77       int numObjects = objs.length;
78       for(int x = 0; x < numObjects; x++) {
79         out.writeObject(objs[x]);
80       }
81   
82       out.flush();
83       out.close();
84     }
85   }
86