@Override
 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
     throws ServletException, IOException {
   DefaultServletDelegate delegate =
       (DefaultServletDelegate) request.getAttribute(DefaultServletSwitch.class.getName());
   if (delegate != null) {
     delegate.doDelegateGet(request, response);
   } else {
     response.sendError(404);
   }
 }
Ejemplo n.º 2
0
  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/plain");
    final PrintWriter w = response.getWriter();

    final int cycles = getIntParam(request, "cycles", 10);
    final int interval = getIntParam(request, "interval", 1000);
    final int flushEvery = getIntParam(request, "flushEvery", 2);

    // Use supplied RuntimeState if available
    final RuntimeState runtimeState =
        (RuntimeState) request.getAttribute(RuntimeState.class.getName());
    if (runtimeState == null) {
      log.warn("No RuntimeState attribute provided, won't report progress");
    } else {
      runtimeState.setEstimatedCompletionTime(
          new Date(System.currentTimeMillis() + cycles * interval));
    }

    w.println("Start at " + new Date());
    try {
      for (int i = 1; i <= cycles; i++) {
        if (i % flushEvery == 0) {
          w.println("Flushing output<br/>");
          w.flush();
        }

        final String msg = String.format("Cycle %d of %d", i, cycles);
        w.printf(msg);
        w.print("\n<br/>");

        if (runtimeState != null) {
          runtimeState.setProgressMessage(msg);
          final int remainingCycles = cycles - i;
          runtimeState.setEstimatedCompletionTime(
              new Date(System.currentTimeMillis() + remainingCycles * interval));
        }

        try {
          Thread.sleep(interval);
        } catch (InterruptedException iex) {
          throw new ServletException("InterruptedException", iex);
        }
      }
      w.println("All done.");
      w.flush();
    } catch (Throwable t) {
      log.info("Exception in doGet", t);
    }
  }
 /**
  * Same as writeResults logic, but counts number of results iterated over.
  *
  * @param request
  * @param write
  * @param iterator
  * @return Set containing all unique paths processed.
  * @throws JSONException
  */
 public Set<String> writeResultsInternal(
     SlingHttpServletRequest request, JSONWriter write, Iterator<Result> iterator)
     throws JSONException {
   final Set<String> uniquePaths = new HashSet<String>();
   final Integer iDepth = (Integer) request.getAttribute("depth");
   int depth = 0;
   if (iDepth != null) {
     depth = iDepth.intValue();
   }
   try {
     javax.jcr.Session jcrSession = request.getResourceResolver().adaptTo(javax.jcr.Session.class);
     final Session session = StorageClientUtils.adaptToSession(jcrSession);
     while (iterator.hasNext()) {
       final Result result = iterator.next();
       uniquePaths.add(result.getPath());
       try {
         if ("authorizable".equals(result.getFirstValue("resourceType"))) {
           AuthorizableManager authManager = session.getAuthorizableManager();
           Authorizable auth = authManager.findAuthorizable((String) result.getFirstValue("id"));
           if (auth != null) {
             write.object();
             ValueMap map = profileService.getProfileMap(auth, jcrSession);
             ExtendedJSONWriter.writeValueMapInternals(write, map);
             write.endObject();
           }
         } else {
           String contentPath = result.getPath();
           final Content content = session.getContentManager().get(contentPath);
           if (content != null) {
             handleContent(content, session, write, depth);
           } else {
             LOGGER.debug("Found null content item while writing results [{}]", contentPath);
           }
         }
       } catch (AccessDeniedException e) {
         // do nothing
       } catch (RepositoryException e) {
         throw new JSONException(e);
       }
     }
   } catch (StorageClientException e) {
     throw new JSONException(e);
   }
   return uniquePaths;
 }
Ejemplo n.º 4
0
 /**
  * This method returns the STATE attribute from request.
  *
  * @param request The sling request
  * @return the toggle state
  */
 public static STATE fromRequest(final SlingHttpServletRequest request) {
   final Object state = request.getAttribute(REQUEST_ATTRIBUTE_NAME_STATE);
   return (null != state && state instanceof STATE) ? (STATE) state : null;
 }
Ejemplo n.º 5
0
 /**
  * Builds a mapped link to the path (resource path) with optional selectors and extension.
  *
  * @param request the request context for path mapping (the result is always mapped)
  * @param url the URL to use (complete) or the path to an addressed resource (without any
  *     extension)
  * @param selectors an optional selector string with all necessary selectors (can be 'null')
  * @param extension an optional extension (can be 'null' for extension determination)
  * @return the mapped url for the referenced resource
  */
 public static String getUrl(
     SlingHttpServletRequest request, String url, String selectors, String extension) {
   LinkMapper mapper = (LinkMapper) request.getAttribute(LinkMapper.LINK_MAPPER_REQUEST_ATTRIBUTE);
   return getUrl(
       request, url, selectors, extension, mapper != null ? mapper : LinkMapper.RESOLVER);
 }
Ejemplo n.º 6
0
  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();
  }