<commit_before>/*
 * Copyright 1998-2009 University Corporation for Atmospheric Research/Unidata
 *
 * Portions of this software were developed by the Unidata Program at the
 * University Corporation for Atmospheric Research.
 *
 * Access and use of this software shall impose the following obligations
 * and understandings on the user. The user is granted the right, without
 * any fee or cost, to use, copy, modify, alter, enhance and distribute
 * this software, and any derivative works thereof, and its supporting
 * documentation for any purpose whatsoever, provided that this entire
 * notice appears in all copies of the software, derivative works and
 * supporting documentation.  Further, UCAR requests that the user credit
 * UCAR/Unidata in any publications that result from the use of this
 * software or in any product that includes this software. The names UCAR
 * and/or Unidata, however, may not be used in any advertising or publicity
 * to endorse or promote any products or commercial entity unless specific
 * written permission is obtained from UCAR/Unidata. The user also
 * understands that UCAR/Unidata is not obligated to provide the user with
 * any support, consulting, training or assistance of any kind with regard
 * to the use, operation and performance of this software nor to provide
 * the user with any updates, revisions, new versions or "bug fixes."
 *
 * THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package thredds.dqc.server.latest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.net.URL;

import thredds.catalog.*;
import thredds.catalog.parser.jdom.InvCatalogFactory10;
import thredds.catalog.query.*;
import thredds.servlet.ServletUtil;
import thredds.servlet.UsageLog;
import thredds.dqc.server.DqcHandler;
import ucar.nc2.constants.FeatureType;

/**
 * _more_
 *
 * @author edavis
 * @since Sep 21, 2005 9:12:56 PM
 */
public class LatestDqcHandler extends DqcHandler

{
  private static org.slf4j.Logger log =
          org.slf4j.LoggerFactory.getLogger( LatestDqcHandler.class );

  protected LatestConfig config;

  /**
   * Default constructor.
   */
  public LatestDqcHandler()
  {
  }

  /**
   * @param req
   * @param res
   * @throws java.io.IOException            if an input or output error is detected while the servlet handles the GET request.
   * @throws javax.servlet.ServletException if the request can't be handled for some reason.
   */
  public void handleRequest( HttpServletRequest req, HttpServletResponse res )
          throws IOException, ServletException
  {
    // Get request path.
    String reqPath = req.getPathInfo();
    // Get path after this handler's name.
    String extraPath = reqPath.substring( this.getHandlerInfo().getName().length() + 1 );

    // Deal with request for DQC document.
    if ( extraPath.equals( ".xml" ) )
    {
      // Create the DQC document.
      QueryCapability dqc = createDqcDocument( req.getContextPath() + req.getServletPath() + "/" + this.getHandlerInfo().getName() );

      // Write DQC doc as response
      DqcFactory dqcFactory = new DqcFactory( false );
      String dqcAsString = dqcFactory.writeXML( dqc );
      PrintWriter out = res.getWriter();
      res.setContentType( "text/xml" );
      res.setStatus( HttpServletResponse.SC_OK );
      out.print( dqcAsString );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_OK, dqcAsString.length() ));
      return;
    }

    // If not a request for DQC document, should not be any extra path info.
    if ( extraPath.length() > 0 )
    {
      String tmpMsg = "Extra path information <" + extraPath + "> not understood.";
      log.error( "handleRequest(): " + tmpMsg );
      res.sendError( HttpServletResponse.SC_BAD_REQUEST, tmpMsg );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_BAD_REQUEST, 0 ));
      return;
    }

    // Determine what LatestConfig.Item was requested.
    String reqItemId = req.getQueryString();
    if ( reqItemId == null )
    {
      // No LatestConfig.Item ID was given in request.
      String tmpMsg = "No latest request ID given.";
      log.error( "handleRequest(): " + tmpMsg );
      res.sendError( HttpServletResponse.SC_NOT_FOUND,
                     "LatestDqcHandler.handleRequest(): " + tmpMsg );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_NOT_FOUND, 0 ));
      return;
    }
    log.debug( "Request for the latest \"" + reqItemId + "\"." );

    // Determine if the requested Item is supported.
    LatestConfig.Item reqItem = this.config.getItem( reqItemId );
    if ( reqItem == null )
    {
      String tmpMsg = "The Item requested, " + reqItemId + ", is not supported.";
      log.error( "handleRequest(): " + tmpMsg );
      res.sendError( HttpServletResponse.SC_NOT_FOUND,
                     "LatestDqcHandler.handleRequest(): " + tmpMsg );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_NOT_FOUND, 0 ));
      return;
    }

    // Check that directory exists
    String reqItemName = reqItem.getName();
    File dir = new File( reqItem.getDirLocation() );
    log.debug( "handleRequest(): requested Item <id=" + reqItemId +
               ", name=" + reqItemName + ", dir=" + dir.getPath() + "> is supported." );
    if ( !dir.isDirectory() )
    {
      // The dataset location is not a directory.
      log.error( "handleRequest(): Directory <" + dir.getPath() +
                 "> given by configuration item <id=" + reqItemId +
                 "> is not a directory." );
      res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,<commit_msg>reduce message from error to info<commit_after>/*
 * Copyright 1998-2009 University Corporation for Atmospheric Research/Unidata
 *
 * Portions of this software were developed by the Unidata Program at the
 * University Corporation for Atmospheric Research.
 *
 * Access and use of this software shall impose the following obligations
 * and understandings on the user. The user is granted the right, without
 * any fee or cost, to use, copy, modify, alter, enhance and distribute
 * this software, and any derivative works thereof, and its supporting
 * documentation for any purpose whatsoever, provided that this entire
 * notice appears in all copies of the software, derivative works and
 * supporting documentation.  Further, UCAR requests that the user credit
 * UCAR/Unidata in any publications that result from the use of this
 * software or in any product that includes this software. The names UCAR
 * and/or Unidata, however, may not be used in any advertising or publicity
 * to endorse or promote any products or commercial entity unless specific
 * written permission is obtained from UCAR/Unidata. The user also
 * understands that UCAR/Unidata is not obligated to provide the user with
 * any support, consulting, training or assistance of any kind with regard
 * to the use, operation and performance of this software nor to provide
 * the user with any updates, revisions, new versions or "bug fixes."
 *
 * THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package thredds.dqc.server.latest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.net.URL;

import thredds.catalog.*;
import thredds.catalog.parser.jdom.InvCatalogFactory10;
import thredds.catalog.query.*;
import thredds.servlet.ServletUtil;
import thredds.servlet.UsageLog;
import thredds.dqc.server.DqcHandler;
import ucar.nc2.constants.FeatureType;

/**
 * _more_
 *
 * @author edavis
 * @since Sep 21, 2005 9:12:56 PM
 */
public class LatestDqcHandler extends DqcHandler

{
  private static org.slf4j.Logger log =
          org.slf4j.LoggerFactory.getLogger( LatestDqcHandler.class );

  protected LatestConfig config;

  /**
   * Default constructor.
   */
  public LatestDqcHandler()
  {
  }

  /**
   * @param req
   * @param res
   * @throws java.io.IOException            if an input or output error is detected while the servlet handles the GET request.
   * @throws javax.servlet.ServletException if the request can't be handled for some reason.
   */
  public void handleRequest( HttpServletRequest req, HttpServletResponse res )
          throws IOException, ServletException
  {
    // Get request path.
    String reqPath = req.getPathInfo();
    // Get path after this handler's name.
    String extraPath = reqPath.substring( this.getHandlerInfo().getName().length() + 1 );

    // Deal with request for DQC document.
    if ( extraPath.equals( ".xml" ) )
    {
      // Create the DQC document.
      QueryCapability dqc = createDqcDocument( req.getContextPath() + req.getServletPath() + "/" + this.getHandlerInfo().getName() );

      // Write DQC doc as response
      DqcFactory dqcFactory = new DqcFactory( false );
      String dqcAsString = dqcFactory.writeXML( dqc );
      PrintWriter out = res.getWriter();
      res.setContentType( "text/xml" );
      res.setStatus( HttpServletResponse.SC_OK );
      out.print( dqcAsString );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_OK, dqcAsString.length() ));
      return;
    }

    // If not a request for DQC document, should not be any extra path info.
    if ( extraPath.length() > 0 )
    {
      String tmpMsg = "Extra path information <" + extraPath + "> not understood.";
      log.error( "handleRequest(): " + tmpMsg );
      res.sendError( HttpServletResponse.SC_BAD_REQUEST, tmpMsg );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_BAD_REQUEST, 0 ));
      return;
    }

    // Determine what LatestConfig.Item was requested.
    String reqItemId = req.getQueryString();
    if ( reqItemId == null )
    {
      // No LatestConfig.Item ID was given in request.
      String tmpMsg = "No latest request ID given.";
      log.info( "handleRequest(): " + tmpMsg );
      res.sendError( HttpServletResponse.SC_NOT_FOUND,
                     "LatestDqcHandler.handleRequest(): " + tmpMsg );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_NOT_FOUND, 0 ));
      return;
    }
    log.debug( "Request for the latest \"" + reqItemId + "\"." );

    // Determine if the requested Item is supported.
    LatestConfig.Item reqItem = this.config.getItem( reqItemId );
    if ( reqItem == null )
    {
      String tmpMsg = "The Item requested, " + reqItemId + ", is not supported.";
      log.error( "handleRequest(): " + tmpMsg );
      res.sendError( HttpServletResponse.SC_NOT_FOUND,
                     "LatestDqcHandler.handleRequest(): " + tmpMsg );
      log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_NOT_FOUND, 0 ));
      return;
    }

    // Check that directory exists
    String reqItemName = reqItem.getName();
    File dir = new File( reqItem.getDirLocation() );
    log.debug( "handleRequest(): requested Item <id=" + reqItemId +
               ", name=" + reqItemName + ", dir=" + dir.getPath() + "> is supported." );
    if ( !dir.isDirectory() )
    {
      // The dataset location is not a directory.
      log.error( "handleRequest(): Directory <" + dir.getPath() +
                 "> given by configuration item <id=" + reqItemId +
                 "> is not a directory." );
      res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
{'fraction_non_alphanumeric': 0.09176044699771779, 'fraction_numerical': 0.004564413315495396, 'mean_word_length': 3.349075975359343, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5217313', 'n_tokens_mistral': 3654, 'n_tokens_neox': 3449, 'n_words': 1226}