Results 1 to 3 of 3
- 04-27-2012, 02:25 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Urgent help - unreported exception java.sql.SQLException; must be caught or declared
Hi all,
I have a below program, when I compile it throws an error "unreported exception java.sql.SQLException;must be caught or declared" in the lines mentioned in italics in the below program. Can someone tell me how to modify this Please?. It would be great if someone could help me in updating the code. Thank you very much in advance.
package org.dspace.authenticate;
import java.security.MessageDigest;
import java.sql.*;
import java.util.Hashtable;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.LogManager;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
/**
* Authentication module to authenticate against a flat LDAP tree where
* all users are in the same unit.
*
* @author Larry Stone, Stuart Lewis
* @version $Revision: 3735 $
*/
public class MbankAuthentication
implements AuthenticationMethod {
/** log4j category */
private static Logger log = Logger.getLogger(LDAPAuthentication.class);
/**
* Let a real auth method return true if it wants.
*/
public boolean canSelfRegister(Context context,
HttpServletRequest request,
String username)
throws SQLException
{
// Registration happens in main Mbank DB, not here
return true;
}
/**
* Nothing here, initialization is done when auto-registering.
*/
public void initEPerson(Context context, HttpServletRequest request,
EPerson eperson)
throws SQLException
{
}
/**
* Cannot change Mbank password through dspace, right?
*/
public boolean allowSetPassword(Context context,
HttpServletRequest request,
String username)
throws SQLException
{
return false;
}
/*
* This is an explicit method.
*/
public boolean isImplicit()
{
return false;
}
/*
* Add authenticated users to the group defined in dspace.cfg by
* the ldap.login.specialgroup key.
*/
public int[] getSpecialGroups(Context context, HttpServletRequest request)
{
// Prevents anonymous users from being added to this group, and the second check
// ensures they are LDAP users
try
{
if (!context.getCurrentUser().getNetid().equals(""))
{
String groupName = ConfigurationManager.getProperty("Mbank.login.spec ialgroup");
if ((groupName != null) && (!groupName.trim().equals("")))
{
Group ldapGroup = Group.findByName(context, groupName);
if (ldapGroup == null)
{
// Oops - the group isn't there.
log.warn(LogManager.getHeader(context,
"Mbank_specialgroup",
"Group defined in Mbank.login.specialgroup does not exist"));
return new int[0];
} else
{
return new int[] { ldapGroup.getID() };
}
}
}
}
catch (Exception npe) {
// The user is not a Mbank user, so we don't need to worry about them
}
return new int[0];
}
/*
*
*
* @return One of:
* SUCCESS, BAD_CREDENTIALS, CERT_REQUIRED, NO_SUCH_USER, BAD_ARGS
*/
public int authenticate(Context context,
String netid,
String password,
String realm,
HttpServletRequest request)
throws SQLException
{
log.info(LogManager.getHeader(context, "auth", "attempting trivial auth of user="+netid));
// Skip out when no netid or password is given.
if (netid == null || password == null)
return BAD_ARGS;
// Locate the eperson
EPerson eperson = null;
try
{
eperson = EPerson.findByNetid(context, netid.toLowerCase());
}
catch (SQLException e)
{
}
boolean loggedIn = false;
Mbank Mbank = new Mbank();
// if they entered a netid that matches an eperson
if (eperson != null)
{
// e-mail address corresponds to active account
if (eperson.getRequireCertificate())
return CERT_REQUIRED;
else if (!eperson.canLogIn())
return BAD_ARGS;
{
if (Mbank.authenticate(netid, password, context))
{
context.setCurrentUser(eperson = EPerson.findByNetid(context, netid.toLowerCase()));
log.info(LogManager
.getHeader(context, "authenticate", "type=Mbank"));
return SUCCESS;
}
else
return BAD_CREDENTIALS;
}
}
// the user does not already exist so try and authenticate them
// with ldap and create an eperson for them
else
{
if (Mbank.authenticate(netid, password, context))
{
// Register the new user automatically
log.info(LogManager.getHeader(context,
"autoregister", "netid=" + netid));
if ((Mbank.email!=null)&&(!Mbank.email.equals("")))
{
try
{
eperson = EPerson.findByNetid(context, Mbank.login);
if (eperson!=null)
{
log.info(LogManager.getHeader(context,
"type=Mbank-login", "type=Mbank_but_already_email"));
context.setIgnoreAuthorization(true);
// Update fields in case they have changed
eperson.setEmail(Mbank.email);
eperson.setFirstName(Mbank.givenName);
eperson.setLastName(Mbank.surName);
eperson.update();
context.commit();
context.setIgnoreAuthorization(false);
context.setCurrentUser(eperson);
return SUCCESS;
}
else
{
if (canSelfRegister(context, request, netid))
{
// TEMPORARILY turn off authorisation
try
{
context.setIgnoreAuthorization(true);
eperson = EPerson.create(context);
// Copy entries
eperson.setEmail(Mbank.email);
eperson.setFirstName(Mbank.givenName);
eperson.setLastName(Mbank.surName);
eperson.setNetid(netid.toLowerCase());
eperson.setCanLogIn(true);
AuthenticationManager.initEPerson(context, request, eperson);
eperson.update();
context.commit();
context.setCurrentUser(eperson);
}
catch (AuthorizeException e)
{
return NO_SUCH_USER;
}
finally
{
context.setIgnoreAuthorization(false);
}
log.info(LogManager.getHeader(context, "authenticate",
"type=ldap-login, created ePerson"));
return SUCCESS;
}
else
{
// No auto-registration for valid certs
log.info(LogManager.getHeader(context,
"failed_login", "type=ldap_but_no_record"));
return NO_SUCH_USER;
}
}
}
catch (AuthorizeException e)
{
eperson = null;
}
finally
{
context.setIgnoreAuthorization(false);
}
}
}
}
return BAD_ARGS;
}
protected class Mbank {
protected String login, password_encrypted, email, name;
protected String givenName, surName;
protected boolean authenticate(String netid, String password, Context context)
{
boolean retval;
Connection conn = DriverManager.getConnection("jdbc:postgresql://dbserver./mydb?user=scott&password=secret&ssl=true");
PreparedStatement st = conn.prepareStatement("SELECT login, password_encrypted, email, name FROM mb.t_subject WHERE (email = ? OR login = ?) AND subject_type IN ('person', 'institution') AND active;");
st.setString(1, netid);
st.setString(2, netid);
ResultSet rs = st.executeQuery();
if (rs.next()) {
login = rs.getString(1);
password_encrypted = rs.getString(2);
email = rs.getString(3);
name = rs.getString(4);
int pos = name.lastIndexOf(' ');
if (pos < 0) {
givenName = surName = name;
} else {
givenName = name.substring(0, pos);
surName = name.substring(pos+1);
}
if (md5sum(password).equals(password_encrypted)) {
retval = true;
} else {
retval = false;
}
} else {
retval = false;
}
rs.close();
st.close();
return retval;
}
String md5sum(String plain)
{
// Calculation
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(plain.getBytes());
byte[] result = md5.digest();
// Hex conversion
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < result.length; i++) {
if (result[i] <= 15 && result[i] >= 0) {
hexString.append("0");
}
hexString.append(Integer.toHexString(0xFF & result[i]));
}
return hexString.toString();
}
}
/*
* Returns URL to which to redirect to obtain credentials (either password
* prompt or e.g. HTTPS port for client cert.); null means no redirect.
*
* @param context
* DSpace context, will be modified (ePerson set) upon success.
*
* @param request
* The HTTP request that started this operation, or null if not applicable.
*
* @param response
* The HTTP response from the servlet method.
*
* @return fully-qualified URL
*/
public String loginPageURL(Context context,
HttpServletRequest request,
HttpServletResponse response)
{
return response.encodeRedirectURL(request.getContextPath( ) +
"/Mbank-login");
}
/**
* Returns message key for title of the "login" page, to use
* in a menu showing the choice of multiple login methods.
*
* @param context
* DSpace context, will be modified (ePerson set) upon success.
*
* @return Message key to look up in i18n message catalog.
*/
public String loginPageTitle(Context context)
{
return "org.dspace.eperson.LDAPAuthentication.title";
}
}
- 04-27-2012, 02:42 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Urgent help - unreported exception java.sql.SQLException; must be caught or decl
Please use [code] tags [/code] when posting code as it is very unlikely anyone will read that lot without them.
Please do not ask for code as refusal often offends.
- 04-27-2012, 02:43 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Urgent help - unreported exception java.sql.SQLException; must be caught or decl
In this case, though, you need to read up on exception handling.
Please do not ask for code as refusal often offends.
Similar Threads
-
Error:unreported exception java.io.IOexception; must be caught or declared to be thro
By misterbreadcrum in forum New To JavaReplies: 7Last Post: 03-21-2012, 08:07 AM -
SQLException caught:No data found
By jttslg in forum Java ServletReplies: 20Last Post: 05-20-2011, 05:21 PM -
SQLException caught: Communications link failure due to underlying exception: **
By sharanya in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-25-2011, 09:46 AM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-14-2011, 11:39 PM -
Unreported exception java.sql.SQLException
By javamula in forum AWT / SwingReplies: 4Last Post: 09-29-2009, 02:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks