/*
* @author : Pushkala
* @version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : CMSXDBServletOnDB.java
*
* Creation / Modification History
* Pushkala 25-Jan-2003 Created
*
*/
package oracle.otnsamples.cmsxdb.dbmanager;
import java.util.HashMap;
import java.util.Collection;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.Types;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import oracle.otnsamples.cmsxdb.admin.Resource;
import oracle.otnsamples.cmsxdb.dbmanager.CMSXDBServletUtils;
import oracle.otnsamples.cmsxdb.exception.CMSAccessException;
/**
* This Servlet handles the Content Management, Foldering and Versioning
* functionalities of the Content Management System application.
*
* The following are the actions performed by this Servlet :
* FOLDERCONTENTS - Get the details of folders and files in the
* specified path.
* RENAME - Rename the input resource with the new name
* CREATEFOLDER - Create a new folder (Container) in the input path
* DELETEFOLDER - Delete the input folder path
* DELETEFILE - Delete the input file path
* MAKEVERSION - Add the resource to version control
* CHECKIN - Checkin the new version of the resource
* CHECKOUT - Checkout the resource from version control
* UNDOCHECKOUT - Undo the checkout of the resource
* INSERTVIEW - Insert the Personalization details of the User
* UPDATEVIEW - Update the Personalization details of the User
* VIEWVERSION - Get the contents of the specified version of
* the resource for viewing
* EDITVERSION - Get the contents of the specified version of
* the resource for editing ie,. also checkout the
* resource from version control
* GETPRIVILEGES - Get the privileges of the User on the resource
*
* The Service method of the Servlet routes each of the actions to
* different methods which in turn calls the PL/SQL package functions
* to perform the required action.
*
* The Servlet returns the status of the operation and the data that was
* generated by the specified operation.
*
*/
public class CMSXDBServletOnDB extends HttpServlet {
/**
* Method which is invoked by the Servlet Engine
* when the Servlet is initialized.
*
* @param config Servlet Configuration Object
* @see javax.servlet.ServletConfig
*
* @exception ServletException Exception raised when Servlet Engine
* tries to initialize this Servlet.
*/
public void init( ServletConfig config )
throws ServletException {
super.init(config);
}
/**
* Method which gets invoked when the Servlet receives HTTP GET Request.
*
* @param request Request Object of the Servlet
* @param response Response Object of the Servlet
*
* @exception IOException if an input or output error is detected
* @exception ServletException if the request for GET could not be handled
*
*/
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
serviceRequest( request, response );
}
/**
* Method which gets invoked when the Servlet receives HTTP POST Request.
*
* @param request Request Object of the Servlet
* @param response Response Object of the Servlet
*
* @exception IOException if an input or output error is detected
* @exception ServletException if the request for POST could not be handled
*
*/
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
serviceRequest( request, response );
}
/**
* Method which gets invoked when the Servlet receives a
* HTTP GET or POST Request.
*
* The User credentials, resource path and requested action are passed as
* input paramters which are processed. Depending on the action requested,
* the calls are routed to the respective methods in this Servlet.
* The return object is written to the Servlet OutputStream.
*
* @param request Request Object of the Servlet
* @param response Response Object of the Servlet
*
* @exception IOException if an input or output error writing out resource
* @exception ServletException if the request could not be handled
*
*/
public void serviceRequest( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
// Database connection object
Connection connection = null;
// Initialize the ObjectOutputStream from the OutputStream of response
ObjectOutputStream outobject =
new ObjectOutputStream(response.getOutputStream());
// return Object
Object retObj = null;
try {
// Get default connection
connection = DriverManager.getConnection("jdbc:oracle:kprb:");
// Get the request parameter values
String username = request.getParameter("username");
String password = request.getParameter("password");
String abspath = request.getParameter("ABSPATH");
String action = request.getParameter("ACTION");
// Depending on the action, the requests are routed to the
// respective methods to perform the action
if ("FOLDERCONTENTS".equals(action)) {
// Get the details of folders and files in the
// input resource path accessible to the User
retObj = getFolderContents(abspath, username, connection);
} else if ("RENAME".equals(action)) {
// Rename the resource
String oldname = request.getParameter("oldname");
String newname = request.getParameter("newname");
String xslcheck = request.getParameter("xslcheck");
retObj = this.rename(abspath, oldname, newname, xslcheck);
} else if ("CREATEFOLDER".equals(action)) {
// Create a new folder
String name = request.getParameter("name");
String status = this.createfolder( abspath + "/" + name );
retObj = status;
} else if ("DELETEFOLDER".equals(action)) {
// Delete folder
String deletePath = request.getParameter("deletepath");
String status = this.deleteResource(deletePath);
retObj = status;
} else if ("DELETEFILE".equals(action)) {
// Delete file
String deletePath = request.getParameter("deletepath");
String status = this.deleteResource(deletePath);
retObj = status;
} else if ("MAKEVERSION".equals(action)) {
// Add the resource to Version Control
String filePath = request.getParameter("filepath");
String status = this.addToSourceControl(filePath);
retObj = status;
} else if ("CHECKIN".equals(action)) {
// Checkin the resource
String filePath = request.getParameter("filepath");
String status = this.checkinResource(filePath);
retObj = status;
} else if ("CHECKOUT".equals(action)) {
// Checkout the resource
String filePath = request.getParameter("filepath");
String status = this.checkoutResource(filePath);
retObj = status;
} else if ("UNDOCHECKOUT".equals(action)) {
// Undo the checkout of the resource
String filePath = request.getParameter("filepath");
String status = this.undoCheckout(filePath);
retObj = status;
} else if ("INSERTVIEW".equals(action)) {
// Insert Personalization details of the User
String xslloc = request.getParameter("xslloc");
String viewtype = request.getParameter("viewtype");
String status =
CMSXDBServletUtils.insertView(abspath, username, xslloc,
viewtype, connection);
retObj = status;
} else if ("UPDATEVIEW".equals(action)) {
// Update Personalization details of the User
String xslloc = request.getParameter("xslloc");
String viewtype = request.getParameter("viewtype");
String status =
CMSXDBServletUtils.updateView(abspath, username, xslloc,
viewtype, connection);
retObj = status;
} else if ( "VIEWVERSION".equals(action) ||
"EDITVERSION".equals(action) ) {
// Get the contents of the specified version of the resource either
// for viewing or for editing in which case also checkout the resource
String version = request.getParameter("version");
String status = CMSXDBServletUtils.getPrevContents(abspath, version,
action, connection);
retObj = status;
} else if( "GETPRIVILEGES".equals(action) ) {
// Get privileges for current user
Resource r = new Resource(abspath, null, null );
ArrayList privlist = new ArrayList();
privlist.add(r);
retObj = this.populatePrivilege(privlist);
}
} catch (SQLException sqlex) {
retObj = new CMSAccessException( "SQLException at the "+
" Database servlet : " +
sqlex.toString() );
} catch (CMSAccessException accex) {
retObj = accex;
} catch (Exception ex) {
retObj = new CMSAccessException( "Generic Exception " +
"at the Database servlet : " +
ex.toString() );
} finally {
// Connection.close() has no effect inside database, since this
// Internal Connection is a implicit data channel
if ( retObj != null ) {
// Write the return object created to the ObjectOutputStream
outobject.writeObject(retObj);
}
outobject.flush();
outobject.close();
}
}
/**
* This method is common to methods which need to call PL/SQL package
* functions. This method takes in the query string of the callablestatement
* and the input parameters to the PL/SQL package function.
* The statement is executed and the results are returned.
*
* @param query Statement Query
* @param param1 first parameter to the query
* @param param2 second parameter to the query
* @param param3 third parameter to the query
* @param param4 fourth parameter to the query
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String processQuery( String query, Object param1,
Object param2, Object param3,
Object param4 )
throws CMSAccessException {
CallableStatement stmt = null;
String status = "Failed to process the input query";
try {
// Get the default database connection
Connection connection = DriverManager.getConnection("jdbc:oracle:kprb:");
// Prepare callable Statement to call PL/SQL Stored Procedure
stmt = connection.prepareCall(query);
// Binds the parameter types of the return value
stmt.registerOutParameter(1, Types.VARCHAR);
if (param1 != null) {
stmt.setObject(2, param1); // Bind parameter
}
if (param2 != null) {
stmt.setObject(3, param2); // Bind parameter
}
if (param3 != null) {
stmt.setObject(4, param3); // Bind parameter
}
if (param4 != null) {
stmt.setObject(5, param4); // Bind parameter
}
// Execute the callable statement
stmt.execute();
// Get return status
status = stmt.getString(1);
} catch(SQLException ex) {
throw new CMSAccessException ( "SQL Exception while processing query : " +
query + " : " + ex.toString() );
} finally {
try {
// close the statement
if ( stmt != null ) stmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQL Exception while closing " +
"statement in process query : " +
query + " : " + ex.toString() );
}
}
return status;
}
/**
* This method is called to create a new folder. This method creates the
* callablestatement query calling the PL/SQL package function to create
* a new folder and then calls the processQuery method to execute the query.
*
* @param abspath Resource path
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String createfolder( String abspath )
throws CMSAccessException {
String status = "Failed to create the folder";
String query = "";
query = "begin ? := CMSADMIN.otncms_xmlcontent.createfolder(?); end;";
status = this.processQuery(query, abspath, null, null, null);
return status;
}
/**
* This method is called to rename a resource. This method creates the
* callablestatement query calling the PL/SQL package function to rename
* a resource and then calls the processQuery method to execute the query.
*
* @param abspath Resource path
* @param oldname oldname
* @param newname newname
* @param xslcheck check for XSL file references
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String rename(String abspath, String oldname,
String newname, String xslcheck)
throws CMSAccessException {
String status = "Failed to rename the resource";
String query =
"begin ? := CMSADMIN.otncms_xmlcontent.rename(?, ?, ?, ?); end;";
status = this.processQuery(query, abspath, oldname, newname, xslcheck);
return status;
}
/**
* This method is called to delete a resource. This method creates the
* callablestatement query calling the PL/SQL package function to delete
* a resource and then calls the processQuery method to execute the query.
*
* @param resourceLoc Resource path
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String deleteResource(String resourceLoc)
throws CMSAccessException {
String status = "Failed to delete the resource";
String query =
"begin ? := CMSADMIN.otncms_xmlcontent.deleteresource(?); end;";
status = this.processQuery(query, resourceLoc, null, null, null);
return status;
}
/**
* This method is called to add the resource to version control. This method
* creates the callablestatement query calling the PL/SQL package function to
* add the resource to version control and then calls the processQuery method
* to execute the query.
*
* @param resourceLoc Resource path
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String addToSourceControl(String resourceLoc)
throws CMSAccessException {
String status = "Failed to make versioned";
String query =
"begin ? := CMSADMIN.otncms_xmlcontent.makeversioned(?); end;";
status = this.processQuery(query, resourceLoc, null, null, null);
return status;
}
/**
* This method is called to checkin the resource to version control.
* This method creates the callablestatement query calling the PL/SQL
* package function to checkin the resource to version control and
* then calls the processQuery method to execute the query.
*
* @param resourceLoc Resource path
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String checkinResource(String resourceLoc)
throws CMSAccessException {
String status = "Failed to checkin the resource";
String query = "begin ? := CMSADMIN.otncms_xmlcontent.checkin(?); end;";
status = this.processQuery(query, resourceLoc, null, null, null);
return status;
}
/**
* This method is called to checkout the resource from version control.
* This method creates the callablestatement query calling the PL/SQL
* package function to checkout the resource from version control and
* then calls the processQuery method to execute the query.
*
* @param resourceLoc Resource path
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String checkoutResource(String resourceLoc)
throws CMSAccessException {
String status = "Failed to checkout the resource";
String query = "begin ? := CMSADMIN.otncms_xmlcontent.checkout(?); end;";
status = this.processQuery(query, resourceLoc, null, null, null);
return status;
}
/**
* This method is called to undo checkout the resource from version control.
* This method creates the callablestatement query calling the PL/SQL
* package function to undo checkout the resource from version control and
* then calls the processQuery method to execute the query.
*
* @param resourceLoc Resource path
*
* @return String Result of the execution of the statement
*
* @exception CMSAccessException if any Exception occurs
*
*/
private String undoCheckout(String resourceLoc)
throws CMSAccessException {
String status = "Failed to undo checkout the resource";
String query =
"begin ? := CMSADMIN.otncms_xmlcontent.undocheckout(?); end;";
status = this.processQuery(query, resourceLoc, null, null, null);
return status;
}
/**
* This method is called to get the folder and file details
* in the input resource path.
*
* @param path Resource path
* @param username username
* @param connection database connection object
*
* @return HashMap HashMap containing the details
*
* @exception CMSAccessException if any Exception occurs
*
*/
private HashMap getFolderContents( String path, String username,
Connection connection )
throws CMSAccessException {
HashMap hm = new HashMap();
Collection folderlist = null;
Collection filelist = null;
folderlist = CMSXDBServletUtils.getFolderContents(path, username,
"FOLDER", connection);
filelist = CMSXDBServletUtils.getFolderContents(path, username,
"FILE", connection);
hm.put("FOLDERS", folderlist);
hm.put("FILES", filelist);
return hm;
}
/**
* Populates the privileges for the current user on the specified resource.
*
* @param results List containing the Resource
*
* @return list of resources with privileges populated
*
* @exception CMSAccessException if populating privileges fails
*/
private ArrayList populatePrivilege( ArrayList results )
throws CMSAccessException {
Connection conn = null;
int status = 0;
CallableStatement cstmt = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:kprb:");
cstmt = conn.prepareCall( " begin ? := otncms_xmlcontent.checkPrivileges(?); end; " );
cstmt.registerOutParameter(1,Types.VARCHAR );
for( int i = 0 ; i < results.size() ; i++ ) {
Resource res = ( Resource )results.get( i );
cstmt.setString(2, res.getResPath( ) );
cstmt.execute( );
res.setPrivileges( this.getPrivArray( cstmt.getString(1) ) );
}
} catch ( SQLException sqlEx ) {
throw new CMSAccessException( " Error checking privileges " + sqlEx.toString() );
} finally {
try {
if( cstmt != null ) cstmt.close();
} catch(SQLException sEx) { }
}
return results;
}
/**
* Split the specified privilege list into Array of privileges.
*
* @param privlist list of privileges
*
* @return array containing the privileges
*/
private String[] getPrivArray( String privlist ) {
StringTokenizer st = new StringTokenizer( privlist, "," );
String[] privArr = new String[ st.countTokens() ];
for( int i = 0; st.hasMoreTokens() ; i++ ) {
privArr[i] = st.nextToken();
}
return privArr;
}
}