Skip to content

Commit

Permalink
avoid servlet/filter postfix cut for short names during name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
xvik committed Aug 21, 2016
1 parent 21fb433 commit 115f46f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private WebUtils() {
* For example, for class "MyCoolFilter" generated name will be ".mycool".
*
* @param filter filter annotation
* @param type filter type
* @param type filter type
* @return filter name or generated name if name not provided
*/
public static String getFilterName(final WebFilter filter, final Class<? extends Filter> type) {
Expand All @@ -34,9 +34,8 @@ public static String getFilterName(final WebFilter filter, final Class<? extends
}

/**
*
* @param servlet servlet annotation
* @param type servlet type
* @param type servlet type
* @return servlet name or generated name if name not provided
*/
public static String getServletName(final WebServlet servlet, final Class<? extends HttpServlet> type) {
Expand Down Expand Up @@ -101,8 +100,10 @@ private static String getAsyncMarker(final boolean async) {

private static String generateName(final Class<?> type, final String keyword) {
String probe = '.' + type.getSimpleName().toLowerCase();
if (probe.endsWith(keyword)) {
probe = probe.substring(0, probe.length() - keyword.length());
final int targetLength = probe.length() - keyword.length();
// leave prefix if remaining part is too short (and if target name equal prefix)
if (probe.endsWith(keyword) && targetLength > 2) {
probe = probe.substring(0, targetLength);
}
return probe;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.vyarus.dropwizard.guice.web

import io.dropwizard.Application
import io.dropwizard.Configuration
import io.dropwizard.setup.Bootstrap
import io.dropwizard.setup.Environment
import org.eclipse.jetty.servlet.ServletHolder
import ru.vyarus.dropwizard.guice.GuiceBundle
import ru.vyarus.dropwizard.guice.test.spock.UseGuiceyApp
import spock.lang.Specification

import javax.inject.Inject
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet


/**
* @author Vyacheslav Rusakov
* @since 22.08.2016
*/
@UseGuiceyApp(GCollApp)
class ServletGenNameCollisionTest extends Specification {

@Inject
Environment environment

def "Check servlet name generation"() {

expect: "OServlet name without postfix cut"
ServletHolder oservlet = environment.getApplicationContext().getServletHandler().getServlet(".oservlet")
oservlet.registration.className == OServlet.name

and: "Servlet name without postfix cut"
ServletHolder servlet = environment.getApplicationContext().getServletHandler().getServlet(".servlet")
servlet.registration.className == Servlet.name
}

static class GCollApp extends Application<Configuration> {
@Override
void initialize(Bootstrap<Configuration> bootstrap) {
bootstrap.addBundle(GuiceBundle.builder()
.useWebInstallers()
.extensions(OServlet, Servlet)
.build())
}

@Override
void run(Configuration configuration, Environment environment) throws Exception {

}
}

@WebServlet("/foo")
static class OServlet extends HttpServlet {}

@WebServlet("/bar")
static class Servlet extends HttpServlet {}
}

0 comments on commit 115f46f

Please sign in to comment.