Beispiel #1
1
  @Override
  protected void writeLayer(
      SlingHttpServletRequest req, SlingHttpServletResponse resp, ImageContext c, Layer layer)
      throws IOException, RepositoryException {

    Image image = new Image(c.resource);
    if (!image.hasContent()) {
      resp.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // get style and set constraints
    image.loadStyleData(c.style);

    // get pure layer
    layer = image.getLayer(false, false, false);
    boolean modified = false;

    if (layer != null) {
      // crop
      modified = image.crop(layer) != null;

      // rotate
      modified |= image.rotate(layer) != null;

      // resize
      modified |= image.resize(layer) != null;

      // apply diff if needed (because we create the layer inline)
      modified |= applyDiff(layer, c);
    }

    // don't cache images on authoring instances
    // Cache-Control: no-cache allows caching (e.g. in the browser cache) but
    // will force revalidation using If-Modified-Since or If-None-Match every time,
    // avoiding aggressive browser caching
    if (!WCMMode.DISABLED.equals(WCMMode.fromRequest(req))) {
      resp.setHeader("Cache-Control", "no-cache");
    }

    if (modified) {
      String mimeType = image.getMimeType();
      if (ImageHelper.getExtensionFromType(mimeType) == null) {
        // get default mime type
        mimeType = "image/png";
      }
      resp.setContentType(mimeType);
      layer.write(mimeType, mimeType.equals("image/gif") ? 255 : 1.0, resp.getOutputStream());
    } else {
      // do not re-encode layer, just spool
      Property data = image.getData();
      InputStream in = data.getStream();
      resp.setContentLength((int) data.getLength());
      resp.setContentType(image.getMimeType());
      IOUtils.copy(in, resp.getOutputStream());
      in.close();
    }
    resp.flushBuffer();
  }
Beispiel #2
0
  @Test
  public void testWcmDisabledMode() {
    WCMMode.DISABLED.toRequest(context.request());
    Parsys parsys = context.request().adaptTo(Parsys.class);

    List<Item> items = parsys.getItems();
    assertEquals(2, items.size());

    Item item1 = items.get(0);
    assertEquals(par1Resource.getPath(), item1.getResourcePath());
    assertNull(item1.getResourceType());
    assertEquals(SECTION_DEFAULT_CLASS_NAME, item1.getCssClassName());
    assertFalse(item1.isNewArea());

    Item item2 = items.get(1);
    assertEquals(par2Resource.getPath(), item2.getResourcePath());
    assertNull(item2.getResourceType());
    assertEquals(SECTION_DEFAULT_CLASS_NAME, item2.getCssClassName());
    assertFalse(item2.isNewArea());
  }