Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base.jee.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import base.jee.JeeBase;
import base.jee.api.API;
import base.jee.api.Settings;
import base.security.User;
import static base.jee.Constants.MAX_IP_ADDRESS_LENGTH;
public abstract class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected Settings settings;
protected JeeBase jee;
protected API api;
public BaseServlet(JeeBase jee) throws IOException {
this.settings = jee.getSettings();
this.jee = jee;
this.api = jee.getAPI();
}
public final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String site = getSite(request);
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
String token = (String)request.getAttribute("token");
if(token == null) {
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equalsIgnoreCase(jee.cookieName)) {
token = cookie.getValue();
break;
}
}
}
User u = jee.getAPI().getUserSession(site, token, getIp(request));
String queryString = request.getQueryString();
if (queryString == null) {
jee.log("DEBUG", "Accessing url: " + request.getRequestURI(), u);
} else {
jee.log("DEBUG", "Accessing url: " + request.getRequestURI() + "?" + queryString, u);
}
doGet(u, request, response);
}
public abstract void doGet(User u, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public static String getSite(HttpServletRequest request) {
String site = request.getHeader("Host");
if(site == null) {
return site;
}
if(site.length() > 100) {
return null;
}
int i = site.indexOf(':');
if(i > 0) {
site = site.substring(0, i);
}
return site.toLowerCase();
}
/**
* Trust the forwarded header only if the connection comes over internal network.
*/
public static String getIp(HttpServletRequest request) throws IOException {
String ip = request.getRemoteAddr();
/* Trust proxy/forwarded information if the source IP is a local ip address.
* This fails in some cases, such as if the web server and load balancer or
* reverse proxy use public IP addresses.
*/
if(ip.startsWith("10.") || ip.startsWith("172.") || ip.startsWith("192.168.") || ip.startsWith("127.") || ip.startsWith("0:0:0:0")) {
if(request.getHeader("HTTP_X_FORWARDED_FOR") != null) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if(request.getHeader("X-Forwarded-For") != null) {
ip = request.getHeader("X-Forwarded-For");
}
}
if(ip != null) {
if(ip.length() > MAX_IP_ADDRESS_LENGTH * 5) {
throw new IOException("Malformed client HTTP request header");
}
if(ip.trim().length() > MAX_IP_ADDRESS_LENGTH) {
ip = ip.substring(0, MAX_IP_ADDRESS_LENGTH);
}
}
return ip;
}
}