@Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    if (_log.isDebugEnabled()) {
      _log.debug("Process service request");
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Set portal port");
    }

    PortalUtil.setPortalInetSocketAddresses(request);

    if (_httpServlets.isEmpty()) {
      response.sendError(
          HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Module framework is unavailable");

      return;
    }

    HttpServlet httpServlet = _httpServlets.get(0);

    httpServlet.service(request, response);
  }
  @Test
  public void testCaptureResponse() throws IOException {

    // Not a portal resiliency action

    SPIAgentResponse spiAgentResponse = new SPIAgentResponse(_SERVLET_CONTEXT_NAME);

    spiAgentResponse.captureResponse(
        new MockHttpServletRequest(),
        new BufferCacheServletResponse(new MockHttpServletResponse()));

    Assert.assertFalse(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNull(spiAgentResponse.metaData);
    Assert.assertNull(spiAgentResponse.byteData);
    Assert.assertNull(spiAgentResponse.stringData);

    // Portal resiliency action, byte model output, empty

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(WebKeys.PORTAL_RESILIENCY_ACTION, true);

    BufferCacheServletResponse bufferCacheServletResponse =
        new BufferCacheServletResponse(new MockHttpServletResponse());

    bufferCacheServletResponse.setByteBuffer(ByteBuffer.allocate(0));

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertNull(spiAgentResponse.byteData);
    Assert.assertNull(spiAgentResponse.stringData);

    // Portal resiliency action, byte model output, native buffer

    byte[] byteArray = new byte[] {(byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5};

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteArray.length);

    byteBuffer.put(byteArray);

    byteBuffer.clear();

    bufferCacheServletResponse.setByteBuffer(byteBuffer);

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertArrayEquals(byteArray, spiAgentResponse.byteData);
    Assert.assertNull(spiAgentResponse.stringData);

    // Portal resiliency action, byte model output, whole buffer

    bufferCacheServletResponse.setByteBuffer(ByteBuffer.wrap(byteArray));

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertSame(byteArray, spiAgentResponse.byteData);
    Assert.assertNull(spiAgentResponse.stringData);

    // Portal resiliency action, byte model output, partial buffer

    bufferCacheServletResponse.setByteBuffer(ByteBuffer.wrap(byteArray, 2, 2));

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertArrayEquals(new byte[] {(byte) 2, (byte) 3}, spiAgentResponse.byteData);
    Assert.assertNull(spiAgentResponse.stringData);

    // Portal resiliency action, char model output, empty

    bufferCacheServletResponse.setString(StringPool.BLANK);

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertNull(spiAgentResponse.byteData);
    Assert.assertNull(spiAgentResponse.stringData);

    // Portal resiliency action, char model output, without footer

    String content = "content";

    bufferCacheServletResponse.setString(content);

    mockHttpServletRequest.setParameter("portalResiliencyPortletShowFooter", StringPool.FALSE);

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertNull(spiAgentResponse.byteData);
    Assert.assertSame(content, spiAgentResponse.stringData);

    // Portal resiliency action, char model output, not HTML, without footer

    bufferCacheServletResponse.setString(content);

    mockHttpServletRequest.setParameter("portalResiliencyPortletShowFooter", StringPool.TRUE);

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertNull(spiAgentResponse.byteData);
    Assert.assertSame(content, spiAgentResponse.stringData);

    // Portal resiliency action, char model output, with footer

    content = "<div>content</div>";

    bufferCacheServletResponse.setString(content);

    mockHttpServletRequest.setLocalAddr("127.0.0.1");
    mockHttpServletRequest.setLocalPort(1234);

    PortalUtil.setPortalInetSocketAddresses(mockHttpServletRequest);

    mockHttpServletRequest.setParameter("portalResiliencyPortletShowFooter", StringPool.TRUE);

    spiAgentResponse.captureResponse(mockHttpServletRequest, bufferCacheServletResponse);

    Assert.assertTrue(spiAgentResponse.portalResiliencyResponse);
    Assert.assertNotNull(spiAgentResponse.metaData);
    Assert.assertNull(spiAgentResponse.byteData);
    Assert.assertEquals(
        "<div>content<div class=\"alert alert-info\"><strong>This "
            + "portlet is from SPI 1234</strong></div></div>",
        spiAgentResponse.stringData);
  }