001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest; 014 015import static org.apache.juneau.http.HttpMethodName.*; 016import static org.apache.juneau.jsonschema.JsonSchemaGenerator.*; 017 018import org.apache.juneau.dto.swagger.*; 019import org.apache.juneau.dto.swagger.ui.*; 020import org.apache.juneau.html.*; 021import org.apache.juneau.jso.*; 022import org.apache.juneau.json.*; 023import org.apache.juneau.plaintext.*; 024import org.apache.juneau.rest.annotation.*; 025import org.apache.juneau.soap.*; 026import org.apache.juneau.uon.*; 027import org.apache.juneau.urlencoding.*; 028import org.apache.juneau.xml.*; 029import org.apache.juneau.xmlschema.*; 030 031/** 032 * Subclass of {@link RestServlet} with default serializers and parsers defined. 033 * 034 * <p> 035 * Supports the following request <code>Accept</code> header values with the resulting response <code>Content-Type</code>: 036 * <table class='styled'> 037 * <tr> 038 * <th>Accept</th> 039 * <th>Content-Type</th> 040 * <th>Serializer</th> 041 * </tr> 042 * <tr> 043 * <td class='code'>application/json<br>text/json</td> 044 * <td class='code'>application/json</td> 045 * <td>{@link JsonSerializer}</td> 046 * </tr> 047 * <tr> 048 * <td class='code'>application/json+simple<br>text/json+simple</td> 049 * <td class='code'>application/json</td> 050 * <td>{@link org.apache.juneau.json.SimpleJsonSerializer}</td> 051 * </tr> 052 * <tr> 053 * <td class='code'>application/json+schema<br>text/json+schema</td> 054 * <td class='code'>application/json</td> 055 * <td>{@link JsonSchemaSerializer}</td> 056 * </tr> 057 * <tr> 058 * <td class='code'>text/xml</td> 059 * <td class='code'>text/xml</td> 060 * <td>{@link XmlDocSerializer}</td> 061 * </tr> 062 * <tr> 063 * <td class='code'>text/xml+schema</td> 064 * <td class='code'>text/xml</td> 065 * <td>{@link XmlSchemaDocSerializer}</td> 066 * </tr> 067 * <tr> 068 * <td class='code'>text/html</td> 069 * <td class='code'>text/html</td> 070 * <td>{@link HtmlDocSerializer}</td> 071 * </tr> 072 * <tr> 073 * <td class='code'>text/html+stripped</td> 074 * <td class='code'>text/html</td> 075 * <td>{@link HtmlStrippedDocSerializer}</td> 076 * </tr> 077 * <tr> 078 * <td class='code'>text/uon</td> 079 * <td class='code'>text/uon</td> 080 * <td>{@link UonSerializer}</td> 081 * </tr> 082 * <tr> 083 * <td class='code'>application/x-www-form-urlencoded</td> 084 * <td class='code'>application/x-www-form-urlencoded</td> 085 * <td>{@link UrlEncodingSerializer}</td> 086 * </tr> 087 * <tr> 088 * <td class='code'>text/xml+soap</td> 089 * <td class='code'>text/xml</td> 090 * <td>{@link SoapXmlSerializer}</td> 091 * </tr> 092 * <tr> 093 * <td class='code'>text/plain</td> 094 * <td class='code'>text/plain</td> 095 * <td>{@link PlainTextSerializer}</td> 096 * </tr> 097 * </table> 098 * <p> 099 * Supports the following request <code>Content-Type</code> header values: 100 * </p> 101 * <table class='styled'> 102 * <tr> 103 * <th>Content-Type</th> 104 * <th>Parser</th> 105 * </tr> 106 * <tr> 107 * <td class='code'>application/json<br>text/json</td> 108 * <td>{@link JsonParser}</td> 109 * </tr> 110 * <tr> 111 * <td class='code'>text/xml<br>application/xml</td> 112 * <td>{@link XmlParser}</td> 113 * </tr> 114 * <tr> 115 * <td class='code'>text/html<br>text/html+stripped</td> 116 * <td>{@link HtmlParser}</td> 117 * </tr> 118 * <tr> 119 * <td class='code'>text/uon</td> 120 * <td>{@link UonParser}</td> 121 * </tr> 122 * <tr> 123 * <td class='code'>application/x-www-form-urlencoded</td> 124 * <td>{@link UrlEncodingParser}</td> 125 * </tr> 126 * <tr> 127 * <td class='code'>text/plain</td> 128 * <td>{@link PlainTextParser}</td> 129 * </tr> 130 * </table> 131 * 132 * <p> 133 * It should be noted that we do NOT add {@link JsoParser} to the list of parsers since this could cause security 134 * issues. 135 * Use caution when using this particular parser as it could inadvertently cause code execution security holes. 136 * 137 * <p> 138 * The list of serializers and parsers can be appended to using the 139 * {@link RestResource#serializers() @RestResource(serializers)} and 140 * {@link RestResource#parsers() @RestResource(parsers)} annotations on subclasses. 141 * 142 * <p> 143 * This subclass also provides a default OPTIONS page by implementing a {@link #getOptions(RestRequest)} that returns a 144 * POJO consisting of beans describing the class. 145 * 146 * <p> 147 * The OPTIONS page can be modified or augmented by overriding this method and providing your own data. 148 * 149 * <h5 class='section'>Notes:</h5> 150 * <ul class='spaced-list'> 151 * <li> 152 * Provides a default HTML stylesheet by setting {@link HtmlDoc#stylesheet() @HtmlDoc(stylesheet)} 153 * to <js>"styles/juneau.css"</js>. 154 * <li> 155 * Provides a default classpath entry "htdocs" by setting 156 * {@link RestResource#staticFiles() @RestResource(staticFiles)} to <code>{<js>"htdocs:htdocs"</js>,<js>"styles:styles"</js>}</code>. 157 * This allows files inside the <code>[servletPackage].htdocs</code> package to be served up under the URL 158 * <code>/servletPath/htdocs</code>. 159 * </ul> 160 * 161 * <h5 class='section'>See Also:</h5> 162 * <ul> 163 * <li class='link'>{@doc juneau-rest-server.Instantiation.BasicRestServlet} 164 * </ul> 165 */ 166@RestResource( 167 168 // Allow OPTIONS requests to be simulated using ?method=OPTIONS query parameter. 169 allowedMethodParams="OPTIONS", 170 171 // HTML-page specific settings. 172 htmldoc=@HtmlDoc( 173 // Basic page navigation links. 174 navlinks={ 175 "up: request:/..", 176 "options: servlet:/?method=OPTIONS" 177 } 178 ) 179) 180public abstract class BasicRestServlet extends RestServlet implements BasicRestConfig { 181 private static final long serialVersionUID = 1L; 182 183 /** 184 * [OPTIONS /*] - Show resource options. 185 * 186 * @param req The HTTP request. 187 * @return A bean containing the contents for the OPTIONS page. 188 */ 189 @RestMethod(name=OPTIONS, path="/*", 190 191 summary="Swagger documentation", 192 description="Swagger documentation for this resource.", 193 194 htmldoc=@HtmlDoc( 195 // Override the nav links for the swagger page. 196 navlinks={ 197 "back: servlet:/", 198 "json: servlet:/?method=OPTIONS&Accept=text/json&plainText=true" 199 }, 200 // Never show aside contents of page inherited from class. 201 aside="NONE" 202 ), 203 204 // POJO swaps to apply to all serializers/parsers on this method. 205 pojoSwaps={ 206 // Use the SwaggerUI swap when rendering Swagger beans. 207 // This is a per-media-type swap that only applies to text/html requests. 208 SwaggerUI.class 209 }, 210 211 // Properties to apply to all serializers/parsers and REST-specific API objects on this method. 212 properties={ 213 // Add descriptions to the following types when not specified: 214 @Property(name=JSONSCHEMA_addDescriptionsTo, value="bean,collection,array,map,enum"), 215 216 // Add x-example to the following types: 217 @Property(name=JSONSCHEMA_addExamplesTo, value="bean,collection,array,map"), 218 219 // Don't generate schema information on the Swagger bean itself or HTML beans. 220 @Property(name=JSONSCHEMA_ignoreTypes, value="Swagger,org.apache.juneau.dto.html5.*") 221 }, 222 223 // Shortcut for boolean properties. 224 flags={ 225 // Use $ref references for bean definitions to reduce duplication in Swagger. 226 JSONSCHEMA_useBeanDefs, 227 228 // When parsing generated beans, ignore unknown properties that may only exist as getters and not setters. 229 BEAN_ignoreUnknownBeanProperties 230 } 231 ) 232 public Swagger getOptions(RestRequest req) { 233 // Localized Swagger for this resource is available through the RestRequest object. 234 return req.getSwagger(); 235 } 236}