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.ObjectHelper; 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 public RestOperationResponseMsgDefinition(VerbDefinition verb) { 059 this(); 060 this.verb = verb; 061 } 062 063 public RestOperationResponseMsgDefinition() { 064 this.code = "200"; 065 this.message = "success"; 066 } 067 068 public String getCode() { 069 return code; 070 } 071 072 public void setCode(String code) { 073 this.code = code; 074 } 075 076 public String getResponseModel() { 077 return responseModel != null ? responseModel : ""; 078 } 079 080 public void setResponseModel(String responseModel) { 081 this.responseModel = responseModel; 082 } 083 084 public String getMessage() { 085 return message; 086 } 087 088 public void setMessage(String message) { 089 this.message = message; 090 } 091 092 public List<RestOperationResponseHeaderDefinition> getHeaders() { 093 return headers; 094 } 095 096 public void setHeaders(List<RestOperationResponseHeaderDefinition> headers) { 097 this.headers = headers; 098 } 099 100 /** 101 * The response code such as a HTTP status code. 102 */ 103 public RestOperationResponseMsgDefinition code(int code) { 104 setCode("" + code); 105 return this; 106 } 107 108 /** 109 * The response code such as a HTTP status code. Can use <tt>general</tt>, or other words 110 * to indicate general error responses that do not map to a specific HTTP status code. 111 */ 112 public RestOperationResponseMsgDefinition code(String code) { 113 setCode(code); 114 return this; 115 } 116 117 /** 118 * The response message (description) 119 */ 120 public RestOperationResponseMsgDefinition message(String msg) { 121 setMessage(msg); 122 return this; 123 } 124 125 /** 126 * The response model 127 */ 128 public RestOperationResponseMsgDefinition responseModel(Class<?> type) { 129 setResponseModel(type.getCanonicalName()); 130 return this; 131 } 132 133 /** 134 * Adds a response header 135 */ 136 public RestOperationResponseHeaderDefinition header(String name) { 137 if (headers == null) { 138 headers = new ArrayList<RestOperationResponseHeaderDefinition>(); 139 } 140 RestOperationResponseHeaderDefinition header = new RestOperationResponseHeaderDefinition(this); 141 header.setName(name); 142 headers.add(header); 143 return header; 144 } 145 146 /** 147 * Ends the configuration of this response message 148 */ 149 public RestDefinition endResponseMessage() { 150 // code and message is mandatory 151 ObjectHelper.notEmpty(code, "code"); 152 ObjectHelper.notEmpty(message, "message"); 153 verb.getResponseMsgs().add(this); 154 return verb.getRest(); 155 } 156 157}