Skip to content

Commit

Permalink
Merge pull request #1514 from rdigiorgio/master
Browse files Browse the repository at this point in the history
Method correction: String guestServletPath(AtmosphereFramework, String)
  • Loading branch information
jfarcand committed Mar 18, 2014
2 parents b44c3a2 + b7f6676 commit c25518a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
29 changes: 28 additions & 1 deletion modules/cpr/src/main/java/org/atmosphere/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IOUtils {
private final static Logger logger = LoggerFactory.getLogger(IOUtils.class);
private final static List<String> knownClasses;
private final static Pattern SERVLET_PATH_PATTERN = Pattern.compile("([\\/]?[\\w-[.]]+|[\\/]\\*\\*)+");

static {
knownClasses = new ArrayList<String>() {
Expand Down Expand Up @@ -109,7 +112,9 @@ public static String guestServletPath(AtmosphereFramework framework, String excl
Class<?> classToScan = loadClass(framework.getClass(), e.getValue().getClassName());

if (scanForAtmosphereFramework(classToScan)) {
servletPath = "/" + e.getValue().getMappings().iterator().next().replace("/", "").replace("*", "");
servletPath = e.getValue().getMappings().iterator().next();
servletPath = getCleanedServletPath(servletPath);

// We already found one.
if (!servletPath.equalsIgnoreCase(exclude)) {
break;
Expand All @@ -128,6 +133,28 @@ public static String guestServletPath(AtmosphereFramework framework) {
return guestServletPath(framework, "");
}

/**
* Used to remove trailing slash and wildcard from a servlet path.<br/><br/>
* Examples :<br/>
* - "/foo/" becomes "/foo"<br/>
* - "foo/bar" becomes "/foo/bar"<br/>
* @param fullServletPath : Servlet mapping
* @return Servlet mapping without trailing slash and wildcard
*/
public static String getCleanedServletPath(String fullServletPath) {
Matcher matcher = SERVLET_PATH_PATTERN.matcher(fullServletPath);

// It should not happen if the servlet path is valid
if (!matcher.find()) return fullServletPath;

String servletPath = matcher.group(0);
if (!servletPath.startsWith("/")) {
servletPath = "/" + servletPath;
}

return servletPath;
}

private static boolean scanForAtmosphereFramework(Class<?> classToScan) {
if (classToScan == null) return false;

Expand Down
30 changes: 30 additions & 0 deletions modules/cpr/src/test/java/org/atmosphere/util/IOUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.atmosphere.util;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

/**
* Created by Romain on 17/03/14.
*/
public class IOUtilsTest {

@Test
public void testGetCleanedServletPath() {
String testFullPath;
String testCleanedPath;

testFullPath = "/foo/bar/*";
testCleanedPath = IOUtils.getCleanedServletPath(testFullPath);
assertEquals(testCleanedPath, "/foo/bar");

testFullPath = "foo/bar/**/*";
testCleanedPath = IOUtils.getCleanedServletPath(testFullPath);
assertEquals(testCleanedPath, "/foo/bar/**");

testFullPath = "/com.zyxabc.abc.Abc/gwtCometEvent*";
testCleanedPath = IOUtils.getCleanedServletPath(testFullPath);
assertEquals(testCleanedPath, "/com.zyxabc.abc.Abc/gwtCometEvent");
}

}

0 comments on commit c25518a

Please sign in to comment.