venice-main-classic/src/com/silverwrist/venice/db/UserNameRewriter.java

107 lines
3.5 KiB
Java
Raw Normal View History

2001-01-31 13:55:37 -07:00
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
2001-01-31 13:55:37 -07:00
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.db;
import java.sql.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.core.IDUtils;
public class UserNameRewriter implements Rewriter
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
public static final String URI_PREFIX = "x-userlink:";
2001-01-31 13:55:37 -07:00
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private DataPool datapool;
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public UserNameRewriter(DataPool datapool)
{
this.datapool = datapool;
} // end constructor
/*--------------------------------------------------------------------------------
* Implementations from interface Rewriter
*--------------------------------------------------------------------------------
*/
public String getName()
{
return null;
} // end getName
public MarkupData rewrite(String data, RewriterServices svc)
{
if (StringUtil.isStringEmpty(data) || (data.length()>64) || !(IDUtils.isValidVeniceID(data)))
return null;
Connection conn = null;
try
{ // get a database connection and create a statement
conn = datapool.getConnection();
Statement stmt = conn.createStatement();
StringBuffer sql = new StringBuffer("SELECT uid FROM users WHERE username = '");
sql.append(SQLUtil.encodeString(data)).append("';");
ResultSet rs = stmt.executeQuery(sql.toString());
if (!(rs.next()))
return null; // not found
} // end try
catch (SQLException e)
{ // if we hit a database error, just return null...
return null;
} // end catch
finally
{ // make sure and release the connection before we go
if (conn!=null)
datapool.releaseConnection(conn);
} // end finally
// build the markup data and return it
StringBuffer open_a = new StringBuffer("<A HREF=\"");
open_a.append(URI_PREFIX).append(data).append("\"");
2001-01-31 13:55:37 -07:00
String catenate = svc.getRewriterAttrValue("ANCHORTAIL");
if (!(StringUtil.isStringEmpty(catenate)))
open_a.append(' ').append(catenate);
open_a.append('>');
return new MarkupData(open_a.toString(),data,"</A>");
} // end rewrite
} // end class UserNameRewriter