Add an utilities module this module will contain classes that simplify currents operation.
- URLInformation.java will help to retrieve url information from spring container. This aim to suppress spring url retrieving operation from main code
- the o.a.l.utils.test package will provide classes for set up the test environment
git-svn-id: https://svn.apache.org/repos/asf/lenya/trunk@1027493 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.lenya.core.utils/pom.xml b/org.apache.lenya.core.utils/pom.xml
new file mode 100644
index 0000000..df0e216
--- /dev/null
+++ b/org.apache.lenya.core.utils/pom.xml
@@ -0,0 +1,143 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.lenya</groupId>
+ <artifactId>lenya-parent</artifactId>
+ <version>2.2.0-SNAPSHOT</version>
+ <relativePath>../org.apache.lenya.parent/pom.xml</relativePath>
+ </parent>
+ <groupId>org.apache.lenya</groupId>
+ <artifactId>lenya-core-utils</artifactId>
+ <packaging>jar</packaging>
+ <name>Apache Lenya Utilities</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <!-- export our test classes in a separate jar -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <includes>
+ <include>**/*TestCase.class</include>
+ <include>**/*Test.class</include>
+ </includes>
+ <excludes>
+ <exclude>**/Abstract*.class</exclude>
+ </excludes>
+ <!-- currently triggers NPE in surefire plugin
+ <systemProperties>
+ <property>
+ <name>junit.test.loglevel</name>
+ <value>3</value>
+ </property>
+ </systemProperties>
+ -->
+ </configuration>
+ </plugin>
+ </plugins>
+ <!-- <resources> -->
+ <!-- <resource> -->
+ <!-- <directory>src/main/resources</directory> -->
+ <!-- <filtering>false</filtering> -->
+ <!-- <excludes> -->
+ <!-- <exclude>META-INF/cocoon/properties/lenya.properties</exclude> -->
+ <!-- </excludes> -->
+ <!-- </resource> -->
+ <!-- <resource> -->
+ <!-- <directory>src/main/resources</directory> -->
+ <!-- <filtering>true</filtering> -->
+ <!-- <includes> -->
+ <!-- <include>META-INF/cocoon/properties/lenya.properties</include> -->
+ <!-- </includes> -->
+ <!-- </resource> -->
+ <!-- </resources> -->
+ </build>
+
+ <properties>
+ <lenya.home>${basedir}/..</lenya.home>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.avalon.framework</groupId>
+ <artifactId>avalon-framework-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-flowscript-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-validator</groupId>
+ <artifactId>commons-validator</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-validation-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-serializers-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-chaperon-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-xsp-impl</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.lenya</groupId>
+ <artifactId>lenya-core-api</artifactId>
+ </dependency>
+ <!-- Tests -->
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-mock</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-core</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <!-- FIXME: This dependency is transitive and should be obtained automatically
+ see http://jira.codehaus.org/browse/MNG-1378 -->
+ <dependency>
+ <groupId>org.apache.cocoon</groupId>
+ <artifactId>cocoon-sitemap-impl</artifactId>
+ <scope>test</scope>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java b/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java
new file mode 100644
index 0000000..fe4a268
--- /dev/null
+++ b/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/URLInformation.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.lenya.utils;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.lenya.cms.publication.Publication;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+/**
+ * This class resolves all Lenya-specific information from a webapp URL.
+ */
+public class URLInformation {
+
+ private String publicationId = null;
+ private String area = null;
+ private String completeArea = null;
+ private String documentUrl = null;
+
+ private String url = null;
+
+ private String webappUrl = null;
+
+ public URLInformation(){
+ webappUrl = getCurrentURI();
+ setSourceURL(webappUrl);
+ }
+
+ private String getCurrentURI(){
+ String currentURI = "";
+ RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+ if (requestAttributes instanceof ServletRequestAttributes) {
+ HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
+ currentURI = request.getRequestURI(); // ==> /default/authoring/
+ }
+ return currentURI;
+ }
+
+ private void setSourceURL(String webappUrl){
+ if (!webappUrl.startsWith("/")) {
+ throw new RuntimeException("The URL [" + webappUrl + "] doesn't start with a slash!");
+ }
+ this.url = webappUrl.substring(1);
+ }
+
+
+ /**
+ * Return the webapp url (with a / at the beginning
+ */
+ public String getWebappUrl(){
+ return this.webappUrl;
+ }
+ /**
+ * Returns the publication ID.
+ * @return A string.
+ */
+ public String getPublicationId() {
+ if (this.publicationId == null) {
+ this.publicationId = extractBeforeSlash(this.url);
+ }
+ return this.publicationId;
+ }
+
+ /**
+ * Returns the area (without the "webdav" prefix).
+ * @return A string.
+ */
+ public String getArea() {
+ if (this.area == null) {
+ String completeArea = getCompleteArea();
+ if (Publication.DAV_AREA.equals(completeArea)) {
+ this.area = Publication.AUTHORING_AREA;
+ } else {
+ this.area = completeArea;
+ }
+ }
+ return this.area;
+ }
+
+ /**
+ * Returns the complete area (including the "webdav" prefix).
+ * @return A string.
+ */
+ public String getCompleteArea() {
+ String pubId = getPublicationId();
+ if (this.completeArea == null && pubId != null) {
+ String pubUrl = this.url.substring(pubId.length());
+ if (pubUrl.startsWith("/")) {
+ this.completeArea = extractBeforeSlash(pubUrl.substring(1));
+ } else {
+ this.completeArea = null;
+ }
+ }
+ return this.completeArea;
+ }
+
+ /**
+ * Returns the document URL.
+ * @return A string.
+ */
+ public String getDocumentUrl() {
+ if (this.documentUrl == null) {
+ String pubId = getPublicationId();
+ String area = getCompleteArea();
+ if (pubId != null && area != null) {
+ String prefix = pubId + "/" + area;
+ this.documentUrl = this.url.substring(prefix.length());
+ }
+ }
+ return this.documentUrl;
+ }
+
+ protected String extractBeforeSlash(String remaining) {
+
+ if (remaining.length() == 0) {
+ return null;
+ }
+
+ String step;
+ int slashIndex = remaining.indexOf('/');
+ if (slashIndex == -1) {
+ step = remaining;
+ } else {
+ step = remaining.substring(0, slashIndex);
+ }
+ return step;
+ }
+
+
+}
diff --git a/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/README b/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/README
new file mode 100644
index 0000000..5aa0727
--- /dev/null
+++ b/org.apache.lenya.core.utils/src/main/java/org/apache/lenya/utils/test/README
@@ -0,0 +1 @@
+This package will contain utilities classes for create a complete test environnement
\ No newline at end of file
diff --git a/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java b/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java
new file mode 100644
index 0000000..c265bc7
--- /dev/null
+++ b/org.apache.lenya.core.utils/src/test/java/org/apache/lenya/utils/URLInformationTest.java
@@ -0,0 +1,61 @@
+package org.apache.lenya.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpSession;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+public class URLInformationTest {
+
+ String testUrl = "/test/authoring/DocumentTest.html";
+
+ @Before
+ public void setMockURL(){
+ String mockURL = testUrl;
+ MockHttpServletRequest request = new MockHttpServletRequest("GET", mockURL);
+ MockHttpSession session = new MockHttpSession();
+ request.setSession(session);
+ RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
+ }
+
+ @Test
+ public void getWebappUrl(){
+ URLInformation urli = new URLInformation();
+ String pubId = urli.getWebappUrl();
+ assertEquals(pubId,testUrl);
+
+ }
+
+ @Test
+ public void getPublicationIdTest(){
+ URLInformation urli = new URLInformation();
+ String pubId = urli.getPublicationId();
+ assertEquals(pubId,"test");
+ }
+
+ @Test
+ public void getAreaTest(){
+ URLInformation urli = new URLInformation();
+ String area = urli.getArea();
+ assertEquals(area, "authoring");
+ }
+
+ @Test
+ public void getCompleteAreaTest(){
+ URLInformation urli = new URLInformation();
+ String area = urli.getCompleteArea();
+ assertEquals(area, "authoring");
+ }
+
+ @Test
+ public void getDocumentURL(){
+ URLInformation urli = new URLInformation();
+ String durl = urli.getDocumentUrl();
+ assertEquals(durl, "/DocumentTest.html");
+ }
+
+}