четверг, 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.