[jboss-svn-commits] JBL Code SVN: r25739 - in labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src: main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Mar 19 11:19:34 EDT 2009
Author: lkrzyzanek
Date: 2009-03-19 11:19:34 -0400 (Thu, 19 Mar 2009)
New Revision: 25739
Added:
labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/
labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponent.java
labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/test/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/
labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/test/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponentTest.java
Log:
added HFURLComponent
Added: labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponent.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponent.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponent.java 2009-03-19 15:19:34 UTC (rev 25739)
@@ -0,0 +1,123 @@
+/*
+ * JBoss.org http://jboss.org/
+ *
+ * Copyright (c) 2009 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Libor Krzyzanek
+ */
+package org.jboss.labs.clearspace.plugin.hfurl.struts.components;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.struts2.StrutsException;
+import org.apache.struts2.components.URL;
+import org.jboss.labs.clearspace.plugin.hfurl.HFURLManager;
+import org.springframework.dao.EmptyResultDataAccessException;
+
+import com.opensymphony.xwork2.util.ValueStack;
+
+/**
+ * @author <a href="mailto:lkrzyzan at redhat.com">Libor Krzyzanek</a>
+ *
+ */
+public class HFURLComponent extends URL {
+
+ private static final Log log = LogFactory.getLog(HFURLComponent.class);
+
+ private HFURLManager hfURLManager;
+
+ private final static String DOCID_PREFIX = "/docs/";
+
+ public HFURLComponent(ValueStack stack, HttpServletRequest req,
+ HttpServletResponse res) {
+ super(stack, req, res);
+ }
+
+ @Override
+ public boolean end(Writer writer, String body) {
+ // write result of parent to String writer and then do modification
+ Writer tempWriter = new StringWriter();
+ boolean result = super.end(tempWriter, body);
+
+ String url = tempWriter.toString();
+ if (log.isDebugEnabled()) {
+ log.debug("original URL: " + url);
+ }
+
+ try {
+ if (url != null && url.contains(DOCID_PREFIX)) {
+ final String documentId = getDocumentId(url);
+ final String hfTitle = hfURLManager.getHfURLTitle(documentId);
+ url = url.replaceFirst(DOCID_PREFIX + documentId, hfURLManager
+ .getHfULRPrefix()
+ + "/" + hfTitle);
+ if (log.isDebugEnabled()) {
+ log.debug("Founded title: " + hfTitle + ", for documentID: "
+ + documentId);
+ log.debug("Transformed URL: " + url);
+ }
+ }
+ } catch (EmptyResultDataAccessException e) {
+ log.debug("HF URL not defined. Returning original URL");
+ } catch (Exception e) {
+ log.error("Error occur while changing URL to Human friendly URL."
+ + " Returning original URL", e);
+ // something wrong - catch it and use original URL
+ }
+
+ try {
+ writer.write(url);
+ } catch (IOException e) {
+ throw new StrutsException("IOError: " + e.getMessage(), e);
+ }
+ return result;
+ }
+
+ /**
+ * Get Document ID from URL
+ *
+ * @param url
+ * URL in standard CS format i.e. /docs/DOC-1234
+ * @return document ID i.e. DOC-1234
+ */
+ protected static String getDocumentId(String url) {
+ final int questionMark = url.indexOf('?');
+ if (questionMark != -1) {
+ url = url.substring(0, questionMark);
+ }
+
+ final int docIDStart = url.indexOf(DOCID_PREFIX) + DOCID_PREFIX.length();
+ int docIDEnd = url.indexOf("/", docIDStart);
+ if (docIDEnd == -1) {
+ docIDEnd = url.length();
+ }
+
+ return url.substring(docIDStart, docIDEnd);
+ }
+
+ public void setHfURLManager(HFURLManager hfURLManager) {
+ this.hfURLManager = hfURLManager;
+ }
+
+}
Property changes on: labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/main/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponent.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/test/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponentTest.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/test/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponentTest.java (rev 0)
+++ labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/test/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponentTest.java 2009-03-19 15:19:34 UTC (rev 25739)
@@ -0,0 +1,46 @@
+/*
+ * JBoss.org http://jboss.org/
+ *
+ * Copyright (c) 2009 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Libor Krzyzanek
+ */
+package org.jboss.labs.clearspace.plugin.hfurl.struts.components;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+/**
+ * Test of HF URL Component
+ *
+ * @author <a href="mailto:lkrzyzan at redhat.com">Libor Krzyzanek</a>
+ *
+ */
+public class HFURLComponentTest {
+
+ @Test
+ public void testGetDocumentId() {
+ assertEquals("DOC-1234", HFURLComponent.getDocumentId("/community/docs/DOC-1234"));
+ assertEquals("DOC-10350", HFURLComponent.getDocumentId("/community/docs/DOC-10350"));
+ assertEquals("DOC-1234", HFURLComponent.getDocumentId("/community/docs/DOC-1234/edit"));
+
+ assertEquals("DOC-1234", HFURLComponent.getDocumentId("/community/docs/DOC-1234?param=1"));
+ assertEquals("DOC-1234", HFURLComponent.getDocumentId("/community/docs/DOC-1234/edit?param=1"));
+
+ }
+
+}
Property changes on: labs/jbosslabs/labs-3.0-build/integration/cs-hfurl/src/test/java/org/jboss/labs/clearspace/plugin/hfurl/struts/components/HFURLComponentTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the jboss-svn-commits
mailing list