Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author: costa $'
7
 *     '$Date: 2005-12-08 14:57:46 -0800 (Thu, 08 Dec 2005) $'
8
 * '$Revision: 2822 $'
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.advancedsearch;
26

    
27
import java.io.File;
28
import java.io.IOException;
29

    
30
import javax.servlet.RequestDispatcher;
31
import javax.servlet.ServletConfig;
32
import javax.servlet.ServletContext;
33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpServlet;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpSession;
38

    
39
import edu.ucsb.nceas.metacat.client.*;
40
import edu.ucsb.nceas.utilities.Options;
41

    
42
/** 
43
 * @author dcosta
44
 * 
45
 * The LoginServlet class executes a metacat login via a metacat client.
46
 */
47
public class LoginServlet extends HttpServlet {
48

    
49
  /*
50
   * Class fields
51
   */
52
  private static final String CONFIG_DIR = "WEB-INF";
53
  private static final String CONFIG_NAME = "metacat.properties";
54
				   
55
  // Instance Variables
56
  private String contextString = "";
57

    
58
  // Methods
59
  
60
  /**
61
   * Executes a metacat login action. On a successful login, the metacat object
62
   * is stored as an attribute of the HttpSession object.
63
   */
64
  public void doPost(HttpServletRequest request, HttpServletResponse response)
65
          throws IOException, ServletException {
66
    RequestDispatcher dispatcher;
67
    HttpSession httpSession = request.getSession();
68
    LoginBean loginBean;
69
    boolean loginSuccess = false;
70
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
71
    MetacatLogin metacatLogin;
72
    String metacatURL;
73
    String organization;
74
    String result;
75
    ServletContext servletContext = httpSession.getServletContext();
76
    String username;
77
    
78
    // First check whether the metacat URL was set as a context-param.
79
    metacatURL = servletContext.getInitParameter("metacatURL");
80
 
81
    // If no metacat URL was configured, then derive the metacat URL from the
82
    // server name and server port.
83
    if (metacatURL == null || metacatURL.equals("")) {
84
      MetacatHelper metacatHelper = new MetacatHelper();
85
      String serverName = request.getServerName();
86
      int serverPort = request.getServerPort();
87
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort, "knb");
88
    }
89
    
90
    if (metacat == null) {
91
      try {
92
        metacat = MetacatFactory.createMetacatConnection(metacatURL);
93
      }
94
      catch (MetacatInaccessibleException mie) {
95
        System.err.println("Metacat Inaccessible:\n" + mie.getMessage());
96
      }
97
    }
98
    
99
    loginBean = (LoginBean) request.getAttribute("loginBean");
100
    metacatLogin = new MetacatLogin(loginBean);
101
    loginSuccess = metacatLogin.executeLogin(metacatURL, metacat);
102
    httpSession.setAttribute("metacat", metacat);
103

    
104
    // If login succeeds, forward to the simple search page. If login fails,
105
    // forward to the login page where an error message will be displayed.
106
    if (loginSuccess) {
107
      httpSession.setAttribute("loggedIn", new Boolean(true));
108
      username = loginBean.getUsername();
109
      httpSession.setAttribute("username", username);
110
      dispatcher = request.getRequestDispatcher("/metacatsearch.jsp");
111
    }
112
    else {
113
      request.setAttribute("loginFailure", new Boolean(true));
114
      dispatcher = request.getRequestDispatcher("/metacatlogin.jsp");
115
    }
116

    
117
    dispatcher.forward(request, response);
118
  }
119

    
120

    
121
  /**
122
   * Initializes the servlet. Reads properties and initializes object fields.
123
   * 
124
   * @throws ServletException
125
   */
126
  public void init(ServletConfig config) throws ServletException {
127
	ServletContext context = null;
128
    String dirPath;
129
    Options options = null;
130

    
131
    super.init(config);
132
    context = config.getServletContext();
133
    dirPath = context.getRealPath(CONFIG_DIR);
134
    File propertyFile = new File(dirPath, CONFIG_NAME);
135
    
136
    try {
137
      options = Options.initialize(propertyFile);
138
    }
139
    catch (IOException e) {
140
      System.err.println("Error in loading options: " + e.getMessage());
141
    }
142
    
143
    this.contextString = options.getOption("context");
144
  }
145

    
146
}
(10-10/14)