001package org.eclipse.aether.internal.impl;
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 java.net.URI;
023import java.net.URISyntaxException;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.List;
027
028import javax.inject.Named;
029
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.artifact.Artifact;
032import org.eclipse.aether.metadata.Metadata;
033import org.eclipse.aether.repository.RemoteRepository;
034import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
035import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
036import org.eclipse.aether.transfer.NoRepositoryLayoutException;
037import org.eclipse.aether.util.ConfigUtils;
038
039/**
040 * Provides a Maven-2 repository layout for repositories with content type {@code "default"}.
041 */
042@Named( "maven2" )
043public final class Maven2RepositoryLayoutFactory
044    implements RepositoryLayoutFactory
045{
046
047    static final String CONFIG_PROP_SIGNATURE_CHECKSUMS = "aether.checksums.forSignature";
048
049    private float priority;
050
051    public float getPriority()
052    {
053        return priority;
054    }
055
056    /**
057     * Sets the priority of this component.
058     * 
059     * @param priority The priority.
060     * @return This component for chaining, never {@code null}.
061     */
062    public Maven2RepositoryLayoutFactory setPriority( float priority )
063    {
064        this.priority = priority;
065        return this;
066    }
067
068    public RepositoryLayout newInstance( RepositorySystemSession session, RemoteRepository repository )
069        throws NoRepositoryLayoutException
070    {
071        if ( !"default".equals( repository.getContentType() ) )
072        {
073            throw new NoRepositoryLayoutException( repository );
074        }
075        boolean forSignature = ConfigUtils.getBoolean( session, false, CONFIG_PROP_SIGNATURE_CHECKSUMS );
076        return forSignature ? Maven2RepositoryLayout.INSTANCE : Maven2RepositoryLayoutEx.INSTANCE;
077    }
078
079    private static class Maven2RepositoryLayout
080        implements RepositoryLayout
081    {
082
083        public static final RepositoryLayout INSTANCE = new Maven2RepositoryLayout();
084
085        private URI toUri( String path )
086        {
087            try
088            {
089                return new URI( null, null, path, null );
090            }
091            catch ( URISyntaxException e )
092            {
093                throw new IllegalStateException( e );
094            }
095        }
096
097        public URI getLocation( Artifact artifact, boolean upload )
098        {
099            StringBuilder path = new StringBuilder( 128 );
100
101            path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
102
103            path.append( artifact.getArtifactId() ).append( '/' );
104
105            path.append( artifact.getBaseVersion() ).append( '/' );
106
107            path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
108
109            if ( artifact.getClassifier().length() > 0 )
110            {
111                path.append( '-' ).append( artifact.getClassifier() );
112            }
113
114            if ( artifact.getExtension().length() > 0 )
115            {
116                path.append( '.' ).append( artifact.getExtension() );
117            }
118
119            return toUri( path.toString() );
120        }
121
122        public URI getLocation( Metadata metadata, boolean upload )
123        {
124            StringBuilder path = new StringBuilder( 128 );
125
126            if ( metadata.getGroupId().length() > 0 )
127            {
128                path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
129
130                if ( metadata.getArtifactId().length() > 0 )
131                {
132                    path.append( metadata.getArtifactId() ).append( '/' );
133
134                    if ( metadata.getVersion().length() > 0 )
135                    {
136                        path.append( metadata.getVersion() ).append( '/' );
137                    }
138                }
139            }
140
141            path.append( metadata.getType() );
142
143            return toUri( path.toString() );
144        }
145
146        public List<Checksum> getChecksums( Artifact artifact, boolean upload, URI location )
147        {
148            return getChecksums( location );
149        }
150
151        public List<Checksum> getChecksums( Metadata metadata, boolean upload, URI location )
152        {
153            return getChecksums( location );
154        }
155
156        private List<Checksum> getChecksums( URI location )
157        {
158            return Arrays.asList( Checksum.forLocation( location, "SHA-1" ), Checksum.forLocation( location, "MD5" ) );
159        }
160
161    }
162
163    private static class Maven2RepositoryLayoutEx
164        extends Maven2RepositoryLayout
165    {
166
167        public static final RepositoryLayout INSTANCE = new Maven2RepositoryLayoutEx();
168
169        @Override
170        public List<Checksum> getChecksums( Artifact artifact, boolean upload, URI location )
171        {
172            if ( isSignature( artifact.getExtension() ) )
173            {
174                return Collections.emptyList();
175            }
176            return super.getChecksums( artifact, upload, location );
177        }
178
179        private boolean isSignature( String extension )
180        {
181            return extension.endsWith( ".asc" );
182        }
183
184    }
185
186}