@Test
  public void request_content_image_that_binary_is_on_main_version() throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    // setup: draft version of content
    ContentEntity content = fixture.findContentByKey(contentKey);

    BinaryDataEntity binaryDataOfMainVersion = content.getMainVersion().getBinaryData("source");

    // exercise & verify
    String imageRequestPath =
        "_image/" + contentKey + "/binary/" + binaryDataOfMainVersion.getKey() + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() > 0);
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  @Test
  public void request_content_image_that_does_not_exist() throws Exception {
    String imageRequestPath = "_image/123.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
  @Test
  public void request_content_image_with_label_that_does_not_exist() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    String imageRequestPath = "_image/" + contentKey + "/label/nonexistinglabel.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
  @Test
  public void request_content_image_that_is_deleted() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);
    ContentEntity content = fixture.findContentByKey(contentKey);
    content.setDeleted(true);

    String imageRequestPath = "_image/" + contentKey + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
  @Test
  public void response_ok_for_request_to_content_image_that_binary_is_not_on_main_version()
      throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    // setup: draft version of content
    ContentEntity content = fixture.findContentByKey(contentKey);
    ContentVersionEntity draftVersion = createDraftVersion("Arn.JPG");
    draftVersion.setContentDataXml(content.getMainVersion().getContentDataAsXmlString());
    content.setDraftVersion(draftVersion);
    content.addVersion(draftVersion);
    fixture.save(draftVersion);

    BinaryDataEntity binaryDataForDraftVersion = factory.createBinaryData("Arn.JPG", bytes.length);
    binaryDataForDraftVersion.setBlobKey(
        content.getMainVersion().getBinaryData("source").getBlobKey());
    fixture.save(binaryDataForDraftVersion);

    ContentBinaryDataEntity contentBinaryDataForDraftVersion =
        factory.createContentBinaryData("source", binaryDataForDraftVersion, draftVersion);
    draftVersion.addContentBinaryData(contentBinaryDataForDraftVersion);
    fixture.save(contentBinaryDataForDraftVersion);

    fixture.flushAndClearHibernateSesssion();

    // exercise & verify
    String imageRequestPath = "_image/" + contentKey + "/label/source.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_version", draftVersion.getKey().toString());
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() > 0);
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  @Test
  public void request_content_image_with_no_access() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    fixture.createAndStoreUserAndUserGroup(
        "user-with-no-access", "testuser fullname", UserType.NORMAL, "testuserstore");
    loginUserInAdmin(fixture.findUserByName("user-with-no-access").getKey());

    String imageRequestPath = "_image/" + contentKey + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
示例#7
0
  @Test
  @Ignore
  public void
      response_ok_for_request_to_content_image_that_is_not_online_when_is_related_to_content_in_preview()
          throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 13, 0, 0, 0), null);

    // setup: preview
    Mockito.when(previewService.isInPreview()).thenReturn(true);

    ContentEntity contentPreviewed = new ContentEntity();

    ContentData contentData = Mockito.mock(ContentData.class);
    Mockito.when(contentData.resolveRelatedContentKeys()).thenReturn(Sets.newHashSet(contentKey));
    ContentVersionEntity versionPreviewed = Mockito.mock(ContentVersionEntity.class);
    Mockito.when(versionPreviewed.getContentData()).thenReturn(contentData);
    versionPreviewed.setContentData(contentData);
    ContentAndVersion contentAndVersionPreviewed =
        new ContentAndVersion(contentPreviewed, versionPreviewed);
    ContentPreviewContext contentPreviewContext =
        new ContentPreviewContext(contentAndVersionPreviewed);
    PreviewContext previewContext = new PreviewContext(contentPreviewContext);

    Mockito.when(previewService.getPreviewContext()).thenReturn(previewContext);

    // exercise & verify
    String imageRequestPath = "_image/" + contentKey;
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() > 0);
  }
示例#8
0
  @Test
  public void renderAvatarShouldReturnModifiedAvatarInResponse()
      throws IOException, NotFoundException {
    MockHttpServletResponse response = new MockHttpServletResponse();
    JCUser user = getUser();
    user.setAvatar(validAvatar);
    user.setAvatarLastModificationTime(new DateTime(1000));
    when(userService.get(anyLong())).thenReturn(user);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(avatarController.IF_MODIFIED_SINCE_HEADER, new Date(0));

    avatarController.renderAvatar(request, response, 0L);

    assertEquals(response.getContentType(), "image/jpeg");
    assertEquals(response.getContentLength(), validAvatar.length);
    assertEquals(response.getContentAsByteArray(), validAvatar);
    assertEquals(response.getHeader("Pragma"), "public");
    List<String> cacheControlHeaders = response.getHeaders("Cache-Control");
    assertTrue(cacheControlHeaders.contains("public"));
    assertTrue(cacheControlHeaders.contains("must-revalidate"));
    assertTrue(cacheControlHeaders.contains("max-age=0"));
    assertNotNull(response.getHeader("Expires")); // System.currentTimeMillis() is used
    assertNotNull(response.getHeader("Last-Modified")); // depends on current timezone
  }
  @Test
  public void testTransferResponse() throws Exception {

    // Exception

    MockRegistrationReference mockRegistrationReference =
        new MockRegistrationReference(new MockIntraband());

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(_spiConfiguration, mockRegistrationReference);

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(_mockHttpServletRequest));

    RecordSPIAgentResponse recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    Exception exception = new Exception();

    httpClientSPIAgent.transferResponse(
        mockHttpServletRequest,
        new BufferCacheServletResponse(new MockHttpServletResponse()),
        exception);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertSame(exception, recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertNull(recordSPIAgentResponse._request);
    Assert.assertNull(recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Response

    SPIAgentRequest spiAgentRequest = new SPIAgentRequest(_mockHttpServletRequest);

    File tempFile = File.createTempFile(HttpClientSPIAgentTest.class.getName(), null);

    Assert.assertTrue(tempFile.exists());

    spiAgentRequest.requestBodyFile = tempFile;

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_REQUEST, spiAgentRequest);

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

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

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Assert.assertFalse(tempFile.exists());
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertNull(recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertSame(mockHttpServletRequest, recordSPIAgentResponse._request);
    Assert.assertSame(
        bufferCacheServletResponse, recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Undeletable request body file

    spiAgentRequest = new SPIAgentRequest(_mockHttpServletRequest);

    tempFile = File.createTempFile(HttpClientSPIAgentTest.class.getName(), null);

    Assert.assertTrue(tempFile.exists());

    spiAgentRequest.requestBodyFile = tempFile;

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_REQUEST, spiAgentRequest);

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    bufferCacheServletResponse = new BufferCacheServletResponse(new MockHttpServletResponse());

    Assert.assertTrue(tempFile.delete());

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Class<?> clazz = Class.forName("java.io.DeleteOnExitHook");

    Field filesField = ReflectionUtil.getDeclaredField(clazz, "files");

    Set<String> files = (Set<String>) filesField.get(null);

    Assert.assertTrue(files.contains(tempFile.getPath()));
  }
  @Test
  public void testTransferResponse() throws IOException {

    // Exception

    MockRegistrationReference mockRegistrationReference =
        new MockRegistrationReference(new MockIntraband());

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(_spiConfiguration, mockRegistrationReference);

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(new MockHttpServletRequest()));

    RecordSPIAgentResponse recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    Exception exception = new Exception();

    httpClientSPIAgent.transferResponse(
        mockHttpServletRequest,
        new BufferCacheServletResponse(new MockHttpServletResponse()),
        exception);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertSame(exception, recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertNull(recordSPIAgentResponse._request);
    Assert.assertNull(recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Response

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(new MockHttpServletRequest()));

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

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

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertNull(recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertSame(mockHttpServletRequest, recordSPIAgentResponse._request);
    Assert.assertSame(
        bufferCacheServletResponse, recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);
  }