Package net.sf.collabreview.web

Source Code of net.sf.collabreview.web.AccountingInfoServlet

/*
   Copyright 2012 Christian Prause and Fraunhofer FIT

   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 net.sf.collabreview.web;

import net.sf.collabreview.core.CollabReviewSingleton;
import net.sf.collabreview.reputation.ReputationMetric;
import net.sf.collabreview.reputation.ReputationMetricManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Collection;

/**
* Interfaces for communication between a CollabReview server and the CollabReview accounting server.
*
* @author Christian Prause (chris)
* @date 2009-10-12 16:57:48
*/
public class AccountingInfoServlet extends HttpServlet {
  /**
   * Apache commons logging logger for class AccountingInfoServlet.
   */
  private static final Log logger = LogFactory.getLog(AccountingInfoServlet.class);

  @Override
  protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    processRequest(httpServletRequest, httpServletResponse);
  }

  private void processRequest(HttpServletRequest request, HttpServletResponse response) {
    // TODO check accounting server credentials (password, IP, ...)
    String method = request.getParameter("method");
    try {
      Document document = DocumentHelper.createDocument();
      Element root = document.addElement("accounting");
      addHeadInfo(root, request);
      if ("getOverallStats".equals(method)) {
        getOverallStats(root);
      } else {
        throw new IllegalArgumentException("Unknown method: " + method);
      }
      OutputFormat format = OutputFormat.createPrettyPrint();
      //format.setTrimText(false); // must do this because dom4j 1.x has an error that removes whitespace from text between two tags
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      XMLWriter output = new XMLWriter(new OutputStreamWriter(baos), format);
      output.write(document);
      output.close();
      response.setContentType("text/xml");
      response.setStatus(HttpServletResponse.SC_OK);
      response.setContentLength(baos.size());
      response.setCharacterEncoding(format.getEncoding());
      response.getOutputStream().write(baos.toByteArray());
    } catch (Exception e) {
      logger.error("An exception occurred when handling an accounting information request", e);
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
  }

  private void addHeadInfo(Element root, HttpServletRequest request) {
    Element head = root.addElement("head");
    head.addElement("protocol").addElement("version").addText("1.0");
    head.addElement("server").addElement("name").addText(request.getServerName());
  }

  private void getOverallStats(Element root) {
    ReputationMetricManager reputationMetricManager = CollabReviewSingleton.get().getReputationMetricManager();
    Element scores = root.addElement("totals");
    Collection<String> contributors = reputationMetricManager.listAllContributors();
    if (!reputationMetricManager.isUptodate()) {
      root.element("head").addElement("warning").addAttribute("info", "out-of-date").addText("Some of the metric results were cached and are possibly out of date.");
    }
    for (String authorName : contributors) {
      Element element = scores.addElement("contributor");
      element.addAttribute("name", authorName);
      for (ReputationMetric reputationMetric : reputationMetricManager.listReputationMetrics()) {
        Float score = reputationMetric.getAuthorScore(authorName);
        if (score == null) {
          score = 0f;
        }
        Element scoreElement = element.addElement("score");
        scoreElement.addAttribute("type", reputationMetric.getName());
        scoreElement.addText(String.format("%.1f", score));
        if (!reputationMetric.isUpToDate()) {
          scoreElement.addAttribute("warning", "out-of-date");
        }
      }
    }
  }
}
TOP

Related Classes of net.sf.collabreview.web.AccountingInfoServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.