Project

General

Profile

1
<%@ page import="edu.ucsb.nceas.metacat.client.*" %>
2
<% 
3
 /**
4
  *  '$RCSfile$'
5
  *      Authors: Matt Jones, CHad Berkley
6
  *    Copyright: 2000 Regents of the University of California and the
7
  *               National Center for Ecological Analysis and Synthesis
8
  *  For Details: http://www.nceas.ucsb.edu/
9
  *
10
  *   '$Author: daigle $'
11
  *     '$Date: 2008-04-02 16:28:31 -0700 (Wed, 02 Apr 2008) $'
12
  * '$Revision: 3780 $'
13
  * 
14
  * This program is free software; you can redistribute it and/or modify
15
  * it under the terms of the GNU General Public License as published by
16
  * the Free Software Foundation; either version 2 of the License, or
17
  * (at your option) any later version.
18
  *
19
  * This program is distributed in the hope that it will be useful,
20
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
  * GNU General Public License for more details.
23
  *
24
  * You should have received a copy of the GNU General Public License
25
  * along with this program; if not, write to the Free Software
26
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
  *
28
  * This is an XSLT (http://www.w3.org/TR/xslt) stylesheet designed to
29
  * convert an XML file showing the resultset of a query
30
  * into an HTML format suitable for rendering with modern web browsers.
31
  */
32

    
33
  //////////////////////////////////////////////////////////////////////////////
34
  //
35
  // NOTE:
36
  //
37
  //  GLOBAL CONSTANTS (SETTINGS SUCH AS METACAT URL, LDAP DOMAIN	AND DEBUG 
38
  //  SWITCH) ARE ALL IN THE INCLUDE FILE "PORTAL_SETTINGS.jsp"
39
  //
40
  //////////////////////////////////////////////////////////////////////////////
41

    
42
  // GLOBAL VARIABLES //////////////////////////////////////////////////////////
43
	String      loginStatus           = null;
44
	String    	sess_sessionId        = null;
45
	String      sessionidField        = null;
46
	String      metacatResponse       = null;
47
	String      posted_ldapUserName   = null;
48
	String      typedUserName         = null;
49
	String      posted_organization   = null;
50
	String      posted_password       = null;
51
	String      loginAction           = null;
52
	String      loginButtonLabel      = null;
53
	String      loginEnabledDisabled  = null;
54
	Metacat     metacat               = null;
55
	HttpSession rfSession             = null;
56
	boolean     isLoggedIn            = false;
57
  
58
  //////////////////////////////////////////////////////////////////////////////	
59
	
60
  try {
61
		rfSession = request.getSession(true);
62
		
63
	} catch(Exception e) {
64
	
65
		throw new ServletException("trying to get session: "+e);
66
	}
67
	
68
	Object sess_sessionIdObj = rfSession.getAttribute("sess_sessionIdObj");
69
	
70
	if (sess_sessionIdObj!=null) {
71
	  sess_sessionId = (String)sess_sessionIdObj;
72
    
73
		// if sess_sessionIdObj is empty string, that means user has logged out
74
		// (had problems if I set it to null, so used empty string instead)
75
		if (sess_sessionId.length() < 1) sess_sessionId = null;
76
	  else isLoggedIn = true;
77
    
78
  } else {
79
		
80
    sess_sessionId = null;
81
	}
82
	
83
	typedUserName   = request.getParameter("username");
84
	typedUserName   = (typedUserName!=null)? typedUserName.trim() : "";
85
	
86
	posted_organization = request.getParameter("organization");
87
	posted_organization = (posted_organization!=null)? posted_organization.trim() : "";
88

    
89
	posted_ldapUserName = "uid=" + typedUserName 
90
                                    + ",o=" + posted_organization + LDAP_DOMAIN;
91
	
92
	posted_password = request.getParameter("password");
93
	posted_password = (posted_password!=null)? posted_password.trim() : "";
94
	
95
	loginAction     = request.getParameter("loginAction");
96
	loginAction     = (loginAction!=null)? loginAction.trim() : "";
97
	
98
  //////////////////////////////////////////////////////////////////////////////	
99
	
100
	if (loginAction.equals(LOGOUT_LABEL)) { 
101
	// DO LOGOUT //////////////////////////////////////////////////////////////		
102
	
103
	  if (sess_sessionId!=null && rfSession.getAttribute("sess_metacatObj")!=null) {
104
		  metacat = (Metacat)(rfSession.getAttribute("sess_metacatObj"));
105
			metacat.logout();			
106
		}
107
		sess_sessionId = null;
108
		rfSession.setAttribute("sess_sessionIdObj", "");
109
		isLoggedIn = false;
110
		
111
	} else if (loginAction.equals(LOGIN_LABEL)) {  
112
	// DO LOGIN ////////////////////////////////////////////////////////////////	
113
	
114
		if (sess_sessionId!=null) isLoggedIn = true;		
115
		else {
116
		
117
		  // get metacat object - either cached from session...
118
			if (rfSession.getAttribute("sess_metacatObj")!=null) {
119
				
120
				metacat = (Metacat)(rfSession.getAttribute("sess_metacatObj"));
121
			
122
			} else {   // ...or create it if it doesn't already exist
123
				try {
124
					metacat = MetacatFactory.createMetacatConnection(SERVLET_URL);
125
				} catch (MetacatInaccessibleException mie) {
126
					throw new ServletException("Metacat connection to "+SERVLET_URL
127
                                                +" failed." + mie.getMessage());
128
				}
129
				if (metacat==null) {
130
					throw new ServletException("Metacat connection to "+SERVLET_URL
131
                                          +" failed - Metacat object is NULL!");
132
				}
133
				rfSession.setAttribute("sess_metacatObj", metacat);
134
			}
135
			// now do login...
136
			try {
137
					metacatResponse = metacat.login(posted_ldapUserName, posted_password);
138
					
139
					if (metacatResponse!=null && metacatResponse.indexOf("<login>") >= 0) {
140
						sess_sessionId = metacat.getSessionId();
141
						rfSession.setAttribute("sess_sessionIdObj", 
142
                                  ((sess_sessionId!=null)? sess_sessionId: "") );
143
						isLoggedIn = true;
144
					} else {
145
						loginStatus = "<em class=\"loginStatus\">Incorrect username or password - please try again</em>";
146
						isLoggedIn = false;
147
					}
148
			} catch (MetacatAuthException mae) {
149
					loginStatus = "<em class=\"loginStatus\">Incorrect username or password! - please try again</em>";
150
					isLoggedIn = false;
151
			} catch (MetacatInaccessibleException mie) {
152
					isLoggedIn = false;
153
					loginStatus = "<em class=\"loginStatus\">ERROR logging in - problems connecting - please try later</em>";
154
			}
155
		}
156
  }
157
	
158
  //////////////////////////////////////////////////////////////////////////////	
159

    
160
  if (isLoggedIn) {
161
		loginButtonLabel     = LOGOUT_LABEL;
162
		loginEnabledDisabled = "disabled";
163
		if (sess_sessionId!=null && sess_sessionId.length()>0) {
164
  	  sessionidField = "<input type=\"hidden\" name=\"sessionid\"     value=\""
165
                                                          +sess_sessionId+"\">";
166
	  } else {
167
	    sessionidField = "";
168
		}
169
		// if loginStatus has already been set above, don't overwrite it...
170
	  if (loginStatus==null) loginStatus 
171
                          = "<em class=\"loginStatus\">You ARE logged in</em>";
172
  } else {
173
		loginButtonLabel     = LOGIN_LABEL;
174
		loginEnabledDisabled = "";
175
	  sessionidField = "";
176
	  // if loginStatus has already been set above, don't overwrite it...
177
	  if (loginStatus==null) loginStatus 
178
                      = "<em class=\"loginStatus\">You are NOT logged in</em>";
179
	}
180

    
181
  if (DEBUG_TO_BROWSER) {
182
%>	
183
<table>							
184
	<tr> 
185
		<td colspan="4" align="left" valign="top" class="text_plain" bgcolor="#ffff00">
186
			<ul>
187
				<li>SERVLET_URL:&nbsp;<%=SERVLET_URL%></li>
188
				<li>rfSession:&nbsp;<%=rfSession%></li>
189
				<li>metacatResponse:&nbsp;<%=metacatResponse%></li>
190
				<li>posted_ldapUserName:&nbsp;<%=posted_ldapUserName%></li>
191
				<li>posted_password:&nbsp;<%=posted_password%></li>
192
				<li>isLoggedIn:&nbsp;<%=isLoggedIn%></li>
193
				<li>sess_sessionId LOCAL:&nbsp;<%=sess_sessionId%></li>
194
				<li>sess_sessionIdObj FROM SESSION:&nbsp;<%=rfSession.getAttribute("sess_sessionIdObj")%></li>
195
			</ul></td>
196
  </tr>
197
</table>
198
<%
199
  }
200
%>
(23-23/24)