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>,
125     * or other words to indicate general error responses that do not map to a
126     * specific HTTP status code
127     */
128    public RestOperationResponseMsgDefinition code(String code) {
129        setCode(code);
130        return this;
131    }
132
133    /**
134     * The response message (description)
135     */
136    public RestOperationResponseMsgDefinition message(String msg) {
137        setMessage(msg);
138        return this;
139    }
140
141    /**
142     * The response model
143     */
144    public RestOperationResponseMsgDefinition responseModel(Class<?> type) {
145        setResponseModel(type.getCanonicalName());
146        return this;
147    }
148
149    /**
150     * Adds an example
151     */
152    public RestOperationResponseMsgDefinition example(String key, String example) {
153        if (examples == null) {
154            examples = new ArrayList<>();
155        }
156        examples.add(new RestPropertyDefinition(key, example));
157        return this;
158    }
159
160    /**
161     * Adds a response header
162     */
163    public RestOperationResponseHeaderDefinition header(String name) {
164        if (headers == null) {
165            headers = new ArrayList<>();
166        }
167        RestOperationResponseHeaderDefinition header = new RestOperationResponseHeaderDefinition(this);
168        header.setName(name);
169        headers.add(header);
170        return header;
171    }
172
173    /**
174     * Ends the configuration of this response message
175     */
176    public RestDefinition endResponseMessage() {
177        // code and message is mandatory
178        StringHelper.notEmpty(code, "code");
179        StringHelper.notEmpty(message, "message");
180        verb.getResponseMsgs().add(this);
181        return verb.getRest();
182    }
183
184}