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: jones $'
11
  *     '$Date: 2004-07-13 12:48:16 -0700 (Tue, 13 Jul 2004) $'
12
  * '$Revision: 2211 $'
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          = "@style-skins-path@/knb2";
58
    String      knbSiteUrl            = "@knb-site-url@";
59
    String      include_portalhead_title = null;
60
	
61
  //////////////////////////////////////////////////////////////////////////////	
62
	
63
  try {
64
		rfSession = request.getSession(true);
65
		
66
	} catch(Exception e) {
67
	
68
		throw new ServletException("trying to get session: "+e);
69
	}
70
	
71
	Object sess_sessionIdObj = rfSession.getAttribute("sess_sessionIdObj");
72
	
73
	if (sess_sessionIdObj!=null) {
74
	  sess_sessionId = (String)sess_sessionIdObj;
75
    
76
		// if sess_sessionIdObj is empty string, that means user has logged out
77
		// (had problems if I set it to null, so used empty string instead)
78
		if (sess_sessionId.length() < 1) sess_sessionId = null;
79
	  else isLoggedIn = true;
80
    
81
  } else {
82
		
83
    sess_sessionId = null;
84
	}
85
	
86
	typedUserName   = request.getParameter("username");
87
	typedUserName   = (typedUserName!=null)? typedUserName.trim() : "";
88
	
89
	posted_organization = request.getParameter("organization");
90
	posted_organization = (posted_organization!=null)? posted_organization.trim() : "";
91

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

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

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