001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.spi;
018
019import java.util.Map;
020
021/**
022 * Configuration use by {@link org.apache.camel.spi.RestConsumerFactory} for Camel components to support
023 * the Camel {@link org.apache.camel.model.rest.RestDefinition rest} DSL.
024 */
025public class RestConfiguration {
026
027    public static final String CORS_ACCESS_CONTROL_ALLOW_ORIGIN = "*";
028    public static final String CORS_ACCESS_CONTROL_ALLOW_METHODS = "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH";
029    public static final String CORS_ACCESS_CONTROL_MAX_AGE = "3600";
030    public static final String CORS_ACCESS_CONTROL_ALLOW_HEADERS = "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers";
031
032    public enum RestBindingMode {
033        auto, off, json, xml, json_xml
034    }
035
036    public enum RestHostNameResolver {
037        localIp, localHostName
038    }
039
040    private String component;
041    private String scheme;
042    private String host;
043    private int port;
044    private String contextPath;
045    private RestHostNameResolver restHostNameResolver = RestHostNameResolver.localHostName;
046    private RestBindingMode bindingMode = RestBindingMode.off;
047    private boolean skipBindingOnErrorCode = true;
048    private boolean enableCORS;
049    private String jsonDataFormat;
050    private String xmlDataFormat;
051    private Map<String, Object> componentProperties;
052    private Map<String, Object> endpointProperties;
053    private Map<String, Object> consumerProperties;
054    private Map<String, Object> dataFormatProperties;
055    private Map<String, String> corsHeaders;
056
057    /**
058     * Gets the name of the Camel component to use as the REST consumer
059     *
060     * @return the component name, or <tt>null</tt> to let Camel search the {@link Registry} to find suitable implementation
061     */
062    public String getComponent() {
063        return component;
064    }
065
066    /**
067     * Sets the name of the Camel component to use as the REST consumer
068     *
069     * @param componentName the name of the component (such as restlet, spark-rest, etc.)
070     */
071    public void setComponent(String componentName) {
072        this.component = componentName;
073    }
074
075    /**
076     * Gets the hostname to use by the REST consumer
077     *
078     * @return the hostname, or <tt>null</tt> to use default hostname
079     */
080    public String getHost() {
081        return host;
082    }
083
084    /**
085     * Sets the hostname to use by the REST consumer
086     *
087     * @param host the hostname
088     */
089    public void setHost(String host) {
090        this.host = host;
091    }
092
093    /**
094     * Gets the scheme to use by the REST consumer
095     *
096     * @return the scheme, or <tt>null</tt> to use default scheme
097     */
098    public String getScheme() {
099        return scheme;
100    }
101
102    /**
103     * Sets the scheme to use by the REST consumer
104     *
105     * @param scheme the scheme
106     */
107    public void setScheme(String scheme) {
108        this.scheme = scheme;
109    }
110
111    /**
112     * Gets the port to use by the REST consumer
113     *
114     * @return the port, or <tt>0</tt> or <tt>-1</tt> to use default port
115     */
116    public int getPort() {
117        return port;
118    }
119
120    /**
121     * Sets the port to use by the REST consumer
122     *
123     * @param port the port number
124     */
125    public void setPort(int port) {
126        this.port = port;
127    }
128
129    /**
130     * Gets the configured context-path
131     *
132     * @return the context path, or <tt>null</tt> if none configured.
133     */
134    public String getContextPath() {
135        return contextPath;
136    }
137
138    /**
139     * Sets a leading context-path the REST services will be using.
140     * <p/>
141     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
142     * is deployed using a context-path.
143     *
144     * @param contextPath the context path
145     */
146    public void setContextPath(String contextPath) {
147        this.contextPath = contextPath;
148    }
149
150    /**
151     * Gets the resolver to use for resolving hostname
152     *
153     * @return the resolver
154     */
155    public RestHostNameResolver getRestHostNameResolver() {
156        return restHostNameResolver;
157    }
158
159    /**
160     * Sets the resolver to use for resolving hostname
161     *
162     * @param restHostNameResolver the resolver
163     */
164    public void setRestHostNameResolver(RestHostNameResolver restHostNameResolver) {
165        this.restHostNameResolver = restHostNameResolver;
166    }
167
168    /**
169     * Sets the resolver to use for resolving hostname
170     *
171     * @param restHostNameResolver the resolver
172     */
173    public void setRestHostNameResolver(String restHostNameResolver) {
174        this.restHostNameResolver = RestHostNameResolver.valueOf(restHostNameResolver);
175    }
176
177    /**
178     * Gets the binding mode used by the REST consumer
179     *
180     * @return the binding mode
181     */
182    public RestBindingMode getBindingMode() {
183        return bindingMode;
184    }
185
186    /**
187     * Sets the binding mode to be used by the REST consumer
188     *
189     * @param bindingMode the binding mode
190     */
191    public void setBindingMode(RestBindingMode bindingMode) {
192        this.bindingMode = bindingMode;
193    }
194
195    /**
196     * Sets the binding mode to be used by the REST consumer
197     *
198     * @param bindingMode the binding mode
199     */
200    public void setBindingMode(String bindingMode) {
201        this.bindingMode = RestBindingMode.valueOf(bindingMode);
202    }
203
204    /**
205     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
206     * <p/>
207     * This option is default <tt>true</tt>.
208     *
209     * @return whether to skip binding on error code
210     */
211    public boolean isSkipBindingOnErrorCode() {
212        return skipBindingOnErrorCode;
213    }
214
215    /**
216     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
217     * <p/>
218     * This option is default <tt>true</tt>.
219     *
220     * @param skipBindingOnErrorCode whether to skip binding on error code
221     */
222    public void setSkipBindingOnErrorCode(boolean skipBindingOnErrorCode) {
223        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
224    }
225
226    /**
227     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
228     * <p/>
229     * This option is default <tt>false</tt>
230     *
231     * @return whether CORS is enabled or not
232     */
233    public boolean isEnableCORS() {
234        return enableCORS;
235    }
236
237    /**
238     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
239     * <p/>
240     * This option is default <tt>false</tt>
241     *
242     * @param enableCORS <tt>true</tt> to enable CORS
243     */
244    public void setEnableCORS(boolean enableCORS) {
245        this.enableCORS = enableCORS;
246    }
247
248    /**
249     * Gets the name of the json data format.
250     * <p/>
251     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
252     *
253     * @return the name, or <tt>null</tt> to use default
254     */
255    public String getJsonDataFormat() {
256        return jsonDataFormat;
257    }
258
259    /**
260     * Sets a custom json data format to be used
261     * <p/>
262     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
263     *
264     * @param name name of the data format
265     */
266    public void setJsonDataFormat(String name) {
267        this.jsonDataFormat = name;
268    }
269
270    /**
271     * Gets the name of the xml data format.
272     * <p/>
273     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
274     *
275     * @return the name, or <tt>null</tt> to use default
276     */
277    public String getXmlDataFormat() {
278        return xmlDataFormat;
279    }
280
281    /**
282     * Sets a custom xml data format to be used.
283     * <p/>
284     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
285     *
286     * @param name name of the data format
287     */
288    public void setXmlDataFormat(String name) {
289        this.xmlDataFormat = name;
290    }
291
292    /**
293     * Gets additional options on component level
294     *
295     * @return additional options
296     */
297    public Map<String, Object> getComponentProperties() {
298        return componentProperties;
299    }
300
301    /**
302     * Sets additional options on component level
303     *
304     * @param componentProperties the options
305     */
306    public void setComponentProperties(Map<String, Object> componentProperties) {
307        this.componentProperties = componentProperties;
308    }
309
310    /**
311     * Gets additional options on endpoint level
312     *
313     * @return additional options
314     */
315    public Map<String, Object> getEndpointProperties() {
316        return endpointProperties;
317    }
318
319    /**
320     * Sets additional options on endpoint level
321     *
322     * @param endpointProperties the options
323     */
324    public void setEndpointProperties(Map<String, Object> endpointProperties) {
325        this.endpointProperties = endpointProperties;
326    }
327
328    /**
329     * Gets additional options on consumer level
330     *
331     * @return additional options
332     */
333    public Map<String, Object> getConsumerProperties() {
334        return consumerProperties;
335    }
336
337    /**
338     * Sets additional options on consumer level
339     *
340     * @param consumerProperties the options
341     */
342    public void setConsumerProperties(Map<String, Object> consumerProperties) {
343        this.consumerProperties = consumerProperties;
344    }
345
346    /**
347     * Gets additional options on data format level
348     *
349     * @return additional options
350     */
351    public Map<String, Object> getDataFormatProperties() {
352        return dataFormatProperties;
353    }
354
355    /**
356     * Sets additional options on data format level
357     *
358     * @param dataFormatProperties the options
359     */
360    public void setDataFormatProperties(Map<String, Object> dataFormatProperties) {
361        this.dataFormatProperties = dataFormatProperties;
362    }
363
364    /**
365     * Gets the CORS headers to use if CORS has been enabled.
366     *
367     * @return the CORS headers
368     */
369    public Map<String, String> getCorsHeaders() {
370        return corsHeaders;
371    }
372
373    /**
374     * Sets the CORS headers to use if CORS has been enabled.
375     *
376     * @param corsHeaders the CORS headers
377     */
378    public void setCorsHeaders(Map<String, String> corsHeaders) {
379        this.corsHeaders = corsHeaders;
380    }
381}