Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 University of New Mexico and the 
4
 *                  Regents of the University of California
5
 *
6
 *   '$Author: jones $'
7
 *     '$Date: 2004-04-01 16:41:58 -0800 (Thu, 01 Apr 2004) $'
8
 * '$Revision: 2094 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.harvesterClient;
26

    
27
import java.io.PrintWriter;
28
import javax.servlet.ServletConfig;
29
import javax.servlet.ServletContext;
30
import javax.servlet.ServletException;
31
import javax.servlet.ServletInputStream;
32
import javax.servlet.http.HttpServlet;
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35
import javax.servlet.http.HttpSession;
36
import javax.servlet.http.HttpUtils;
37
import javax.servlet.ServletOutputStream;
38
import edu.ucsb.nceas.metacat.AuthSession;
39

    
40
/**
41
 *  LoginServlet implements a Harvester servlet to login to Metacat
42
 */
43
public class LoginServlet extends HttpServlet {
44

    
45
  public void destroy() {
46
    // Close all connections
47
    System.out.println("Destroying LoginServlet");
48
  }
49

    
50
  /**
51
   *  Handle "GET" method requests from HTTP clients
52
   *
53
   *  @param  request   The request
54
   *  @param  response  The response
55
   *  @throws ServletException, java.io.IOException
56
   */
57
  public void doGet(HttpServletRequest request, HttpServletResponse response)
58
          throws ServletException, java.io.IOException {
59
    // Process the data and send back the response
60
    handleGetOrPost(request, response);
61
  }
62

    
63
  /**
64
   *  Handle "POST" method requests from HTTP clients
65
   *
66
   *  @param  request   The request
67
   *  @param  response  The response
68
   *  @throws ServletException, java.io.IOException
69
   */
70
  public void doPost(HttpServletRequest request, HttpServletResponse response)
71
          throws ServletException, java.io.IOException {
72
    // Process the data and send back the response
73
    handleGetOrPost(request, response);
74
  }
75

    
76
  /**
77
   *  Handle "GET" or "POST" method requests from HTTP clients
78
   *
79
   *  @param  request   The request
80
   *  @param  response  The response
81
   *  @throws ServletException, java.io.IOException
82
   */
83
  private void handleGetOrPost(HttpServletRequest request,
84
                               HttpServletResponse response)
85
          throws ServletException, java.io.IOException {
86
    AuthSession authSession = null;
87
    HttpSession httpSession;
88
    boolean isValid;
89
    PrintWriter out = response.getWriter();
90
    String passwd = request.getParameter("passwd");
91
    String user = request.getParameter("user");
92

    
93
    response.setContentType("text/plain");
94

    
95
    try {
96
      authSession = new AuthSession();
97
    } 
98
    catch (Exception e) {
99
      out.println("Error creating AuthSession: " + e.getMessage());
100
      return;
101
    }
102

    
103
    isValid = authSession.authenticate(request, user, passwd);
104
    
105
    if (isValid) {
106
      System.out.println(authSession.getMessage());
107
      httpSession = request.getSession(true);
108
      httpSession.putValue("Musername", user);
109
      httpSession.putValue("Mpassword", passwd);
110
      response.sendRedirect("../style/skins/dev/harvesterUpload.html");
111
    }
112
    else {
113
      out.println("Error authenticating Metacat login: " + 
114
                  authSession.getMessage());
115
    }
116
  }
117
}
(8-8/9)