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 org.apache.juneau.dto.swagger.*; 016import org.apache.juneau.html.*; 017import org.apache.juneau.html.annotation.*; 018import org.apache.juneau.jso.*; 019import org.apache.juneau.json.*; 020import org.apache.juneau.plaintext.*; 021import org.apache.juneau.rest.annotation.*; 022import org.apache.juneau.soap.*; 023import org.apache.juneau.uon.*; 024import org.apache.juneau.urlencoding.*; 025import org.apache.juneau.xml.*; 026 027/** 028 * Subclass of {@link RestServlet} with default serializers and parsers defined. 029 * 030 * <p> 031 * Supports the following request <c>Accept</c> header values with the resulting response <c>Content-Type</c>: 032 * <table class='styled'> 033 * <tr> 034 * <th>Accept</th> 035 * <th>Content-Type</th> 036 * <th>Serializer</th> 037 * </tr> 038 * <tr> 039 * <td class='code'>application/json<br>text/json</td> 040 * <td class='code'>application/json</td> 041 * <td>{@link JsonSerializer}</td> 042 * </tr> 043 * <tr> 044 * <td class='code'>application/json+simple<br>text/json+simple</td> 045 * <td class='code'>application/json</td> 046 * <td>{@link org.apache.juneau.json.SimpleJsonSerializer}</td> 047 * </tr> 048 * <tr> 049 * <td class='code'>application/json+schema<br>text/json+schema</td> 050 * <td class='code'>application/json</td> 051 * <td>{@link JsonSchemaSerializer}</td> 052 * </tr> 053 * <tr> 054 * <td class='code'>text/xml</td> 055 * <td class='code'>text/xml</td> 056 * <td>{@link XmlDocSerializer}</td> 057 * </tr> 058 * <tr> 059 * <td class='code'>text/xml+schema</td> 060 * <td class='code'>text/xml</td> 061 * <td>{@link org.apache.juneau.xmlschema.XmlSchemaDocSerializer}</td> 062 * </tr> 063 * <tr> 064 * <td class='code'>text/html</td> 065 * <td class='code'>text/html</td> 066 * <td>{@link HtmlDocSerializer}</td> 067 * </tr> 068 * <tr> 069 * <td class='code'>text/html+stripped</td> 070 * <td class='code'>text/html</td> 071 * <td>{@link HtmlStrippedDocSerializer}</td> 072 * </tr> 073 * <tr> 074 * <td class='code'>text/uon</td> 075 * <td class='code'>text/uon</td> 076 * <td>{@link UonSerializer}</td> 077 * </tr> 078 * <tr> 079 * <td class='code'>application/x-www-form-urlencoded</td> 080 * <td class='code'>application/x-www-form-urlencoded</td> 081 * <td>{@link UrlEncodingSerializer}</td> 082 * </tr> 083 * <tr> 084 * <td class='code'>text/xml+soap</td> 085 * <td class='code'>text/xml</td> 086 * <td>{@link SoapXmlSerializer}</td> 087 * </tr> 088 * <tr> 089 * <td class='code'>text/plain</td> 090 * <td class='code'>text/plain</td> 091 * <td>{@link PlainTextSerializer}</td> 092 * </tr> 093 * </table> 094 * <p> 095 * Supports the following request <c>Content-Type</c> header values: 096 * </p> 097 * <table class='styled'> 098 * <tr> 099 * <th>Content-Type</th> 100 * <th>Parser</th> 101 * </tr> 102 * <tr> 103 * <td class='code'>application/json<br>text/json</td> 104 * <td>{@link JsonParser}</td> 105 * </tr> 106 * <tr> 107 * <td class='code'>text/xml<br>application/xml</td> 108 * <td>{@link XmlParser}</td> 109 * </tr> 110 * <tr> 111 * <td class='code'>text/html<br>text/html+stripped</td> 112 * <td>{@link HtmlParser}</td> 113 * </tr> 114 * <tr> 115 * <td class='code'>text/uon</td> 116 * <td>{@link UonParser}</td> 117 * </tr> 118 * <tr> 119 * <td class='code'>application/x-www-form-urlencoded</td> 120 * <td>{@link UrlEncodingParser}</td> 121 * </tr> 122 * <tr> 123 * <td class='code'>text/plain</td> 124 * <td>{@link PlainTextParser}</td> 125 * </tr> 126 * </table> 127 * 128 * <p> 129 * It should be noted that we do NOT add {@link JsoParser} to the list of parsers since this could cause security 130 * issues. 131 * Use caution when using this particular parser as it could inadvertently cause code execution security holes. 132 * 133 * <p> 134 * The list of serializers and parsers can be appended to using the 135 * {@link Rest#serializers() @Rest(serializers)} and 136 * {@link Rest#parsers() @Rest(parsers)} annotations on subclasses. 137 * 138 * <p> 139 * This subclass also provides a default OPTIONS page by implementing a {@link #getOptions(RestRequest)} that returns a 140 * POJO consisting of beans describing the class. 141 * 142 * <p> 143 * The OPTIONS page can be modified or augmented by overriding this method and providing your own data. 144 * 145 * <ul class='notes'> 146 * <li> 147 * Provides a default HTML stylesheet by setting {@link HtmlDocConfig#stylesheet() HtmlDocConfig(stylesheet)} 148 * to <js>"styles/juneau.css"</js>. 149 * <li> 150 * Provides a default classpath entry "htdocs" by setting 151 * {@link Rest#staticFiles() @Rest(staticFiles)} to <code>{<js>"htdocs:htdocs"</js>,<js>"styles:styles"</js>}</code>. 152 * This allows files inside the <c>[servletPackage].htdocs</c> package to be served up under the URL 153 * <c>/servletPath/htdocs</c>. 154 * </ul> 155 * 156 * <ul class='seealso'> 157 * <li class='link'>{@doc juneau-rest-server.Instantiation.BasicRestServlet} 158 * </ul> 159 */ 160@Rest( 161 // Allow OPTIONS requests to be simulated using ?method=OPTIONS query parameter. 162 allowedMethodParams="OPTIONS" 163) 164@HtmlDocConfig( 165 // Basic page navigation links. 166 navlinks={ 167 "up: request:/..", 168 "options: servlet:/?method=OPTIONS", 169 "stats: servlet:/stats" 170 } 171) 172public abstract class BasicRestServlet extends RestServlet implements BasicRestConfig { 173 private static final long serialVersionUID = 1L; 174 175 /** 176 * [OPTIONS /*] - Show resource options. 177 * 178 * @param req The HTTP request. 179 * @return A bean containing the contents for the OPTIONS page. 180 */ 181 @Override /* BasicRestConfig */ 182 public Swagger getOptions(RestRequest req) { 183 // Localized Swagger for this resource is available through the RestRequest object. 184 return req.getSwagger(); 185 } 186 187 /** 188 * [* /error] - Error occurred. 189 * 190 * <p> 191 * Servlet chains will often automatically redirect to <js>"/error"</js> when any sort of error condition occurs 192 * (such as failed authentication) and will set appropriate response parameters (such as an <c>WWW-Authenticate</c> 193 * response header). 194 * 195 * <p> 196 * These responses should be left as-is without any additional processing. 197 */ 198 @Override /* BasicRestConfig */ 199 public void error() {} 200 201 /** 202 * [GET /stats] - Timing statistics. 203 * 204 * <p> 205 * Timing statistics for method invocations on this resource. 206 * 207 * @return A collection of timing statistics for each annotated method on this resource. 208 */ 209 @Override /* BasicRestConfig */ 210 public RestContextStats getStats(RestRequest req) { 211 return req.getContext().getStats(); 212 } 213}