<commit_before>// Copyright (C) 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.caja.service;

import com.google.caja.lexer.FetchedData;
import com.google.caja.lexer.InputSource;
import com.google.caja.lexer.escaping.Escaping;
import com.google.caja.reporting.Message;
import com.google.caja.reporting.MessageContext;
import com.google.caja.reporting.MessageQueue;
import com.google.caja.reporting.SimpleMessageQueue;
import com.google.caja.util.Pair;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Handles HTTP servlet invocation of a {@link CajolingService}.
 *
 * @author <EMAIL> (<NAME>)
 * @author <EMAIL> (<NAME>)
 */
public class CajolingServlet extends HttpServlet {
  private static final long serialVersionUID = 5055670217887121398L;
  private static final Pair<String, String> UMP =
    Pair.pair("Access-Control-Allow-Origin", "*");

  private static class HttpContentHandlerArgs extends ContentHandlerArgs {
    private final HttpServletRequest request;

    public HttpContentHandlerArgs(HttpServletRequest request) {
      this.request = request;
    }

    @Override
    public String get(String name) {
      return request.getParameter(name);
    }
  }

  private final CajolingService service;

  /**
   * Appengine insists on a zero-argument constructor
   *
   * @deprecated Do not use this; instead pass in a CajolingService you've
   *             constructed with the correct host argument (self URL).
   */
  @Deprecated
  public CajolingServlet() {
    this(new CajolingService());
  }

  public CajolingServlet(CajolingService service) {
    this.service = service;
  }

  /**
   * Set an error status on a servlet response and close its stream cleanly.
   *
   * @param resp a servlet response.
   * @param error an error message.
   */
  private static void closeBadRequest(HttpServletResponse resp,
      int httpStatus, String error)
      throws ServletException {
    try {
      resp.sendError(httpStatus, error);
    } catch (IOException ex) {
      throw (ServletException) new ServletException().initCause(ex);
    }
  }

  /**
   * Set an error status on a servlet response and close its stream cleanly.
   *
   * @param resp a servlet response.
   * @param httpStatus status response level.
   * @param mq a {@link MessageQueue} with messages to include as an error page.
   */
  private static void closeBadRequest(HttpServletResponse resp,
      int httpStatus, MessageQueue mq)
      throws ServletException {
    closeBadRequest(resp, httpStatus, serializeMessageQueue(mq));
  }

  // TODO(jasvir): The service like the gwt version should accumulate
  // input sources and use html snippet producer to produce messages
  private static String serializeMessageQueue(MessageQueue mq) {
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    for (Message m : mq.getMessages()) {
      sb.append(m.getMessageLevel().name()).append(": ");
      Escaping.escapeXml(m.format(mc), false, sb);
      sb.append("\n");
    }
    return sb.toString();
  }

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException {
    if (req.getContentType() == null) {
      closeBadRequest(resp, HttpServletResponse.SC_BAD_REQUEST,
          "Supplied Content-type is null");
      return;
    }

    FetchedData fetchedData;<commit_msg>UMP workaround for browsers that implement only CORS http://codereview.appspot.com/4560046<commit_after>// Copyright (C) 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.caja.service;

import com.google.caja.lexer.FetchedData;
import com.google.caja.lexer.InputSource;
import com.google.caja.lexer.escaping.Escaping;
import com.google.caja.reporting.Message;
import com.google.caja.reporting.MessageContext;
import com.google.caja.reporting.MessageQueue;
import com.google.caja.reporting.SimpleMessageQueue;
import com.google.caja.util.Pair;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Handles HTTP servlet invocation of a {@link CajolingService}.
 *
 * @author <EMAIL> (<NAME>)
 * @author <EMAIL> (<NAME>)
 */
public class CajolingServlet extends HttpServlet {
  private static final long serialVersionUID = 5055670217887121398L;
  private static final Pair<String, String> UMP =
    Pair.pair("Access-Control-Allow-Origin", "*");

  private static class HttpContentHandlerArgs extends ContentHandlerArgs {
    private final HttpServletRequest request;

    public HttpContentHandlerArgs(HttpServletRequest request) {
      this.request = request;
    }

    @Override
    public String get(String name) {
      return request.getParameter(name);
    }
  }

  private final CajolingService service;

  /**
   * Appengine insists on a zero-argument constructor
   *
   * @deprecated Do not use this; instead pass in a CajolingService you've
   *             constructed with the correct host argument (self URL).
   */
  @Deprecated
  public CajolingServlet() {
    this(new CajolingService());
  }

  public CajolingServlet(CajolingService service) {
    this.service = service;
  }

  /**
   * Set an error status on a servlet response and close its stream cleanly.
   *
   * @param resp a servlet response.
   * @param error an error message.
   */
  private static void closeBadRequest(HttpServletResponse resp,
      int httpStatus, String error)
      throws ServletException {
    try {
      resp.sendError(httpStatus, error);
    } catch (IOException ex) {
      throw (ServletException) new ServletException().initCause(ex);
    }
  }

  /**
   * Set an error status on a servlet response and close its stream cleanly.
   *
   * @param resp a servlet response.
   * @param httpStatus status response level.
   * @param mq a {@link MessageQueue} with messages to include as an error page.
   */
  private static void closeBadRequest(HttpServletResponse resp,
      int httpStatus, MessageQueue mq)
      throws ServletException {
    closeBadRequest(resp, httpStatus, serializeMessageQueue(mq));
  }

  // TODO(jasvir): The service like the gwt version should accumulate
  // input sources and use html snippet producer to produce messages
  private static String serializeMessageQueue(MessageQueue mq) {
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    for (Message m : mq.getMessages()) {
      sb.append(m.getMessageLevel().name()).append(": ");
      Escaping.escapeXml(m.format(mc), false, sb);
      sb.append("\n");
    }
    return sb.toString();
  }

  @Override
  protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    // CORS requires that browsers do an OPTIONS request before allowing
    // cross-site POSTs.  UMP does not require this, but no browser implements
    // UMP at this time.  So, we reply to the OPTIONS request to trick
    // browsers into effectively implementing UMP.
    resp.setHeader("Access-Control-Allow-Origin", "*");
    resp.setHeader("Access-Control-Allow-Methods", "GET, POST");
    resp.setHeader("Access-Control-Allow-Headers", "Content-Type");
    resp.setHeader("Access-Control-Max-Age", "86400");
  }

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException {
    if (req.getContentType() == null) {
      closeBadRequest(resp, HttpServletResponse.SC_BAD_REQUEST,
          "Supplied Content-type is null");
      return;
    }

    FetchedData fetchedData;
{'fraction_non_alphanumeric': 0.0944206008583691, 'fraction_numerical': 0.007454257962502824, 'mean_word_length': 3.636125654450262, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3514102', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2439, 'n_words': 790}