content
| - ---++ODS Controller for JSP API Login Source Code
The following source code represents the ODS Controller for JSP API Login users.jsp file:
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.HttpURLConnection" %>
<%@ page import="java.net.MalformedURLException" %>
<%@ page import="java.net.ProtocolException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.security.MessageDigest" %>
<%@ page import="java.security.NoSuchAlgorithmException" %>
<%@ page import="sun.misc.BASE64Encoder" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="javax.xml.xpath.*" %>
<%@ page import="org.xml.sax.InputSource" %>
<%@ page import="org.w3c.dom.*" %>
Virtuoso Web Applications
<%!
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
Document createDocument (String S)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if (factory == null)
throw new RuntimeException("Unable to create XML document factory");
DocumentBuilder builder = factory.newDocumentBuilder();
if (builder == null)
throw new RuntimeException("Unable to create XML document factory");
StringReader stringReader = new StringReader(S);
InputSource is = new InputSource(stringReader);
return builder.parse(is);
}
catch (Exception e)
{
throw new RuntimeException("Error creating XML document factory : " + e.getMessage());
}
}
String encrypt (String S)
{
String hash = new String("");
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] textBytes = S.getBytes("UTF-8");
md.update(textBytes);
for (byte b : md.digest()) {
hash += Integer.toHexString(b & 0xff);
}
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return hash;
}
String httpParam (String prefix, String key, String value)
throws Exception
{
String S = "";
if (value != null)
S = prefix + key + "=" + URLEncoder.encode(value);
return S;
}
String httpRequest (String httpMethod, String method, String params)
throws Exception
{
HttpURLConnection connection = null;
DataOutputStream wr = null;
BufferedReader rd = null;
StringBuilder sb = null;
String line = null;
URL serverAddress = null;
Boolean isFirst = true;
try {
serverAddress = new URL("http://localhost:8005/ods/api/"+method);
//Set up the initial connection
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setRequestMethod(httpMethod);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setReadTimeout(10000);
connection.connect();
//get the output stream writer and write the output to the server
wr = new DataOutputStream(connection.getOutputStream());
if (params != null) {
wr.writeBytes(params);
}
wr.flush ();
wr.close ();
//read the result from the server
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
if (!isFirst)
sb.append('\n');
sb.append(line);
isFirst = false;
}
rd.close ();
return sb.toString();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (ProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
//close the connection, set all objects to null
connection.disconnect();
rd = null;
sb = null;
wr = null;
connection = null;
}
throw new Exception ("Bad request!");
}
String xpathEvaluate (Document doc, String xpathString)
throws XPathExpressionException
{
return xpath.evaluate(xpathString, doc);
}
void outFormTitle (javax.servlet.jsp.JspWriter out, String formName)
throws IOException
{
if (formName.equals("login"))
out.print("Login");
if (formName.equals("register"))
out.print("Register");
if (formName.equals("user"))
out.print("View Profile");
if (formName.equals("profile"))
out.print("Edit Profile");
}
void outSelectOptions (javax.servlet.jsp.JspWriter out, String fieldValue, String listValue)
throws IOException, SQLException
{
outSelectOptions (out, fieldValue, listValue, null);
}
void outSelectOptions (javax.servlet.jsp.JspWriter out, String fieldValue, String listValue, String paramValue)
{
try
{
String params;
params = httpParam ("", "key", listValue);
if (paramValue != null)
params += httpParam ("&", "param", paramValue);
String retValue = httpRequest ("GET", "lookup.list", params);
Document doc = createDocument(retValue);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/items/item/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
String F = nodes.item(i).getNodeValue();
out.print ("");
}
} catch (Exception e) {
}
}
%>
<%
String $_form = request.getParameter("form");
if ($_form == null)
$_form = "login";
String $_sid = request.getParameter("sid");
String $_realm = "wa";
String $_error = "";
String $_retValue;
Document $_document = null;
String params = null;
try
{
if ($_form.equals("login"))
{
if (request.getParameter("lf_login") != null)
{
try
{
params = httpParam ( "", "user_name", request.getParameter("lf_uid")) +
httpParam ("&", "password_hash", encrypt (request.getParameter("lf_uid")+request.getParameter("lf_password")));
$_retValue = httpRequest ("GET", "user.authenticate", params);
if ($_retValue.indexOf("") == 0)
{
$_document = createDocument($_retValue);
throw new Exception(xpathEvaluate($_document, "/failed/message"));
}
$_sid = $_retValue;
$_form = "user";
}
catch (Exception e)
{
$_error = e.getMessage();
}
}
}
if ($_form.equals("user"))
{
if (request.getParameter("uf_profile") != null)
$_form = "profile";
}
if ($_form.equals("profile"))
{
if (request.getParameter("pf_update") != null)
{
try {
params = httpParam ( "", "sid" , $_sid) +
httpParam ("&", "realm" , $_realm) +
httpParam ("&", "mail" , request.getParameter("pf_mail")) +
httpParam ("&", "title" , request.getParameter("pf_title")) +
httpParam ("&", "firstName" , request.getParameter("pf_firstName")) +
httpParam ("&", "lastName" , request.getParameter("pf_lastName")) +
httpParam ("&", "fullName" , request.getParameter("pf_fullName")) +
httpParam ("&", "gender" , request.getParameter("pf_gender")) +
httpParam ("&", "birthday" , request.getParameter("pf_birthday")) +
httpParam ("&", "icq" , request.getParameter("pf_icq")) +
httpParam ("&", "skype" , request.getParameter("pf_skype")) +
httpParam ("&", "yahoo" , request.getParameter("pf_yahoo")) +
httpParam ("&", "aim" , request.getParameter("pf_aim")) +
httpParam ("&", "msn" , request.getParameter("pf_msn")) +
httpParam ("&", "homeDefaultMapLocation", request.getParameter("pf_homeDefaultMapLocation")) +
httpParam ("&", "homeCountry" , request.getParameter("pf_homecountry")) +
httpParam ("&", "homeState" , request.getParameter("pf_homestate")) +
httpParam ("&", "homeCity" , request.getParameter("pf_homecity")) +
httpParam ("&", "homeCode" , request.getParameter("pf_homecode")) +
httpParam ("&", "homeAddress1" , request.getParameter("pf_homeaddress1")) +
httpParam ("&", "homeAddress2" , request.getParameter("pf_homeaddress2")) +
httpParam ("&", "homeTimezone" , request.getParameter("pf_homeTimezone")) +
httpParam ("&", "homeLatitude" , request.getParameter("pf_homelat")) +
httpParam ("&", "homeLongitude" , request.getParameter("pf_homelng")) +
httpParam ("&", "homePhone" , request.getParameter("pf_homePhone")) +
httpParam ("&", "homeMobile" , request.getParameter("pf_homeMobile")) +
httpParam ("&", "businessIndustry" , request.getParameter("pf_businessIndustry")) +
httpParam ("&", "businessOrganization" , request.getParameter("pf_businessOrganization")) +
httpParam ("&", "businessHomePage" , request.getParameter("pf_businessHomePage")) +
httpParam ("&", "businessJob" , request.getParameter("pf_businessJob")) +
httpParam ("&", "businessCountry" , request.getParameter("pf_businesscountry")) +
httpParam ("&", "businessState" , request.getParameter("pf_businessstate")) +
httpParam ("&", "businessCity" , request.getParameter("pf_businesscity")) +
httpParam ("&", "businessCode" , request.getParameter("pf_businesscode")) +
httpParam ("&", "businessAddress1" , request.getParameter("pf_businessaddress1")) +
httpParam ("&", "businessAddress2" , request.getParameter("pf_businessaddress2")) +
httpParam ("&", "businessTimezone" , request.getParameter("pf_businessTimezone")) +
httpParam ("&", "businessLatitude" , request.getParameter("pf_businesslat")) +
httpParam ("&", "businessLongitude" , request.getParameter("pf_businesslng")) +
httpParam ("&", "businessPhone" , request.getParameter("pf_businessPhone")) +
httpParam ("&", "businessMobile" , request.getParameter("pf_businessMobile")) +
httpParam ("&", "businessRegNo" , request.getParameter("pf_businessRegNo")) +
httpParam ("&", "businessCareer" , request.getParameter("pf_businessCareer")) +
httpParam ("&", "businessEmployees" , request.getParameter("pf_businessEmployees")) +
httpParam ("&", "businessVendor" , request.getParameter("pf_businessVendor")) +
httpParam ("&", "businessService" , request.getParameter("pf_businessService")) +
httpParam ("&", "businessOther" , request.getParameter("pf_businessOther")) +
httpParam ("&", "businessNetwork" , request.getParameter("pf_businessNetwork")) +
httpParam ("&", "businessResume" , request.getParameter("pf_businessResume")) +
httpParam ("&", "securitySecretQuestion", request.getParameter("pf_securitySecretQuestion")) +
httpParam ("&", "securitySecretAnswer" , request.getParameter("pf_securitySecretAnswer")) +
httpParam ("&", "securitySiocLimit" , request.getParameter("pf_securitySiocLimit"));
$_retValue = httpRequest ("POST", "user.update.fields", params);
if ($_retValue.indexOf("") == 0)
{
$_document = createDocument($_retValue);
throw new Exception(xpathEvaluate($_document, "/failed/message"));
}
$_form = "user";
}
catch (Exception e)
{
$_error = e.getMessage();
$_form = "login";
}
}
else if (request.getParameter("pf_cancel") != null)
{
$_form = "user";
}
}
if ($_form.equals("user") || $_form.equals("profile"))
{
try
{
params = httpParam ( "", "sid" , $_sid) +
httpParam ("&", "realm" , $_realm);
if ($_form.equals("profile"))
params += httpParam ("&", "short", "0");
$_retValue = httpRequest ("GET", "user.info", params);
$_document = createDocument($_retValue);
if ("".compareTo(xpathEvaluate($_document, "/failed/message")) != 0)
throw new Exception (xpathEvaluate($_document, "/failed/message"));
}
catch (Exception e)
{
$_error = e.getMessage();
$_form = "login";
}
}
if ($_form.equals("login"))
{
$_sid = "";
}
}
catch (Exception e)
{
$_error = "Failure to connect to JDBC. " + e.getMessage();
}
%>
|