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.archiva.repository.storage.StorageAsset;
024import org.apache.commons.lang3.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.springframework.stereotype.Service;
028
029import javax.inject.Inject;
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    extends AbstractPolicy implements PreDownloadPolicy
040{
041    private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
042    private static final String ID = "cache-failures";
043
044    /**
045     * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.
046     * All resource requests are allowed thru to the remote repo.
047     */
048    public static final StandardOption NO = StandardOption.NO;
049
050    /**
051     * The YES policy setting means that the existence of old failures is checked, and will
052     * prevent the request from being performed against the remote repo.
053     */
054    public static final StandardOption YES = StandardOption.YES;
055
056    @Inject
057    private UrlFailureCache urlFailureCache;
058
059    private List<PolicyOption> options = new ArrayList<>( 2 );
060
061    public CachedFailuresPolicy()
062    {
063        super();
064        options.add( NO );
065        options.add( YES );
066    }
067
068    @Override
069    public void applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
070        throws PolicyViolationException, PolicyConfigurationException
071    {
072        if ( !options.contains( policySetting ) )
073        {
074            // Not a valid code.
075            throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
076                                                        "], valid settings are [" + StringUtils.join(
077                options.iterator(), "," ) + "]" );
078        }
079
080        if ( NO.equals( policySetting ) )
081        {
082            // Skip.
083            log.debug( "OK to fetch, check-failures policy set to NO." );
084            return;
085        }
086
087        String url = request.getProperty( "url" );
088
089        if ( StringUtils.isNotBlank( url ) )
090        {
091            if ( urlFailureCache.hasFailedBefore( url ) )
092            {
093                throw new PolicyViolationException(
094                    "NO to fetch, check-failures detected previous failure on url: " + url );
095            }
096        }
097
098        log.debug( "OK to fetch, check-failures detected no issues." );
099    }
100
101    @Override
102    public PolicyOption getDefaultOption()
103    {
104        return NO;
105    }
106
107    @Override
108    public String getId()
109    {
110        return ID;
111    }
112
113
114    @Override
115    public List<PolicyOption> getOptions()
116    {
117        return options;
118    }
119}