001    package org.apache.archiva.repository.content.legacy;
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    
022    import org.apache.archiva.model.ArchivaArtifact;
023    import org.apache.archiva.model.ArtifactReference;
024    import org.apache.archiva.repository.content.PathParser;
025    import org.apache.archiva.repository.content.maven2.ArtifactExtensionMapping;
026    import org.apache.archiva.repository.layout.LayoutException;
027    import org.apache.commons.lang.StringUtils;
028    
029    import javax.inject.Inject;
030    import javax.inject.Named;
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    /**
035     * AbstractLegacyRepositoryContent
036     *
037     *
038     */
039    public abstract class AbstractLegacyRepositoryContent
040    {
041        private static final String PATH_SEPARATOR = "/";
042    
043        private static final Map<String, String> typeToDirectoryMap;
044    
045        static
046        {
047            typeToDirectoryMap = new HashMap<String, String>();
048            typeToDirectoryMap.put( "ejb-client", "ejb" );
049            typeToDirectoryMap.put( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN, "plugin" );
050            typeToDirectoryMap.put( "distribution-tgz", "distribution" );
051            typeToDirectoryMap.put( "distribution-zip", "distribution" );
052            typeToDirectoryMap.put( "javadoc", "javadoc.jar" );
053        }
054    
055        /**
056         *
057         */
058        @Inject
059        @Named( value = "pathParser#legacy" )
060        private PathParser legacyPathParser;
061    
062        public ArtifactReference toArtifactReference( String path )
063            throws LayoutException
064        {
065            return legacyPathParser.toArtifactReference( path );
066        }
067    
068        public String toPath( ArchivaArtifact reference )
069        {
070            if ( reference == null )
071            {
072                throw new IllegalArgumentException( "Artifact reference cannot be null" );
073            }
074    
075            return toPath( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(),
076                           reference.getClassifier(), reference.getType() );
077        }
078    
079        public String toPath( ArtifactReference reference )
080        {
081            if ( reference == null )
082            {
083                throw new IllegalArgumentException( "Artifact reference cannot be null" );
084            }
085    
086            return toPath( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(),
087                           reference.getClassifier(), reference.getType() );
088        }
089    
090        private String toPath( String groupId, String artifactId, String version, String classifier, String type )
091        {
092            StringBuilder path = new StringBuilder();
093    
094            path.append( groupId ).append( PATH_SEPARATOR );
095            path.append( getDirectory( type ) ).append( PATH_SEPARATOR );
096    
097            if ( version != null )
098            {
099                path.append( artifactId ).append( '-' ).append( version );
100    
101                if ( StringUtils.isNotBlank( classifier ) )
102                {
103                    path.append( '-' ).append( classifier );
104                }
105    
106                path.append( '.' ).append( ArtifactExtensionMapping.getExtension( type ) );
107            }
108    
109            return path.toString();
110        }
111    
112        private String getDirectory( String type )
113        {
114            String dirname = typeToDirectoryMap.get( type );
115    
116            if ( dirname != null )
117            {
118                return dirname + "s";
119            }
120    
121            // Default process.
122            return type + "s";
123        }
124    
125        public void setLegacyPathParser( PathParser parser )
126        {
127            this.legacyPathParser = parser;
128        }
129    }