此代码中有错误:

public String getVersion() {
    String result = null;
    int index = this.getRequest().getProtocol().indexOf(47);
    if (index != -1) {
        result = this.getRequest().getProtocol().substring(index + 1);
    }

    return result;
}

错误是“找不到必需的”主机“http头。”:
10:34:29,889 INFO  [org.restlet] (Thread-6 (HornetQ-client-global-threads-1773619305)) Couldn't find the mandatory "Host" HTTP header.
10:34:29,910 WARNING [org.restlet.Component.Server] (Thread-6 (HornetQ-client-global-threads-1773619305)) Error while handling an HTTP server call: java.lang.NullPointerException
    at org.restlet.ext.servlet.internal.ServletCall.getVersion(ServletCall.java:370) [org.restlet.ext.servlet-2.4.0.jar:]
    at org.restlet.engine.adapter.ServerAdapter.toRequest(ServerAdapter.java:229) [org.restlet-2.4.0.jar:]
    at org.restlet.ext.servlet.internal.ServletServerAdapter.toRequest(ServletServerAdapter.java:62) [org.restlet.ext.servlet-2.4.0.jar:]
    at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:141) [org.restlet-2.4.0.jar:]
    at org.restlet.ext.servlet.ServerServlet.service(ServerServlet.java:1117) [org.restlet.ext.servlet-2.4.0.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]

触发此操作的代码是基本的:
Queue queue = QueueFactory.getQueue(milkID + "-queue");
String taskUrl = "/milks/" + milkID;
queue.add(TaskOptions.Builder.withUrl(taskUrl)
    .param("param1", param1));

从这个队列发出的调用似乎丢失了一些东西,所以restlet被抛出。奇怪的是,同样的代码在gae cloud(appspot)中工作,甚至在appscale上,只有capedwarf抛出了这个错误。怎么了?

最佳答案

我做了一项工作:

@Override
public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String host = req.getHeader("Host");
        String _protocol = req.getProtocol(); // e.g. HTTP/1.1
        if(host == null || host.isEmpty() || _protocol == null || _protocol.isEmpty()) {
            host = "127.0.0.1:8080";
            HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req, "HTTP/1.1");
            requestWrapper.addHeader("Host", host);
            System.out.println("====================================================");
            System.out.println("Updating Host: " + host);
            System.out.println("====================================================");
            chain.doFilter(requestWrapper, response);
        } else {
            chain.doFilter(req, response);
        }

    }

但它并没有减少错误,反而破坏了capedwarf运行时的混乱:
00:52:14,692 INFO  [org.hornetq.ra] (ServerService Thread Pool -- 62) HQ151003: HornetQ resource adaptor stopped
00:52:14,663 ERROR [io.undertow.request] (Thread-0 (HornetQ-client-global-threads-710971154)) UT005023: Exception handling request to null: javax.servlet.ServletException: UT010051: Deployment ROOT.war has stopped
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:55) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at com.mycompany.HostFilter.doFilter(HostFilter.java:48) [classes:]
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.jboss.capedwarf.appidentity.GAEFilter.doFilter(GAEFilter.java:62) [capedwarf-appidentity-2.0.0-SNAPSHOT.jar:2.0.0-SNAPSHOT]
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:248) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchMockRequest(ServletInitialHandler.java:235) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.spec.RequestDispatcherImpl.mock(RequestDispatcherImpl.java:425) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.jboss.as.capedwarf.services.Hack.invoke(Hack.java:50)
    at org.jboss.as.capedwarf.services.ServletExecutor.dispatch(ServletExecutor.java:82)
    at org.jboss.as.capedwarf.services.ServletExecutorConsumer.onMessage(ServletExecutorConsumer.java:126)
    at org.hornetq.jms.client.JMSMessageListenerWrapper.onMessage(JMSMessageListenerWrapper.java:103)
    at org.hornetq.core.client.impl.ClientConsumerImpl.callOnMessage(ClientConsumerImpl.java:1116)
    at org.hornetq.core.client.impl.ClientConsumerImpl.access$500(ClientConsumerImpl.java:56)
    at org.hornetq.core.client.impl.ClientConsumerImpl$Runner.run(ClientConsumerImpl.java:1251)
    at org.hornetq.utils.OrderedExecutorFactory$OrderedExecutor$1.run(OrderedExecutorFactory.java:104)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [rt.jar:1.8.0_163]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [rt.jar:1.8.0_163]
    at java.lang.Thread.run(Thread.java:748) [rt.jar:1.8.0_163]

最好的解决方案是使用gae sdk运行:
./dev_appserver.sh <path_to_app>

而且它工作得很好,没有问题,看起来capedwarf错过了传递servlet链中的重要内容。

10-08 00:23