You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2011/04/22 11:42:21 UTC
svn commit: r1095901 - in
/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler: ./
src/main/java/org/apache/myfaces/commons/resourcehandler/
src/main/java/org/apache/myfaces/commons/resourcehandler/config/
src/test/java/org/apache/myfaces/c...
Author: jakobk
Date: Fri Apr 22 09:42:20 2011
New Revision: 1095901
URL: http://svn.apache.org/viewvc?rev=1095901&view=rev
Log:
MFCOMMONS-30 Change URL management of Advanced JSF 2 ResourceHandler (+ some tests)
Added:
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandlerTest.java
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/library/
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/library/resource.css
Modified:
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/pom.xml
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResource.java
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandler.java
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ResourceUtils.java
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfig.java
myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfigParser.java
Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/pom.xml?rev=1095901&r1=1095900&r2=1095901&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/pom.xml (original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/pom.xml Fri Apr 22 09:42:20 2011
@@ -69,6 +69,20 @@
<dependency>
<groupId>org.apache.myfaces.test</groupId>
<artifactId>myfaces-test20</artifactId>
+ <version>1.0.2</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- for some reason needed by myfaces test -->
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.el</groupId>
+ <artifactId>el-api</artifactId>
+ <version>1.0</version>
<scope>test</scope>
</dependency>
Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResource.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResource.java?rev=1095901&r1=1095900&r2=1095901&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResource.java (original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResource.java Fri Apr 22 09:42:20 2011
@@ -62,16 +62,6 @@ public class AdvancedResource extends Re
private static final String COMPRESSED_FILE_SUFFIX = ".gzip";
/**
- * URL path separator between locale, library name and resource name.
- */
- public static final String PATH_SEPARATOR = "/$/";
-
- /**
- * Regex of PATH_SEPARATOR for String.split();
- */
- public static final String PATH_SEPARATOR_REGEX = "/\\$/";
-
- /**
* Size of the byte array buffer.
*/
private static final int BUFFER_SIZE = 2048;
@@ -123,12 +113,14 @@ public class AdvancedResource extends Re
if (!resourceExists())
{
// do not use the locale prefix
+ // NOTE that for the request path ResourceUtils.getRequestLocalePrefix() will be used
this.requestedLocalePrefix = null;
}
}
else
{
// do not use the locale prefix
+ // NOTE that for the request path ResourceUtils.getRequestLocalePrefix() will be used
this.requestedLocalePrefix = null;
}
}
@@ -150,6 +142,20 @@ public class AdvancedResource extends Re
}
}
+ @Override
+ public void setLibraryName(String libraryName)
+ {
+ // trim slashes
+ super.setLibraryName(ResourceUtils.trimSlashes(libraryName));
+ }
+
+ @Override
+ public void setResourceName(String resourceName)
+ {
+ // trim slashes
+ super.setResourceName(ResourceUtils.trimSlashes(resourceName));
+ }
+
/**
* Returns true if this resource really exists.
*
@@ -205,15 +211,10 @@ public class AdvancedResource extends Re
path.append("/");
// calculate current localePrefix (could be different from the one requested, e.g. on locale change)
- String requestLocalePrefix = ResourceUtils.getRequestLocalePrefix();
- if (requestLocalePrefix != null)
- {
- path.append(requestLocalePrefix);
- path.append(PATH_SEPARATOR);
- }
-
+ path.append(ResourceUtils.getRequestLocalePrefix());
+ path.append("/");
path.append(getLibraryName());
- path.append(PATH_SEPARATOR);
+ path.append("/");
path.append(getResourceName());
return facesContext.getApplication().getViewHandler().getResourceURL(facesContext, path.toString());
Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandler.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandler.java?rev=1095901&r1=1095900&r2=1095901&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandler.java (original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandler.java Fri Apr 22 09:42:20 2011
@@ -19,6 +19,7 @@
package org.apache.myfaces.commons.resourcehandler;
import org.apache.myfaces.commons.resourcehandler.config.AdvancedResourceHandlerConfig;
+import org.apache.myfaces.commons.resourcehandler.config.AdvancedResourceHandlerConfigParser;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
@@ -87,6 +88,10 @@ public class AdvancedResourceHandler ext
{
this.wrappedHandler = wrappedHandler;
this.config = new AdvancedResourceHandlerConfig();
+
+ // parse the config
+ AdvancedResourceHandlerConfigParser configParser = new AdvancedResourceHandlerConfigParser();
+ configParser.parse(config);
}
@Override
@@ -115,23 +120,29 @@ public class AdvancedResourceHandler ext
// check if libraryName is null and the current request is a resource request
// in this case we need to check if this resource request should be handled by us
- // --> valid resource url: de/$_$/library/Name/$_$/resource/Name
if (libraryName == null && facesContext.getApplication().getResourceHandler().isResourceRequest(facesContext))
{
- // try to extract the libraryName and the locale from the resourceName
- String[] urlParts = resourceName.split(AdvancedResource.PATH_SEPARATOR_REGEX);
- if (urlParts.length == 3)
- {
- // locale + libraryName + resourceName
- requestedLocalePrefix = urlParts[0];
- libraryName = urlParts[1];
- resourceName = urlParts[2];
- }
- else if(urlParts.length == 2)
+ // extract the libraryName and the locale from the resourceName
+ // --> valid resource url: de/library/Name/resourceName.css
+
+ // trim any slashes at begin or end
+ resourceName = ResourceUtils.trimSlashes(resourceName);
+
+ // locale (chars before the first '/' in resourceName)
+ final int firstSlash = resourceName.indexOf('/');
+ if (firstSlash != -1)
{
- // libraryName + resourceName
- libraryName = urlParts[0];
- resourceName = urlParts[1];
+ requestedLocalePrefix = resourceName.substring(0, firstSlash);
+
+ // libraryName (everything between the first and the last slash)
+ final int lastSlash = resourceName.lastIndexOf('/');
+ if (firstSlash != lastSlash) {
+ // we have a libraryName - get it!
+ libraryName = resourceName.substring(firstSlash + 1, lastSlash);
+ }
+
+ // real resourceName (everything after the last slash)
+ resourceName = resourceName.substring(lastSlash + 1);
}
}
@@ -148,7 +159,7 @@ public class AdvancedResourceHandler ext
else
{
// the given resource should be handled by this ResourceHandler (otherwise there would not be
- // a match in resourcePackageMapping), but it does not exist.
+ // a match in config.isAdvancedLibrary(libraryName)), but it does not exist.
return null;
}
}
@@ -159,4 +170,13 @@ public class AdvancedResourceHandler ext
}
}
+ AdvancedResourceHandlerConfig getConfig()
+ {
+ return config;
+ }
+
+ void setConfig(AdvancedResourceHandlerConfig config)
+ {
+ this.config = config;
+ }
}
Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ResourceUtils.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ResourceUtils.java?rev=1095901&r1=1095900&r2=1095901&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ResourceUtils.java (original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ResourceUtils.java Fri Apr 22 09:42:20 2011
@@ -186,25 +186,25 @@ public class ResourceUtils
{
FacesContext facesContext = FacesContext.getCurrentInstance();
Locale locale = facesContext.getViewRoot().getLocale();
-
- if (locale != null)
+ if (locale == null)
{
- String language = locale.getLanguage();
- String country = locale.getCountry();
-
- if (country != null)
- {
- // de_AT
- return language + "_" + country;
- }
- else
- {
- // de
- return language;
- }
+ // fallback to default locale
+ locale = Locale.getDefault();
+ }
+
+ String language = locale.getLanguage();
+ String country = locale.getCountry();
+
+ if (country != null)
+ {
+ // de_AT
+ return language + "_" + country;
+ }
+ else
+ {
+ // de
+ return language;
}
-
- return null;
}
/**
@@ -399,6 +399,44 @@ public class ResourceUtils
}
/**
+ * Removes leading and trailing slashes (= '/') from the given String.
+ *
+ * @param s
+ * @return
+ */
+ public static String trimSlashes(String s)
+ {
+ if (s != null)
+ {
+ // remove at begin
+ int index = 0;
+ int startIndex = index;
+ while (s.charAt(index) == '/')
+ {
+ index++;
+ }
+ if (index != startIndex)
+ {
+ s = s.substring(index);
+ }
+
+ // remove at end
+ index = s.length() - 1;
+ startIndex = index;
+ while (s.charAt(index) == '/')
+ {
+ index--;
+ }
+ if (index != startIndex)
+ {
+ s = s.substring(0, index + 1);
+ }
+ }
+
+ return s;
+ }
+
+ /**
* Checks if the user agent supports GZIP compression on basis of the "Accept-Encoding" header field.
* Created according to RFC2616, section 14.3 Accept-Encoding.
*
Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfig.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfig.java?rev=1095901&r1=1095900&r2=1095901&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfig.java (original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfig.java Fri Apr 22 09:42:20 2011
@@ -18,6 +18,8 @@
*/
package org.apache.myfaces.commons.resourcehandler.config;
+import org.apache.myfaces.commons.resourcehandler.ResourceUtils;
+
import java.util.Collection;
import java.util.HashSet;
@@ -36,10 +38,6 @@ public class AdvancedResourceHandlerConf
{
// use HashSet to perform contains() in constant time!!
libraries = new HashSet<String>();
-
- // parse config files to add content
- AdvancedResourceHandlerConfigParser parser = new AdvancedResourceHandlerConfigParser();
- parser.parse(this);
}
/**
@@ -48,7 +46,7 @@ public class AdvancedResourceHandlerConf
*
* @param library
*/
- void addLibrary(String library)
+ public void addLibrary(String library)
{
libraries.add(library);
}
@@ -61,7 +59,7 @@ public class AdvancedResourceHandlerConf
*/
public boolean isAdvancedLibrary(String library)
{
- return libraries.contains(library);
+ return libraries.contains(ResourceUtils.trimSlashes(library));
}
}
Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfigParser.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfigParser.java?rev=1095901&r1=1095900&r2=1095901&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfigParser.java (original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/config/AdvancedResourceHandlerConfigParser.java Fri Apr 22 09:42:20 2011
@@ -31,11 +31,11 @@ import java.net.URL;
import java.util.Enumeration;
/**
- * XML-parser for AdvancedResourceHandlerConfig.
+ * XML-parser for AdvancedResourceHandlerConfig.
*
* @author Jakob Korherr
*/
-class AdvancedResourceHandlerConfigParser
+public class AdvancedResourceHandlerConfigParser
{
public void parse(AdvancedResourceHandlerConfig config)
Added: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandlerTest.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandlerTest.java?rev=1095901&view=auto
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandlerTest.java (added)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/java/org/apache/myfaces/commons/resourcehandler/AdvancedResourceHandlerTest.java Fri Apr 22 09:42:20 2011
@@ -0,0 +1,115 @@
+/*
+ * 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.myfaces.commons.resourcehandler;
+
+import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
+import org.apache.myfaces.test.mock.resource.MockResourceHandler;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import javax.faces.application.ProjectStage;
+import javax.faces.application.Resource;
+
+/**
+ * Test class for AdvancedResourceHandler.
+ *
+ * @author Jakob Korherr
+ */
+@RunWith(JUnit4.class)
+public class AdvancedResourceHandlerTest extends AbstractJsfTestCase
+{
+
+ private AdvancedResourceHandler advancedResourceHandler;
+ private MockResourceHandler mockResourceHandler;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ // ProjectStage = Development --> disables GZIP compression
+ servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, ProjectStage.Development.name());
+
+ mockResourceHandler = (MockResourceHandler) application.getResourceHandler();
+ advancedResourceHandler = new AdvancedResourceHandler(mockResourceHandler);
+ }
+
+ @After
+ public void tearDown() throws Exception
+ {
+ mockResourceHandler = null;
+ advancedResourceHandler = null;
+
+ super.tearDown();
+ }
+
+ @Test
+ public void testCreateResourceInResourceRequestWithLibraryName_validResource() throws Exception
+ {
+ // we are in a resource request
+ mockResourceHandler.setResourceRequest(true);
+
+ // add test library as advanced library
+ advancedResourceHandler.getConfig().addLibrary("my/library");
+
+ // create the resource just like handleResourceRequest() would do
+ Resource resource = advancedResourceHandler.createResource("/de/my/library/resource.css");
+
+ Assert.assertNotNull(resource);
+ Assert.assertEquals("my/library", resource.getLibraryName());
+ Assert.assertEquals("resource.css", resource.getResourceName());
+ }
+
+ @Test
+ public void testCreateResourceInResourceRequestNoLibraryName_nullResource() throws Exception
+ {
+ // we are in a resource request
+ mockResourceHandler.setResourceRequest(true);
+
+ // create the resource just like handleResourceRequest() would do
+ Resource resource = advancedResourceHandler.createResource("/de/resource.css");
+
+ // resource must be null, b/c we can't map a libraryName that is null (there can't be a config entry for it)
+ Assert.assertNull(resource);
+ }
+
+ @Test
+ public void testCreateResource_trimSlashes() throws Exception
+ {
+ // this is not the resource request --> no special handling of resourceName
+ mockResourceHandler.setResourceRequest(false);
+
+ // add test library as advanced library
+ advancedResourceHandler.getConfig().addLibrary("my/library");
+
+ // create the resource just like handleResourceRequest() would do
+ Resource resource = advancedResourceHandler.createResource("/resource.css", "/my/library/");
+
+ // resource must exist, leading and traling slashes in resourceName and libraryName must be removed
+ Assert.assertNotNull(resource);
+ Assert.assertEquals("resource.css", resource.getResourceName());
+ Assert.assertEquals("my/library", resource.getLibraryName());
+
+ }
+
+}
Added: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/library/resource.css
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/library/resource.css?rev=1095901&view=auto
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/library/resource.css (added)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/test/resources/META-INF/resources/my/library/resource.css Fri Apr 22 09:42:20 2011
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/* some css rules here */
\ No newline at end of file