View Javadoc
1   /**
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.swid.builder;
28  
29  import gov.nist.secauto.swid.builder.util.Util;
30  
31  import java.net.URI;
32  import java.util.Objects;
33  
34  public class LinkBuilder extends AbstractLanguageSpecificBuilder<LinkBuilder> {
35  
36    public static LinkBuilder create() {
37      return new LinkBuilder();
38    }
39  
40    private String artifact;
41    private URI href;
42    private String media;
43    private LinkOwnership ownership;
44    private String rel;
45    private String mediaType;
46    private LinkUse use;
47  
48    protected LinkBuilder() {
49      super();
50    }
51  
52    @Override
53    public void reset() {
54      this.artifact = null;
55      this.href = null;
56      this.media = null;
57      this.ownership = null;
58      this.rel = null;
59      this.mediaType = null;
60      this.use = null;
61    }
62  
63    public String getArtifact() {
64      return artifact;
65    }
66  
67    public URI getHref() {
68      return href;
69    }
70  
71    public String getMedia() {
72      return media;
73    }
74  
75    public LinkOwnership getOwnership() {
76      return ownership;
77    }
78  
79    public String getRel() {
80      return rel;
81    }
82  
83    public String getMediaType() {
84      return mediaType;
85    }
86  
87    public LinkUse getUse() {
88      return use;
89    }
90  
91    /**
92     * For use with rel="installationmedia" to identify the canonical name for an installation media
93     * resource.
94     * 
95     * @param artifact
96     *          a cononical name for the installation media
97     * @return the same builder instance
98     */
99    public LinkBuilder artifact(String artifact) {
100     Util.requireNonEmpty(artifact, "artifact");
101     this.artifact = artifact;
102     return this;
103   }
104 
105   /**
106    * Sets the to-be-built link's href to the provided value.
107    * 
108    * @param uri
109    *          a URI identifying the linked resource
110    * @return the same builder instance
111    */
112   public LinkBuilder href(URI uri) {
113     Objects.requireNonNull(uri, "url");
114     this.href = uri;
115     return this;
116   }
117 
118   /**
119    * Sets the to-be-built link's media to the provided value.
120    * 
121    * @param query
122    *          a media query as defined by ISO/IEC 19770-2:2015
123    * @return the same builder instance
124    */
125   public LinkBuilder media(String query) {
126     Util.requireNonEmpty(query, "query");
127     this.media = query;
128     return this;
129   }
130 
131   /**
132    * Sets the to-be-built link's ownership to the provided value.
133    * 
134    * @param ownership
135    *          a valid non-null enumeration value
136    * @return the same builder instance
137    */
138   public LinkBuilder ownership(LinkOwnership ownership) {
139     Objects.requireNonNull(ownership, "ownership");
140     this.ownership = ownership;
141     return this;
142   }
143 
144   /**
145    * Sets the to-be-built link's rel to the provided value.
146    * 
147    * @param rel
148    *          the link relation type
149    * @return the same builder instance
150    */
151   public LinkBuilder rel(String rel) {
152     Util.requireNonEmpty(rel, "rel");
153     this.rel = rel;
154     return this;
155   }
156 
157   /**
158    * Provide the IANA MediaType for the resource targeted by the href attribute; this provides the
159    * consumer with knowledge of the format of the referenced resource.
160    * 
161    * The <a href="http://www.iana.org/assignments/media-types/media-types.xhtml">The IANA Media Type
162    * Registry</a> provides more details on possible values.
163    * 
164    * @param mediaType
165    *          a valid media type value
166    * @return the same builder instance
167    */
168   public LinkBuilder mediaType(String mediaType) {
169     Util.requireNonEmpty(mediaType, "mediaType");
170     this.mediaType = mediaType;
171     return this;
172   }
173 
174   /**
175    * Sets the to-be-built link's use to the provided value.
176    * 
177    * @param use
178    *          a valid, non-null enumeration value
179    * @return the same builder instance
180    */
181   public LinkBuilder use(LinkUse use) {
182     Objects.requireNonNull(use, "use");
183     this.use = use;
184     return this;
185   }
186 
187   @Override
188   public void validate() throws ValidationException {
189     super.validate();
190     validateNonNull("href", href);
191     validateNonEmpty("href", href.toString());
192     validateNonEmpty("href", rel);
193   }
194 }