我正在尝试在嵌入的restlet + jetty中启用压缩,但在我的测试应用程序中,压缩仅适用于servlet直接处理的请求,而不适用于restlet处理的请求。
我的测试代码:
package restlettest;
import java.io.IOException;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlets.GzipFilter;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.ext.servlet.ServerServlet;
import org.restlet.routing.Router;
public class RestletTestServerJettyEmbedded {
public static class RestletTestApplication extends Application {
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/sayHello", HelloWorldResource.class);
return router;
}
}
@SuppressWarnings("serial")
public static class JettyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("Hello from jetty!");
};
}
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
final ServletContextHandler serveltContext = new ServletContextHandler(
ServletContextHandler.SESSIONS);
serveltContext.setContextPath("/");
serveltContext.setInitParameter("org.restlet.application",
RestletTestApplication.class.getName());
serveltContext.addFilter(GzipFilter.class, "/*",
EnumSet.of(DispatcherType.REQUEST));
serveltContext.addServlet(ServerServlet.class, "/restlet/*");
serveltContext.addServlet(JettyServlet.class, "/jetty/*");
server.setHandler(serveltContext);
server.start();
server.join();
}
}
项目POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devmao.restlettest</groupId>
<artifactId>restlettest</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>8.1.7.v20120910</jetty.version>
<restlet.version>2.1.0</restlet.version>
</properties>
<dependencies>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- /Jetty -->
<!-- Restlet -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>${restlet.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.httpclient</artifactId>
<version>${restlet.version}</version>
<scope>compile</scope>
</dependency>
<!-- /Restlet -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<!-- Restlet Repositories -->
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
<!-- /Restlet Repositories -->
</repositories>
</project>
测试 servlet 请求:
$ curl -i -H 'Accept-Encoding: gzip,deflate' http://localhost:8080/jetty
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Encoding: gzip
Transfer-Encoding: chunked
Server: Jetty(8.1.7.v20120910)
测试 restlet 请求:
$ curl -i -H 'Accept-Encoding: gzip,deflate' http://localhost:8080/restlet/sayHello
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: text/plain;charset=UTF-8
Date: Tue, 06 Nov 2012 09:24:01 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.1.0
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 12