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
  String      relativeRoot          = ".";
58
  
59
  //////////////////////////////////////////////////////////////////////////////	
60
	
61
  try {
62
		rfSession = request.getSession(true);
63
		
64
	} catch(Exception e) {
65
	
66
		throw new ServletException("trying to get session: "+e);
67
	}
68
	
69
	Object sess_sessionIdObj = rfSession.getAttribute("sess_sessionIdObj");
70
	
71
	if (sess_sessionIdObj!=null) {
72
	  sess_sessionId = (String)sess_sessionIdObj;
73
    
74
		// if sess_sessionIdObj is empty string, that means user has logged out
75
		// (had problems if I set it to null, so used empty string instead)
76
		if (sess_sessionId.length() < 1) sess_sessionId = null;
77
	  else isLoggedIn = true;
78
    
79
  } else {
80
		
81
    sess_sessionId = null;
82
	}
83
	
84
	typedUserName   = request.getParameter("username");
85
	typedUserName   = (typedUserName!=null)? typedUserName.trim() : "";
86
	
87
	posted_organization = request.getParameter("organization");
88
	posted_organization = (posted_organization!=null)? posted_organization.trim() : "";
89

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

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

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