1   /*
2    * Created on Aug 19, 2007
3    * 
4    * Copyright 2005 CafeSip.org 
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    *
10   *	http://www.apache.org/licenses/LICENSE-2.0 
11   *
12   * Unless required by applicable law or agreed to in writing, software 
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License.
17   *
18   */
19  package org.cafesip.gwtcomp.examples.server;
20  
21  import java.io.File;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.cafesip.gwtcomp.server.FileUploadAction;
31  import org.cafesip.gwtcomp.server.FormResponse;
32  
33  /**
34   * @author Amit Chatterjee
35   * 
36   */
37  public class FileUploadActionSample implements FileUploadAction
38  {
39      private String name;
40  
41      private String email;
42  
43      private String address;
44  
45      private String city;
46  
47      private String county;
48  
49      private String caption;
50  
51      private String description;
52  
53      private HashMap<String, File> files;
54  
55      /*
56       * @see org.cafesip.gwtcomp.server.FileUploadAction#onSubmit(javax.servlet.http.HttpServlet,
57       *      javax.servlet.http.HttpServletRequest)
58       */
59      public FormResponse onSubmit(HttpServlet servlet, HttpServletRequest request)
60      {
61          System.out.println("The user entered the following values: ");
62          System.out.println("Name: " + name);
63          System.out.println("Email: " + email);
64          System.out.println("Address: " + address);
65          System.out.println("City: " + city);
66          System.out.println("County: " + county);
67          System.out.println("Caption: " + caption);
68          System.out.println("Description: " + description);
69          
70          System.out.println("Uploaded File(s):");
71          Iterator<Map.Entry<String, File>> i = files.entrySet().iterator();
72          while (i.hasNext())
73          {
74              Map.Entry<String, File> entry = i.next();
75              System.out.println(entry.getKey() + ": " + entry.getValue());
76          }
77          
78          return validate();        
79      }
80      
81      private FormResponse validate()
82      {
83          if ((name == null) || (name.trim().length() == 0))
84          {
85              return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
86                  "You have not entered your name", true);
87          }
88          
89          if ((email == null) || (email.trim().length() == 0))
90          {
91              return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
92                  "You have not entered your email address", true);
93          }
94          
95          if ((caption == null) || (caption.trim().length() == 0))
96          {
97              return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
98                  "You have not entered the caption", true);
99          }
100         
101         if ((files == null) || (files.size() == 0))
102         {
103             return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
104             "You have not provided a file to upload", true);
105         }
106         
107         return new FormResponse(HttpServletResponse.SC_OK, "OK", true);
108     }
109 
110     /*
111      * @see org.cafesip.gwtcomp.server.FileUploadAction#setFileList(java.util.HashMap)
112      */
113     public void setFileList(HashMap<String, File> files)
114     {
115         this.files = files;
116     }
117 
118     /**
119      * @return Returns the address.
120      */
121     public String getAddress()
122     {
123         return address;
124     }
125 
126     /**
127      * @param address
128      *            The address to set.
129      */
130     public void setAddress(String address)
131     {
132         this.address = address;
133     }
134 
135     /**
136      * @return Returns the caption.
137      */
138     public String getCaption()
139     {
140         return caption;
141     }
142 
143     /**
144      * @param caption
145      *            The caption to set.
146      */
147     public void setCaption(String caption)
148     {
149         this.caption = caption;
150     }
151 
152     /**
153      * @return Returns the city.
154      */
155     public String getCity()
156     {
157         return city;
158     }
159 
160     /**
161      * @param city
162      *            The city to set.
163      */
164     public void setCity(String city)
165     {
166         this.city = city;
167     }
168 
169     /**
170      * @return Returns the county.
171      */
172     public String getCounty()
173     {
174         return county;
175     }
176 
177     /**
178      * @param county
179      *            The county to set.
180      */
181     public void setCounty(String county)
182     {
183         this.county = county;
184     }
185 
186     /**
187      * @return Returns the description.
188      */
189     public String getDescription()
190     {
191         return description;
192     }
193 
194     /**
195      * @param description
196      *            The description to set.
197      */
198     public void setDescription(String description)
199     {
200         this.description = description;
201     }
202 
203     /**
204      * @return Returns the email.
205      */
206     public String getEmail()
207     {
208         return email;
209     }
210 
211     /**
212      * @param email
213      *            The email to set.
214      */
215     public void setEmail(String email)
216     {
217         this.email = email;
218     }
219 
220     /**
221      * @return Returns the name.
222      */
223     public String getName()
224     {
225         return name;
226     }
227 
228     /**
229      * @param name
230      *            The name to set.
231      */
232     public void setName(String name)
233     {
234         this.name = name;
235     }
236 
237 }