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.resolution;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.eclipse.aether.RepositorySystem;
026import org.eclipse.aether.RequestTrace;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.repository.RemoteRepository;
029
030/**
031 * A request to read an artifact descriptor.
032 *
033 * @see RepositorySystem#readArtifactDescriptor(org.eclipse.aether.RepositorySystemSession, ArtifactDescriptorRequest)
034 */
035public final class ArtifactDescriptorRequest {
036
037    private Artifact artifact;
038
039    private List<RemoteRepository> repositories = Collections.emptyList();
040
041    private String context = "";
042
043    private RequestTrace trace;
044
045    /**
046     * Creates an uninitialized request.
047     */
048    public ArtifactDescriptorRequest() {
049        // enables default constructor
050    }
051
052    /**
053     * Creates a request with the specified properties.
054     *
055     * @param artifact The artifact whose descriptor should be read, may be {@code null}.
056     * @param repositories The repositories to resolve the descriptor from, may be {@code null}.
057     * @param context The context in which this request is made, may be {@code null}.
058     */
059    public ArtifactDescriptorRequest(Artifact artifact, List<RemoteRepository> repositories, String context) {
060        setArtifact(artifact);
061        setRepositories(repositories);
062        setRequestContext(context);
063    }
064
065    /**
066     * Gets the artifact whose descriptor shall be read.
067     *
068     * @return The artifact or {@code null} if not set.
069     */
070    public Artifact getArtifact() {
071        return artifact;
072    }
073
074    /**
075     * Sets the artifact whose descriptor shall be read. Eventually, a valid request must have an artifact set.
076     *
077     * @param artifact The artifact, may be {@code null}.
078     * @return This request for chaining, never {@code null}.
079     */
080    public ArtifactDescriptorRequest setArtifact(Artifact artifact) {
081        this.artifact = artifact;
082        return this;
083    }
084
085    /**
086     * Gets the repositories to resolve the descriptor from.
087     *
088     * @return The repositories, never {@code null}.
089     */
090    public List<RemoteRepository> getRepositories() {
091        return repositories;
092    }
093
094    /**
095     * Sets the repositories to resolve the descriptor from.
096     *
097     * @param repositories The repositories, may be {@code null}.
098     * @return This request for chaining, never {@code null}.
099     */
100    public ArtifactDescriptorRequest setRepositories(List<RemoteRepository> repositories) {
101        if (repositories == null) {
102            this.repositories = Collections.emptyList();
103        } else {
104            this.repositories = repositories;
105        }
106        return this;
107    }
108
109    /**
110     * Adds the specified repository for the resolution of the artifact descriptor.
111     *
112     * @param repository The repository to add, may be {@code null}.
113     * @return This request for chaining, never {@code null}.
114     */
115    public ArtifactDescriptorRequest addRepository(RemoteRepository repository) {
116        if (repository != null) {
117            if (this.repositories.isEmpty()) {
118                this.repositories = new ArrayList<>();
119            }
120            this.repositories.add(repository);
121        }
122        return this;
123    }
124
125    /**
126     * Gets the context in which this request is made.
127     *
128     * @return The context, never {@code null}.
129     */
130    public String getRequestContext() {
131        return context;
132    }
133
134    /**
135     * Sets the context in which this request is made.
136     *
137     * @param context The context, may be {@code null}.
138     * @return This request for chaining, never {@code null}.
139     */
140    public ArtifactDescriptorRequest setRequestContext(String context) {
141        this.context = (context != null) ? context : "";
142        return this;
143    }
144
145    /**
146     * Gets the trace information that describes the higher level request/operation in which this request is issued.
147     *
148     * @return The trace information about the higher level operation or {@code null} if none.
149     */
150    public RequestTrace getTrace() {
151        return trace;
152    }
153
154    /**
155     * Sets the trace information that describes the higher level request/operation in which this request is issued.
156     *
157     * @param trace The trace information about the higher level operation, may be {@code null}.
158     * @return This request for chaining, never {@code null}.
159     */
160    public ArtifactDescriptorRequest setTrace(RequestTrace trace) {
161        this.trace = trace;
162        return this;
163    }
164
165    @Override
166    public String toString() {
167        return getArtifact() + " < " + getRepositories();
168    }
169}