Access remote systems


The portlet API provides a ContentAccessService, which allows portlets to access remote systems or content from remote URLs, including URLs located on the other side of a proxy server. You should always use this service to access remote content if you cannot be certain whether a firewall will be present after the portlet is deployed into a production environment. The following topics describe how portlets can use the content access service.

See Portlet services for an overview of portlet services and how to access them.

 

IBM example

The following example shows how a portlet written to the IBM portlet API uses the content access service to setup an HTTP input stream to a remote system. This example is similar to the JSR 168 example, except that the portlet uses the PortletContext.getService() method to retrieve the portlet service instead of JNDI.


import org.apache.jetspeed.portlet.service.ContentAccessService;
...

protected InputStream getHTTPInputStream(String urlS, PortletRequest request, PortletResponse response)
          throws MalformedURLException, IOException {

  if(getPortletLog().isInfoEnabled()) {
    getPortletLog().info("getHTTPInputStream: " + urlS);
  }
  try {
    PortletContext context = getPortletConfig().getContext();
    ContentAccessService contentAccessservice = 
      (ContentAccessService)context.getService(org.apache.jetspeed.portlet.service.ContentAccessService.class);
  } catch(PortletServiceException e) {
      getPortletLog().error("MyPortlet: ContentAccessPortletService error: ",e);
      throw new IOException("ContentAccessPortletService Error: " + e);
  }
}

 

getMarkup and character sets

The getMarkup and include methods in ContentAccessService require a Web site to return the character set used to encode the content; this information is normally included in the HTTP response headers. However, sometimes Web sites do not specify the character set. In this case, ContentAccessService assumes the content is encoded with UTF-8. However, if the Web site contains non-English content (for example, double-byte character languages such as Japanese and Korean), content will not render properly.

Developers can work around this problem by using the getInputStream method, which handles the character set conversion manually. For example:

InputStream stream = contentAccessService.getInputStream(url, portletRequest, portletResponse);

Reader reader = new InputStreamReader(stream, mySpecifiedCharset);

 

See also

Home |

 

IBM is a trademark of the IBM Corporation in the United States, other countries, or both.