private void customizeCompression(Connector connector) { ProtocolHandler handler = connector.getProtocolHandler(); if (handler instanceof AbstractHttp11Protocol) { AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler; Compression compression = getCompression(); protocol.setCompression("on"); protocol.setCompressionMinSize(compression.getMinResponseSize()); protocol.setCompressableMimeType( StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes())); if (getCompression().getExcludedUserAgents() != null) { protocol.setNoCompressionUserAgents( StringUtils.arrayToCommaDelimitedString( getCompression().getExcludedUserAgents())); } } }
@Override public HandlerWrapper createGzipHandler(Compression compression) { try { Class<?> handlerClass = ClassUtils.forName(GZIP_HANDLER_JETTY_8, getClass().getClassLoader()); HandlerWrapper handler = (HandlerWrapper) handlerClass.newInstance(); ReflectionUtils.findMethod(handlerClass, "setMinGzipSize", int.class) .invoke(handler, compression.getMinResponseSize()); ReflectionUtils.findMethod(handlerClass, "setMimeTypes", Set.class) .invoke(handler, new HashSet<String>( Arrays.asList(compression.getMimeTypes()))); if (compression.getExcludedUserAgents() != null) { ReflectionUtils.findMethod(handlerClass, "setExcluded", Set.class) .invoke(handler, new HashSet<String>( Arrays.asList(compression.getExcludedUserAgents()))); } return handler; } catch (Exception ex) { throw new RuntimeException("Failed to configure Jetty 8 gzip handler", ex); } }
@Override public HandlerWrapper createGzipHandler(Compression compression) { try { Class<?> handlerClass = ClassUtils.forName(GZIP_HANDLER_JETTY_9_2, getClass().getClassLoader()); HandlerWrapper gzipHandler = (HandlerWrapper) handlerClass.newInstance(); ReflectionUtils.findMethod(handlerClass, "setMinGzipSize", int.class) .invoke(gzipHandler, compression.getMinResponseSize()); ReflectionUtils .findMethod(handlerClass, "addIncludedMimeTypes", String[].class) .invoke(gzipHandler, new Object[] { compression.getMimeTypes() }); if (compression.getExcludedUserAgents() != null) { ReflectionUtils.findMethod(handlerClass, "setExcluded", Set.class) .invoke(gzipHandler, new HashSet<String>( Arrays.asList(compression.getExcludedUserAgents()))); } return gzipHandler; } catch (Exception ex) { throw new RuntimeException("Failed to configure Jetty 9.2 gzip handler", ex); } }
private void customizeCompression(Connector connector) { ProtocolHandler handler = connector.getProtocolHandler(); if (handler instanceof AbstractHttp11Protocol) { AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler; Compression compression = getCompression(); protocol.setCompression("on"); protocol.setCompressionMinSize(compression.getMinResponseSize()); protocol.setCompressableMimeTypes( StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes())); if (getCompression().getExcludedUserAgents() != null) { protocol.setNoCompressionUserAgents( StringUtils.arrayToCommaDelimitedString( getCompression().getExcludedUserAgents())); } } }
@Override public HandlerWrapper createGzipHandler(Compression compression) { try { Class<?> handlerClass = ClassUtils.forName(GZIP_HANDLER_JETTY_9_3, getClass().getClassLoader()); HandlerWrapper handler = (HandlerWrapper) handlerClass.newInstance(); ReflectionUtils.findMethod(handlerClass, "setMinGzipSize", int.class) .invoke(handler, compression.getMinResponseSize()); ReflectionUtils .findMethod(handlerClass, "setIncludedMimeTypes", String[].class) .invoke(handler, new Object[] { compression.getMimeTypes() }); if (compression.getExcludedUserAgents() != null) { ReflectionUtils .findMethod(handlerClass, "setExcludedAgentPatterns", String[].class) .invoke(handler, new Object[] { compression.getExcludedUserAgents() }); } return handler; } catch (Exception ex) { throw new RuntimeException("Failed to configure Jetty 9.3 gzip handler", ex); } }
@Override public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) { configurableEmbeddedServletContainer.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error404")); configurableEmbeddedServletContainer.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/error403")); Compression compression = new Compression(); compression.setEnabled(true); compression.setMimeTypes(mimeTypes); configurableEmbeddedServletContainer.setCompression(compression); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param useForwardHeaders if x-forward headers should be used * @param autoStart if the server should be started * @param compression compression configuration * @param serverHeader string to be used in HTTP header */ public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, boolean useForwardHeaders, boolean autoStart, Compression compression, String serverHeader) { this.builder = builder; this.manager = manager; this.contextPath = contextPath; this.useForwardHeaders = useForwardHeaders; this.autoStart = autoStart; this.compression = compression; this.serverHeader = serverHeader; }
private Predicate[] getCompressionPredicates(Compression compression) { List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(new MaxSizePredicate(compression.getMinResponseSize())); predicates.add(new CompressibleMimeTypePredicate(compression.getMimeTypes())); if (compression.getExcludedUserAgents() != null) { for (String agent : compression.getExcludedUserAgents()) { RequestHeaderAttribute agentHeader = new RequestHeaderAttribute( new HttpString(HttpHeaders.USER_AGENT)); predicates.add(Predicates.not(Predicates.regex(agentHeader, agent))); } } return predicates.toArray(new Predicate[predicates.size()]); }
@Override public HandlerWrapper createGzipHandler(Compression compression) { GzipHandler handler = new GzipHandler(); handler.setMinGzipSize(compression.getMinResponseSize()); handler.setIncludedMimeTypes(compression.getMimeTypes()); if (compression.getExcludedUserAgents() != null) { handler.setExcludedAgentPatterns(compression.getExcludedUserAgents()); } return handler; }
@Override @SuppressWarnings("serial") // Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646 protected String setUpFactoryForCompression(final int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception { char[] chars = new char[contentSize]; Arrays.fill(chars, 'F'); final String testContent = new String(chars); AbstractEmbeddedServletContainerFactory factory = getFactory(); Compression compression = new Compression(); compression.setEnabled(true); if (mimeTypes != null) { compression.setMimeTypes(mimeTypes); } if (excludedUserAgents != null) { compression.setExcludedUserAgents(excludedUserAgents); } factory.setCompression(compression); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentLength(contentSize); resp.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain"); resp.getWriter().print(testContent); } }, "/test.txt")); this.container.start(); return testContent; }
@Override public HandlerWrapper createGzipHandler(Compression compression) { GzipHandler gzipHandler = new GzipHandler(); gzipHandler.setMinGzipSize(compression.getMinResponseSize()); gzipHandler.addIncludedMimeTypes(compression.getMimeTypes()); if (compression.getExcludedUserAgents() != null) { gzipHandler.setExcluded(new HashSet<String>( Arrays.asList(compression.getExcludedUserAgents()))); } return gzipHandler; }
public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean useForwardHeaders, boolean autoStart, Compression compression) { this.builder = builder; this.manager = manager; this.contextPath = contextPath; this.useForwardHeaders = useForwardHeaders; this.autoStart = autoStart; this.compression = compression; }
private Predicate[] getCompressionPredicates(Compression compression) { List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(Predicates.maxContentSize(compression.getMinResponseSize())); predicates.add(new CompressibleMimeTypePredicate(compression.getMimeTypes())); if (compression.getExcludedUserAgents() != null) { for (String agent : compression.getExcludedUserAgents()) { RequestHeaderAttribute agentHeader = new RequestHeaderAttribute( new HttpString(HttpHeaders.USER_AGENT)); predicates.add(Predicates.not(Predicates.regex(agentHeader, agent))); } } return predicates.toArray(new Predicate[predicates.size()]); }
@Override public HandlerWrapper createGzipHandler(Compression compression) { GzipHandler gzipHandler = new GzipHandler(); gzipHandler.setMinGzipSize(compression.getMinResponseSize()); gzipHandler.setMimeTypes( new HashSet<String>(Arrays.asList(compression.getMimeTypes()))); if (compression.getExcludedUserAgents() != null) { gzipHandler.setExcluded(new HashSet<String>( Arrays.asList(compression.getExcludedUserAgents()))); } return gzipHandler; }
public Compression getCompression() { return this.compression; }
public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean autoStart, Compression compression) { this(builder, manager, contextPath, port, false, autoStart, compression); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param port the port to listen on (not used) * @param useForwardHeaders if x-forward headers should be used * @param autoStart if the server should be started * @param compression compression configuration * @deprecated as of 1.4 in favor of * {@link #UndertowEmbeddedServletContainer(Undertow.Builder, DeploymentManager, String, boolean, boolean, Compression)} */ @Deprecated public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean useForwardHeaders, boolean autoStart, Compression compression) { this(builder, manager, contextPath, useForwardHeaders, autoStart, compression); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param port the port to listen on (not used) * @param useForwardHeaders if x-forward headers should be used * @param autoStart if the server should be started * @param compression compression configuration * @param serverHeader string to be used in HTTP header * @deprecated as of 1.4 in favor of * {@link #UndertowEmbeddedServletContainer(Undertow.Builder, DeploymentManager, String, boolean, boolean, Compression, String)} */ @Deprecated public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean useForwardHeaders, boolean autoStart, Compression compression, String serverHeader) { this(builder, manager, contextPath, useForwardHeaders, autoStart, compression, serverHeader); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param useForwardHeaders if x-forward headers should be used * @param autoStart if the server should be started * @param compression compression configuration */ public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, boolean useForwardHeaders, boolean autoStart, Compression compression) { this(builder, manager, contextPath, useForwardHeaders, autoStart, compression, null); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param port the port to listen on (not used) * @param useForwardHeaders if x-forward headers should be used * @param autoStart if the server should be started * @param compression compression configuration * @deprecated as of 1.4 in favor of * {@link #UndertowEmbeddedServletContainer(Builder, DeploymentManager, String, boolean, boolean, Compression)} */ @Deprecated public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean useForwardHeaders, boolean autoStart, Compression compression) { this(builder, manager, contextPath, useForwardHeaders, autoStart, compression); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param port the port to listen on (not used) * @param useForwardHeaders if x-forward headers should be used * @param autoStart if the server should be started * @param compression compression configuration * @param serverHeader string to be used in HTTP header * @deprecated as of 1.4 in favor of * {@link #UndertowEmbeddedServletContainer(Builder, DeploymentManager, String, boolean, boolean, Compression, String)} */ @Deprecated public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean useForwardHeaders, boolean autoStart, Compression compression, String serverHeader) { this(builder, manager, contextPath, useForwardHeaders, autoStart, compression, serverHeader); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param port the port to listen on (not used) * @param autoStart if the server should be started * @param compression compression configuration * @deprecated as of 1.4 in favor of * {@link #UndertowEmbeddedServletContainer(Undertow.Builder, DeploymentManager, String, boolean, Compression)} */ @Deprecated public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean autoStart, Compression compression) { this(builder, manager, contextPath, false, autoStart, compression); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param autoStart if the server should be started * @param compression compression configuration */ public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, boolean autoStart, Compression compression) { this(builder, manager, contextPath, false, autoStart, compression); }
/** * Create a new {@link UndertowEmbeddedServletContainer} instance. * @param builder the builder * @param manager the deployment manager * @param contextPath the root context path * @param port the port to listen on (not used) * @param autoStart if the server should be started * @param compression compression configuration * @deprecated as of 1.4 in favor of * {@link #UndertowEmbeddedServletContainer(Builder, DeploymentManager, String, boolean, Compression)} */ @Deprecated public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean autoStart, Compression compression) { this(builder, manager, contextPath, false, autoStart, compression); }
HandlerWrapper createGzipHandler(Compression compression);