示例#1
1
文件: img.GET.java 项目: tiennv90/SVN
  @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();
  }
  String getScript(SlingHttpServletRequest request, UserProperties userProperties) {
    SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
    XSSAPI xssAPI = bindings.getSling().getService(XSSAPI.class).getRequestSpecificAPI(request);

    StringBuilder res = new StringBuilder();
    boolean isDisabled = WCMMode.fromRequest(request).equals(WCMMode.DISABLED);
    res.append("<input");
    res.append(" id=\"").append(xssAPI.encodeForHTMLAttr(id)).append("\"");
    res.append(" class=\"").append(xssAPI.encodeForHTMLAttr(clazz)).append("\"");
    res.append(" type=\"").append(xssAPI.encodeForHTMLAttr(type)).append("\"");
    res.append(" name=\"").append(xssAPI.encodeForHTMLAttr(name)).append("\"");
    res.append("/>");

    // change field upon ccm changes in author mode, listening for store 'update' events
    if (!isDisabled) {
      res.append("<script type=\"text/javascript\">");
      res.append("if( window.CQ_Analytics && CQ_Analytics.CCM) {");
      res.append("$CQ(function() {");
      res.append(
          "var store = CQ_Analytics.CCM.getRegisteredStore(CQ_Analytics.ProfileDataMgr.STORENAME);");
      res.append("if(store) {");
      res.append("var name = store.getProperty('")
          .append(xssAPI.encodeForJSString(propertyName))
          .append("', true) || '';");
      res.append("var el = document.getElementById('")
          .append(xssAPI.encodeForJSString(id))
          .append("');");
      res.append("if( el) {");
      res.append("el.value = name;");
      res.append("}");
      res.append("}");
      res.append("CQ_Analytics.CCM.addListener('storesloaded', function() {");
      res.append(
          "var store = CQ_Analytics.CCM.getRegisteredStore(CQ_Analytics.ProfileDataMgr.STORENAME);");
      res.append("if(store && store.addListener) {");
      res.append("var name = store.getProperty('")
          .append(xssAPI.encodeForJSString(propertyName))
          .append("', true) || '';");
      res.append("var el = document.getElementById('")
          .append(xssAPI.encodeForJSString(id))
          .append("');");
      res.append("if( el) {");
      res.append("el.value = name;");
      res.append("}");
      res.append("store.addListener('update', function() {");
      res.append("var name = store.getProperty('")
          .append(xssAPI.encodeForJSString(propertyName))
          .append("', true) || '';");
      res.append("var el = document.getElementById('")
          .append(xssAPI.encodeForJSString(id))
          .append("');");
      res.append("if( el) {");
      res.append("el.value = name;");
      res.append("}");
      res.append("});");
      res.append("}");
      res.append("});");
      res.append("});");
      res.append("}");
      res.append("</script>");
    }
    // slightly different here, because there is no store 'update' event
    else {
      res.append("<script type=\"text/javascript\">");
      res.append("if( window.CQ_Analytics && CQ_Analytics.CCM) {");
      res.append("$CQ(function() {");
      res.append(
          "var store = CQ_Analytics.CCM.getRegisteredStore(CQ_Analytics.ProfileDataMgr.STORENAME);");
      res.append("if(store) {");
      res.append("var name = store.getProperty('")
          .append(xssAPI.encodeForJSString(propertyName))
          .append("', true) || '';");
      res.append("var el = document.getElementById('")
          .append(xssAPI.encodeForJSString(id))
          .append("');");
      res.append("if( el) {");
      res.append("el.value = name;");
      res.append("}");
      res.append("}");
      res.append("CQ_Analytics.CCM.addListener('storesloaded', function() {");
      res.append(
          "var store = CQ_Analytics.CCM.getRegisteredStore(CQ_Analytics.ProfileDataMgr.STORENAME);");
      res.append("if(store) {");
      res.append("var name = store.getProperty('")
          .append(xssAPI.encodeForJSString(propertyName))
          .append("', true) || '';");
      res.append("var el = document.getElementById('")
          .append(xssAPI.encodeForJSString(id))
          .append("');");
      res.append("if( el) {");
      res.append("el.value = name;");
      res.append("}");
      res.append("}");
      res.append("});");
      res.append("});");
      res.append("}");
      res.append("</script>");
    }

    return res.toString();
  }