This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.policies;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.policies.urlcache.UrlFailureCache;
023import org.apache.commons.lang.StringUtils;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.stereotype.Service;
027
028import javax.inject.Inject;
029import java.io.File;
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Properties;
033
034/**
035 * {@link PreDownloadPolicy} to check if the requested url has failed before.
036 */
037@Service( "preDownloadPolicy#cache-failures" )
038public class CachedFailuresPolicy
039    implements PreDownloadPolicy
040{
041    private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
042
043    /**
044     * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.
045     * All resource requests are allowed thru to the remote repo.
046     */
047    public static final String NO = "no";
048
049    /**
050     * The YES policy setting means that the existence of old failures is checked, and will
051     * prevent the request from being performed against the remote repo.
052     */
053    public static final String YES = "yes";
054
055    @Inject
056    private UrlFailureCache urlFailureCache;
057
058    private List<String> options = new ArrayList<>( 2 );
059
060    public CachedFailuresPolicy()
061    {
062        options.add( NO );
063        options.add( YES );
064    }
065
066    @Override
067    public void applyPolicy( String policySetting, Properties request, File localFile )
068        throws PolicyViolationException, PolicyConfigurationException
069    {
070        if ( !options.contains( policySetting ) )
071        {
072            // Not a valid code.
073            throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
074                                                        "], valid settings are [" + StringUtils.join(
075                options.iterator(), "," ) + "]" );
076        }
077
078        if ( NO.equals( policySetting ) )
079        {
080            // Skip.
081            log.debug( "OK to fetch, check-failures policy set to NO." );
082            return;
083        }
084
085        String url = request.getProperty( "url" );
086
087        if ( StringUtils.isNotBlank( url ) )
088        {
089            if ( urlFailureCache.hasFailedBefore( url ) )
090            {
091                throw new PolicyViolationException(
092                    "NO to fetch, check-failures detected previous failure on url: " + url );
093            }
094        }
095
096        log.debug( "OK to fetch, check-failures detected no issues." );
097    }
098
099    @Override
100    public String getDefaultOption()
101    {
102        return NO;
103    }
104
105    @Override
106    public String getId()
107    {
108        return "cache-failures";
109    }
110
111    @Override
112    public String getName()
113    {
114        return "Cache failures";
115    }
116
117    @Override
118    public List<String> getOptions()
119    {
120        return options;
121    }
122}