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: leinfelder $'
7
 *     '$Date: 2016-01-11 10:03:56 -0800 (Mon, 11 Jan 2016) $'
8
 * '$Revision: 9484 $'
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

    
29
import javax.servlet.ServletException;
30
import javax.servlet.http.HttpServlet;
31
import javax.servlet.http.HttpServletRequest;
32
import javax.servlet.http.HttpServletResponse;
33
import javax.servlet.http.HttpSession;
34

    
35
import edu.ucsb.nceas.metacat.AuthSession;
36
import edu.ucsb.nceas.metacat.properties.PropertyService;
37
import edu.ucsb.nceas.metacat.util.SystemUtil;
38

    
39

    
40
/**
41
 *  HarvesterRegistrationLogin implements a servlet to login to the Harvester
42
 *  Registration servlet.
43
 */
44
public class HarvesterRegistrationLogin extends HttpServlet {
45

    
46
    final String LDAP_DOMAIN = ",dc=ecoinformatics,dc=org";
47

    
48
    /**
49
     *  Handle "GET" method requests from HTTP clients
50
     *
51
     *  @param  req   The request
52
     *  @param  res   The response
53
     *  @throws ServletException, java.io.IOException
54
     */
55
    public void doGet(HttpServletRequest req, HttpServletResponse res)
56
                throws ServletException, java.io.IOException {
57
        handleGetOrPost(req, res);
58
    }
59

    
60

    
61
    /**
62
     *  Handle "POST" method requests from HTTP clients
63
     *
64
     *  @param  req   The request
65
     *  @param  res  The response
66
     *  @throws ServletException, java.io.IOException
67
     */
68
    public void doPost(HttpServletRequest req, HttpServletResponse res)
69
                throws ServletException, java.io.IOException {
70
        handleGetOrPost(req, res);
71
    }
72

    
73

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

    
95
        if ((uid == null) || (uid.equals(""))) {
96
          out.println("Invalid login: no Username specified.");
97
          return;
98
        }
99
        else if ((o == null) || (o.equals(""))) {
100
          out.println("Invalid login: no Organization selected.");
101
          return;
102
        }
103
        else if ((passwd == null) || (passwd.equals(""))) {
104
          out.println("Invalid login: no Password specified.");
105
          return;
106
        }
107
        else {
108
          user = "uid=" + uid + ",o=" + o + LDAP_DOMAIN;
109
        }
110

    
111
        res.setContentType("text/plain");
112
        
113
        try {
114
          authSession = new AuthSession();
115
          isValid = authSession.authenticate(req, user, passwd);
116
          authSessionMessage = authSession.getMessage();
117
          System.out.println("authSession.authenticate(): "+authSessionMessage);
118
          out.println("authSession.authenticate(): " + authSessionMessage);
119

    
120
          if (isValid) {
121
            httpSession = req.getSession(true);
122
            httpSession.setAttribute("username", user);
123
            httpSession.setAttribute("password", passwd);
124
            String context = PropertyService.getProperty("application.context");
125
            res.sendRedirect("/" + context + "/harvesterRegistration");
126
          }
127
          else {
128
            out.println("Invalid login");
129
          }
130
        } 
131
        catch (Exception e) {
132
          System.out.println("Error in AuthSession()" + e.getMessage());
133
        }
134
    }
135
}
(8-8/11)