1 /*
2 * Licensed to The Apereo Foundation under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 *
7 * The Apereo Foundation licenses this file to you under the Educational
8 * Community License, Version 2.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of the License
10 * at:
11 *
12 * http://opensource.org/licenses/ecl2.txt
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 *
20 */
21
22 package org.opencastproject.runtimeinfo.rest;
23
24 import org.opencastproject.util.JaxbXmlSchemaGenerator;
25 import org.opencastproject.util.doc.DocData;
26 import org.opencastproject.util.doc.rest.RestParameter;
27
28 import org.apache.commons.lang3.StringEscapeUtils;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 /**
34 * Represents a single parameter for an endpoint.
35 */
36 public final class RestParamData {
37 public enum Type {
38 BOOLEAN, FILE, STRING, TEXT, INTEGER, FLOAT, LONG
39 };
40
41 private String name; // unique key
42 private String defaultValue;
43 private String type;
44 private String description;
45 private String xmlSchema;
46 private boolean required = false;
47 private boolean path = false; // This will be true for a path parameter.
48
49 /**
50 * Attributes are used for adjusting how the input field of this parameter is rendered in the test form. Currently,
51 * the template uses 3 attribute values. "rows" and "cols" are used to control the size of text box for a TEXT type
52 * parameter. "size" is used to control the size of text box for other types of parameter. Please look at the template
53 * to see how this is used.
54 */
55 private Map<String, String> attributes = new HashMap<>();
56
57 /**
58 * Convenient constructor: take a RestParameter annotation and create a RestParamData from it.
59 *
60 * @param restParam
61 * the RestParameter annotation type that is to be transformed to RestParamData
62 */
63 public RestParamData(RestParameter restParam) {
64 this(restParam.name(),
65 Type.valueOf(restParam.type().name()),
66 restParam.defaultValue(),
67 restParam.description(),
68 JaxbXmlSchemaGenerator.getXmlSchema(restParam.jaxbClass()));
69 }
70
71 /**
72 * Create a parameter for this endpoint, the thing you are adding it to indicates if required or optional
73 *
74 * @param name
75 * the parameter name (this is the parameter itself)
76 * @param type
77 * [optional] the type of this parameter
78 * @param defaultValue
79 * [optional] the default value which is used if this param is missing
80 * @param description
81 * [optional] the description to display with this param
82 * @param xmlSchema
83 * [optional] the XML schema to display for this param
84 * @throws IllegalArgumentException
85 * when name is null or non-alphanumeric
86 */
87 public RestParamData(String name, Type type, String defaultValue, String description, String xmlSchema)
88 throws IllegalArgumentException {
89 if (!DocData.isValidName(name)) {
90 throw new IllegalArgumentException("Name must not be null and must be alphanumeric.");
91 }
92 if (type == null) {
93 type = Type.STRING;
94 }
95 this.name = name;
96 this.type = type.name().toLowerCase();
97 if ((defaultValue == null) || (defaultValue.isEmpty())) {
98 if ((type == Type.INTEGER) || (type == Type.FLOAT) || (type == Type.LONG)) {
99 this.defaultValue = "0";
100 } else {
101 this.defaultValue = null;
102 }
103 } else {
104 this.defaultValue = defaultValue;
105 }
106 if ((description == null) || (description.isEmpty())) {
107 this.description = null;
108 } else {
109 this.description = description;
110 }
111 this.xmlSchema = xmlSchema;
112 }
113
114 /**
115 * Attributes are used for adjusting rendering of form elements related to this parameter.
116 *
117 * @param key
118 * the attribute key (e.g. size)
119 * @param value
120 * the attribute value (e.g. 80)
121 * @throws IllegalArgumentException
122 * when key is null
123 */
124 public void setAttribute(String key, String value) throws IllegalArgumentException {
125 if (key == null) {
126 throw new IllegalArgumentException("Key must not be null.");
127 }
128 if (value == null) {
129 attributes.remove(key);
130 } else {
131 attributes.put(key, value);
132 }
133 }
134
135 /**
136 * Get the value indexed by key in attributes.
137 *
138 * @param key
139 * the attribute key (e.g. size)
140 * @return the value indexed by the key or null if that value does not exists
141 */
142 public String getAttribute(String key) {
143 if (key == null) {
144 return null;
145 }
146 return attributes.get(key);
147 }
148
149 /**
150 * Get the name of this parameter.
151 *
152 * @return name of this parameter
153 */
154 public String getName() {
155 return name;
156 }
157
158 /**
159 * Get the default value of this parameter.
160 *
161 * @return default value of this parameter
162 */
163 public String getDefaultValue() {
164 return defaultValue;
165 }
166
167 /**
168 * @return an HTML formatted version of the default value for display
169 */
170 public String getEscapedDefaultValue() {
171 return StringEscapeUtils.escapeHtml4(defaultValue);
172 }
173
174 /**
175 * @return an HTML formatted version of the xml schema for display
176 */
177 public String getEscapedXmlSchema() {
178 return StringEscapeUtils.escapeXml(xmlSchema);
179 }
180
181 /**
182 * Get the type of this parameter.
183 *
184 * @return type of this parameter
185 */
186 public String getType() {
187 return type;
188 }
189
190 /**
191 * Get the description of this parameter.
192 *
193 * @return description of this parameter
194 */
195 public String getDescription() {
196 return description;
197 }
198
199 /**
200 * Get the attributes used for adjusting rendering of form elements related to this parameter.
201 *
202 * @return the attributes used for adjusting rendering of form elements related to this parameter
203 */
204 public Map<String, String> getAttributes() {
205 return attributes;
206 }
207
208 /**
209 * Return whether this parameter is required.
210 *
211 * @return a boolean indicating whether this parameter is required.
212 */
213 public boolean isRequired() {
214 return required;
215 }
216
217 /**
218 * Return whether this parameter is a path parameter.
219 *
220 * @return a boolean indicating whether this parameter is a path parameter.
221 */
222 public boolean isPath() {
223 return path;
224 }
225
226 /**
227 * Set whether this parameter is a path parameter.
228 *
229 * @param path
230 * a boolean specifying whether this parameter is a path parameter.
231 */
232 public void setPath(boolean path) {
233 this.path = path;
234 }
235
236 /**
237 * Set whether this parameter is required.
238 *
239 * @param required
240 * if true then this parameter is require, otherwise it is optional
241 */
242 public void setRequired(boolean required) {
243 this.required = required;
244 }
245
246 /**
247 * Return a string representation of this RestParamData object.
248 *
249 * @return a string representation of this RestParamData object
250 */
251 @Override
252 public String toString() {
253 return "PAR:" + name + ":(" + type + "):" + defaultValue;
254 }
255
256 /**
257 * @return the xmlSchema
258 */
259 public String getXmlSchema() {
260 return xmlSchema;
261 }
262
263 /**
264 * @param xmlSchema
265 * the xmlSchema to set
266 */
267 public void setXmlSchema(String xmlSchema) {
268 this.xmlSchema = xmlSchema;
269 }
270 }