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