org.apache.juneau.rest.RestServletDefault Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.rest;
import static org.apache.juneau.http.HttpMethodName.*;
import static org.apache.juneau.serializer.Serializer.*;
import org.apache.juneau.dto.swagger.*;
import org.apache.juneau.html.*;
import org.apache.juneau.jso.*;
import org.apache.juneau.json.*;
import org.apache.juneau.msgpack.*;
import org.apache.juneau.plaintext.*;
import org.apache.juneau.rest.annotation.*;
import org.apache.juneau.soap.*;
import org.apache.juneau.uon.*;
import org.apache.juneau.urlencoding.*;
import org.apache.juneau.xml.*;
/**
* Subclass of {@link RestServlet} with default serializers and parsers defined.
*
*
* Supports the following request Accept
header values with the resulting response Content-Type
:
*
*
* Accept
* Content-Type
* Serializer
*
*
* application/json
text/json
* application/json
* {@link JsonSerializer}
*
*
* application/json+simple
text/json+simple
* application/json
* {@link org.apache.juneau.json.JsonSerializer.Simple}
*
*
* application/json+schema
text/json+schema
* application/json
* {@link JsonSchemaSerializer}
*
*
* text/xml
* text/xml
* {@link XmlDocSerializer}
*
*
* text/xml+schema
* text/xml
* {@link XmlSchemaDocSerializer}
*
*
* text/html
* text/html
* {@link HtmlDocSerializer}
*
*
* text/html+stripped
* text/html
* {@link HtmlStrippedDocSerializer}
*
*
* text/uon
* text/uon
* {@link UonSerializer}
*
*
* application/x-www-form-urlencoded
* application/x-www-form-urlencoded
* {@link UrlEncodingSerializer}
*
*
* text/xml+soap
* text/xml
* {@link SoapXmlSerializer}
*
*
* text/plain
* text/plain
* {@link PlainTextSerializer}
*
*
*
* Supports the following request Content-Type
header values:
*
*
*
* Content-Type
* Parser
*
*
* application/json
text/json
* {@link JsonParser}
*
*
* text/xml
application/xml
* {@link XmlParser}
*
*
* text/html
text/html+stripped
* {@link HtmlParser}
*
*
* text/uon
* {@link UonParser}
*
*
* application/x-www-form-urlencoded
* {@link UrlEncodingParser}
*
*
* text/plain
* {@link PlainTextParser}
*
*
*
*
* It should be noted that we do NOT add {@link JsoParser} to the list of parsers since this could cause security
* issues.
* Use caution when using this particular parser as it could inadvertently cause code execution security holes.
*
*
* The list of serializers and parsers can be appended to using the
* {@link RestResource#serializers() @RestResource.serializers()} and
* {@link RestResource#parsers() @RestResource.parsers()} annotations on subclasses.
*
*
* This subclass also provides a default OPTIONS page by implementing a {@link #getOptions(RestRequest)} that returns a
* POJO consisting of beans describing the class.
*
*
*
* The OPTIONS page can be modified or augmented by overriding this method and providing your own data.
*
*
Other Notes
*
* -
* Provides a default HTML stylesheet by setting {@link HtmlDoc#stylesheet() @HtmlDoc.stylesheet()}
* to
"styles/juneau.css" .
* -
* Provides a default classpath entry "htdocs" by setting
* {@link RestResource#staticFiles() @RestResource.staticFiles()} to
"{htdocs:'htdocs'}" .
* This allows files inside the [servletPackage].htdocs
package to be served up under the URL
* /servletPath/htdocs
.
*
*/
@RestResource(
serializers={
HtmlDocSerializer.class, // HTML must be listed first because Internet Explore does not include text/html in their Accept header.
HtmlStrippedDocSerializer.class,
HtmlSchemaDocSerializer.class,
JsonSerializer.class,
JsonSerializer.Simple.class,
JsonSchemaSerializer.class,
XmlDocSerializer.class,
XmlSchemaDocSerializer.class,
UonSerializer.class,
UrlEncodingSerializer.class,
MsgPackSerializer.class,
SoapXmlSerializer.class,
PlainTextSerializer.class
},
parsers={
JsonParser.class,
XmlParser.class,
HtmlParser.class,
UonParser.class,
UrlEncodingParser.class,
MsgPackParser.class,
PlainTextParser.class
},
properties={
// URI-resolution is disabled by default. Need to enable it.
@Property(name=SERIALIZER_uriResolution, value="ROOT_RELATIVE")
},
allowMethodParam="OPTIONS",
htmldoc=@HtmlDoc(
header={
"$R{servletTitle}
",
"$R{methodSummary,$R{servletDescription}}
",
""
},
stylesheet="servlet:/styles/light.css",
head={
""
}
),
// These are static files that are served up by the servlet under the specified sub-paths.
staticFiles="{htdocs:'htdocs',styles:'styles'}"
)
public abstract class RestServletDefault extends RestServlet {
private static final long serialVersionUID = 1L;
/**
* [OPTIONS /*] - Show resource options.
*
* @param req The HTTP request.
* @return A bean containing the contents for the OPTIONS page.
*/
@RestMethod(name=OPTIONS, path="/*",
htmldoc=@HtmlDoc(
navlinks={
"back: servlet:/",
"json: servlet:/?method=OPTIONS&Accept=text/json&plainText=true"
},
aside="NONE"
),
summary="Swagger documentation",
description="Auto-generated swagger documentation for this resource"
)
public Swagger getOptions(RestRequest req) {
return req.getSwagger();
}
}