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.*;
016
017import org.apache.juneau.dto.swagger.*;
018import org.apache.juneau.html.*;
019import org.apache.juneau.jso.*;
020import org.apache.juneau.json.*;
021import org.apache.juneau.plaintext.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.soap.*;
024import org.apache.juneau.uon.*;
025import org.apache.juneau.urlencoding.*;
026import org.apache.juneau.xml.*;
027
028/**
029 * Subclass of {@link RestServlet} with default serializers and parsers defined.
030 * 
031 * <p>
032 * Supports the following request <code>Accept</code> header values with the resulting response <code>Content-Type</code>:
033 * <table class='styled'>
034 *    <tr>
035 *       <th>Accept</th>
036 *       <th>Content-Type</th>
037 *       <th>Serializer</th>
038 *    </tr>
039 *    <tr>
040 *       <td class='code'>application/json<br>text/json</td>
041 *       <td class='code'>application/json</td>
042 *       <td>{@link JsonSerializer}</td>
043 *    </tr>
044 *    <tr>
045 *       <td class='code'>application/json+simple<br>text/json+simple</td>
046 *       <td class='code'>application/json</td>
047 *       <td>{@link org.apache.juneau.json.JsonSerializer.Simple}</td>
048 *    </tr>
049 *    <tr>
050 *       <td class='code'>application/json+schema<br>text/json+schema</td>
051 *       <td class='code'>application/json</td>
052 *       <td>{@link JsonSchemaSerializer}</td>
053 *    </tr>
054 *    <tr>
055 *       <td class='code'>text/xml</td>
056 *       <td class='code'>text/xml</td>
057 *       <td>{@link XmlDocSerializer}</td>
058 *    </tr>
059 *    <tr>
060 *       <td class='code'>text/xml+schema</td>
061 *       <td class='code'>text/xml</td>
062 *       <td>{@link XmlSchemaDocSerializer}</td>
063 *    </tr>
064 *    <tr>
065 *       <td class='code'>text/html</td>
066 *       <td class='code'>text/html</td>
067 *       <td>{@link HtmlDocSerializer}</td>
068 *    </tr>
069 *    <tr>
070 *       <td class='code'>text/html+stripped</td>
071 *       <td class='code'>text/html</td>
072 *       <td>{@link HtmlStrippedDocSerializer}</td>
073 *    </tr>
074 *    <tr>
075 *       <td class='code'>text/uon</td>
076 *       <td class='code'>text/uon</td>
077 *       <td>{@link UonSerializer}</td>
078 *    </tr>
079 *    <tr>
080 *       <td class='code'>application/x-www-form-urlencoded</td>
081 *       <td class='code'>application/x-www-form-urlencoded</td>
082 *       <td>{@link UrlEncodingSerializer}</td>
083 *    </tr>
084 *    <tr>
085 *       <td class='code'>text/xml+soap</td>
086 *       <td class='code'>text/xml</td>
087 *       <td>{@link SoapXmlSerializer}</td>
088 *    </tr>
089 *    <tr>
090 *       <td class='code'>text/plain</td>
091 *       <td class='code'>text/plain</td>
092 *       <td>{@link PlainTextSerializer}</td>
093 *    </tr>
094 * </table>
095 * <p>
096 * Supports the following request <code>Content-Type</code> header values:
097 * </p>
098 * <table class='styled'>
099 *    <tr>
100 *       <th>Content-Type</th>
101 *       <th>Parser</th>
102 *    </tr>
103 *    <tr>
104 *       <td class='code'>application/json<br>text/json</td>
105 *       <td>{@link JsonParser}</td>
106 *    </tr>
107 *    <tr>
108 *       <td class='code'>text/xml<br>application/xml</td>
109 *       <td>{@link XmlParser}</td>
110 *    </tr>
111 *    <tr>
112 *       <td class='code'>text/html<br>text/html+stripped</td>
113 *       <td>{@link HtmlParser}</td>
114 *    </tr>
115 *    <tr>
116 *       <td class='code'>text/uon</td>
117 *       <td>{@link UonParser}</td>
118 *    </tr>
119 *    <tr>
120 *       <td class='code'>application/x-www-form-urlencoded</td>
121 *       <td>{@link UrlEncodingParser}</td>
122 *    </tr>
123 *    <tr>
124 *       <td class='code'>text/plain</td>
125 *       <td>{@link PlainTextParser}</td>
126 *    </tr>
127 * </table>
128 * 
129 * <p>
130 * It should be noted that we do NOT add {@link JsoParser} to the list of parsers since this could cause security
131 * issues.
132 * Use caution when using this particular parser as it could inadvertently cause code execution security holes.
133 * 
134 * <p>
135 * The list of serializers and parsers can be appended to using the
136 * {@link RestResource#serializers() @RestResource.serializers()} and
137 * {@link RestResource#parsers() @RestResource.parsers()} annotations on subclasses.
138 * 
139 * <p>
140 * This subclass also provides a default OPTIONS page by implementing a {@link #getOptions(RestRequest)} that returns a
141 * POJO consisting of beans describing the class.
142 * <img class='bordered' src='doc-files/OptionsPage.png'>
143 * 
144 * <p>
145 * The OPTIONS page can be modified or augmented by overriding this method and providing your own data.
146 * 
147 * <h5 class='section'>Notes:</h5>
148 * <ul class='spaced-list'>
149 *    <li>
150 *       Provides a default HTML stylesheet by setting {@link HtmlDoc#stylesheet() @HtmlDoc.stylesheet()}
151 *       to <js>"styles/juneau.css"</js>.
152 *    <li>
153 *       Provides a default classpath entry "htdocs" by setting
154 *       {@link RestResource#staticFiles() @RestResource.staticFiles()} to <code>{<js>"htdocs:htdocs"</js>,<js>"styles:styles"</js>}</code>.
155 *       This allows files inside the <code>[servletPackage].htdocs</code> package to be served up under the URL
156 *       <code>/servletPath/htdocs</code>.
157 * </ul>
158 * 
159 * <h5 class='section'>See Also:</h5>
160 * <ul>
161 *    <li class='link'><a class="doclink" href="../../../../overview-summary.html#juneau-rest-server.BasicRestServlet">Overview &gt; juneau-rest-server &gt; BasicRestServlet</a>
162 * </ul>
163 */
164@RestResource(
165   allowedMethodParams="OPTIONS",
166   htmldoc=@HtmlDoc(
167      navlinks={
168         "up: request:/..",
169         "options: servlet:/?method=OPTIONS"
170      }
171   )
172)
173public abstract class BasicRestServlet extends RestServlet implements BasicRestConfig {
174   private static final long serialVersionUID = 1L;
175
176   /**
177    * [OPTIONS /*] - Show resource options.
178    * 
179    * @param req The HTTP request.
180    * @return A bean containing the contents for the OPTIONS page.
181    */
182   @RestMethod(name=OPTIONS, path="/*",
183      htmldoc=@HtmlDoc(
184         navlinks={
185            "back: servlet:/",
186            "json: servlet:/?method=OPTIONS&Accept=text/json&plainText=true"
187         },
188         aside="NONE"
189      ),
190      summary="Swagger documentation",
191      description="Auto-generated swagger documentation for this resource"
192   )
193   public Swagger getOptions(RestRequest req) {
194      return req.getSwagger();
195   }
196}