private HttpResponse checkLink()

in src/main/java/org/apache/maven/doxia/linkcheck/validation/OnlineHTTPLinkValidator.java [320:419]


    private HttpResponse checkLink( String url, int nbRedirect )
            throws IOException, HttpException
    {

        // check if we've redirected too many times
        int max = MAX_NB_REDIRECT;
        if ( this.http.getHttpClientParameters() != null
            && this.http.getHttpClientParameters().get( "http.protocol.max-redirects" ) != null )
        {
            try
            {
                max = Integer.parseInt(
                        this.http.getHttpClientParameters().get( "http.protocol.max-redirects" ).toString() );
            }
            catch ( NumberFormatException e )
            {
                LOG.warn( "HttpClient parameter '" + "http.protocol.max-redirects"
                        + "' is not a number. Ignoring!" );
            }
        }
        if ( nbRedirect > max )
        {
            throw new HttpException( "Maximum number of redirections (" + max + ") exceeded" );
        }

        HttpUriRequest request;
        try
        {
            if ( "HEAD".equalsIgnoreCase( this.http.getMethod() ) )
            {
                request = new HttpHead( url );
            }
            else if ( "GET".equalsIgnoreCase( this.http.getMethod() ) )
            {
                request = new HttpGet( url );
            }
            else
            {
                LOG.error( "Unsupported method: " + this.http.getMethod() + ", using 'get'." );
                request = new HttpGet( url );
            }
        }
        catch ( IllegalArgumentException ex )
        {
            throw new HttpException( "Invalid URL " + url, ex );
        }

        HttpResponse response = cl.execute( request );

        StatusLine statusLine = response.getStatusLine();
        if ( statusLine == null )
        {
            LOG.error( "Unknown error validating link : " + url );
            return null;
        }

        int statusCode = response.getStatusLine().getStatusCode();
        if ( statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT )
        {
            Header[] locationHeader = response.getHeaders( "location" );

            if ( locationHeader.length == 0 )
            {
                LOG.error( "Site sent redirect, but did not set Location header" );
                return response;
            }

            String newLink = locationHeader[0].getValue();

            // Be careful to absolute/relative links
            if ( !newLink.startsWith( "http://" ) && !newLink.startsWith( "https://" ) )
            {
                if ( newLink.startsWith( "/" ) )
                {
                    URL oldUrl = new URL( url );
                    newLink = oldUrl.getProtocol() + "://" + oldUrl.getHost()
                            + ( oldUrl.getPort() > 0 ? ":" + oldUrl.getPort() : "" ) + newLink;
                }
                else
                {
                    newLink = url + newLink;
                }
            }

            LOG.debug( "[" + url + "] is redirected to [" + newLink + "]" );

            HttpResponse oldResponse = response;
            response = checkLink( newLink, nbRedirect + 1 );

            // Restore the status to "Moved permanently" | "Moved temporarily" | "Temporary redirect"
            // if the new location is found to allow us to report it
            if ( statusCode == HttpStatus.SC_OK && nbRedirect == 0 )
            {
                return oldResponse;
            }
        }
        return response;
    }