Tuesday, August 21, 2012

How-to fix: ResourceServlet._setHeaders(): Content type for /oracle/webcenter/portalapp/shared/png/image.png is NULL!

While browsing the error log of webcenter portal application I noticed recurrent error:

[2012-08-02T17:21:00.486+02:00] [WC_Portlet1] [WARNING] [] [org.apache.myfaces.trinidad.webapp.ResourceServlet] [tid: [ACTIVE].ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'] [userId: myUser] [ecid: 0000JZbvEp6Cwk85njP5iZ1G6c2e0000NQ,0] [APP: Portal_application1#V2.0] ResourceServlet._setHeaders(): Content type for /oracle/webcenter/portalapp/shared/png/settings.png is NULL![[
Cause: Unknown file extension
]]

The error isn't fatal, but clutters the log. To fix this error I did small investigation. In the first step I went to org.apache.myfaces.trinidad.webapp.ResourceServlet class source (I found it on grepcode.com). Then I found the source of our error in _setHeaders function:

if (contentType == null || "content/unknown".equals(contentType))
    {
      url = connection.getURL();
      resourcePath = url.getPath();

      // 'Case' statement for unknown content types
      if (resourcePath.endsWith(".css"))
        contentType = "text/css";
      else if (resourcePath.endsWith(".js"))
        contentType = "application/x-javascript";
      else if (resourcePath.endsWith(".cur") || resourcePath.endsWith(".ico"))
        contentType = "image/vnd.microsoft.icon";
      else
        contentType = getServletContext().getMimeType(resourcePath);

      // The resource has an file extension we have not
      // included in the case statement above
      if (contentType == null)
      {
        _LOG.warning("ResourceServlet._setHeaders(): " +
                     "Content type for {0} is NULL!\n" +
                     "Cause: Unknown file extension",
                     resourcePath);
      }
    }
  
It can be seen clearly the problem lays in line 14:

contentType = getServletContext().getMimeType(resourcePath);

where function  getMimeType definitely returns null.

To check what happens in this function I went to documentation of javax.servlet.ServletContext class. In javadoc we can read:

"Returns (getMimeType) the MIME type of the specified file, or null if the MIME type is not known. The MIME type is determined by the configuration of the servlet container, and may be specified in a web application deployment descriptor. Common MIME types are "text/html" and "image/gif"."

The key to solve our problem lays in words: "The MIME type is determined by the configuration of the servlet container, and may be specified in a web application deployment descriptor"

The mime types for web application can be configured in a web.xml deployment descriptor. A mapping between an extension and a mime type defines fhe mime-mapping element. As described in documentation the following elements you can define within a mime-mapping element:

<extension>  A string describing an extension, for example: txt.
<mime-type> A string describing the defined mime type, for example: text/plain.

In accordance with these instructions to configure mime-types for png files I added to my web.xml descriptor:

<mime-mapping>

    <extension>png</extension>

    <mime-type>image/png</mime-type>

</mime-mapping>
 
After redeployment all should work fine. And really works:)

No comments:

Post a Comment