private org.glassfish.grizzly.http.HttpServerFilter createHttpServerCodecFilter(final Http http) {

    int maxRequestHeaders = MimeHeaders.MAX_NUM_HEADERS_DEFAULT;
    int maxResponseHeaders = MimeHeaders.MAX_NUM_HEADERS_DEFAULT;
    boolean isChunkedEnabled = true;
    int headerBufferLengthBytes =
        org.glassfish.grizzly.http.HttpServerFilter.DEFAULT_MAX_HTTP_PACKET_HEADER_SIZE;

    String defaultResponseType = null;

    if (http != null) {
      isChunkedEnabled = Boolean.parseBoolean(http.getChunkingEnabled());

      headerBufferLengthBytes = Integer.parseInt(http.getHeaderBufferLengthBytes());

      defaultResponseType = http.getDefaultResponseType();

      maxRequestHeaders = Integer.parseInt(http.getMaxRequestHeaders());

      maxResponseHeaders = Integer.parseInt(http.getMaxResponseHeaders());
    }

    return createHttpServerCodecFilter(
        http,
        isChunkedEnabled,
        headerBufferLengthBytes,
        defaultResponseType,
        configureKeepAlive(http),
        obtainDelayedExecutor(),
        maxRequestHeaders,
        maxResponseHeaders);
  }
 protected KeepAlive configureKeepAlive(final Http http) {
   int timeoutInSeconds = 60;
   int maxConnections = 256;
   if (http != null) {
     try {
       timeoutInSeconds = Integer.parseInt(http.getTimeoutSeconds());
     } catch (NumberFormatException ex) {
       //                String msg = _rb.getString("pewebcontainer.invalidKeepAliveTimeout");
       String msg = "pewebcontainer.invalidKeepAliveTimeout";
       msg =
           MessageFormat.format(msg, http.getTimeoutSeconds(), Integer.toString(timeoutInSeconds));
       LOGGER.log(Level.WARNING, msg, ex);
     }
     try {
       maxConnections = Integer.parseInt(http.getMaxConnections());
     } catch (NumberFormatException ex) {
       //                String msg =
       // _rb.getString("pewebcontainer.invalidKeepAliveMaxConnections");
       String msg = "pewebcontainer.invalidKeepAliveMaxConnections";
       msg = MessageFormat.format(msg, http.getMaxConnections(), Integer.toString(maxConnections));
       LOGGER.log(Level.WARNING, msg, ex);
     }
   }
   final KeepAlive keepAlive = new KeepAlive();
   keepAlive.setIdleTimeoutInSeconds(timeoutInSeconds);
   keepAlive.setMaxRequestsCount(maxConnections);
   return keepAlive;
 }
 protected void configureWebSocketSupport(
     final ServiceLocator habitat,
     final NetworkListener listener,
     final Http http,
     final FilterChainBuilder filterChainBuilder) {
   final boolean websocketsSupportEnabled =
       Boolean.parseBoolean(http.getWebsocketsSupportEnabled());
   if (websocketsSupportEnabled) {
     AddOn wsAddOn =
         loadAddOn(habitat, "websocket", "org.glassfish.grizzly.websockets.WebSocketAddOn");
     if (wsAddOn != null) {
       if (!configureElement(habitat, listener, http, wsAddOn)) {
         // Dealing with a WebSocketAddOn created by reflection vs
         // an HK2 service.  We need to pass the configuration data
         // manually via reflection.
         try {
           Method m = wsAddOn.getClass().getDeclaredMethod("setTimeoutInSeconds", Long.TYPE);
           m.invoke(wsAddOn, Long.parseLong(http.getWebsocketsTimeoutSeconds()));
         } catch (Exception e) {
           if (LOGGER.isLoggable(Level.WARNING)) {
             LOGGER.log(Level.WARNING, e.toString(), e);
           }
         }
       }
       wsAddOn.setup(null, filterChainBuilder);
       isWebSocketEnabled = true;
     }
   }
 }
Esempio n. 4
0
 @Test
 public void defaultValueTest() {
   Protocols protocols = getHabitat().getService(Protocols.class);
   for (Protocol protocol : protocols.getProtocol()) {
     Http http = protocol.getHttp();
     System.out.println(http.getCompressableMimeType());
   }
 }
 protected Set<ContentEncoding> configureCompressionEncodings(Http http) {
   final String mode = http.getCompression();
   int compressionMinSize = Integer.parseInt(http.getCompressionMinSizeBytes());
   CompressionLevel compressionLevel;
   try {
     compressionLevel = CompressionLevel.getCompressionLevel(mode);
   } catch (IllegalArgumentException e) {
     try {
       // Try to parse compression as an int, which would give the
       // minimum compression size
       compressionLevel = CompressionLevel.ON;
       compressionMinSize = Integer.parseInt(mode);
     } catch (Exception ignore) {
       compressionLevel = CompressionLevel.OFF;
     }
   }
   final String compressableMimeTypesString = http.getCompressableMimeType();
   final String noCompressionUserAgentsString = http.getNoCompressionUserAgents();
   final String[] compressableMimeTypes =
       ((compressableMimeTypesString != null)
           ? compressableMimeTypesString.split(",")
           : new String[0]);
   final String[] noCompressionUserAgents =
       ((noCompressionUserAgentsString != null)
           ? noCompressionUserAgentsString.split(",")
           : new String[0]);
   final ContentEncoding gzipContentEncoding =
       new GZipContentEncoding(
           GZipContentEncoding.DEFAULT_IN_BUFFER_SIZE,
           GZipContentEncoding.DEFAULT_OUT_BUFFER_SIZE,
           new CompressionEncodingFilter(
               compressionLevel,
               compressionMinSize,
               compressableMimeTypes,
               noCompressionUserAgents,
               GZipContentEncoding.getGzipAliases()));
   final ContentEncoding lzmaEncoding =
       new LZMAContentEncoding(
           new CompressionEncodingFilter(
               compressionLevel,
               compressionMinSize,
               compressableMimeTypes,
               noCompressionUserAgents,
               LZMAContentEncoding.getLzmaAliases()));
   final Set<ContentEncoding> set = new HashSet<ContentEncoding>(2);
   set.add(gzipContentEncoding);
   set.add(lzmaEncoding);
   return set;
 }
  protected org.glassfish.grizzly.http.HttpServerFilter createHttpServerCodecFilter(
      final Http http,
      final boolean isChunkedEnabled,
      final int headerBufferLengthBytes,
      final String defaultResponseType,
      final KeepAlive keepAlive,
      final DelayedExecutor delayedExecutor,
      final int maxRequestHeaders,
      final int maxResponseHeaders) {
    final org.glassfish.grizzly.http.HttpServerFilter httpCodecFilter =
        new org.glassfish.grizzly.http.HttpServerFilter(
            isChunkedEnabled,
            headerBufferLengthBytes,
            defaultResponseType,
            keepAlive,
            delayedExecutor,
            maxRequestHeaders,
            maxResponseHeaders);

    if (http != null) { // could be null for HTTP redirect
      httpCodecFilter.setMaxPayloadRemainderToSkip(
          Integer.parseInt(http.getMaxSwallowingInputBytes()));
    }

    return httpCodecFilter;
  }
  @SuppressWarnings({"deprecation"})
  protected void configureHttpProtocol(
      final ServiceLocator habitat,
      final NetworkListener networkListener,
      final Http http,
      final FilterChainBuilder filterChainBuilder,
      boolean secure) {
    transactionTimeoutMillis = Long.parseLong(http.getRequestTimeoutSeconds()) * 1000;
    filterChainBuilder.add(
        new IdleTimeoutFilter(
            obtainDelayedExecutor(), Integer.parseInt(http.getTimeoutSeconds()), TimeUnit.SECONDS));
    final org.glassfish.grizzly.http.HttpServerFilter httpServerFilter =
        createHttpServerCodecFilter(http);
    final Set<ContentEncoding> contentEncodings = configureContentEncodings(http);
    for (ContentEncoding contentEncoding : contentEncodings) {
      httpServerFilter.addContentEncoding(contentEncoding);
    }
    //        httpServerFilter.getMonitoringConfig().addProbes(
    //                serverConfig.getMonitoringConfig().getHttpConfig().getProbes());
    filterChainBuilder.add(httpServerFilter);
    final FileCache fileCache = configureHttpFileCache(http.getFileCache());
    fileCache.initialize(obtainDelayedExecutor());
    final FileCacheFilter fileCacheFilter = new FileCacheFilter(fileCache);
    //        fileCache.getMonitoringConfig().addProbes(
    //                serverConfig.getMonitoringConfig().getFileCacheConfig().getProbes());
    filterChainBuilder.add(fileCacheFilter);
    final HttpServerFilter webServerFilter =
        new HttpServerFilter(getHttpServerFilterConfiguration(http), obtainDelayedExecutor());

    final HttpHandler httpHandler = getHttpHandler();
    httpHandler.setAllowEncodedSlash(GrizzlyConfig.toBoolean(http.getEncodedSlashEnabled()));
    webServerFilter.setHttpHandler(httpHandler);
    //        webServerFilter.getMonitoringConfig().addProbes(
    //                serverConfig.getMonitoringConfig().getWebServerConfig().getProbes());
    filterChainBuilder.add(webServerFilter);

    configureSpdySupport(habitat, networkListener, http.getSpdy(), filterChainBuilder, secure);

    // TODO: evaluate comet/websocket support over SPDY.
    configureCometSupport(habitat, networkListener, http, filterChainBuilder);

    configureWebSocketSupport(habitat, networkListener, http, filterChainBuilder);

    configureAjpSupport(habitat, networkListener, http, filterChainBuilder);
  }
  protected void configureAjpSupport(
      final ServiceLocator habitat,
      final NetworkListener networkListener,
      final Http http,
      final FilterChainBuilder filterChainBuilder) {

    final boolean jkSupportEnabled =
        http.getJkEnabled() != null
            ? Boolean.parseBoolean(http.getJkEnabled())
            : Boolean.parseBoolean(networkListener.getJkEnabled());

    if (jkSupportEnabled) {
      final AddOn ajpAddOn = loadAddOn(habitat, "ajp", "org.glassfish.grizzly.http.ajp.AjpAddOn");
      if (ajpAddOn != null) {
        configureElement(habitat, networkListener, http, ajpAddOn);
        ajpAddOn.setup(null, filterChainBuilder);
        isAjpEnabled = true;
      }
    }
  }
  protected void configureCometSupport(
      final ServiceLocator habitat,
      final NetworkListener networkListener,
      final Http http,
      final FilterChainBuilder filterChainBuilder) {

    if (GrizzlyConfig.toBoolean(http.getCometSupportEnabled())) {
      final AddOn cometAddOn =
          loadAddOn(habitat, "comet", "org.glassfish.grizzly.comet.CometAddOn");
      if (cometAddOn != null) {
        configureElement(habitat, networkListener, http, cometAddOn);
        cometAddOn.setup(null, filterChainBuilder);
        isCometEnabled = true;
      }
    }
  }
 protected ServerFilterConfiguration getHttpServerFilterConfiguration(Http http) {
   final ServerFilterConfiguration serverFilterConfiguration = new ServerFilterConfiguration();
   serverFilterConfiguration.setScheme(http.getScheme());
   serverFilterConfiguration.setPassTraceRequest(true);
   serverFilterConfiguration.setTraceEnabled(Boolean.valueOf(http.getTraceEnabled()));
   int maxRequestParameters;
   try {
     maxRequestParameters = Integer.parseInt(http.getMaxRequestParameters());
   } catch (NumberFormatException nfe) {
     maxRequestParameters = Http.MAX_REQUEST_PARAMETERS;
   }
   serverFilterConfiguration.setMaxRequestParameters(maxRequestParameters);
   serverFilterConfiguration.setMaxPostSize(Integer.parseInt(http.getMaxPostSizeBytes()));
   serverFilterConfiguration.setMaxFormPostSize(Integer.parseInt(http.getMaxFormPostSizeBytes()));
   serverFilterConfiguration.setMaxBufferedPostSize(
       Integer.parseInt(http.getMaxSavePostSizeBytes()));
   return serverFilterConfiguration;
 }