org.dmonix.servlet.JSONServlet Maven / Gradle / Ivy
Show all versions of simple-servlet-framework Show documentation
/**
* Copyright 2016 Peter Nerg
*
* Licensed 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.dmonix.servlet;
import javascalautils.Try;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import static javascalautils.TryCompanion.Try;
/**
* Extends the standard {@link HttpServlet} by providing a nicer way to read/write request/response data.
* @author Peter Nerg
* @since 1.0
*/
public abstract class JSONServlet extends HttpServlet implements RequestParser, ResponseBuilder {
@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
writeResponse(resp, getWithTry(new Request(req)));
}
@Override
protected final void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
writeResponse(resp, deleteWithTry(new Request(req)));
}
@Override
protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
writeResponse(resp, postWithTry(new Request(req)));
}
@Override
protected final void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
writeResponse(resp, putWithTry(new Request(req)));
}
@Override
protected final void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//get all methods the sub class is implementing
Set methods = Arrays.stream(getClass().getDeclaredMethods()).map(Method::getName).collect(Collectors.toSet());
StringBuilder allow = new StringBuilder();
//OPTIONS and TRACE are always enabled
allow.append("OPTIONS");
allow.append(", ");
allow.append("TRACE");
//if GET is supported then HEAD is also supported
if (isOperationSupported(methods, "get")) {
allow.append(", ");
allow.append("GET");
allow.append(", ");
allow.append("HEAD");
}
if (isOperationSupported(methods, "post")) {
allow.append(", ");
allow.append("POST");
}
if (isOperationSupported(methods, "put")) {
allow.append(", ");
allow.append("PUT");
}
if (isOperationSupported(methods, "delete")) {
allow.append(", ");
allow.append("DELETE");
}
resp.setHeader("Allow", allow.toString());
}
private boolean isOperationSupported(Set methods, String operation) {
return methods.contains(operation) || methods.contains(operation+"WithTry");
}
/**
* Implements the GET method.
* Should be overridden by servlets needing to support this method.
* If not override a default 405 - Not supported is returned.
* @param request The request data
* @return The response data.
* @throws ServletException Any exception during processing
* @throws IOException Any exception during processing
*/
protected Response get(Request request) throws ServletException, IOException {
return ErrorResponseUnsupportedOperation();
}
/**
* Implements the GET method.
* Should be overridden by servlets needing to support this method.
* If not overridden this method invokes {@link #get(Request)}.
* In other words this method takes precedence over {@link #get(Request)} so overriding both will not make sense.
* @param request The request data
* @return The response data in case of Success, else a Failure
* @since 1.1
* @see #get(Request)
*/
protected Try getWithTry(Request request) {
return Try(() -> get(request));
}
/**
* Implements the DELETE method.
* Should be overridden by servlets needing to support this method.
* If not overridden a default 405 - Not supported is returned.
* @param request The request data
* @return The response data.
* @throws ServletException Any exception during processing
* @throws IOException Any exception during processing
*/
protected Response delete(Request request) throws ServletException, IOException {
return ErrorResponseUnsupportedOperation();
}
/**
* Implements the DELETE method.
* Should be overridden by servlets needing to support this method.
* If not overridden this method invokes {@link #delete(Request)}.
* In other words this method takes precedence over {@link #delete(Request)} so overriding both will not make sense.
* @param request The request data
* @return The response data in case of Success, else a Failure
* @since 1.1
* @see #get(Request)
*/
protected Try deleteWithTry(Request request) {
return Try(() -> delete(request));
}
/**
* Implements the POST method.
* Should be overridden by servlets needing to support this method.
* If not overridden a default 405 - Not supported is returned.
* @param request The request data
* @return The response data.
* @throws ServletException Any exception during processing
* @throws IOException Any exception during processing
*/
protected Response post(Request request) throws ServletException, IOException {
return ErrorResponseUnsupportedOperation();
}
/**
* Implements the POST method.
* Should be overridden by servlets needing to support this method.
* If not overridden this method invokes {@link #post(Request)}.
* In other words this method takes precedence over {@link #post(Request)} so overriding both will not make sense.
* @param request The request data
* @return The response data in case of Success, else a Failure
* @since 1.1
* @see #get(Request)
*/
protected Try postWithTry(Request request) {
return Try(() -> post(request));
}
/**
* Implements the PUT method.
* Should be overridden by servlets needing to support this method.
* If not overridden a default 405 - Not supported is returned.
* @param request The request data
* @return The response data.
* @throws ServletException Any exception during processing
* @throws IOException Any exception during processing
*/
protected Response put(Request request) throws ServletException, IOException {
return ErrorResponseUnsupportedOperation();
}
/**
* Implements the PUT method.
* Should be overridden by servlets needing to support this method.
* If not overridden this method invokes {@link #put(Request)}.
* In other words this method takes precedence over {@link #put(Request)} so overriding both will not make sense.
* @param request The request data
* @return The response data in case of Success, else a Failure
* @since 1.1
* @see #get(Request)
*/
protected Try putWithTry(Request request) {
return Try(() -> put(request));
}
}