/**
   * This method sets the default properties on a comment being created. These properties are:
   *
   * <ul>
   *   <li>Date ({@link Comment#PROP_DATE}
   *   <li>Email ({@link CollabUser#PROP_EMAIL}
   *   <li>Website ({@link CollabUser#PROP_WEBSITE}
   *   <li>IP Address ({@link Comment#PROP_IP_ADDRESS}
   *   <li>User Agent ({@link Comment#PROP_USER_AGENT}
   *   <li>Referer ({@link Comment#PROP_REFERER}
   * </ul>
   *
   * @param request The request from which to retrieve the parameters for the properties to be set.
   * @param node The comment node upon which to set the properties.
   * @throws RepositoryException If an error occurs setting the properties.
   */
  protected void setCommentProperties(final SlingHttpServletRequest request, final Node node)
      throws RepositoryException {

    // date
    final Calendar cal = Calendar.getInstance();
    node.setProperty(Comment.PROP_DATE, cal);

    // email
    String email = request.getParameter(CollabUser.PROP_EMAIL);
    if (email == null) {
      email = "";
    }
    node.setProperty(CollabUser.PROP_EMAIL, email);

    // website
    String website = request.getParameter(CollabUser.PROP_WEBSITE);
    if (website == null) {
      website = "";
    } else if (!"".equals(website) && !website.matches("^.*\\:\\/\\/.*$")) {
      website = "http://" + website;
    }
    node.setProperty(CollabUser.PROP_WEBSITE, website);

    // ip
    node.setProperty(Comment.PROP_IP_ADDRESS, request.getRemoteAddr());
    // userAgent
    node.setProperty(Comment.PROP_USER_AGENT, request.getHeader("User-Agent"));
    // referer
    node.setProperty(Comment.PROP_REFERER, request.getHeader("Referer"));

    // authorizable id
    setAuthorizableId(request, node);

    log.debug("additional properties set on " + node.getPath());
  }
 /**
  * Allow origin access control.
  *
  * @param request The sling request
  * @param response The sling response
  */
 protected void allowCORS(
     final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
   if (null == externalizer) {
     return;
   }
   final String localOrigin = externalizer.absoluteLink(request, request.getScheme(), "");
   if (!(localOrigin.equals(request.getHeader("Origin")))) {
     response.setHeader("Access-Control-Allow-Credentials", "true");
     response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
     response.setHeader("Access-Control-Allow-Headers", "CONTENT-TYPE, LOCATION, *");
     response.setHeader("Access-Control-Expose-Headers", "Content-Type, Location");
   }
 }
  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {

    // setup SSE headers
    response.setContentType("text/event-stream");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Connection", "keep-alive");
    // needed to allow the JavaScript EventSource API to make a call from author to this server and
    // listen for the events
    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
    response.setHeader("Access-Control-Allow-Credentials", "true");

    String agentName =
        request.getResource().getParent().getParent().adaptTo(ReplicationAgent.class).getName();
    PrintWriter writer = response.getWriter();
    while (true) {
      try {
        synchronized (cachedEvents) {
          cachedEvents.wait();
          Collection<String> eventsForAgent = cachedEvents.get(agentName);
          if (eventsForAgent != null) {
            for (String event : eventsForAgent) {
              writeEvent(writer, agentName + "-queue-event", event);
            }
          }
        }
      } catch (InterruptedException e) {
        log.error("error during SSE", e);
        throw new ServletException(e);
      }
    }
  }
예제 #4
0
 /**
  * {@inheritDoc}
  *
  * @see
  *     org.apache.sling.api.servlets.SlingAllMethodsServlet#doPut(org.apache.sling.api.SlingHttpServletRequest,
  *     org.apache.sling.api.SlingHttpServletResponse)
  */
 @Override
 protected void doPut(SlingHttpServletRequest request, SlingHttpServletResponse response)
     throws ServletException, IOException {
   String proxyRequest = request.getHeader("Sakai-Proxy-Request-Body");
   boolean proxyStream = true;
   if ("0".equals(proxyRequest)) {
     proxyStream = false;
   }
   dispatch(request, response, proxyStream);
 }
 /**
  * Return if origins are the same.
  *
  * @param request The sling request
  * @return if origins are the same.
  */
 protected Boolean isCORS(final SlingHttpServletRequest request) {
   if (null == externalizer) {
     return false;
   }
   final String localOrigin = externalizer.absoluteLink(request, request.getScheme(), "");
   if (!(localOrigin.equals(request.getHeader("Origin")))) {
     return true;
   }
   return false;
 }
예제 #6
0
  protected void dispatch(
      SlingHttpServletRequest request, SlingHttpServletResponse response, boolean userInputStream)
      throws ServletException, IOException {
    try {

      Resource resource = request.getResource();
      if (!resource.getPath().startsWith(PROXY_PATH_PREFIX)) {
        response.sendError(
            HttpServletResponse.SC_FORBIDDEN,
            "Proxying templates may only be stored in " + PROXY_PATH_PREFIX);
        return;
      }
      Node node = resource.adaptTo(Node.class);
      if (!userInputStream) {
        Value[] v = JcrUtils.getValues(node, SAKAI_REQUEST_STREAM_BODY);
        if (v != null && v.length > 0) {
          userInputStream = Boolean.parseBoolean(v[0].getString());
        }
      }
      Map<String, String> headers = new ConcurrentHashMap<String, String>();
      for (Enumeration<?> enames = request.getHeaderNames(); enames.hasMoreElements(); ) {

        String name = (String) enames.nextElement();
        if (!headerBacklist.contains(name)) {
          headers.put(name, request.getHeader(name));
        }
      }
      // search for special headers.
      if (headers.containsKey(BASIC_USER)) {
        String user = headers.get(BASIC_USER);
        String password = headers.get(BASIC_PASSWORD);
        Base64 base64 = new Base64();
        String passwordDigest =
            new String(base64.encode((user + ":" + password).getBytes("UTF-8")));
        String digest = BASIC + passwordDigest.trim();
        headers.put(AUTHORIZATION, digest);
      }

      for (Entry<String, String> e : headers.entrySet()) {
        if (e.getKey().startsWith(":")) {
          headers.remove(e.getKey());
        }
      }

      // collect the parameters and store into a mutable map.
      RequestParameterMap parameterMap = request.getRequestParameterMap();
      Map<String, Object> templateParams = new ConcurrentHashMap<String, Object>(parameterMap);

      // search for special parameters.
      if (parameterMap.containsKey(BASIC_USER)) {
        String user = parameterMap.getValue(BASIC_USER).getString();
        String password = parameterMap.getValue(BASIC_PASSWORD).getString();
        Base64 base64 = new Base64();
        String passwordDigest =
            new String(base64.encode((user + ":" + password).getBytes("UTF-8")));
        String digest = BASIC + passwordDigest.trim();
        headers.put(AUTHORIZATION, digest);
      }

      // we might want to pre-process the headers
      if (node.hasProperty(ProxyPreProcessor.SAKAI_PREPROCESSOR)) {
        String preprocessorName =
            node.getProperty(ProxyPreProcessor.SAKAI_PREPROCESSOR).getString();
        ProxyPreProcessor preprocessor = preProcessors.get(preprocessorName);
        if (preprocessor != null) {
          preprocessor.preProcessRequest(request, headers, templateParams);
        } else {
          LOGGER.warn(
              "Unable to find pre processor of name {} for node {} ",
              preprocessorName,
              node.getPath());
        }
      }
      ProxyPostProcessor postProcessor = defaultPostProcessor;
      // we might want to post-process the headers
      if (node.hasProperty(ProxyPostProcessor.SAKAI_POSTPROCESSOR)) {
        String postProcessorName =
            node.getProperty(ProxyPostProcessor.SAKAI_POSTPROCESSOR).getString();
        if (postProcessors.containsKey(postProcessorName)) {
          postProcessor = postProcessors.get(postProcessorName);
        }
        if (postProcessor == null) {
          LOGGER.warn(
              "Unable to find post processor of name {} for node {} ",
              postProcessorName,
              node.getPath());
          postProcessor = defaultPostProcessor;
        }
      }

      ProxyResponse proxyResponse =
          proxyClientService.executeCall(node, headers, templateParams, null, -1, null);
      try {
        postProcessor.process(templateParams, response, proxyResponse);
      } finally {
        proxyResponse.close();
      }
    } catch (IOException e) {
      throw e;
    } catch (ProxyClientException e) {
      response.sendError(500, e.getMessage());
    } catch (RepositoryException e) {
      response.sendError(500, e.getMessage());
    }
  }
예제 #7
0
 public static int getDefaultPort(SlingHttpServletRequest request) {
   if (!request.isSecure()) {
     return 80;
   }
   return FORWARDED_SSL_ON.equalsIgnoreCase(request.getHeader(FORWARDED_SSL_HEADER)) ? 80 : 443;
 }