View Javadoc
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 java.util.ArrayList;
25  import java.util.List;
26  
27  public class RestFormData {
28  
29    /**
30     * This indicates whether the form submission should be an ajax submit or a normal submit.
31     */
32    private boolean ajaxSubmit = false;
33  
34    /**
35     * If this is true then the upload contains a body parameter (i.e. a file upload option).
36     */
37    private boolean fileUpload = false;
38  
39    /**
40     * This indicates whether this test form is for a basic endpoint which has no parameters.
41     */
42    private boolean basic = false;
43  
44    /**
45     * The form parameters.
46     */
47    private List<RestParamData> items;
48  
49    /**
50     * Constructor which will auto-populate the form using the data in the endpoint, this will enable the ajax submit if
51     * it is possible to do so.
52     *
53     * @param endpoint
54     *          a RestEndpointData object populated with all parameters it needs
55     * @throws IllegalArgumentException
56     *           when endpoint is null
57     */
58    public RestFormData(RestEndpointData endpoint) throws IllegalArgumentException {
59      if (endpoint == null) {
60        throw new IllegalArgumentException("Endpoint must not be null.");
61      }
62      ajaxSubmit = true;
63      items = new ArrayList<RestParamData>(3);
64      boolean hasUpload = false;
65      if (endpoint.getPathParams() != null) {
66        for (RestParamData param : endpoint.getPathParams()) {
67          param.setRequired(true);
68          items.add(param);
69        }
70      }
71      if (endpoint.getRequiredParams() != null) {
72        for (RestParamData param : endpoint.getRequiredParams()) {
73          param.setRequired(true);
74          if (RestParamData.Type.FILE.name().equalsIgnoreCase(param.getType())) {
75            hasUpload = true;
76          }
77          items.add(param);
78        }
79      }
80      if (endpoint.getOptionalParams() != null) {
81        for (RestParamData param : endpoint.getOptionalParams()) {
82          param.setRequired(false);
83          if (RestParamData.Type.FILE.name().equalsIgnoreCase(param.getType())) {
84            hasUpload = true;
85          }
86          items.add(param);
87        }
88      }
89      if (endpoint.getBodyParam() != null) {
90        RestParamData param = endpoint.getBodyParam();
91        param.setRequired(true);
92        if (RestParamData.Type.FILE.name().equalsIgnoreCase(param.getType())) {
93          hasUpload = true;
94        }
95        items.add(param);
96      }
97      if (hasUpload) {
98        fileUpload = true;
99        ajaxSubmit = false;
100     }
101     if (items.isEmpty() && endpoint.isGetMethod()) {
102       basic = true;
103     }
104   }
105 
106   /**
107    * Returns true if this form has no parameter in it, false if there is parameter in it.
108    *
109    * @return a boolean indicating whether this form contains any parameter
110    */
111   public boolean isEmpty() {
112     boolean empty = true;
113     if (items != null && !items.isEmpty()) {
114       empty = false;
115     }
116     return empty;
117   }
118 
119   /**
120    * Controls whether the form will be submitted via ajax and the content rendered on the page, NOTE that uploading any
121    * files or downloading any content that is binary will require not using ajax submit, also note that there may be
122    * other cases where ajax submission will fail to work OR where normal submission will fail to work (using PUT/DELETE)
123    *
124    * @param ajaxSubmit
125    *          a boolean indicating whether ajax submit is used
126    */
127   public void setAjaxSubmit(boolean ajaxSubmit) {
128     this.ajaxSubmit = ajaxSubmit;
129   }
130 
131   /**
132    * Set this to true if the file contains a file upload control, this will be determined automatically for
133    * auto-generated forms.
134    *
135    * @param fileUpload
136    *          a boolean indicating whether there is file upload in this test
137    */
138   public void setFileUpload(boolean fileUpload) {
139     this.fileUpload = fileUpload;
140     if (fileUpload) {
141       ajaxSubmit = false;
142     }
143   }
144 
145   /**
146    * Returns a string representation of this form.
147    *
148    * @return a string representation of this form
149    */
150   @Override
151   public String toString() {
152     if (items == null) {
153       return "FORM:items=0";
154     }
155     return "FORM:items=" + items.size();
156   }
157 
158   /**
159    * Returns whether the form submission should be an ajax submission or a normal submission.
160    *
161    * @return a boolean indicating whether the form submission should be an ajax submission or a normal submission
162    */
163   public boolean isAjaxSubmit() {
164     return ajaxSubmit;
165   }
166 
167   /**
168    * Returns whether the form contains a body parameter (i.e. a file upload option).
169    *
170    * @return a boolean indicating whether the form contains a body parameter (i.e. a file upload option)
171    */
172   public boolean isFileUpload() {
173     return fileUpload;
174   }
175 
176   /**
177    * Returns whether this form is for a basic endpoint which has no parameters.
178    *
179    * @return a boolean indicating whether this form is for a basic endpoint which has no parameters
180    */
181   public boolean isBasic() {
182     return basic;
183   }
184 
185   /**
186    * Returns the list of form parameters.
187    *
188    * @return a list of form parameters
189    */
190   public List<RestParamData> getItems() {
191     return items;
192   }
193 
194 }