Patrick's Lucky Dip - Showing an Objective Document |
||
The ProblemWe wanted to be able to link our corporate intranet to Objective. All our strategic documents, in fact all our documents, are stored in Objective. Therefore, we had the choice of making copies on the intranet or interfacing them in some way. Making copies seemed like a bad choice - they would get quickly out of sync, with decisions made based on out of date information. So we built an interface. Actually, none of the original work is my own, but I've re-written it completely for presentation here. In doing so I have simplified it considerably, removing site-specific error trapping and also a method of dealing with two people accessing the same document at the same time. This involved adding to the document name when transferring it to the application server. This made the code more difficult to follow and it is relatively simple to add back in. The SolutionWe wrote a jsp page to present a word document based on the document id passed in as a parameter. The page is invoked as http://the.url/goes/here/showDocument.jsp?oid=[doc id]. You can download the source code and it is also explained below. You need to remember to import the objective packages: <%@ page import="com.objective.oji.*"%> <%@ page import="java.lang.*"%> <%@ page import="java.net.*"%> <%@ page import="oracle.jdbc.pool.OracleDataSource"%> <%@ page import="java.sql.*"%> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> The next bit is to read the objective document id oid passed in as a parameter. We check to see if the oid begins with the letter "z", which would indicate that the object in question is an alias rather than the original. In Objective, an alias is a pointer to the original and is used to allow people to store a reference to a document in more than one file. However, it does need careful handling, so we'll deal with that in a different page.
<%
// This jsp calls two other jsp files: alias.jsp and nodoc.jsp
// alias.jsp is used to handle the reading of document aliases
// nodoc.jsp is used to display an error page when the document cannot be found
//
// Note: there are a number of places within the code where user ids, server names
// passwords and ip addresses have been removed. You will have to replace these
// with the appropriate text for your own installation.
//Read querystring to get ID number
String oid="A1234"; // an error page
try
{
oid = request.getParameter("oid").toUpperCase();
}
catch(Exception e)
{
oid = "A1234";
}
// deal with alias's
// a document alias always begins with the letter z
if( oid.substring(0,1).equals("Z") )
{
response.sendRedirect( "alias.jsp?oid=" + oid );
}
Now we connect to Objective, and get the document doc that the oid identifies. We then get the current version of the document ver. I've not included the error page nodoc.jsp as I'm sure you could write this very easily yourself.
// application object
OjiApplication objective = new OjiApplication();
// login to the server
OjiSession apiSession = objective.loginUser("username", "password", "server", portNumber);
//Get Object from Objective server
OjiDocument doc = (OjiDocument) obj;
OjiDocVersion ver;
try
{
ver = DOC.getCurrentVersion();
}
catch(Exception e)
{
response.sendRedirect( "nodoc.jsp?oid=" + oid );
}
Now we extract from the latest version of the document the extension and filename. We convert the filename into a web-safe version of the name, stripping out any dodgy characters. This is the point at which you could add a unique string to the filename.
// get the file extension
String extension = ver.getFileType().toLowerCase();
// make a web-safe version of the filename, replacing any dodgy characters
String safeLabel = doc.getLabelForView();
char qu = (char) 34;
String quotes = "" + qu;
char bs = (char) 92;
String backslash = "" + bs;
safeLabel = safeLabel.replaceAll(quotes,"");
safeLabel = safeLabel.replaceAll("\\"+backslash+"+","");
safeLabel = safeLabel.replaceAll("/","");
safeLabel = safeLabel.replaceAll(":","");
safeLabel = safeLabel.replaceAll("\\*+","");
safeLabel = safeLabel.replaceAll("\\?+","");
safeLabel = safeLabel.replaceAll("<","");
safeLabel = safeLabel.replaceAll(">","");
safeLabel = safeLabel.replaceAll("\\|+","");
int maxLength = 100;
if( safeLabel.length() > maxLength )
{
safeLabel = safeLabel.substring( 0, maxLength ) + "...";
}
String filename= "[" + oid + "] " + safeLabel + "." + extension;
Now we use the getRemoteFile method of the ver document version object to stream the file from the Objective server to the application server, saving it in a directory of your choice. Having got the correct file, we can log out from Objective and use the response.sendRedirect(...) method to cause the browser to navigate to the document.
String server=request.getServerName().toLowerCase();
String base="";
if ( server.equals( "<serverName>" ) || server.equals( "<server ip address>" ) )
{
try
{
// get the file from Objective and put in on the application server
ver.getRemoteFile( "<directory on app server>" + filename);
}
catch(Exception e)
{
response.sendRedirect("nodoc.jsp?oid=" + oid );
}
base="http://<server name>:<port>/<path to this jsp application>/";
}
apiSession.logout();
response.sendRedirect( base + filename );
If something went wrong along the way, we can display a little error message. %> <http> <head> </head> <body> Error encountered. Should have been redirected to view document <%=filename%>. </body> </html> I hope this is useful, and that in attempting to make it easier to follow I've not deleted something essential. Any errors in the code are therefore entirely my own and it comes with absolutely no warrenty at all. Patrick Haston |
||