Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method correction: String guestServletPath(AtmosphereFramework, String) #1514

Merged
merged 1 commit into from
Mar 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}

}