Tuesday, February 21, 2006
Extension for TrimPath
Javascript Templates from TrimPath is good to work with. The documentation is quite good. It was fast and easy to get started. At the beginning I liked the idea of having the JST Template as the part of the page (hidden textarea), but it turned out that I will not be able to reuse in other pages. The suggested alternative of keeping *.jst files on the web server and load them into the browser using XMLHttpRequest sounded bit interesting.
I just extended TrimPath object by adding the function processTemplateFile, which does and XMLHttpRequest and fetch the jstfile and merge it with the context. You can pass function to get callback with processed template, response status and status text or pass dom element whose innerHTML will be updated once the template is processed.
Look at the extension I have added exttemplate.js
Example:
I just extended TrimPath object by adding the function processTemplateFile, which does and XMLHttpRequest and fetch the jstfile and merge it with the context. You can pass function to get callback with processed template, response status and status text or pass dom element whose innerHTML will be updated once the template is processed.
Look at the extension I have added exttemplate.js
Example:
function mergeWithFile(trmfile, context, getcallback) {
var resultdiv = document.getElementById("result");
if(getcallback != null and getcallback == false)
TrimPath.processTemplateFile(trmfile, context, resultdiv);
else
TrimPath.processTemplateFile(trmfile, context, handler);
function handler(mergedJST, status, statusText) {
if(mergedJST == null) {
mergedJST = "HTTP STATUS: " + status + "," + statusText;
}
resultdiv.innerHTML = mergedJST;
}
}
function mergeWithoutCallback() {
var context = { values : ["prasad", "me"] };
mergeWithFile("jstfiles/mytemplate.jst", context, false);
}
function mergeWithCallback() {
var context = { values : ["prasad", "me"] };
mergeWithFile("jstfiles/mytemplate.jst", context);
// Same as
// mergeWithFile("jstfiles/mytemplate.jst", context, true);
}
Monday, February 20, 2006
Tomcat WebApplication Context File
You can deploy tomcat web application by creating a <webapp>.xml file. This method is handy while you are developing the WebApplication (where you don't want to build the war file, deploy it to Tomcat/webapps) and it also allows reloading the context for the web application when any class file (in WEB-INF/classes) directory changes.
For this to work, you should have the xml file (name same as "application path"+ ".xml") in Tomcat/conf/Catalina/localhost directory, you don't need to put the war file under Tomcat/webapps.
Let say, our web application directory is "/home/prasad/apps/TestApp/web" and
we like to access it as "http://localhost:8080/testapp". Then, you have to create the "testapp.xml" in Tomcat/conf/localhost directory with the following content in it:
<xml version="1.0">
<Context path="/testapp"
docBase="/home/prasad/apps/TestApp/web"
reloadable="true" />
The reloadable attribute is the one the allows reloading of context when the class file changes. docBase attribute should point to the web directory (which has child directory named WEB-INF), otherwise Tomcat can delete the context file, as its not valid.
Creation of context file is useful when you want to secure you web application, ie. apply security constraints to access files of you web application and you want to Tomcat to use your tables to authenticate user access. Here is how you can setup Tomcat's JDBC authentication:
<?xml version="1.0"?>
<Context path="/testapp"
docBase="/home/prasad/apps/TestApp/web"
reloadable="true">
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="jdbc.DatabaseDriver"
connectionURL="jdbc:protocol:appauthdb"
connectionName="admin" connectionPassword="admin"
userTable="utable" userNameCol="uname" userCredCol="passwd"
userRoleTable="rtable" roleNameCol="rname"/>
</Context>
Table Structure:
create table utable (uname varchar(255), passwd varchar(255), primary key(uname, passwd))
create table rtable (uname varchar(255), rname varchar(255))
This basically sets up Realm for this web application. [By default, there is Realm defined for entire Engine or Host in Tomcat/conf/server.xml] You are basically setting the context to use JDBC Realm and acess appauthdb database tables utable and rtable to authenticate the user.
To check out the Realm usage you have to define the security-constraint in web.xml file in (/home/prasad/apps/TestApp/WEB-INF/web.xml).
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>appuser</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<!-- Commented
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
-->
</login-config>
We defined the security contraint for the entire web application (url-pattern) and all user with role (role-name) appuser can access the resource of web application. Once done add values to utable and rtable.
insert into utable(uname, passwd) values ('prasad', 'somepwd')
insert into rtable(uname, rname) values ('prasad', 'appuser')
When you visit, http://localhost:8080/testapp, you get a HTTP Authentication dialog box, and you can see only 'prasad' is allowed to use the application.
Hope this piece of information is useful.
For this to work, you should have the xml file (name same as "application path"+ ".xml") in Tomcat/conf/Catalina/localhost directory, you don't need to put the war file under Tomcat/webapps.
Let say, our web application directory is "/home/prasad/apps/TestApp/web" and
we like to access it as "http://localhost:8080/testapp". Then, you have to create the "testapp.xml" in Tomcat/conf/localhost directory with the following content in it:
<xml version="1.0">
<Context path="/testapp"
docBase="/home/prasad/apps/TestApp/web"
reloadable="true" />
The reloadable attribute is the one the allows reloading of context when the class file changes. docBase attribute should point to the web directory (which has child directory named WEB-INF), otherwise Tomcat can delete the context file, as its not valid.
Creation of context file is useful when you want to secure you web application, ie. apply security constraints to access files of you web application and you want to Tomcat to use your tables to authenticate user access. Here is how you can setup Tomcat's JDBC authentication:
<?xml version="1.0"?>
<Context path="/testapp"
docBase="/home/prasad/apps/TestApp/web"
reloadable="true">
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="jdbc.DatabaseDriver"
connectionURL="jdbc:protocol:appauthdb"
connectionName="admin" connectionPassword="admin"
userTable="utable" userNameCol="uname" userCredCol="passwd"
userRoleTable="rtable" roleNameCol="rname"/>
</Context>
Table Structure:
create table utable (uname varchar(255), passwd varchar(255), primary key(uname, passwd))
create table rtable (uname varchar(255), rname varchar(255))
This basically sets up Realm for this web application. [By default, there is Realm defined for entire Engine or Host in Tomcat/conf/server.xml] You are basically setting the context to use JDBC Realm and acess appauthdb database tables utable and rtable to authenticate the user.
To check out the Realm usage you have to define the security-constraint in web.xml file in (/home/prasad/apps/TestApp/WEB-INF/web.xml).
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>appuser</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<!-- Commented
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
-->
</login-config>
We defined the security contraint for the entire web application (url-pattern) and all user with role (role-name) appuser can access the resource of web application. Once done add values to utable and rtable.
insert into utable(uname, passwd) values ('prasad', 'somepwd')
insert into rtable(uname, rname) values ('prasad', 'appuser')
When you visit, http://localhost:8080/testapp, you get a HTTP Authentication dialog box, and you can see only 'prasad' is allowed to use the application.
Hope this piece of information is useful.
Wednesday, February 15, 2006
IE supports Conditional Comments
I came across a situation where I had to write few CSS rules (call it a hack!) to make things look the same on both IE and Firefox. I actually wrote IE specific rules in a different file with the intension to include it only when page is rendered on IE. Its quite interesting to know the support for Conditional Comments available in IE 5+.
Well the article gives the complete details about conditional comments.
Brief:
IE supports if conditional construct within the HTML comment.
Example, to include a stylesheet when HTML is displayed in IE.
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="cssfiles/forie.css"/>
<![endif]-->
<!--[if !IE 7]>
<script type="text/javascript">
document.write("<b>IE Version Used is not 7</b>");
</script>
<![endif]-->
Well the article gives the complete details about conditional comments.
Brief:
IE supports if conditional construct within the HTML comment.
Example, to include a stylesheet when HTML is displayed in IE.
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="cssfiles/forie.css"/>
<![endif]-->
<!--[if !IE 7]>
<script type="text/javascript">
document.write("<b>IE Version Used is not 7</b>");
</script>
<![endif]-->
Thursday, February 09, 2006
Feed discovery extension
Feeds available on a site is currently discovered by feed readers by looking at link rel="alternate" tag, which is present on the page:
<link rel="alternate" type="application/rss+xml"
title="MySiteMainFeed" href="http://localhost.com/main.xml" />
The difficulty with this is, you have to specify link rel="alternate" for each of the feed available on the site. To simplify this we can have link rel="alternate" type="application/feedlist+xml" on the page which provides list of available feeds on your site.
Example:
<link rel="alternate" type="application/feedlist+xml"
title="MySiteMainFeed" href="http://localhost.com/feedlist.xml" />
feedlist.xml will be:
<?xml version="1.0"?>
<feedlist>
<link rel="alternate" title="RSS Content" type="application/rss+xml"
href="http://localhost.com/rss_feed.xml"/>
<link rel="alternate" title="Atom Content" type="application/atom+xml"
href="http://localhost.com/atom_feed.xml"/>
<link rel="alternate" title="Comment" type="application/rss+xml"
href="http://localhost.com/comment_rss_feed.xml"/>
</feedlist>
<link rel="alternate" type="application/rss+xml"
title="MySiteMainFeed" href="http://localhost.com/main.xml" />
The difficulty with this is, you have to specify link rel="alternate" for each of the feed available on the site. To simplify this we can have link rel="alternate" type="application/feedlist+xml" on the page which provides list of available feeds on your site.
Example:
<link rel="alternate" type="application/feedlist+xml"
title="MySiteMainFeed" href="http://localhost.com/feedlist.xml" />
feedlist.xml will be:
<?xml version="1.0"?>
<feedlist>
<link rel="alternate" title="RSS Content" type="application/rss+xml"
href="http://localhost.com/rss_feed.xml"/>
<link rel="alternate" title="Atom Content" type="application/atom+xml"
href="http://localhost.com/atom_feed.xml"/>
<link rel="alternate" title="Comment" type="application/rss+xml"
href="http://localhost.com/comment_rss_feed.xml"/>
</feedlist>