/** * Copyright (C) 2010 Orbeon, Inc. * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU Lesser General Public License as published by the Free Software Foundation; either version * 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html */ package org.orbeon.oxf.portlet; import java.io.IOException; import java.io.Writer; import javax.portlet.MimeResponse; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.orbeon.oxf.common.OXFException; public class CopyOfWSRP2Utils { private static final String BASE_TAG = "wsrp_rewrite"; private static final char START_TAG_MARKER = '?'; private static final String END_TAG = '/' + BASE_TAG; private static final char NAMESPACE_PLACEHOLDER_MARKER = '_'; private MimeResponse mimeResponse; private Writer out; private String content; private String namespace; private boolean encodeForXML; private int currentPosition; public CopyOfWSRP2Utils(MimeResponse mimeResponse, String content, String namespace, boolean encodeForXML) throws IOException { this.mimeResponse = mimeResponse; this.content = content; this.namespace = namespace; this.encodeForXML = encodeForXML; currentPosition = 0; out = mimeResponse.getWriter(); } private void parse() throws IOException { int nextTagPosition; while ((nextTagPosition = content.indexOf(BASE_TAG, currentPosition)) != -1) { passthruUntil(nextTagPosition); currentPosition = nextTagPosition + BASE_TAG.length(); parseTag(); } writeRemainder(); } private void passthruUntil(int nextTagPosition) throws IOException { out.write(content, currentPosition, nextTagPosition - currentPosition); } private void writeRemainder() throws IOException { passthruUntil(content.length()); } private void parseTag() throws IOException { if (isEscapeTag()) { out.write(BASE_TAG); currentPosition += BASE_TAG.length(); } else if (isStartTag()) { int endTagPosition = content.indexOf(END_TAG); if (endTagPosition == -1) throw new OXFException("wsrp-rewrite tag at position " + (currentPosition - BASE_TAG.length()) + "is missing a closing tag"); String tagValue = content.substring(currentPosition + 1, endTagPosition); currentPosition = endTagPosition + END_TAG.length(); String decodedPortletURL = new WsrpToPortletURL(mimeResponse, tagValue).decode(); if (encodeForXML) decodedPortletURL = StringEscapeUtils.escapeXml(decodedPortletURL); out.write(decodedPortletURL); } else if (isNamespacePlaceholder()) { out.write(namespace); ++currentPosition; } else throw new OXFException("Invalid wsrp rewrite tagging at position " + currentPosition + " near '" + StringUtils.substring(content, currentPosition - BASE_TAG.length(), currentPosition + 10) + "'"); } private boolean isNamespacePlaceholder() { return content.charAt(currentPosition) == NAMESPACE_PLACEHOLDER_MARKER; } private boolean isStartTag() { return content.charAt(currentPosition) == START_TAG_MARKER; } private boolean isEscapeTag() { return StringUtils.substring(content, currentPosition, currentPosition + BASE_TAG.length()).equals(BASE_TAG); } /** * This method parses a string containing WSRP Consumer URL and namespace * encoding and encode the URLs and namespaces as per the Portlet API. * * It is possible to escape by using the string wsrp_rewritewsrp_rewrite. */ public static void write(MimeResponse response, String content, String namespace, boolean encodeForXML) throws IOException { new CopyOfWSRP2Utils(response, content, namespace, encodeForXML).parse(); } }