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} and {@link org.apache.camel.spi.RestApiConsumerFactory}
023 * for Camel components to support 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        allLocalIp, localIp, localHostName
038    }
039
040    private String component;
041    private String apiComponent;
042    private String producerComponent;
043    private String producerApiDoc;
044    private String scheme;
045    private String host;
046    private String apiHost;
047    private int port;
048    private String contextPath;
049    private String apiContextPath;
050    private String apiContextRouteId;
051    private String apiContextIdPattern;
052    private boolean apiContextListing;
053    private boolean apiVendorExtension;
054    private RestHostNameResolver hostNameResolver = RestHostNameResolver.allLocalIp;
055    private RestBindingMode bindingMode = RestBindingMode.off;
056    private boolean skipBindingOnErrorCode = true;
057    private boolean enableCORS;
058    private String jsonDataFormat;
059    private String xmlDataFormat;
060    private Map<String, Object> componentProperties;
061    private Map<String, Object> endpointProperties;
062    private Map<String, Object> consumerProperties;
063    private Map<String, Object> dataFormatProperties;
064    private Map<String, Object> apiProperties;
065    private Map<String, String> corsHeaders;
066
067    /**
068     * Gets the name of the Camel component to use as the REST consumer
069     *
070     * @return the component name, or <tt>null</tt> to let Camel search the {@link Registry} to find suitable implementation
071     */
072    public String getComponent() {
073        return component;
074    }
075
076    /**
077     * Sets the name of the Camel component to use as the REST consumer
078     *
079     * @param componentName the name of the component (such as restlet, spark-rest, etc.)
080     */
081    public void setComponent(String componentName) {
082        this.component = componentName;
083    }
084
085    /**
086     * Gets the name of the Camel component to use as the REST API (such as swagger)
087     *
088     * @return the component name, or <tt>null</tt> to let Camel use the default name <tt>swagger</tt>
089     */
090    public String getApiComponent() {
091        return apiComponent;
092    }
093
094    /**
095     * Sets the name of the Camel component to use as the REST API (such as swagger)
096     *
097     * @param apiComponent the name of the component (such as swagger)
098     */
099    public void setApiComponent(String apiComponent) {
100        this.apiComponent = apiComponent;
101    }
102
103    /**
104     * Gets the name of the Camel component to use as the REST producer
105     *
106     * @return the component name, or <tt>null</tt> to let Camel search the {@link Registry} to find suitable implementation
107     */
108    public String getProducerComponent() {
109        return producerComponent;
110    }
111
112    /**
113     * Sets the name of the Camel component to use as the REST producer
114     *
115     * @param componentName the name of the component (such as restlet, jetty, etc.)
116     */
117    public void setProducerComponent(String componentName) {
118        this.producerComponent = componentName;
119    }
120
121    /**
122     * Gets the location of the api document (swagger api) the REST producer will use
123     * to validate the REST uri and query parameters are valid accordingly to the api document.
124     */
125    public String getProducerApiDoc() {
126        return producerApiDoc;
127    }
128
129    /**
130     * Sets the location of the api document (swagger api) the REST producer will use
131     * to validate the REST uri and query parameters are valid accordingly to the api document.
132     * This requires adding camel-swagger-java to the classpath, and any miss configuration
133     * will let Camel fail on startup and report the error(s).
134     * <p/>
135     * The location of the api document is loaded from classpath by default, but you can use
136     * <tt>file:</tt> or <tt>http:</tt> to refer to resources to load from file or http url.
137     */
138    public void setProducerApiDoc(String producerApiDoc) {
139        this.producerApiDoc = producerApiDoc;
140    }
141
142    /**
143     * Gets the hostname to use by the REST consumer
144     *
145     * @return the hostname, or <tt>null</tt> to use default hostname
146     */
147    public String getHost() {
148        return host;
149    }
150
151    /**
152     * Sets the hostname to use by the REST consumer
153     *
154     * @param host the hostname
155     */
156    public void setHost(String host) {
157        this.host = host;
158    }
159
160    public String getApiHost() {
161        return apiHost;
162    }
163
164    /**
165     * To use an specific hostname for the API documentation (eg swagger)
166     * <p/>
167     * This can be used to override the generated host with this configured hostname
168     */
169    public void setApiHost(String apiHost) {
170        this.apiHost = apiHost;
171    }
172
173    /**
174     * Gets the scheme to use by the REST consumer
175     *
176     * @return the scheme, or <tt>null</tt> to use default scheme
177     */
178    public String getScheme() {
179        return scheme;
180    }
181
182    /**
183     * Sets the scheme to use by the REST consumer
184     *
185     * @param scheme the scheme
186     */
187    public void setScheme(String scheme) {
188        this.scheme = scheme;
189    }
190
191    /**
192     * Gets the port to use by the REST consumer
193     *
194     * @return the port, or <tt>0</tt> or <tt>-1</tt> to use default port
195     */
196    public int getPort() {
197        return port;
198    }
199
200    /**
201     * Sets the port to use by the REST consumer
202     *
203     * @param port the port number
204     */
205    public void setPort(int port) {
206        this.port = port;
207    }
208
209    /**
210     * Gets the configured context-path
211     *
212     * @return the context path, or <tt>null</tt> if none configured.
213     */
214    public String getContextPath() {
215        return contextPath;
216    }
217
218    /**
219     * Sets a leading context-path the REST services will be using.
220     * <p/>
221     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
222     * is deployed using a context-path. Or for components such as <tt>camel-jetty</tt> or <tt>camel-netty4-http</tt>
223     * that includes a HTTP server.
224     *
225     * @param contextPath the context path
226     */
227    public void setContextPath(String contextPath) {
228        this.contextPath = contextPath;
229    }
230
231    public String getApiContextPath() {
232        return apiContextPath;
233    }
234
235    /**
236     * Sets a leading API context-path the REST API services will be using.
237     * <p/>
238     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
239     * is deployed using a context-path.
240     *
241     * @param contextPath the API context path
242     */
243    public void setApiContextPath(String contextPath) {
244        this.apiContextPath = contextPath;
245    }
246
247    public String getApiContextRouteId() {
248        return apiContextRouteId;
249    }
250
251    /**
252     * Sets the route id to use for the route that services the REST API.
253     * <p/>
254     * The route will by default use an auto assigned route id.
255     *
256     * @param apiContextRouteId  the route id
257     */
258    public void setApiContextRouteId(String apiContextRouteId) {
259        this.apiContextRouteId = apiContextRouteId;
260    }
261
262    public String getApiContextIdPattern() {
263        return apiContextIdPattern;
264    }
265
266    /**
267     * Optional CamelContext id pattern to only allow Rest APIs from rest services within CamelContext's which name matches the pattern.
268     * <p/>
269     * The pattern <tt>#name#</tt> refers to the CamelContext name, to match on the current CamelContext only.
270     * For any other value, the pattern uses the rules from {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)}
271     *
272     * @param apiContextIdPattern  the pattern
273     */
274    public void setApiContextIdPattern(String apiContextIdPattern) {
275        this.apiContextIdPattern = apiContextIdPattern;
276    }
277
278    public boolean isApiContextListing() {
279        return apiContextListing;
280    }
281
282    /**
283     * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover
284     * these contexts, if <tt>false</tt> then only the current CamelContext is in use.
285     */
286    public void setApiContextListing(boolean apiContextListing) {
287        this.apiContextListing = apiContextListing;
288    }
289
290    public boolean isApiVendorExtension() {
291        return apiVendorExtension;
292    }
293
294    /**
295     * Whether vendor extension is enabled in the Rest APIs. If enabled then Camel will include additional information
296     * as vendor extension (eg keys starting with x-) such as route ids, class names etc.
297     * Not all 3rd party API gateways and tools supports vendor-extensions when importing your API docs.
298     */
299    public void setApiVendorExtension(boolean apiVendorExtension) {
300        this.apiVendorExtension = apiVendorExtension;
301    }
302
303    /**
304     * Gets the resolver to use for resolving hostname
305     *
306     * @return the resolver
307     * @deprecated use getHostNameResolver
308     */
309    @Deprecated
310    public RestHostNameResolver getRestHostNameResolver() {
311        return getHostNameResolver();
312    }
313
314    /**
315     * Sets the resolver to use for resolving hostname
316     *
317     * @param restHostNameResolver the resolver
318     * @deprecated use setHostNameResolver
319     */
320    @Deprecated
321    public void setRestHostNameResolver(RestHostNameResolver restHostNameResolver) {
322        setHostNameResolver(restHostNameResolver);
323    }
324
325    /**
326     * Sets the resolver to use for resolving hostname
327     *
328     * @param restHostNameResolver the resolver
329     * @deprecated use setHostNameResolver
330     */
331    @Deprecated
332    public void setRestHostNameResolver(String restHostNameResolver) {
333        settHostNameResolver(restHostNameResolver);
334    }
335
336    /**
337     * Gets the resolver to use for resolving hostname
338     *
339     * @return the resolver
340     */
341    public RestHostNameResolver getHostNameResolver() {
342        return hostNameResolver;
343    }
344
345    /**
346     * Sets the resolver to use for resolving hostname
347     *
348     * @param hostNameResolver the resolver
349     */
350    public void setHostNameResolver(RestHostNameResolver hostNameResolver) {
351        this.hostNameResolver = hostNameResolver;
352    }
353
354    /**
355     * Sets the resolver to use for resolving hostname
356     *
357     * @param hostNameResolver the resolver
358     */
359    public void settHostNameResolver(String hostNameResolver) {
360        this.hostNameResolver = RestHostNameResolver.valueOf(hostNameResolver);
361    }
362
363    /**
364     * Gets the binding mode used by the REST consumer
365     *
366     * @return the binding mode
367     */
368    public RestBindingMode getBindingMode() {
369        return bindingMode;
370    }
371
372    /**
373     * Sets the binding mode to be used by the REST consumer
374     *
375     * @param bindingMode the binding mode
376     */
377    public void setBindingMode(RestBindingMode bindingMode) {
378        this.bindingMode = bindingMode;
379    }
380
381    /**
382     * Sets the binding mode to be used by the REST consumer
383     *
384     * @param bindingMode the binding mode
385     */
386    public void setBindingMode(String bindingMode) {
387        this.bindingMode = RestBindingMode.valueOf(bindingMode);
388    }
389
390    /**
391     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
392     * <p/>
393     * This option is default <tt>true</tt>.
394     *
395     * @return whether to skip binding on error code
396     */
397    public boolean isSkipBindingOnErrorCode() {
398        return skipBindingOnErrorCode;
399    }
400
401    /**
402     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
403     * <p/>
404     * This option is default <tt>true</tt>.
405     *
406     * @param skipBindingOnErrorCode whether to skip binding on error code
407     */
408    public void setSkipBindingOnErrorCode(boolean skipBindingOnErrorCode) {
409        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
410    }
411
412    /**
413     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
414     * <p/>
415     * This option is default <tt>false</tt>
416     *
417     * @return whether CORS is enabled or not
418     */
419    public boolean isEnableCORS() {
420        return enableCORS;
421    }
422
423    /**
424     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
425     * <p/>
426     * This option is default <tt>false</tt>
427     *
428     * @param enableCORS <tt>true</tt> to enable CORS
429     */
430    public void setEnableCORS(boolean enableCORS) {
431        this.enableCORS = enableCORS;
432    }
433
434    /**
435     * Gets the name of the json data format.
436     * <p/>
437     * <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.
438     *
439     * @return the name, or <tt>null</tt> to use default
440     */
441    public String getJsonDataFormat() {
442        return jsonDataFormat;
443    }
444
445    /**
446     * Sets a custom json data format to be used
447     * <p/>
448     * <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.
449     *
450     * @param name name of the data format
451     */
452    public void setJsonDataFormat(String name) {
453        this.jsonDataFormat = name;
454    }
455
456    /**
457     * Gets the name of the xml data format.
458     * <p/>
459     * <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.
460     *
461     * @return the name, or <tt>null</tt> to use default
462     */
463    public String getXmlDataFormat() {
464        return xmlDataFormat;
465    }
466
467    /**
468     * Sets a custom xml data format to be used.
469     * <p/>
470     * <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.
471     *
472     * @param name name of the data format
473     */
474    public void setXmlDataFormat(String name) {
475        this.xmlDataFormat = name;
476    }
477
478    /**
479     * Gets additional options on component level
480     *
481     * @return additional options
482     */
483    public Map<String, Object> getComponentProperties() {
484        return componentProperties;
485    }
486
487    /**
488     * Sets additional options on component level
489     *
490     * @param componentProperties the options
491     */
492    public void setComponentProperties(Map<String, Object> componentProperties) {
493        this.componentProperties = componentProperties;
494    }
495
496    /**
497     * Gets additional options on endpoint level
498     *
499     * @return additional options
500     */
501    public Map<String, Object> getEndpointProperties() {
502        return endpointProperties;
503    }
504
505    /**
506     * Sets additional options on endpoint level
507     *
508     * @param endpointProperties the options
509     */
510    public void setEndpointProperties(Map<String, Object> endpointProperties) {
511        this.endpointProperties = endpointProperties;
512    }
513
514    /**
515     * Gets additional options on consumer level
516     *
517     * @return additional options
518     */
519    public Map<String, Object> getConsumerProperties() {
520        return consumerProperties;
521    }
522
523    /**
524     * Sets additional options on consumer level
525     *
526     * @param consumerProperties the options
527     */
528    public void setConsumerProperties(Map<String, Object> consumerProperties) {
529        this.consumerProperties = consumerProperties;
530    }
531
532    /**
533     * Gets additional options on data format level
534     *
535     * @return additional options
536     */
537    public Map<String, Object> getDataFormatProperties() {
538        return dataFormatProperties;
539    }
540
541    /**
542     * Sets additional options on data format level
543     *
544     * @param dataFormatProperties the options
545     */
546    public void setDataFormatProperties(Map<String, Object> dataFormatProperties) {
547        this.dataFormatProperties = dataFormatProperties;
548    }
549
550    public Map<String, Object> getApiProperties() {
551        return apiProperties;
552    }
553
554    /**
555     * Sets additional options on api level
556     *
557     * @param apiProperties the options
558     */
559    public void setApiProperties(Map<String, Object> apiProperties) {
560        this.apiProperties = apiProperties;
561    }
562
563    /**
564     * Gets the CORS headers to use if CORS has been enabled.
565     *
566     * @return the CORS headers
567     */
568    public Map<String, String> getCorsHeaders() {
569        return corsHeaders;
570    }
571
572    /**
573     * Sets the CORS headers to use if CORS has been enabled.
574     *
575     * @param corsHeaders the CORS headers
576     */
577    public void setCorsHeaders(Map<String, String> corsHeaders) {
578        this.corsHeaders = corsHeaders;
579    }
580}