четверг, 29 мая 2014 г.

Generate javascript with jsp

What if you need to dynamically  generate your javascript file on server? I mean no JSON file, but regular javascript files.

You can achieve this with jsp like so:

1) Set the header on your jsp "<%@page language="java" contentType="text/JavaScript" pageEncoding="UTF-8" isELIgnored="true"%>".

Pay attention to attribute isELIgnored. If you use jQuery or some other js library then you'd better set isELIgnored to true to prevent servlet container to interpret the constructions like ${} as EL constructs.

2) In web.xml create the record for servlet like
  <servlet>
        <servlet-name>initjs</servlet-name>
        <jsp-file>/init.jsp</jsp-file>
    </servlet>

and create servlet-mapping like this:
<servlet-mapping>
        <servlet-name>initjs</servlet-name>
        <url-pattern>/init.js</url-pattern>
    </servlet-mapping>


Deploy your application and access you jsp as if it were js file: /init.js. You will see your javascript file generated by servlet container.

четверг, 22 мая 2014 г.

Use PUMA API from within WebSphere Portal 7 and WebSphere Application Server 7

There is a moment when you need to create users from within portal or application server. PUMA API allows you to do management work with you users.

The crucial interface for working with PUMA API is PumaHome.

Portal
In portlet you will use instance of com.ibm.portal.um.portletservice.PumaHome which you get from com.ibm.portal.portlet.service.PortletServiceHome like this:

pumaHome = psh.getPortletService(com.ibm.portal.um.portletservice.PumaHome.class);


Instance of com.ibm.portal.portlet.service.PortletServiceHome you can obtain from JNDI lookup by name "portletservice/com.ibm.portal.um.portletservice.PumaHome".

In Spring one can do it like this:    <jee:jndi-lookup id="userManagement" jndi-name="portletservice/com.ibm.portal.um.portletservice.PumaHome" resource-ref="true"></jee:jndi-lookup>

Application Server
If you want to manage users from WebSphere Application Server, then you should use instance of com.ibm.portal.um.PumaHome which you can get from JNDI lookup by name "portal:service/usermanagement/Puma".

вторник, 6 мая 2014 г.

WebSphere Application Server 7\Websphere Portal Server 7. Download files containing non-ASCII characters (Cyrillic example).

In portlet applications there is often a need to download files.
Nowadays people want to exchange files named in languages other then english.
In this post I want to describe how to download files, which contain russian characters in their names.

As you know in portlet environment one need to use ResourceResponse class to output any content to the client.

Before you output the content to the client you need to set proper http headers on response.
For file downloading you have to set header Content-disposition: attachment; filename="your file name".

HTTP specification RFC2616 prohibits the use of non-ASCII symbols in HTTP headers, but that doesn't mean you can violate this rule.

RFC2231 provides the mechanism to specify the character set before the attribute, like this filename*=utf-8''uri_encoded_filename.

If you implement this solution you might found out that sometimes Firefox allows you to save the file with the given filename and other times Firefox will allow you to save the file with blank filename.

If you look at the response in sniffer like wireshark you can find out that your header Content-disposition: attachment; filename="your file name" had been magically transferred to Content-disposition: attachment; filename=""some_junk. That's why Firefox asks you save file with a blank name.

I had find out the following workaround: prepend the "your file name" with exclamation mark, so this is the result:
Content-disposition:attachment;  filename="!" + fileName+ "\"; filename*=utf-8''" + encodedFilename

After this modification the server no longer breaks your intentions and doesn't modify your header to Content-disposition: attachment; filename=""some_junk.

This behavior I found out on WAS 7.0.0.21.

четверг, 30 января 2014 г.

Deploy artifacts from Ivy to Artifactory

After some time struggling with deploying jar files to Apache Archiva I decided to throw it out and replace with Artifactory oss version.
The installation of Artifactory is simple - just drop the war file into Tomcat webapp.

What is important for me is that Artifactory supports deployment of artifacts with Ivy.

Use IBM JRE 7 with Tomcat 7 on Windows 7

If you have running Apache Tomcat on Windows you'll most likely run it on Oracle JVM.
What if you need to run Tomcat on IBM JRE?
You can use Apache Service Manager application to change the settings of JRE.
Open Apache Service Manager, goto Java tab.
There you see "Java Virtual Machine" field. My current value for Oracle JRE is  "C:\jre6\bin\client\jvm.dll".
Change it to file jre\bin\j9vm\jvm.dll.
Press apply.
Start Tomcat.
In your server status you can check that Tomcat is running on IBM JRE.

вторник, 28 января 2014 г.

WebSphere Portal. Run XmlAccess on remote hosts.

If you want to automate the configuration and deployment task on WebSphere Portal, eventually, you will use XmlAccess.

IBM provides ant task called XmlAccess. My goal is to achieve the invokation of configuration tasks on remote machine from my machine. I use Windows 7 and ant 1.8.4 . Remote installations of WebSphere Portal use Linux.

четверг, 16 января 2014 г.

Learning jQuery Deferreds

Started to learn Learning jQuery Deferreds.

The aim of the book is to give the high level overview of the deferreds and their implementation in jQuery particularly.
Here are the benefits of this book:
1) Introduction to promises and deferreds (through nice informal analogies). No special skills are necessary.
2) A lot of practical ready recipes. You can apply them immediately.
3) Exercises and answers.
4) The size of the book is rather small. You can read it fast.

After reading this book you will have another tool in your belt. Deffereds  are not the silver bullet, but sometimes is will allow you to write maintainable javascript code.

Some examples on deferreds application can be found on http://learn.jquery.com/code-organization/deferreds/examples/.

Update 08.07.2014: jQuery contains useful method $.when. Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events. In practice you can pass it multiple deffereds and the method will resolve as soon as all the Deferreds resolve, or reject as soon as one of the Deferreds is rejected.

So, if you pass some ajax requests and want to wait when ALL of them will be finished with success or failed you can't use $.when.

I have find out the following solution to this in the net:

$(function(){
 $.whenAll = function( firstParam ) {
     var args = arguments,
         sliceDeferred = [].slice,
         i = 0,
         length = args.length,
         count = length,
         rejected,
         deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise )
             ? firstParam
             : jQuery.Deferred();

     function resolveFunc( i, reject ) {
         return function( value ) {
             rejected = true;
             args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
             if ( !( --count ) ) {
                 // Strange bug in FF4:
                 // Values changed onto the arguments object sometimes end up as undefined values
                 // outside the $.when method. Cloning the object into a fresh array solves the issue
                 var fn = rejected ? deferred.rejectWith : deferred.resolveWith;
                 fn.call(deferred, deferred, sliceDeferred.call( args, 0 ));
             }
         };
     }

     if ( length > 1 ) {
         for( ; i < length; i++ ) {
             if ( args[ i ] && jQuery.isFunction( args[ i ].promise ) ) {
                 args[ i ].promise().then( resolveFunc(i), resolveFunc(i, true) );
             } else {
                 --count;
             }
         }
         if ( !count ) {
             deferred.resolveWith( deferred, args );
         }
     } else if ( deferred !== firstParam ) {
         deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
     }
     return deferred.promise();
 };
});

This function allows you to pass multiple deffereds and wait until all of them will be resolved or rejected.

среда, 8 января 2014 г.

Thoughts about AspectJ in Action: Enterprise AOP with Spring Applications

Have finished reading book AspectJ in Action.
This is a rather lengthy book, but it covers AOP in depth. First, AOP theory and AspectJ as a reference implementation. Second, Spring AOP and the use of AspectJ and Spring altogether.

The author writes that AOP is not the silver bullet. As always choose the right tool for the right job.

I would also recommend read Spring documentation on AOP together with this book. Also, the book contains the path to gradually adoption of AOP in your projects.