Files
Brainstorm/Foundation Web Application/src/com/foundation/web/DefaultResourceRequestHandler.java

121 lines
6.4 KiB
Java

package com.foundation.web;
import com.common.debug.Debug;
import com.common.util.IHashMap;
import com.common.util.LiteHashMap;
import com.foundation.web.WebApplication.PathSubstitution;
import com.foundation.web.interfaces.IConnectionContext;
import com.foundation.web.interfaces.IRequest;
import com.foundation.web.interfaces.IResponse;
import com.foundation.web.interfaces.ISession;
public class DefaultResourceRequestHandler extends ResourceRequestHandler {
private IHashMap nameSubstitutionMap = new LiteHashMap(10);
private IHashMap pathSubstitutionMap = new LiteHashMap(10);
/**
* DefaultResourceRequestHandler constructor.
*/
public DefaultResourceRequestHandler() {
}//DefaultResourceRequestHandler()//
/* (non-Javadoc)
* @see com.foundation.web.ResourceRequestHandler#processWebSocketUpgrade(com.foundation.web.interfaces.IRequest, com.foundation.web.interfaces.IResponse, com.foundation.web.interfaces.ISession, boolean, boolean, com.foundation.web.interfaces.IConnectionContext, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
public void processWebSocketUpgrade(IRequest request, IResponse response, ISession session, boolean isSecure, boolean clientHadBadSession, IConnectionContext connectionContext, String connection, String secWebSocketKey, String secWebSocketProtocol, String secWebSocketVersion, String origin) {
response.setError(IResponse.ERROR_UPGRADE_REJECTED);
}//processWebSocketUpgrade()//
/**
* Adds a resource name subtitution mapping where the old resource name will be replaced by the new resource name automatically prior to the request being processed.
* @param oldName The old resource name.
* @param newName The new resource name.
*/
public void addResourceNameSubstitution(String oldName, String newName) {
nameSubstitutionMap.put(oldName, newName);
}//addResourceNameSubstitution()//
/**
* Adds a path subtitution mapping where the old path will be replaced by the new path automatically prior to the request being processed.
* @param oldPath The old resource name. eg: "/"
* @param newPath The new resource name. eg: "/subdir/index.html"
* @param forward Whether the substitution should constitute a forward (where the client is told of the new path) versus a redirect.
*/
public void addPathSubstitution(String oldPath, String newPath, boolean forward) {
pathSubstitutionMap.put(oldPath, new PathSubstitution(newPath, forward));
}//addPathSubstitution()//
/* (non-Javadoc)
* @see com.foundation.web.ResourceRequestHandler#performPathSubstitution(java.lang.String, com.foundation.web.interfaces.IRequest, com.foundation.web.interfaces.IResponse)
*/
protected String performPathSubstitution(String path, IRequest request, IResponse response) {
PathSubstitution substitutePath = (PathSubstitution) pathSubstitutionMap.get(path);
//First attempt to find a substitute path, then check to see if there is a substitute resource.//
if(substitutePath != null) {
if(substitutePath.forward()) {
//Update the Browser's URL if a substitute is found so that additional requests that are relative to the primary resource's path don't fail spectacularly.//
response.setForwardUri(substitutePath.getPath() + (request.getParameterString() == null ? "" : request.getParameterString()));
path = null;
}//if//
else {
path = substitutePath.getPath();
}//else//
}//if//
else {
int lastForwardSlash = path.lastIndexOf('/');
int lastBackSlash = path.lastIndexOf('\\');
int lastSlash = Math.max(lastForwardSlash, lastBackSlash);
//boolean hasDot = path.indexOf('.', lastSlash) != -1; //Note: This requires all accessed resources to have a dot in the name! Is this desired? Without this we can't tell if this is supposed to use the default resource in the directory though.
//String resourceName = hasDot && lastSlash + 2 != path.length() ? path.substring(lastSlash + 1) : "";
String resourceName = path.substring(lastSlash + 1);
String newResourceName = (String) nameSubstitutionMap.get(resourceName);
if(newResourceName != null) {
if(resourceName.length() > 0) {
path = path.substring(0, lastSlash + 1) + newResourceName;
}//if//
else {
path += newResourceName;
}//else//
}//if//
}//else//
return path;
}//performPathSubstitution()//
/* (non-Javadoc)
* @see com.foundation.web.ResourceRequestHandler#getMaxMessageSize(com.foundation.web.interfaces.IRequest, com.foundation.web.SessionData, com.foundation.web.SecureSessionData)
*/
public int getMaxMessageSize(IRequest request, SessionData sessionData, SecureSessionData secureSessionData) {
return 0;
}//getMaxMessageSize()//
/* (non-Javadoc)
* @see com.foundation.web.ResourceRequestHandler#processRequest(java.lang.String, com.foundation.web.interfaces.IRequest, com.foundation.web.interfaces.IResponse, com.foundation.web.SessionData, com.foundation.web.SecureSessionData, boolean, com.foundation.web.interfaces.IConnectionContext)
*/
public void processRequest(String requestPath, IRequest request, IResponse response, SessionData sessionData, SecureSessionData secureSessionData, boolean isSecure, IConnectionContext connectionContext) {
try {
int extensionDecimal = requestPath.lastIndexOf('.');
boolean cacheIndefinately = false;
//Set images to cache indefinately.//
if(extensionDecimal != -1) {
String extension = requestPath.substring(extensionDecimal + 1).toLowerCase();
if(extension.equals("gif") || extension.equals("png") || extension.equals("jpg") || extension.equals("jpeg")) {
cacheIndefinately = true;
}//if//
}//if//
FileContent.setContentResource(response, requestPath, request, request.getCacheDate(), null, cacheIndefinately);
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
}//processRequest()//
/* (non-Javadoc)
* @see com.foundation.web.ResourceRequestHandler#processRequest(com.foundation.web.IWebRequestHandler, java.lang.String, com.foundation.web.interfaces.IRequest, com.foundation.web.interfaces.IResponse, com.foundation.web.SessionData, com.foundation.web.SecureSessionData, boolean, com.foundation.web.interfaces.IConnectionContext)
*/
public void processRequest(IWebRequestHandler handler, String requestPath, IRequest request, IResponse response, SessionData sessionData, SecureSessionData secureSessionData, boolean isSecure, IConnectionContext connectionContext) {
try {
handler.processRequest(request, response, sessionData, secureSessionData);
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
}//processRequest()//
}//DefaultResourceRequestHandler//