View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.tooling.commons.contentpackagebuilder;
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.util.Date;
28  
29  import org.apache.commons.io.IOUtils;
30  
31  /**
32   * Builds a {@link ContentPackage} instance with metadata.
33   * This class is not thread-safe.
34   */
35  public final class ContentPackageBuilder {
36  
37    private final PackageMetadata metadata = new PackageMetadata();
38  
39    /**
40     * Set content package name.
41     * @param value Package name
42     * @return this
43     */
44    public ContentPackageBuilder name(String value) {
45      metadata.setName(value);
46      return this;
47    }
48  
49    /**
50     * Set content package group.
51     * @param value Package group
52     * @return this
53     */
54    public ContentPackageBuilder group(String value) {
55      metadata.setGroup(value);
56      return this;
57    }
58  
59    /**
60     * Set content package description.
61     * @param value Package description
62     * @return this
63     */
64    public ContentPackageBuilder description(String value) {
65      metadata.setDescription(value);
66      return this;
67    }
68  
69    /**
70     * Set use name who created the package.
71     * @param value Created by user name (default: 'admin')
72     * @return this
73     */
74    public ContentPackageBuilder createdBy(String value) {
75      metadata.setCreatedBy(value);
76      return this;
77    }
78  
79    /**
80     * Set timestamp for package creation.
81     * @param value Creation timestamp (default: now)
82     * @return this
83     */
84    public ContentPackageBuilder created(Date value) {
85      metadata.setCreated(value);
86      return this;
87    }
88  
89    /**
90     * Set package version.
91     * @param value Package version
92     * @return this
93     */
94    public ContentPackageBuilder version(String value) {
95      metadata.setVersion(value);
96      return this;
97    }
98  
99    /**
100    * Set access control handling.
101    * @param value Access control handling mode
102    * @return this
103    */
104   public ContentPackageBuilder acHandling(AcHandling value) {
105     metadata.setAcHandling(value);
106     return this;
107   }
108 
109   /**
110    * Set package type.
111    * @param value Package type.
112    * @return this
113    */
114   public ContentPackageBuilder packageType(String value) {
115     metadata.setPackageType(value);
116     return this;
117   }
118 
119   /**
120    * Set requires root status
121    * @param value Requires root status
122    * @return this
123    */
124   public ContentPackageBuilder requiresRoot(boolean value) {
125     metadata.setRequiresRoot(value);
126     return this;
127   }
128 
129   /**
130    * Set requires restart status
131    * @param value Requires restart status
132    * @return this
133    */
134   public ContentPackageBuilder requiresRestart(boolean value) {
135     metadata.setRequiresRestart(value);
136     return this;
137   }
138 
139   /**
140    * Set allow index definitions root status
141    * @param value allow index definitions status
142    * @return this
143    */
144   public ContentPackageBuilder allowIndexDefinitions(boolean value) {
145     metadata.setAllowIndexDefinitions(value);
146     return this;
147   }
148 
149   /**
150    * Creates a package filter with this root path.
151    * This implicitly adds a {@link PackageFilter} with this pah and no further rules.
152    * If this is executed multiple times multiple filters are addded.
153    * @param value Root path for package
154    * @return this
155    */
156   public ContentPackageBuilder rootPath(String value) {
157     metadata.addFilter(new PackageFilter(value));
158     return this;
159   }
160 
161   /**
162    * Add package filter.
163    * If this is executed multiple times multiple filters are added.
164    * @param value Package filter optionally with include/exclude rules.
165    * @return this
166    */
167   public ContentPackageBuilder filter(PackageFilter value) {
168     metadata.addFilter(value);
169     return this;
170   }
171 
172   /**
173    * Add custom package metadata property.
174    * @param property Property name
175    * @param value Property value
176    * @return this
177    */
178   public ContentPackageBuilder property(String property, Object value) {
179     metadata.addProperty(property, value);
180     return this;
181   }
182 
183   /**
184    * Register a XML namespace that is used by your content added to the JCR XML.
185    * This method can be called multiple times to register multiple namespaces.
186    * The JCR namespaces "jcr", "nt", "cq" and "sling" are registered by default.
187    * @param prefix Namespace prefix
188    * @param uri Namespace URI
189    * @return this
190    */
191   public ContentPackageBuilder xmlNamespace(String prefix, String uri) {
192     metadata.addXmlNamespace(prefix, uri);
193     return this;
194   }
195 
196   /**
197    * Set thumbnail PNG image.
198    * @param is Input stream with Thumbnail PNG image binary data
199    * @return this
200    * @throws IOException I/O exception
201    */
202   public ContentPackageBuilder thumbnailImage(InputStream is) throws IOException {
203     metadata.setThumbnailImage(IOUtils.toByteArray(is));
204     return this;
205   }
206 
207   /**
208    * Build {@link ContentPackage} to which additional content (Pages or binary files) can be added.
209    * Please make sure you call the {@link ContentPackage#close()} method when all content was added.
210    * @param outputStream Output stream
211    * @return Content package
212    * @throws IOException I/O exception
213    */
214   public ContentPackage build(OutputStream outputStream) throws IOException {
215     return new ContentPackage(metadata, outputStream);
216   }
217 
218   /**
219    * Build {@link ContentPackage} to which additional content (Pages or binary files) can be added.
220    * Please make sure you call the {@link ContentPackage#close()} method when all content was added.
221    * @param file Output file
222    * @return Content package
223    * @throws IOException I/O exception
224    */
225   public ContentPackage build(File file) throws IOException {
226     return build(new FileOutputStream(file));
227   }
228 
229 }