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.model.rest;
018
019import java.util.ArrayList;
020import java.util.List;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.util.StringHelper;
030
031/**
032 * To specify the rest operation response messages using Swagger.
033 * <p/>
034 * This maps to the Swagger Response Message Object.
035 */
036@Metadata(label = "rest")
037@XmlRootElement(name = "responseMessage")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class RestOperationResponseMsgDefinition {
040
041    @XmlTransient
042    private VerbDefinition verb;
043
044    @XmlAttribute
045    @Metadata(defaultValue = "200")
046    private String code;
047
048    @XmlAttribute(required = true)
049    private String message;
050
051    @XmlAttribute
052    @Metadata(defaultValue = "")
053    private String responseModel;
054
055    @XmlElement(name = "header")
056    private List<RestOperationResponseHeaderDefinition> headers;
057
058    @XmlElement(name = "examples")
059    private List<RestPropertyDefinition> examples;
060
061    public RestOperationResponseMsgDefinition(VerbDefinition verb) {
062        this();
063        this.verb = verb;
064    }
065
066    public RestOperationResponseMsgDefinition() {
067        this.code = "200";
068        this.message = "success";
069    }
070
071    public String getCode() {
072        return code;
073    }
074
075    public void setCode(String code) {
076        this.code = code;
077    }
078
079    public String getResponseModel() {
080        return responseModel != null ? responseModel : "";
081    }
082
083    public void setResponseModel(String responseModel) {
084        this.responseModel = responseModel;
085    }
086
087    public String getMessage() {
088        return message;
089    }
090
091    public void setMessage(String message) {
092        this.message = message;
093    }
094
095    public List<RestOperationResponseHeaderDefinition> getHeaders() {
096        return headers;
097    }
098
099    public void setHeaders(List<RestOperationResponseHeaderDefinition> headers) {
100        this.headers = headers;
101    }
102
103    public List<RestPropertyDefinition> getExamples() {
104        return examples;
105    }
106
107    /**
108     * Examples of response messages
109     */
110    public void setExamples(List<RestPropertyDefinition> examples) {
111        this.examples = examples;
112    }
113
114    /**
115     * The response code such as a HTTP status code
116     */
117    public RestOperationResponseMsgDefinition code(int code) {
118        setCode("" + code);
119        return this;
120    }
121
122    /**
123     * The response code such as a HTTP status code. Can use <tt>general</tt>, or other words
124     * to indicate general error responses that do not map to a specific HTTP status code
125     */
126    public RestOperationResponseMsgDefinition code(String code) {
127        setCode(code);
128        return this;
129    }
130
131    /**
132     * The response message (description)
133     */
134    public RestOperationResponseMsgDefinition message(String msg) {
135        setMessage(msg);
136        return this;
137    }
138
139    /**
140     * The response model
141     */
142    public RestOperationResponseMsgDefinition responseModel(Class<?> type) {
143        setResponseModel(type.getCanonicalName());
144        return this;
145    }
146
147    /**
148     * Adds an example
149     */
150    public RestOperationResponseMsgDefinition example(String key, String example) {
151        if (examples == null) {
152            examples = new ArrayList<>();
153        }
154        examples.add(new RestPropertyDefinition(key, example));
155        return this;
156    }
157
158    /**
159     * Adds a response header
160     */
161    public RestOperationResponseHeaderDefinition header(String name) {
162        if (headers == null) {
163            headers = new ArrayList<>();
164        }
165        RestOperationResponseHeaderDefinition header = new RestOperationResponseHeaderDefinition(this);
166        header.setName(name);
167        headers.add(header);
168        return header;
169    }
170
171    /**
172     * Ends the configuration of this response message
173     */
174    public RestDefinition endResponseMessage() {
175        // code and message is mandatory
176        StringHelper.notEmpty(code, "code");
177        StringHelper.notEmpty(message, "message");
178        verb.getResponseMsgs().add(this);
179        return verb.getRest();
180    }
181
182}