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 scheme;
043    private String host;
044    private int port;
045    private String contextPath;
046    private String apiContextPath;
047    private String apiContextRouteId;
048    private String apiContextIdPattern;
049    private boolean apiContextListing;
050    private RestHostNameResolver restHostNameResolver = RestHostNameResolver.allLocalIp;
051    private RestBindingMode bindingMode = RestBindingMode.off;
052    private boolean skipBindingOnErrorCode = true;
053    private boolean enableCORS;
054    private String jsonDataFormat;
055    private String xmlDataFormat;
056    private Map<String, Object> componentProperties;
057    private Map<String, Object> endpointProperties;
058    private Map<String, Object> consumerProperties;
059    private Map<String, Object> dataFormatProperties;
060    private Map<String, Object> apiProperties;
061    private Map<String, String> corsHeaders;
062
063    /**
064     * Gets the name of the Camel component to use as the REST consumer
065     *
066     * @return the component name, or <tt>null</tt> to let Camel search the {@link Registry} to find suitable implementation
067     */
068    public String getComponent() {
069        return component;
070    }
071
072    /**
073     * Sets the name of the Camel component to use as the REST consumer
074     *
075     * @param componentName the name of the component (such as restlet, spark-rest, etc.)
076     */
077    public void setComponent(String componentName) {
078        this.component = componentName;
079    }
080
081    /**
082     * Gets the name of the Camel component to use as the REST API (such as swagger)
083     *
084     * @return the component name, or <tt>null</tt> to let Camel use the default name <tt>swagger</tt>
085     */
086    public String getApiComponent() {
087        return apiComponent;
088    }
089
090    /**
091     * Sets the name of the Camel component to use as the REST API (such as swagger)
092     *
093     * @param apiComponent the name of the component (such as swagger)
094     */
095    public void setApiComponent(String apiComponent) {
096        this.apiComponent = apiComponent;
097    }
098
099    /**
100     * Gets the hostname to use by the REST consumer
101     *
102     * @return the hostname, or <tt>null</tt> to use default hostname
103     */
104    public String getHost() {
105        return host;
106    }
107
108    /**
109     * Sets the hostname to use by the REST consumer
110     *
111     * @param host the hostname
112     */
113    public void setHost(String host) {
114        this.host = host;
115    }
116
117    /**
118     * Gets the scheme to use by the REST consumer
119     *
120     * @return the scheme, or <tt>null</tt> to use default scheme
121     */
122    public String getScheme() {
123        return scheme;
124    }
125
126    /**
127     * Sets the scheme to use by the REST consumer
128     *
129     * @param scheme the scheme
130     */
131    public void setScheme(String scheme) {
132        this.scheme = scheme;
133    }
134
135    /**
136     * Gets the port to use by the REST consumer
137     *
138     * @return the port, or <tt>0</tt> or <tt>-1</tt> to use default port
139     */
140    public int getPort() {
141        return port;
142    }
143
144    /**
145     * Sets the port to use by the REST consumer
146     *
147     * @param port the port number
148     */
149    public void setPort(int port) {
150        this.port = port;
151    }
152
153    /**
154     * Gets the configured context-path
155     *
156     * @return the context path, or <tt>null</tt> if none configured.
157     */
158    public String getContextPath() {
159        return contextPath;
160    }
161
162    /**
163     * Sets a leading context-path the REST services will be using.
164     * <p/>
165     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
166     * is deployed using a context-path. Or for components such as <tt>camel-jetty</tt> or <tt>camel-netty4-http</tt>
167     * that includes a HTTP server.
168     *
169     * @param contextPath the context path
170     */
171    public void setContextPath(String contextPath) {
172        this.contextPath = contextPath;
173    }
174
175    public String getApiContextPath() {
176        return apiContextPath;
177    }
178
179    /**
180     * Sets a leading API context-path the REST API services will be using.
181     * <p/>
182     * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application
183     * is deployed using a context-path.
184     *
185     * @param contextPath the API context path
186     */
187    public void setApiContextPath(String contextPath) {
188        this.apiContextPath = contextPath;
189    }
190
191    public String getApiContextRouteId() {
192        return apiContextRouteId;
193    }
194
195    /**
196     * Sets the route id to use for the route that services the REST API.
197     * <p/>
198     * The route will by default use an auto assigned route id.
199     *
200     * @param apiContextRouteId  the route id
201     */
202    public void setApiContextRouteId(String apiContextRouteId) {
203        this.apiContextRouteId = apiContextRouteId;
204    }
205
206    public String getApiContextIdPattern() {
207        return apiContextIdPattern;
208    }
209
210    /**
211     * Optional CamelContext id pattern to only allow Rest APIs from rest services within CamelContext's which name matches the pattern.
212     * <p/>
213     * The pattern <tt>#name#</tt> refers to the CamelContext name, to match on the current CamelContext only.
214     * For any other value, the pattern uses the rules from {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)}
215     *
216     * @param apiContextIdPattern  the pattern
217     */
218    public void setApiContextIdPattern(String apiContextIdPattern) {
219        this.apiContextIdPattern = apiContextIdPattern;
220    }
221
222    public boolean isApiContextListing() {
223        return apiContextListing;
224    }
225
226    /**
227     * Sets whether listing of all available CamelContext's with REST services in the JVM is enabled. If enabled it allows to discover
228     * these contexts, if <tt>false</tt> then only the current CamelContext is in use.
229     */
230    public void setApiContextListing(boolean apiContextListing) {
231        this.apiContextListing = apiContextListing;
232    }
233
234    /**
235     * Gets the resolver to use for resolving hostname
236     *
237     * @return the resolver
238     */
239    public RestHostNameResolver getRestHostNameResolver() {
240        return restHostNameResolver;
241    }
242
243    /**
244     * Sets the resolver to use for resolving hostname
245     *
246     * @param restHostNameResolver the resolver
247     */
248    public void setRestHostNameResolver(RestHostNameResolver restHostNameResolver) {
249        this.restHostNameResolver = restHostNameResolver;
250    }
251
252    /**
253     * Sets the resolver to use for resolving hostname
254     *
255     * @param restHostNameResolver the resolver
256     */
257    public void setRestHostNameResolver(String restHostNameResolver) {
258        this.restHostNameResolver = RestHostNameResolver.valueOf(restHostNameResolver);
259    }
260
261    /**
262     * Gets the binding mode used by the REST consumer
263     *
264     * @return the binding mode
265     */
266    public RestBindingMode getBindingMode() {
267        return bindingMode;
268    }
269
270    /**
271     * Sets the binding mode to be used by the REST consumer
272     *
273     * @param bindingMode the binding mode
274     */
275    public void setBindingMode(RestBindingMode bindingMode) {
276        this.bindingMode = bindingMode;
277    }
278
279    /**
280     * Sets the binding mode to be used by the REST consumer
281     *
282     * @param bindingMode the binding mode
283     */
284    public void setBindingMode(String bindingMode) {
285        this.bindingMode = RestBindingMode.valueOf(bindingMode);
286    }
287
288    /**
289     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
290     * <p/>
291     * This option is default <tt>true</tt>.
292     *
293     * @return whether to skip binding on error code
294     */
295    public boolean isSkipBindingOnErrorCode() {
296        return skipBindingOnErrorCode;
297    }
298
299    /**
300     * Whether to skip binding output if there is a custom HTTP error code, and instead use the response body as-is.
301     * <p/>
302     * This option is default <tt>true</tt>.
303     *
304     * @param skipBindingOnErrorCode whether to skip binding on error code
305     */
306    public void setSkipBindingOnErrorCode(boolean skipBindingOnErrorCode) {
307        this.skipBindingOnErrorCode = skipBindingOnErrorCode;
308    }
309
310    /**
311     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
312     * <p/>
313     * This option is default <tt>false</tt>
314     *
315     * @return whether CORS is enabled or not
316     */
317    public boolean isEnableCORS() {
318        return enableCORS;
319    }
320
321    /**
322     * To specify whether to enable CORS which means Camel will automatic include CORS in the HTTP headers in the response.
323     * <p/>
324     * This option is default <tt>false</tt>
325     *
326     * @param enableCORS <tt>true</tt> to enable CORS
327     */
328    public void setEnableCORS(boolean enableCORS) {
329        this.enableCORS = enableCORS;
330    }
331
332    /**
333     * Gets the name of the json data format.
334     * <p/>
335     * <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.
336     *
337     * @return the name, or <tt>null</tt> to use default
338     */
339    public String getJsonDataFormat() {
340        return jsonDataFormat;
341    }
342
343    /**
344     * Sets a custom json data format to be used
345     * <p/>
346     * <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.
347     *
348     * @param name name of the data format
349     */
350    public void setJsonDataFormat(String name) {
351        this.jsonDataFormat = name;
352    }
353
354    /**
355     * Gets the name of the xml data format.
356     * <p/>
357     * <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.
358     *
359     * @return the name, or <tt>null</tt> to use default
360     */
361    public String getXmlDataFormat() {
362        return xmlDataFormat;
363    }
364
365    /**
366     * Sets a custom xml data format to be used.
367     * <p/>
368     * <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.
369     *
370     * @param name name of the data format
371     */
372    public void setXmlDataFormat(String name) {
373        this.xmlDataFormat = name;
374    }
375
376    /**
377     * Gets additional options on component level
378     *
379     * @return additional options
380     */
381    public Map<String, Object> getComponentProperties() {
382        return componentProperties;
383    }
384
385    /**
386     * Sets additional options on component level
387     *
388     * @param componentProperties the options
389     */
390    public void setComponentProperties(Map<String, Object> componentProperties) {
391        this.componentProperties = componentProperties;
392    }
393
394    /**
395     * Gets additional options on endpoint level
396     *
397     * @return additional options
398     */
399    public Map<String, Object> getEndpointProperties() {
400        return endpointProperties;
401    }
402
403    /**
404     * Sets additional options on endpoint level
405     *
406     * @param endpointProperties the options
407     */
408    public void setEndpointProperties(Map<String, Object> endpointProperties) {
409        this.endpointProperties = endpointProperties;
410    }
411
412    /**
413     * Gets additional options on consumer level
414     *
415     * @return additional options
416     */
417    public Map<String, Object> getConsumerProperties() {
418        return consumerProperties;
419    }
420
421    /**
422     * Sets additional options on consumer level
423     *
424     * @param consumerProperties the options
425     */
426    public void setConsumerProperties(Map<String, Object> consumerProperties) {
427        this.consumerProperties = consumerProperties;
428    }
429
430    /**
431     * Gets additional options on data format level
432     *
433     * @return additional options
434     */
435    public Map<String, Object> getDataFormatProperties() {
436        return dataFormatProperties;
437    }
438
439    /**
440     * Sets additional options on data format level
441     *
442     * @param dataFormatProperties the options
443     */
444    public void setDataFormatProperties(Map<String, Object> dataFormatProperties) {
445        this.dataFormatProperties = dataFormatProperties;
446    }
447
448    public Map<String, Object> getApiProperties() {
449        return apiProperties;
450    }
451
452    /**
453     * Sets additional options on api level
454     *
455     * @param apiProperties the options
456     */
457    public void setApiProperties(Map<String, Object> apiProperties) {
458        this.apiProperties = apiProperties;
459    }
460
461    /**
462     * Gets the CORS headers to use if CORS has been enabled.
463     *
464     * @return the CORS headers
465     */
466    public Map<String, String> getCorsHeaders() {
467        return corsHeaders;
468    }
469
470    /**
471     * Sets the CORS headers to use if CORS has been enabled.
472     *
473     * @param corsHeaders the CORS headers
474     */
475    public void setCorsHeaders(Map<String, String> corsHeaders) {
476        this.corsHeaders = corsHeaders;
477    }
478}