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.graph.DependencyNode;
029import org.eclipse.aether.repository.RemoteRepository;
030
031/**
032 * A request to resolve an artifact.
033 *
034 * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
035 * @see Artifact#getFile()
036 */
037public final class ArtifactRequest {
038
039    private Artifact artifact;
040
041    private DependencyNode node;
042
043    private List<RemoteRepository> repositories = Collections.emptyList();
044
045    private String context = "";
046
047    private RequestTrace trace;
048
049    /**
050     * Creates an uninitialized request.
051     */
052    public ArtifactRequest() {
053        // enables default constructor
054    }
055
056    /**
057     * Creates a request with the specified properties.
058     *
059     * @param artifact The artifact to resolve, may be {@code null}.
060     * @param repositories The repositories to resolve the artifact from, may be {@code null}.
061     * @param context The context in which this request is made, may be {@code null}.
062     */
063    public ArtifactRequest(Artifact artifact, List<RemoteRepository> repositories, String context) {
064        setArtifact(artifact);
065        setRepositories(repositories);
066        setRequestContext(context);
067    }
068
069    /**
070     * Creates a request from the specified dependency node.
071     *
072     * @param node The dependency node to resolve, may be {@code null}.
073     */
074    public ArtifactRequest(DependencyNode node) {
075        setDependencyNode(node);
076        setRepositories(node.getRepositories());
077        setRequestContext(node.getRequestContext());
078    }
079
080    /**
081     * Gets the artifact to resolve.
082     *
083     * @return The artifact to resolve or {@code null}.
084     */
085    public Artifact getArtifact() {
086        return artifact;
087    }
088
089    /**
090     * Sets the artifact to resolve.
091     *
092     * @param artifact The artifact to resolve, may be {@code null}.
093     * @return This request for chaining, never {@code null}.
094     */
095    public ArtifactRequest setArtifact(Artifact artifact) {
096        this.artifact = artifact;
097        return this;
098    }
099
100    /**
101     * Gets the dependency node (if any) for which to resolve the artifact.
102     *
103     * @return The dependency node to resolve or {@code null} if unknown.
104     */
105    public DependencyNode getDependencyNode() {
106        return node;
107    }
108
109    /**
110     * Sets the dependency node to resolve.
111     *
112     * @param node The dependency node to resolve, may be {@code null}.
113     * @return This request for chaining, never {@code null}.
114     */
115    public ArtifactRequest setDependencyNode(DependencyNode node) {
116        this.node = node;
117        if (node != null) {
118            setArtifact(node.getDependency().getArtifact());
119        }
120        return this;
121    }
122
123    /**
124     * Gets the repositories to resolve the artifact from.
125     *
126     * @return The repositories, never {@code null}.
127     */
128    public List<RemoteRepository> getRepositories() {
129        return repositories;
130    }
131
132    /**
133     * Sets the repositories to resolve the artifact from.
134     *
135     * @param repositories The repositories, may be {@code null}.
136     * @return This request for chaining, never {@code null}.
137     */
138    public ArtifactRequest setRepositories(List<RemoteRepository> repositories) {
139        if (repositories == null) {
140            this.repositories = Collections.emptyList();
141        } else {
142            this.repositories = repositories;
143        }
144        return this;
145    }
146
147    /**
148     * Adds the specified repository for the resolution.
149     *
150     * @param repository The repository to add, may be {@code null}.
151     * @return This request for chaining, never {@code null}.
152     */
153    public ArtifactRequest addRepository(RemoteRepository repository) {
154        if (repository != null) {
155            if (this.repositories.isEmpty()) {
156                this.repositories = new ArrayList<>();
157            }
158            this.repositories.add(repository);
159        }
160        return this;
161    }
162
163    /**
164     * Gets the context in which this request is made.
165     *
166     * @return The context, never {@code null}.
167     */
168    public String getRequestContext() {
169        return context;
170    }
171
172    /**
173     * Sets the context in which this request is made.
174     *
175     * @param context The context, may be {@code null}.
176     * @return This request for chaining, never {@code null}.
177     */
178    public ArtifactRequest setRequestContext(String context) {
179        this.context = (context != null) ? context : "";
180        return this;
181    }
182
183    /**
184     * Gets the trace information that describes the higher level request/operation in which this request is issued.
185     *
186     * @return The trace information about the higher level operation or {@code null} if none.
187     */
188    public RequestTrace getTrace() {
189        return trace;
190    }
191
192    /**
193     * Sets the trace information that describes the higher level request/operation in which this request is issued.
194     *
195     * @param trace The trace information about the higher level operation, may be {@code null}.
196     * @return This request for chaining, never {@code null}.
197     */
198    public ArtifactRequest setTrace(RequestTrace trace) {
199        this.trace = trace;
200        return this;
201    }
202
203    @Override
204    public String toString() {
205        return getArtifact() + " < " + getRepositories();
206    }
207}