Google Guice on Google Appengine: mapping with wor

2020-03-25 05:51发布

I have a Google Appengine/Guice/Wicket Application. My problem is that due to the mapping I can't access the /_ah/admin Page anymore.

My Servlet Module says:

serve( "/*" ).with( WicketServlet.class, getWicketServletParams() );

so far it is more or less expected that accessing /_ah/admin gives a 404.

I problem is that I don't find a workaround.

I tried different combinations of serveRegex(), but even

serveRegex( "/.*" ).with( WicketServlet.class, getWicketServletParams() );

leads to problems, as the URL dispatching of Wicket gets broken. The application keeps on repeating the Path (e.g. /list becomes /list/list etc.).

Any ideas?

4条回答
We Are One
2楼-- · 2020-03-25 06:17

I know this is is a pretty old question with an accepted anser, however the solution is more straight forward than URL rewriting. In your Guice servlet module you can pass configuration parameters as follows:


Map wicketParams = new HashMap(3);
wicketParams.put(WicketFilter.IGNORE_PATHS_PARAM, "/_ah/*");
wicketParams.put(WicketFilter.FILTER_MAPPING_PARAM, ROOT_FILTER_MAPPING_URL);
filter("/*").through(GuiceWicketFilter.class, wicketParams );
查看更多
Luminary・发光体
3楼-- · 2020-03-25 06:23

I solved the same problem with Spring/GAE using the UrlRewriteFilter. Please take a look at the source here. I assume a similar solution could be used for your situation.

<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
    <rule>
        <from>^/appstats/(.*)$</from>
        <to last="true">/appstats/$1</to>
    </rule>
    <rule>
        <from>^/_ah/(.*)$</from>
        <to last="true">/_ah/$1</to>
    </rule>
    <rule>
        <from>^/(.*)$</from>
        <to last="true">/app/$1</to>
    </rule>

查看更多
仙女界的扛把子
4楼-- · 2020-03-25 06:26

I have found a more elegant solution :

Instead of using the GuiceFilter, subclass it to intercept _ah/* calls and let the servlet container do its regular job instead of letting Guice intercepting it. Here I prevent Guice from intercepting /_ah/* but /_ah/warmup because /_ah/warmup is supposed to be handled by the programmer.

package com.kya.guice.mvc;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.google.inject.servlet.GuiceFilter;

public class GaeSafeGuiceFilter extends GuiceFilter {

    private static final Pattern p = Pattern.compile("/_ah/.*");

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;

        if (p.matcher(req.getRequestURI()).matches() && !req.getRequestURI().equals("/_ah/warmup")) {
            chain.doFilter(request, response);
            return ;
        }

        super.doFilter(request, response, chain);
    }
}

You then just have to change your web.xml with :

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.kya.guice.mvc.GaeSafeGuiceFilter</filter-class>
</filter>

instead of com.google.inject.servlet.GuiceFilter

查看更多
beautiful°
5楼-- · 2020-03-25 06:35

Just for reference, here is a way to do it with serveRegex

serveRegex("/(?!_ah).*").with( WicketServlet.class, getWicketServletParams() );
查看更多
登录 后发表回答