Exemplo n.º 1
0
  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    StringBuilder builder = new StringBuilder();
    Object resourceName = request.getParameter("resource");
    LogQuery query = LogQuery.Builder.withDefaults();

    if (resourceName != null) {
      builder.append("<b>REQUEST LOG for : " + resourceName + "</b> <br />");
      builder.append("<b>Resource | IP Address| Method | Time </b> <hr/>");

      for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(record.getStartTimeUsec() / 1000);
        if (record.getResource().contains(resourceName.toString())) {
          builder.append(
              record.getResource()
                  + " "
                  + record.getIp()
                  + " "
                  + record.getMethod()
                  + " "
                  + cal.getTime().toString()
                  + "<br/>");
        }
      }
      writer.println(builder.toString());

      /*Object cronHeader = request.getHeader("X-AppEngine-Cron");
      if(cronHeader != null && Boolean.getBoolean(cronHeader.toString()) == Boolean.TRUE){
      */ new MailProviderImpl()
          .sendMail(
              "*****@*****.**",
              "*****@*****.**",
              "NotesOnFly-Admin",
              "Resource: " + resourceName + " Usage Report",
              builder.toString());
      // }

    } else {
      writer.println("No Resource criteria provided.");
    }
  }
  private CapedwarfRequestLogs createCapedwarfRequestLogs(
      ServletRequest servletRequest, long startTimeMillis, CapedwarfEnvironment environment) {
    long startTimeUsec = startTimeMillis * 1000;

    CapedwarfRequestLogs capedwarfRequestLogs = new CapedwarfRequestLogs();
    RequestLogs requestLogs = capedwarfRequestLogs.getRequestLogs();
    requestLogs.setRequestId(generateId());
    requestLogs.setStartTimeUsec(startTimeUsec);
    requestLogs.setEndTimeUsec(startTimeUsec);
    requestLogs.setFinished(false);

    requestLogs.setAppId(environment.getAppId());
    requestLogs.setModuleId(environment.getModuleId());
    requestLogs.setVersionId(environment.getVersionId());

    if (servletRequest instanceof HttpServletRequest) {
      HttpServletRequest request = (HttpServletRequest) servletRequest;
      String queryString = request.getQueryString();
      requestLogs.setResource(
          request.getRequestURI() + (queryString == null ? "" : ("?" + queryString)));
      requestLogs.setUserAgent(request.getHeader("User-Agent"));
      requestLogs.setMethod(request.getMethod());
      requestLogs.setReferrer(request.getHeader("referer"));
      requestLogs.setHost(
          request.getServerName()
              + (request.getServerPort() == 80 ? "" : (":" + request.getServerPort())));
    }

    requestLogs.setHttpVersion(servletRequest.getProtocol());

    requestLogs.setInstanceKey(""); // TODO
    requestLogs.setReplicaIndex(-1); // TODO

    requestLogs.setPendingTime(0); // TODO
    requestLogs.setLatency(0); // TODO

    //        requestLogs.setUrlMapEntry();

    //        requestLogs.setTaskName();
    //        requestLogs.setTaskQueueName();
    //        requestLogs.setWasLoadingRequest();

    requestLogs.setNickname("");
    requestLogs.setIp(servletRequest.getRemoteAddr());

    requestLogs.setCost(0);
    requestLogs.setMcycles(0);
    requestLogs.setApiMcycles(0);

    // combined='93.103.26.101 - - [17/Jan/2013:08:07:11 -0800] "GET /favicon.ico HTTP/1.1" 404 0 -
    // "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko)
    // Chrome/24.0.1312.52 Safari/537.17"',
    requestLogs.setCombined(
        requestLogs.getIp()
            + " - "
            + requestLogs.getNickname()
            + (requestLogs.getNickname().isEmpty() ? "" : " ")
            + "- ["
            + DATE_FORMAT.format(requestLogs.getStartTimeUsec() / 1000L)
            + "] \""
            + requestLogs.getMethod()
            + " "
            + requestLogs.getResource()
            + " "
            + requestLogs.getHttpVersion()
            + "\" "
            + requestLogs.getStatus()
            + " "
            + requestLogs.getResponseSize()
            + " - \""
            + requestLogs.getUserAgent()
            + "\"");

    //        requestLogs.setOffset();  TODO
    return capedwarfRequestLogs;
  }