org.dmonix.servlet.Request 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.Option;
import javascalautils.Try;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static javascalautils.OptionCompanion.Option;
/**
* Wrapper for the {@link HttpServletRequest} class.
* @author Peter Nerg
* @since 1.0
*/
public final class Request {
private final HttpServletRequest request;
public Request(HttpServletRequest request) {
this.request = request;
}
/**
* Get the wrapped {@link HttpServletRequest}
* @return The original HTTP request
*/
public HttpServletRequest request() {
return request;
}
/**
* Get all the cookies from the request.
* In contrast to the getCookies method in HTTPServletRequest this won't return null
* @return The cookies, empty if no cookies
* @since 1.6
*/
public List cookies() {
return Option(request().getCookies()). //wrap stupid null return in Option
map(cookies -> Arrays.asList(cookies)). //map the array to a list
getOrElse(() -> Collections.EMPTY_LIST); //in case the array was null, return empty list
}
/**
* Get the path info as specified in the URI.
*
* @return The path info, i.e. the last part of the URI
*/
public Option getPathInfo() {
return ParserUtils.getPathInfo(request);
}
/**
* Get the path info as specified in the URI.
* Returns a Success containing the path if such exists, else a Failure with a JSONServlet exception containing an error response
* @return The path info, i.e. the last part of the URI
* @since 1.3
*/
public Try getPathInfoAsTry() {
return ParserUtils.getPathInfoAsTry(request);
}
/**
* Parses an object from the json stream in the HTTP request
* @param type The type to parse
* @param The return type
* @return The parsed object
*/
public Try fromJson(Class type) {
return fromJson("UTF-8", type);
}
/**
* Parses an object from the json stream in the HTTP request
* @param charsetName The charset to expect
* @param type The type to parse
* @param The return type
* @return The parsed object
*/
public Try fromJson(String charsetName, Class type) {
return ParserUtils.fromJson(request, charsetName, type);
}
}