<commit_before>package org.commonjava.aprox.depgraph.rest.resolve;

import static org.commonjava.aprox.depgraph.util.RequestUtils.getBooleanParamWithDefault;
import static org.commonjava.aprox.depgraph.util.RequestUtils.getLongParamWithDefault;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.commonjava.aprox.depgraph.inject.DepgraphSpecific;
import org.commonjava.aprox.depgraph.util.RequestAdvisor;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.cartographer.agg.AggregationOptions;
import org.commonjava.maven.cartographer.agg.DefaultAggregatorOptions;
import org.commonjava.maven.cartographer.data.CartoDataException;
import org.commonjava.maven.cartographer.discover.DefaultDiscoveryConfig;
import org.commonjava.maven.cartographer.discover.DiscoverySourceManager;
import org.commonjava.maven.cartographer.ops.ResolveOps;
import org.commonjava.util.logging.Logger;
import org.commonjava.web.json.ser.JsonSerializer;

@Path( "/depgraph/resolve/{from: (.+)}" )
@Produces( MediaType.APPLICATION_JSON )
@RequestScoped
public class ResolverResource
{

    private final Logger logger = new Logger( getClass() );

    @Inject
    private ResolveOps ops;

    @Inject
    @DepgraphSpecific
    private JsonSerializer serializer;

    @Inject
    private DiscoverySourceManager sourceManager;

    @Inject
    private RequestAdvisor requestAdvisor;

    @Path( "/{g}/{a}/{v}" )
    @GET
    public Response resolveGraph( @PathParam( "from" ) final String from, @PathParam( "g" ) final String groupId,
                                  @PathParam( "a" ) final String artifactId, @PathParam( "v" ) final String version,
                                  @Context final HttpServletRequest request,
                                  @QueryParam( "recurse" ) @DefaultValue( "true" ) final boolean recurse )
    {
        Response response = Response.status( Status.NO_CONTENT )
                                    .build();

        final URI source = sourceManager.createSourceURI( from );
        if ( source == null )
        {
            final String message =
                String.format( "Invalid source format: '%s'. Use the form: '%s' instead.", from,
                               sourceManager.getFormatHint() );
            logger.warn( message );
            response = Response.status( Status.BAD_REQUEST )
                               .entity( message )
                               .build();
            return response;
        }

        final ProjectVersionRef ref = new ProjectVersionRef( groupId, artifactId, version );
        final AggregationOptions options = createAggregationOptions( request, source );

        List<ProjectVersionRef> resolved;
        try
        {
            resolved = ops.resolve( from, options, ref );

            final String json = serializer.toString( Collections.singletonMap( "resolvedTopLevelGAVs", resolved ) );
            response = Response.ok( json )
                               .build();

        }
        catch ( final CartoDataException e )
        {
            logger.error( "Failed to resolve graph: %s from: %s. Reason: %s", e, ref, from, e.getMessage() );

            response = Response.serverError()
                               .build();<commit_msg>Fixes #40. Always return the resolved top-level GAV even if it's already been resolved...unless there's some error condition, of course.<commit_after>package org.commonjava.aprox.depgraph.rest.resolve;

import static org.commonjava.aprox.depgraph.util.RequestUtils.getBooleanParamWithDefault;
import static org.commonjava.aprox.depgraph.util.RequestUtils.getLongParamWithDefault;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.commonjava.aprox.depgraph.inject.DepgraphSpecific;
import org.commonjava.aprox.depgraph.util.RequestAdvisor;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.cartographer.agg.AggregationOptions;
import org.commonjava.maven.cartographer.agg.DefaultAggregatorOptions;
import org.commonjava.maven.cartographer.data.CartoDataException;
import org.commonjava.maven.cartographer.discover.DefaultDiscoveryConfig;
import org.commonjava.maven.cartographer.discover.DiscoverySourceManager;
import org.commonjava.maven.cartographer.ops.ResolveOps;
import org.commonjava.util.logging.Logger;
import org.commonjava.web.json.ser.JsonSerializer;

@Path( "/depgraph/resolve/{from: (.+)}" )
@Produces( MediaType.APPLICATION_JSON )
@RequestScoped
public class ResolverResource
{

    private final Logger logger = new Logger( getClass() );

    @Inject
    private ResolveOps ops;

    @Inject
    @DepgraphSpecific
    private JsonSerializer serializer;

    @Inject
    private DiscoverySourceManager sourceManager;

    @Inject
    private RequestAdvisor requestAdvisor;

    @Path( "/{g}/{a}/{v}" )
    @GET
    public Response resolveGraph( @PathParam( "from" ) final String from, @PathParam( "g" ) final String groupId,
                                  @PathParam( "a" ) final String artifactId, @PathParam( "v" ) final String version,
                                  @Context final HttpServletRequest request,
                                  @QueryParam( "recurse" ) @DefaultValue( "true" ) final boolean recurse )
    {
        Response response = Response.status( Status.NO_CONTENT )
                                    .build();

        final URI source = sourceManager.createSourceURI( from );
        if ( source == null )
        {
            final String message =
                String.format( "Invalid source format: '%s'. Use the form: '%s' instead.", from,
                               sourceManager.getFormatHint() );
            logger.warn( message );
            response = Response.status( Status.BAD_REQUEST )
                               .entity( message )
                               .build();
            return response;
        }

        final ProjectVersionRef ref = new ProjectVersionRef( groupId, artifactId, version );
        final AggregationOptions options = createAggregationOptions( request, source );

        List<ProjectVersionRef> resolved;
        try
        {
            resolved = ops.resolve( from, options, ref );
            if ( resolved == null || resolved.isEmpty() )
            {
                resolved = Collections.singletonList( ref );
            }

            final String json = serializer.toString( Collections.singletonMap( "resolvedTopLevelGAVs", resolved ) );
            response = Response.ok( json )
                               .build();

        }
        catch ( final CartoDataException e )
        {
            logger.error( "Failed to resolve graph: %s from: %s. Reason: %s", e, ref, from, e.getMessage() );

            response = Response.serverError()
                               .build();
{'fraction_non_alphanumeric': 0.10002525890376357, 'fraction_numerical': 0.00025258903763576663, 'mean_word_length': 2.6918414918414917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6127091', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2117, 'n_words': 479}