Thoughts Electrique

Posts Tagged ‘Sourcecode’

JSP to download OpenCms resources as a ZIP

Thursday, June 25th, 2009

What often bugged me is that I can upload a set of files into OpenCms as a ZIP file but can not download a tree of files as such (without the use of the database export feature). I had hacked together this little JSP some time ago. It allows you to dynamically download a set of OpenCms resources as a ZIP file.

The JSP has no interface since I mainly use it for development. Simply create a JSP somewhere in the OpenCms VFS and then open the file via the browser. E.g.:

“http://workplace.com/system/createzip?source=/system/modules/foo&filename=foo-module.zip

The source parameter specifies which directory to zip up and the filename parameter which filename to use for the generated file. You can only zip up resources with a total size of 10 megabytes. This is mainly to prevent OutOfMemory errors to happen. You can easily increase the size by altering the variable maxSourceSize to any value you need.

If you find any bugs or want to give feedback simply leave a comment or send me an email.

(more…)

Manipulating OpenCms XMLContent programatically (some examples)

Friday, December 12th, 2008

Because someone asked on the mailing list and I keep forgetting the respective API calls to manipulate an OpenCms XMLContent programatically.

This is just a raw code dump not a step by step tutorial.

You can also download a ZIP file for importing it into OpenCms. But you have to adjust the paths manually.

(more…)

Use the power of JSP tag files in OpenCms

Friday, December 12th, 2008

Since version 7 OpenCms is a Servlet 2.4 and JSP 2.0 conforming application. In addition to the improved expression language (EL) JSP 2.0 also brings in the possibility of developing custom tags(actions) using JSP rather than writing Java code. This allows for some pretty neat things and enables you to go one step further towards cleaner view code. I’ve used this in one of my last projects extensively to refactor common code like pagers and link generation. For example:

<cms:include file="/system/modules/com.example.ocms.commons/elements/pager">
  <cms:param name="currentPage">
    <c:out value="${model.currentPage}"/>
  </cms:param>
  <cms:param name="pageCount">
    <c:out value="${model.pageCount}"/>
  </cms:param>
</cms:include>

Became:

<wt:pager model="${model}"/>

Not does it only look cleaner and is much more focused, it also decouples your view more from the underlying CMS solution. Of course you could also develop a custom tag library using Java (which I have done a few times) but using a JSP the advantage that code can be changed on the fly and even from a web designer. You can always refactor your JSP taglib into a Java one if you need to do it. There are some limitations of JSP tag files (like you can’t use scriptlets in the body) but they are easy to get around.

But as always: This is no silver bullet. There are problems with tag files which may not be obvious at the first glance.

(more…)

Better SEO for external links in OpenCms Navigation

Wednesday, November 26th, 2008

The Problem

If you put an external link into the OpenCms navigation the resulting link will target the external link file which then in turns redirects the surfer to the external page. This is bad if you really want to optimize your navigation for search engines, because search engines won’t see the real link inside the navigation.

The solution

You can use the following JSP snippet to get the real URL from a CmsJspNavElement.

// We assume that the variable "link" holds an CmsJspNavElement and
// "cmsa" holds an CmsJspActionElement.

// Prevent external links from beeing a redirect
// therefore extract the real URL out of the file
String fileUri = link.getResourceName();
CmsResource navResource = cmsa.getCmsObject().readResource(fileUri);
if (navResource.getTypeId() == CmsResourceTypePointer.getStaticTypeId()) {
    CmsFile navFile = CmsFile.upgrade(navResource,cmsa.getCmsObject());
    String externalLink = new String(navFile.getContents());
    externalLink = externalLink.trim();
}

Further reading

Google Analytics with OpenCms

Wednesday, November 19th, 2008

Avoid the wrong track

For a while I have been using Google Analytics for my website statistics (additionally to AWStats) and I really like the reports (Note: Yahoo! now has a competing product out there called “Yahoo! Web Analytics” which I might check out in the future). I’ve also installed various tracking software (like etracker - which is a German service) on different CMS solutions at customer sites.

Nearly all services offer some copy&paste code to throw into your HTML/JSP/PHP/Whatever, but if you’re using a CMS system things might be a bit more complicated. In most CMS systems you have two different kinds of traffic: Visitor generated and editor generated. Editors browse the site while checking their changes or proofreading text in the offline area (unpublished content). You most likely don’t want to have this traffic screw up your statistics, so it makes sense to filter out the traffic. Mostly a simple if(…)-clause is all that’s needed to achieve this. Here is my solution for OpenCms:

<%@ page session="false" %>
<%@ page import="org.opencms.jsp.*,org.opencms.file.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  CmsJspActionElement cmsa = new CmsJspActionElement(pageContext,request,response);
  CmsUser currentUser = cmsa.getRequestContext().currentUser();
  boolean displayAnalytics = currentUser.isGuestUser() || currentUser.isWebuser();
  pageContext.setAttribute("displayAnalytics",new Boolean(displayAnalytics));
%>
<c:if test="${displayAnalytics}">
  <!-- Your Google Analytics JavaScript goes here -->
</c:if>

Of course you can easily create a JSP from this and include it in your template.