001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.spi.connector;
020
021import java.io.File;
022import java.util.Collections;
023import java.util.List;
024
025import org.eclipse.aether.RequestTrace;
026import org.eclipse.aether.metadata.Metadata;
027import org.eclipse.aether.repository.RemoteRepository;
028import org.eclipse.aether.transfer.MetadataTransferException;
029import org.eclipse.aether.transfer.TransferListener;
030
031/**
032 * A download of metadata from a remote repository. A repository connector processing this download has to use
033 * {@link #setException(MetadataTransferException)} to report the results of the transfer.
034 */
035public final class MetadataDownload extends MetadataTransfer {
036
037    private String checksumPolicy = "";
038
039    private String context = "";
040
041    private List<RemoteRepository> repositories = Collections.emptyList();
042
043    /**
044     * Creates a new uninitialized download.
045     */
046    public MetadataDownload() {
047        // enables default constructor
048    }
049
050    /**
051     * Creates a new download with the specified properties.
052     *
053     * @param metadata The metadata to download, may be {@code null}.
054     * @param context The context in which this download is performed, may be {@code null}.
055     * @param file The local file to download the metadata to, may be {@code null}.
056     * @param checksumPolicy The checksum policy, may be {@code null}.
057     */
058    public MetadataDownload(Metadata metadata, String context, File file, String checksumPolicy) {
059        setMetadata(metadata);
060        setFile(file);
061        setChecksumPolicy(checksumPolicy);
062        setRequestContext(context);
063    }
064
065    @Override
066    public MetadataDownload setMetadata(Metadata metadata) {
067        super.setMetadata(metadata);
068        return this;
069    }
070
071    @Override
072    public MetadataDownload setFile(File file) {
073        super.setFile(file);
074        return this;
075    }
076
077    /**
078     * Gets the checksum policy for this transfer.
079     *
080     * @return The checksum policy, never {@code null}.
081     */
082    public String getChecksumPolicy() {
083        return checksumPolicy;
084    }
085
086    /**
087     * Sets the checksum policy for this transfer.
088     *
089     * @param checksumPolicy The checksum policy, may be {@code null}.
090     * @return This transfer for chaining, never {@code null}.
091     */
092    public MetadataDownload setChecksumPolicy(String checksumPolicy) {
093        this.checksumPolicy = (checksumPolicy != null) ? checksumPolicy : "";
094        return this;
095    }
096
097    /**
098     * Gets the context of this transfer.
099     *
100     * @return The context id, never {@code null}.
101     */
102    public String getRequestContext() {
103        return context;
104    }
105
106    /**
107     * Sets the request context of this transfer.
108     *
109     * @param context The context id, may be {@code null}.
110     * @return This transfer for chaining, never {@code null}.
111     */
112    public MetadataDownload setRequestContext(String context) {
113        this.context = (context != null) ? context : "";
114        return this;
115    }
116
117    /**
118     * Gets the remote repositories that are being aggregated by the physically contacted remote repository (i.e. a
119     * repository manager).
120     *
121     * @return The remote repositories being aggregated, never {@code null}.
122     */
123    public List<RemoteRepository> getRepositories() {
124        return repositories;
125    }
126
127    /**
128     * Sets the remote repositories that are being aggregated by the physically contacted remote repository (i.e. a
129     * repository manager).
130     *
131     * @param repositories The remote repositories being aggregated, may be {@code null}.
132     * @return This transfer for chaining, never {@code null}.
133     */
134    public MetadataDownload setRepositories(List<RemoteRepository> repositories) {
135        if (repositories == null) {
136            this.repositories = Collections.emptyList();
137        } else {
138            this.repositories = repositories;
139        }
140        return this;
141    }
142
143    @Override
144    public MetadataDownload setException(MetadataTransferException exception) {
145        super.setException(exception);
146        return this;
147    }
148
149    @Override
150    public MetadataDownload setListener(TransferListener listener) {
151        super.setListener(listener);
152        return this;
153    }
154
155    @Override
156    public MetadataDownload setTrace(RequestTrace trace) {
157        super.setTrace(trace);
158        return this;
159    }
160
161    @Override
162    public String toString() {
163        return getMetadata() + " - " + getFile();
164    }
165}