KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > FeedbackServlet


1 /*
2  * FeedbackServlet.java
3  *
4  * Version: $Revision: 1.6 $
5  *
6  * Date: $Date: 2006/11/02 20:04:01 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.servlet;
41
42 import java.io.IOException JavaDoc;
43 import java.net.InetAddress JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.Date JavaDoc;
46
47 import javax.mail.MessagingException JavaDoc;
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51
52 import org.apache.log4j.Logger;
53 import org.dspace.app.webui.util.JSPManager;
54 import org.dspace.authorize.AuthorizeException;
55 import org.dspace.core.ConfigurationManager;
56 import org.dspace.core.Context;
57 import org.dspace.core.Email;
58 import org.dspace.core.LogManager;
59 import org.dspace.eperson.EPerson;
60
61 /**
62  * Servlet for handling user feedback
63  *
64  * @author Peter Breton
65  * @author Robert Tansley
66  * @version $Revision: 1.6 $
67  */

68 public class FeedbackServlet extends DSpaceServlet
69 {
70     /** log4j category */
71     private static Logger log = Logger.getLogger(FeedbackServlet.class);
72
73     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
74             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
75             SQLException JavaDoc, AuthorizeException
76     {
77         // Obtain information from request
78
// The page where the user came from
79
String JavaDoc fromPage = request.getHeader("Referer");
80
81         // Prevent spammers and splogbots from poisoning the feedback page
82
String JavaDoc host = ConfigurationManager.getProperty("dspace.hostname");
83
84         String JavaDoc basicHost = "";
85         if (host.equals("localhost") || host.equals("127.0.0.1")
86                 || host.equals(InetAddress.getLocalHost().getHostAddress()))
87             basicHost = host;
88         else
89         {
90             // cut off all but the hostname, to cover cases where more than one URL
91
// arrives at the installation; e.g. presence or absence of "www"
92
int lastDot = host.lastIndexOf(".");
93             basicHost = host.substring(host.substring(0, lastDot).lastIndexOf("."));
94         }
95
96         if (fromPage == null || fromPage.indexOf(basicHost) == -1)
97         {
98             throw new AuthorizeException();
99         }
100
101         // The email address they provided
102
String JavaDoc formEmail = request.getParameter("email");
103
104         // Browser
105
String JavaDoc userAgent = request.getHeader("User-Agent");
106
107         // Session id
108
String JavaDoc sessionID = request.getSession().getId();
109
110         // User email from context
111
EPerson currentUser = context.getCurrentUser();
112         String JavaDoc authEmail = null;
113
114         if (currentUser != null)
115         {
116             authEmail = currentUser.getEmail();
117         }
118
119         // Has the user just posted their feedback?
120
if (request.getParameter("submit") != null)
121         {
122             String JavaDoc feedback = request.getParameter("feedback");
123
124             // Check all data is there
125
if ((formEmail == null) || formEmail.equals("")
126                     || (feedback == null) || feedback.equals(""))
127             {
128                 log.info(LogManager.getHeader(context, "show_feedback_form",
129                         "problem=true"));
130                 request.setAttribute("feedback.problem", new Boolean JavaDoc(true));
131                 JSPManager.showJSP(request, response, "/feedback/form.jsp");
132
133                 return;
134             }
135
136             // All data is there, send the email
137
try
138             {
139                 Email email = ConfigurationManager.getEmail("feedback");
140                 email.addRecipient(ConfigurationManager
141                         .getProperty("feedback.recipient"));
142
143                 email.addArgument(new Date JavaDoc()); // Date
144
email.addArgument(formEmail); // Email
145
email.addArgument(authEmail); // Logged in as
146
email.addArgument(fromPage); // Referring page
147
email.addArgument(userAgent); // User agent
148
email.addArgument(sessionID); // Session ID
149
email.addArgument(feedback); // The feedback itself
150

151                 // Replying to feedback will reply to email on form
152
email.setReplyTo(formEmail);
153
154                 email.send();
155
156                 log.info(LogManager.getHeader(context, "sent_feedback", "from="
157                         + formEmail));
158
159                 JSPManager.showJSP(request, response,
160                         "/feedback/acknowledge.jsp");
161             }
162             catch (MessagingException JavaDoc me)
163             {
164                 log.warn(LogManager.getHeader(context,
165                         "error_mailing_feedback", ""), me);
166
167                 JSPManager.showInternalError(request, response);
168             }
169         }
170         else
171         {
172             // Display feedback form
173
log.info(LogManager.getHeader(context, "show_feedback_form",
174                     "problem=false"));
175             request.setAttribute("authenticated.email", authEmail);
176             JSPManager.showJSP(request, response, "/feedback/form.jsp");
177         }
178     }
179
180     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
181             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
182             SQLException JavaDoc, AuthorizeException
183     {
184         // Treat as a GET
185
doDSGet(context, request, response);
186     }
187 }
188
Popular Tags